220 lines
6.2 KiB
C++
220 lines
6.2 KiB
C++
|
/*
|
||
|
* 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;
|
||
|
|
||
|
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 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()
|
||
|
{
|
||
|
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::onAuthorizationReceived(QString token, QString verifier)
|
||
|
{
|
||
|
switch (oauthManager->lastError())
|
||
|
{
|
||
|
case KQOAuthManager::NoError:
|
||
|
qDebug() << "- authorization received -";
|
||
|
m_appSettings->setValue("access_token", token);
|
||
|
emit authorizationReceived();
|
||
|
break;
|
||
|
default:
|
||
|
qDebug() << "- authorization error -";
|
||
|
qDebug() << oauthManager->lastError();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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:
|
||
|
{
|
||
|
// qDebug() << ":: 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);
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
case Pnut::CREATED:
|
||
|
{
|
||
|
if (endpoint == "/posts")
|
||
|
{
|
||
|
qDebug() << "Success!";
|
||
|
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);
|
||
|
}
|