Newer
Older
saori / saoridon.cpp
@TAM (Teppei Tamra) TAM (Teppei Tamra) on 12 Jun 2018 6 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 "saoridon.h"
#include "saoriapplication.h"
#include "saoridef.h"
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QJsonDocument>

#include <QJsonArray>
#include <QEventLoop>
#include <QUrlQuery>
#include <QDebug>

Saoridon::Saoridon(QUrl instanceUrl,QObject *parent) : QObject(parent)
{
    setInstance(instanceUrl);
    getInstanceInfomation();
}

const QUrl Saoridon::instance()
{
    return m_instance;
}

void Saoridon::setInstance(const QUrl instanceUrl)
{
    m_instance = instanceUrl;
    return;
}

const QString Saoridon::clientId()
{
    return m_clientId;
}

void Saoridon::setClientId(const QString id)
{
    m_clientId = id;
    return;
}

const QString Saoridon::clientSecret()
{
    return m_clientSecret;
}

void Saoridon::setClientSecret(const QString secret)
{
    m_clientSecret = secret;
}

bool Saoridon::clientRedistration()
{
    QEventLoop event;
    QUrlQuery params;
    params.addQueryItem("client_name",SAORI_CLIENT_NAME);
    params.addQueryItem("redirect_uris",SAORI_CLIENT_REDIRECT_URI);
    params.addQueryItem("scopes",SAORI_CLIENT_SCOPES);
    params.addQueryItem("website",SAORI_CLIENT_WEBSITE);
    QNetworkRequest request;
    request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/x-www-form-urlencoded"));
    request.setUrl(QUrl(instance().url() + SAORI_MASTODON_APIPATH_APPS));
    auto *reply = SaoriApplication::saori()->manager->post(request,params.toString().toLatin1());
    connect(reply,&QNetworkReply::finished,&event,&QEventLoop::quit);
    event.exec();
    if (reply->error() != QNetworkReply::NoError) {
        reply->deleteLater();
        return false;
    }
    QJsonObject json = QJsonDocument::fromJson(reply->readAll()).object();
    QString id = json["client_id"].toString();
    QString secret = json["client_secret"].toString();
    if (id.isEmpty() || secret.isEmpty()) return false;
    setClientId(id);
    setClientSecret(secret);
    reply->deleteLater();
    return true;
}

const QUrl Saoridon::getAuthorizedUrl()
{
    //if (clientId().isEmpty() || clientSecret().isEmpty()) return QUrl();
    QUrlQuery params;
    params.addQueryItem("client_id",clientId());
    params.addQueryItem("response_type","code");
    params.addQueryItem("redirect_uri","urn:ietf:wg:oauth:2.0:oob");
    params.addQueryItem("scope",SAORI_CLIENT_SCOPES);
    QUrl url = instance().url() + SAORI_MASTODON_APIPATH_AUTHORIZE;
    url.setQuery(params);
    return url;



}

const QString Saoridon::getAccessToken(const QString code)
{
    QEventLoop event;
    QUrlQuery params;
    params.addQueryItem("grant_type","authorization_code");
    params.addQueryItem("redirect_uri","urn:ietf:wg:oauth:2.0:oob");
    params.addQueryItem("client_id",clientId());
    params.addQueryItem("client_secret",clientSecret());
    params.addQueryItem("code",code);
    QNetworkRequest request;
    request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/x-www-form-urlencoded"));
    request.setUrl(QUrl(instance().url() + SAORI_MASTODON_APIPATH_TOKEN));
    auto *reply = SaoriApplication::saori()->manager->post(request,params.toString().toLatin1());
    connect(reply,&QNetworkReply::finished,&event,&QEventLoop::quit);
    event.exec();
    if (reply->error() != QNetworkReply::NoError) {
        reply->deleteLater();
        return QString();
    }
    QJsonObject json = QJsonDocument::fromJson(reply->readAll()).object();
    QString token = json["access_token"].toString();
    reply->deleteLater();
    return token;
}

void Saoridon::getInstanceInfomation()
{
    QNetworkRequest request;
    request.setUrl(QUrl(instance().url() + SAORI_MASTODON_APIPATH_INSTANCE));
    auto *reply = SaoriApplication::saori()->manager->get(request);
    connect(reply,&QNetworkReply::finished,[=](){
        if (reply->error() == QNetworkReply::NoError) {
            m_instanceInfo = QJsonDocument::fromJson(reply->readAll()).object();
            emit instanceInfomationChanged();
        }
        reply->deleteLater();
    });

    m_timelineMap.clear();
    m_timelineMap["home"] = QUrl(instance().url() + SAORI_MASTODON_APIPATH_TIMELINE + "/home");
    m_timelineMap["local"] = QUrl(instance().url() + SAORI_MASTODON_APIPATH_TIMELINE + "/public?local=1");
    m_timelineMap["public"] = QUrl(instance().url() + SAORI_MASTODON_APIPATH_TIMELINE + "/public");
    m_timelineMap["notifications"] = QUrl(instance().url() + SAORI_MASTODON_APIPATH_NOTIFICATION + "/");
    m_timelineMap["accounts"] = QUrl(instance().url() + SAORI_MASTODON_APIPATH_ACCOUNTS);
    m_timelineMap["favourites"] = QUrl(instance().url() + SAORI_MASTODON_APIPATH_FAVOURITES);
    return;
}

const QJsonValue Saoridon::instanceInfo(const QString key)
{
    return m_instanceInfo[key];
}

const QUrl Saoridon::timelineUrl(const QString timeline)
{
    QStringList t = timeline.split(':');
    if (t.count() == 0) return QUrl();
    if (t[0] == "accounts") {
        if (t.count() < 2) return QUrl();
        QUrl u = m_timelineMap["accounts"];
        if (t.count() == 3) u.setUrl(u.toString() + "/" + t[1] + "/" + t[2]);
        else u.setUrl(u.toString() + "/" + t[1]);
        return u;
    }
    return m_timelineMap[t[0]];
}

const QUrl Saoridon::addQuery(const QUrl url, const QString key, const QString value)
{
    QUrl newurl = url;
    QUrlQuery q(url.query());
    q.addQueryItem(key,value);
    newurl.setQuery(q);
    return newurl;
}

const QUrl Saoridon::addQuery(const QUrl url, const QUrlQuery query)
{
    QUrl newurl = url;
    QUrlQuery q(url.query());
    auto list = query.queryItems();
    list += q.queryItems();
    q.setQueryItems(list);
    newurl.setQuery(q);
    return newurl;

}