Newer
Older
saori / saoriaccount.cpp
@TAM (Teppei Tamra) TAM (Teppei Tamra) on 4 May 2018 4 KB 基本的な投稿を実装
/***

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"
           ;
    return tl;

}

void SaoriAccount::getTimelineData(const QString timeline, const QUrlQuery query)
{
    // TODO max_id、since_id、limitの処理を追加すべし。
    QUrl url = instance()->timelineUrl(timeline);
    if (url.isEmpty()) return;
    QNetworkRequest request = createHearder();
    QUrlQuery q = query;
    q.addQueryItem("limit","10");
    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->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());
    });
}