Newer
Older
saori / saoriaccount.cpp
@TAM (Teppei Tamra) TAM (Teppei Tamra) on 17 Apr 2018 3 KB URLにqueryを安全に足すコードを追加。
/***

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();
            QMap<QString,QString> info;
            for(auto it = json.begin();it != json.end();it ++) {
                info[it.key()] = it.value().toString();
            }
            m_accountInfo = info;
            emit accountInfomationChanged();
        }
        reply->deleteLater();
    });
    return;
}

QNetworkRequest SaoriAccount::createHearder()
{
    QNetworkRequest request;
    request.setRawHeader("Authorization","Bearer " + accessToken().toLatin1());
    return request;
}

const QString 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)
{
    // TODO max_id、since_id、limitの処理を追加すべし。
    QUrl url = instance()->timelineUrl(timeline);
    if (url.isEmpty()) return;
    QNetworkRequest request = createHearder();
    request.setUrl(instance()->addQuery(url,"limit","10"));
    auto *reply = SaoriApplication::saori()->manager->get(request);
    connect(reply,&QNetworkReply::finished,[=](){
        if (reply->error() == QNetworkReply::NoError) {
            qDebug() << request.url().toString();
            QByteArray data = reply->readAll();
            emit apiData(timeline,data);
        }
        reply->deleteLater();
    });
    return;
}