/* * Copyright (C) 2016 Morgan McMillian * * This file is apart of the Goober application, a client for pnut.io * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "Pnut.h" #include "globals.h" #include #include const QString Pnut::PNUT_API_ROOT = QString("https://api.pnut.io/v0"); const QString Pnut::PNUT_AUTH_URL = QString("https://pnut.io/oauth/authenticate"); Pnut::Pnut() { req_id = 0; before_id = 0; since_id = 0; authInProgress = false; m_appSettings = new QSettings("Morgan McMillian", "Goober"); oauthRequest = new KQOAuthRequest; oauthManager = new KQOAuthManager(this); bool auth_req = connect(oauthManager, SIGNAL(authorizationReceived(QString,QString)), this, SLOT(onAuthorizationReceived(QString,QString))); Q_ASSERT(auth_req); bool net_req = connect(oauthManager, SIGNAL(requestReady(QByteArray)), this, SLOT(onRequestReady(QByteArray))); Q_ASSERT(net_req); bool conn_req = connect(oauthManager, SIGNAL(authorizedRequestReady(QByteArray,int)), this, SLOT(onAuthorizedRequestReady(QByteArray,int))); Q_ASSERT(conn_req); qDebug() << "Pnut created, I am life."; } Pnut::~Pnut() { delete oauthRequest; delete oauthManager; qDebug() << "Pnut destroyed, I am death."; } int Pnut::sinceId() const { return since_id; } void Pnut::setSinceId(const int& id) { since_id = id; } int Pnut::beforeId() const { return before_id; } void Pnut::setBeforeId(const int& id) { before_id = id; } void Pnut::authorize() { if (authInProgress != true) { authInProgress = true; QUrl url(PNUT_AUTH_URL); KQOAuthParameters parameters; parameters.insert("scope", "basic stream write_post follow update_profile presence"); oauthManager->getOauth2UserAuthorization(url, PNUT_CLIENT_ID, parameters); } } void Pnut::getRequest(QUrl url, KQOAuthParameters parameters, int id) { oauthRequest->clearRequest(); oauthRequest->initRequest(KQOAuthRequest::AuthorizedRequest, url); oauthRequest->setToken(m_appSettings->value("access_token").toString()); oauthRequest->setHttpMethod(KQOAuthRequest::GET); oauthRequest->setRequestOAuthMethod(KQOAuthRequest::OAUTH2); oauthRequest->setAdditionalParameters(parameters); oauthManager->executeAuthorizedRequest(oauthRequest, id); } void Pnut::postRequest(QUrl url, KQOAuthParameters parameters, int id, QByteArray data) { oauthRequest->clearRequest(); oauthRequest->initRequest(KQOAuthRequest::AuthorizedRequest, url); oauthRequest->setToken(m_appSettings->value("access_token").toString()); oauthRequest->setHttpMethod(KQOAuthRequest::POST); oauthRequest->setRequestOAuthMethod(KQOAuthRequest::OAUTH2); oauthRequest->setContentType("application/json"); oauthRequest->setAdditionalParameters(parameters); oauthRequest->setRawData(data); oauthManager->executeAuthorizedRequest(oauthRequest, id); } void Pnut::putRequest(QUrl url, KQOAuthParameters parameters, int id) { oauthRequest->clearRequest(); oauthRequest->initRequest(KQOAuthRequest::AuthorizedRequest, url); oauthRequest->setToken(m_appSettings->value("access_token").toString()); oauthRequest->setHttpMethod(KQOAuthRequest::PUT); oauthRequest->setRequestOAuthMethod(KQOAuthRequest::OAUTH2); oauthRequest->setAdditionalParameters(parameters); oauthManager->executeAuthorizedRequest(oauthRequest, id); } void Pnut::deleteRequest(QUrl url, KQOAuthParameters parameters, int id) { oauthRequest->clearRequest(); oauthRequest->initRequest(KQOAuthRequest::AuthorizedRequest, url); oauthRequest->setToken(m_appSettings->value("access_token").toString()); oauthRequest->setHttpMethod(KQOAuthRequest::DELETE); oauthRequest->setRequestOAuthMethod(KQOAuthRequest::OAUTH2); oauthRequest->setAdditionalParameters(parameters); oauthManager->executeAuthorizedRequest(oauthRequest, id); } void Pnut::onAuthorizationReceived(QString token, QString verifier) { switch (oauthManager->lastError()) { case KQOAuthManager::NoError: qDebug() << "- authorization received -"; m_appSettings->setValue("access_token", token); getUserInfo(); emit authorizationReceived(); break; default: qDebug() << "- authorization error -"; qDebug() << oauthManager->lastError(); } authInProgress = false; } void Pnut::onRequestReady(QByteArray data) { qDebug() << "- onRequestReady -"; switch (oauthManager->lastError()) { case KQOAuthManager::RequestUnauthorized: qDebug() << "- Unauthorized -"; m_appSettings->remove("access_token"); m_appSettings->remove("username"); emit authorizationRequired(); break; case KQOAuthManager::NetworkError: qDebug() << "- NetworkError -"; getUserInfo(); break; } } void Pnut::onAuthorizedRequestReady(QByteArray data, int id) { QString endpoint = req_map[id]; qDebug() << "- onAuthorizedRequestReady"; qDebug() << "- id: " << id; qDebug() << "- endpoint: " << endpoint; qDebug() << data; QVariant variant; bb::data::JsonDataAccess jda; variant = jda.loadFromBuffer(data); QVariantMap meta = variant.toMap()["meta"].toMap(); int code = meta["code"].toInt(); switch (code) { case Pnut::OK: { if (endpoint.startsWith("/posts/streams/")) { Pnut::RequestType rtype = rtype_map[id]; emit streamReceived(variant.toMap()["data"].toList(), rtype); req_map.remove(id); rtype_map.remove(id); } else if (endpoint == ":me") { QVariantMap userinfo(variant.toMap()["data"].toMap()); m_appSettings->setValue("username", userinfo.value("username").toString()); } else if (endpoint == ":thread") { emit threadReceived(variant.toMap()["data"].toList()); req_map.remove(id); } else if (endpoint == ":logout") { qDebug() << "Logout successful!"; req_map.remove(id); } break; } case Pnut::CREATED: { if (endpoint == "/posts") { qDebug() << "Success!"; req_map.remove(id); } else { qDebug() << "Good but not sure what"; req_map.remove(id); } break; } default: { qDebug() << ":: Error - " << code; qDebug() << meta; } } } void Pnut::getStream(QString endpoint, Pnut::RequestType rtype) { QUrl url(PNUT_API_ROOT + endpoint); KQOAuthParameters parameters; switch (rtype) { case Pnut::STREAM_NEWER: if (since_id > 0) parameters.insert("since_id", QString::number(since_id)); break; case Pnut::STREAM_OLDER: if (before_id > 0) parameters.insert("before_id", QString::number(before_id)); break; } req_map[++req_id] = endpoint; rtype_map[req_id] = rtype; getRequest(url, parameters, req_id); } void Pnut::sendPost(QString text) { QByteArray post; QVariantMap map; map["text"] = text; QVariant data(map); bb::data::JsonDataAccess jda; jda.saveToBuffer(data, &post); QUrl url(PNUT_API_ROOT + "/posts"); KQOAuthParameters parameters; req_map[++req_id] = "/posts"; postRequest(url, parameters, req_id, post); } void Pnut::sendReply(QString text, int pid) { QByteArray post; QVariantMap map; map["text"] = text; map["reply_to"] = pid; QVariant data(map); bb::data::JsonDataAccess jda; jda.saveToBuffer(data, &post); QUrl url(PNUT_API_ROOT + "/posts"); KQOAuthParameters parameters; req_map[++req_id] = "/posts"; postRequest(url, parameters, req_id, post); } void Pnut::getUser(QString uid) { QUrl url(PNUT_API_ROOT + "/users/" + uid); KQOAuthParameters parameters; req_map[++req_id] = "/users/" + uid; getRequest(url, parameters, req_id); } void Pnut::getUserInfo() { QUrl url(PNUT_API_ROOT + "/users/me"); KQOAuthParameters parameters; req_map[++req_id] = ":me"; getRequest(url, parameters, req_id); } void Pnut::getThread(QString pid) { QUrl url(PNUT_API_ROOT + "/posts/" + pid + "/thread"); KQOAuthParameters parameters; req_map[++req_id] = ":thread"; getRequest(url, parameters, req_id); } void Pnut::setBookmark(QString pid) { QUrl url(PNUT_API_ROOT + "/posts/" + pid + "/bookmark"); KQOAuthParameters parameters; req_map[++req_id] = ":bookmark"; putRequest(url, parameters, req_id); } void Pnut::deleteBookmark(QString pid) { QUrl url(PNUT_API_ROOT + "/posts/" + pid + "/bookmark"); KQOAuthParameters parameters; req_map[++req_id] = ":bookmark"; deleteRequest(url, parameters, req_id); } void Pnut::repost(QString pid) { QUrl url(PNUT_API_ROOT + "/posts/" + pid + "/repost"); KQOAuthParameters parameters; req_map[++req_id] = ":repost"; putRequest(url, parameters, req_id); } void Pnut::deleteRepost(QString pid) { QUrl url(PNUT_API_ROOT + "/posts/" + pid + "/repost"); KQOAuthParameters parameters; req_map[++req_id] = ":repost"; deleteRequest(url, parameters, req_id); } void Pnut::logout() { QUrl url(PNUT_API_ROOT + "/token"); KQOAuthParameters parameters; req_map[++req_id] = ":logout"; deleteRequest(url, parameters, req_id); }