This repository has been archived on 2023-11-19. You can view files and clone it, but cannot push or open issues or pull requests.
goober-bb10/src/Pnut.cpp

461 lines
14 KiB
C++
Raw Normal View History

2016-09-22 23:21:20 +00:00
/*
* Copyright (C) 2016 Morgan McMillian <gilag@monkeystew.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "Pnut.h"
#include "globals.h"
#include <QDebug>
#include <bb/data/JsonDataAccess>
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;
2016-09-22 23:21:20 +00:00
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);
2016-09-22 23:21:20 +00:00
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.";
2016-09-22 23:21:20 +00:00
}
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);
}
2016-09-22 23:21:20 +00:00
}
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);
}
2016-09-24 15:13:45 +00:00
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);
}
2016-09-22 23:21:20 +00:00
void Pnut::onAuthorizationReceived(QString token, QString verifier)
{
2016-10-21 23:53:19 +00:00
qDebug() << "--check check--";
qDebug() << oauthManager->lastError();
qDebug() << "token: " << token;
qDebug() << "verif: " << verifier;
2016-09-22 23:21:20 +00:00
switch (oauthManager->lastError())
{
2016-10-21 23:53:19 +00:00
case KQOAuthManager::RequestValidationError:
2016-09-22 23:21:20 +00:00
case KQOAuthManager::NoError:
qDebug() << "- authorization received -";
m_appSettings->setValue("access_token", token);
getUserInfo();
2016-09-22 23:21:20 +00:00
emit authorizationReceived();
break;
default:
qDebug() << "- authorization error -";
qDebug() << oauthManager->lastError();
}
authInProgress = false;
}
void Pnut::onRequestReady(QByteArray data)
{
qDebug() << "- onRequestReady -";
switch (oauthManager->lastError())
{
2016-10-01 18:18:40 +00:00
case KQOAuthManager::RequestUnauthorized:
qDebug() << "- Unauthorized -";
m_appSettings->remove("access_token");
m_appSettings->remove("username");
emit authorizationRequired();
break;
case KQOAuthManager::NetworkError:
qDebug() << "- NetworkError -";
2016-10-01 18:18:40 +00:00
getUserInfo();
break;
}
2016-09-22 23:21:20 +00:00
}
void Pnut::onAuthorizedRequestReady(QByteArray data, int id)
{
QString endpoint = req_map[id];
qDebug() << "- onAuthorizedRequestReady";
qDebug() << "- id: " << id;
qDebug() << "- endpoint: " << endpoint;
qDebug() << data;
2016-09-22 23:21:20 +00:00
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/") || endpoint == "/users/me/mentions")
2016-09-22 23:21:20 +00:00
{
Pnut::RequestType rtype = rtype_map[id];
emit streamReceived(variant.toMap()["data"].toList(), rtype);
req_map.remove(id);
rtype_map.remove(id);
2016-09-24 15:13:45 +00:00
}
else if (endpoint == ":me")
{
QVariantMap userinfo(variant.toMap()["data"].toMap());
m_appSettings->setValue("username", userinfo.value("username").toString());
2016-09-22 23:21:20 +00:00
}
2016-09-24 15:13:45 +00:00
else if (endpoint == ":thread")
{
emit threadReceived(variant.toMap()["data"].toList());
req_map.remove(id);
}
2016-10-01 18:18:40 +00:00
else if (endpoint == ":logout")
{
qDebug() << "Logout successful!";
req_map.remove(id);
m_appSettings->remove("access_token");
m_appSettings->remove("username");
emit authorizationRequired();
2016-10-01 18:18:40 +00:00
}
2016-10-21 13:09:34 +00:00
else if (endpoint == ":follow")
{
qDebug() << "Follow successful!";
req_map.remove(id);
emit followSuccess(variant.toMap()["data"].toMap());
2016-10-21 13:09:34 +00:00
}
else if (endpoint == ":unfollow")
{
qDebug() << "Unfollow successful!";
req_map.remove(id);
emit unfollowSuccess(variant.toMap()["data"].toMap());
}
else if (endpoint == ":block")
{
qDebug() << "Block successful!";
req_map.remove(id);
emit blockSuccess(variant.toMap()["data"].toMap());
}
else if (endpoint == ":unblock")
{
qDebug() << "Unblock successful!";
req_map.remove(id);
emit unblockSuccess(variant.toMap()["data"].toMap());
}
else if (endpoint == ":mute")
{
qDebug() << "Mute successful!";
req_map.remove(id);
emit muteSuccess(variant.toMap()["data"].toMap());
}
else if (endpoint == ":unmute")
{
qDebug() << "Unmute successful!";
req_map.remove(id);
emit unmuteSuccess(variant.toMap()["data"].toMap());
2016-10-21 13:09:34 +00:00
}
else {
qDebug() << "GOT SOMETHING NEW!";
qDebug() << endpoint;
}
2016-09-22 23:21:20 +00:00
break;
}
case Pnut::CREATED:
{
if (endpoint == "/posts")
{
qDebug() << "Success!";
req_map.remove(id);
}
2016-10-01 18:18:40 +00:00
else
{
qDebug() << "Good but not sure what";
req_map.remove(id);
}
2016-09-22 23:21:20 +00:00
break;
}
default:
{
qDebug() << ":: Error - " << code;
qDebug() << meta;
}
}
}
void Pnut::getStream(QString endpoint, Pnut::RequestType rtype)
{
QUrl url(PNUT_API_ROOT + endpoint);
KQOAuthParameters parameters;
parameters.insert("count", "50");
2016-09-22 23:21:20 +00:00
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);
}
2016-09-24 15:13:45 +00:00
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);
}
2016-10-01 18:18:40 +00:00
void Pnut::logout()
{
QUrl url(PNUT_API_ROOT + "/token");
KQOAuthParameters parameters;
req_map[++req_id] = ":logout";
deleteRequest(url, parameters, req_id);
}
2016-10-21 13:09:34 +00:00
void Pnut::followUser(QString uid)
{
QUrl url(PNUT_API_ROOT + "/users/" + uid + "/follow");
KQOAuthParameters parameters;
req_map[++req_id] = ":follow";
putRequest(url, parameters, req_id);
}
void Pnut::unfollowUser(QString uid)
{
QUrl url(PNUT_API_ROOT + "/users/" + uid + "/follow");
KQOAuthParameters parameters;
req_map[++req_id] = ":unfollow";
deleteRequest(url, parameters, req_id);
}
void Pnut::blockUser(QString uid)
{
QUrl url(PNUT_API_ROOT + "/users/" + uid + "/block");
KQOAuthParameters parameters;
req_map[++req_id] = ":block";
putRequest(url, parameters, req_id);
}
void Pnut::unblockUser(QString uid)
{
QUrl url(PNUT_API_ROOT + "/users/" + uid + "/block");
KQOAuthParameters parameters;
req_map[++req_id] = ":unblock";
deleteRequest(url, parameters, req_id);
}
void Pnut::muteUser(QString uid)
{
QUrl url(PNUT_API_ROOT + "/users/" + uid + "/mute");
KQOAuthParameters parameters;
req_map[++req_id] = ":mute";
putRequest(url, parameters, req_id);
}
void Pnut::unmuteUser(QString uid)
{
QUrl url(PNUT_API_ROOT + "/users/" + uid + "/mute");
KQOAuthParameters parameters;
req_map[++req_id] = ":unmute";
deleteRequest(url, parameters, req_id);
}
void Pnut::deletePost(QString pid)
{
QUrl url(PNUT_API_ROOT + "/posts/" + pid);
KQOAuthParameters parameters;
req_map[++req_id] = ":post";
deleteRequest(url, parameters, req_id);
}