Newer
Older
saori / saoriaccount.cpp
/***

The MIT License

Copyright (c) 2018 Teppei Tamra (TAM)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

***/

#include "saoriaccount.h"
#include "saoriapplication.h"
#include "saoriview.h"
#include "saoridef.h"

SaoriAccount::SaoriAccount(const QString accountName, Saoridon *instance, const QString accsessToken, QObject *parent) : QObject(parent)
{
    m_name = accountName;
    m_accessToken = accsessToken;
    m_instance = instance;
    getAccountInfomation();
}

const QString SaoriAccount::accessToken()
{
    return m_accessToken;
}

void SaoriAccount::setAccessToken(QString token)
{
    m_accessToken = token;
    return;
}

const QString SaoriAccount::name()
{
    return m_name;
}

void SaoriAccount::setName(const QString name)
{
    m_name = name;
    return;
}

Saoridon *SaoriAccount::instance()
{
    return m_instance;
}

void SaoriAccount::getAccountInfomation()
{
    QNetworkRequest request = createHearder();
    request.setUrl(QUrl(m_instance->instance().url() + SAORI_MASTODON_APIPATH_ACCOUNTS + "/verify_credentials"));
    auto *reply = SaoriApplication::saori()->manager->get(request);
    connect(reply,&QNetworkReply::finished,[=](){
        if (reply->error() == QNetworkReply::NoError) {
            QJsonObject json = QJsonDocument::fromJson(reply->readAll()).object();
            m_accountInfo = json;
            emit accountInfomationChanged();
        }
        reply->deleteLater();
    });
    return;
}

QNetworkRequest SaoriAccount::createHearder()
{
    QNetworkRequest request;
    request.setRawHeader("Authorization","Bearer " + accessToken().toLatin1());
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    return request;
}

const QJsonValue SaoriAccount::accountInfo(const QString key)
{
    return m_accountInfo[key];
}

const QStringList SaoriAccount::timelineList()
{
    QStringList tl;
    tl  << "home"
        << "local"
        << "public"
        << "notifications"
        << "favourites"
           ;
    return tl;

}

void SaoriAccount::getTimelineData(const QString timeline, const QUrlQuery query)
{
    QUrl url = instance()->timelineUrl(timeline);
    int limit = SaoriApplication::saori()->setting()->value(SAORI_SETTING_GETLIMIT,SAORI_SETTING_GETLIMIT_DEFAULT).toInt();
    if (url.isEmpty()) return;
    QNetworkRequest request = createHearder();
    QUrlQuery q = query;
    q.addQueryItem("limit",QString::number(limit));
    request.setUrl(instance()->addQuery(url,q));
    auto *reply = SaoriApplication::saori()->manager->get(request);
    connect(reply,&QNetworkReply::finished,[=](){
        if (reply->error() == QNetworkReply::NoError) {
            QByteArray data = reply->readAll();
            emit apiData(timeline,data,reply->rawHeader("Link"));
        }
        reply->deleteLater();
    });
    return;
}

void SaoriAccount::postNewStatus(const QString timeline, const QString data)
{
    QUrl url(instance()->instance().toString() + SAORI_MASTODON_APIPATH_POST);
    QNetworkRequest request = createHearder();
    request.setUrl(url);
    auto *reply = SaoriApplication::saori()->manager->post(request,data.toUtf8());
    connect(reply,&QNetworkReply::finished,[=](){
        emit posted(timeline,reply->error());
        reply->deleteLater();
    });
}

void SaoriAccount::getRelationship(qlonglong id)
{
    QNetworkRequest request = createHearder();
    QUrlQuery q;
    q.addQueryItem("id",QString::number(id));
    request.setUrl(instance()->addQuery(m_instance->instance().url() + SAORI_MASTODON_APIPATH_ACCOUNTS + "/relationships",q));
    auto *reply = SaoriApplication::saori()->manager->get(request);
    connect(reply,&QNetworkReply::finished,[=](){
        if (reply->error() == QNetworkReply::NoError) {
            QJsonArray json = QJsonDocument::fromJson(reply->readAll()).array();
            emit relationship(id,json.first().toObject());
        }
        reply->deleteLater();
    });
    return;
}

void SaoriAccount::reblogStatus(qlonglong id, bool state)
{
    QNetworkRequest request = createHearder();
    QString u = m_instance->instance().url();
    if (state) u += QString(SAORI_MASTODON_APIPATH_STATUS_REBLOG).arg(QString::number(id));
    else u += QString(SAORI_MASTODON_APIPATH_STATUS_UNREBLOG).arg(QString::number(id));
    request.setUrl(u);
    auto *reply = SaoriApplication::saori()->manager->post(request,QByteArray());
    connect(reply,&QNetworkReply::finished,[=](){
        if (reply->error() == QNetworkReply::NoError) {
            emit updateStatus(id,reply->readAll());
        }
        reply->deleteLater();
    });
    return;
}

void SaoriAccount::favouriteStatus(qlonglong id, bool state)
{
    QNetworkRequest request = createHearder();
    QString u = m_instance->instance().url();
    if (state) u += QString(SAORI_MASTODON_APIPATH_STATUS_FAVOURITE).arg(QString::number(id));
    else u += QString(SAORI_MASTODON_APIPATH_STATUS_UNFAVOURITE).arg(QString::number(id));
    request.setUrl(u);
    auto *reply = SaoriApplication::saori()->manager->post(request,QByteArray());
    connect(reply,&QNetworkReply::finished,[=](){
        if (reply->error() == QNetworkReply::NoError) {
            emit updateStatus(id,reply->readAll());
        }
        reply->deleteLater();
    });
    return;
}