Commit 2c4c33fc authored by kaniini's avatar kaniini

account: implement firehose

parent 3fec4875
......@@ -356,6 +356,9 @@ void Account::validateToken()
});
fetchInstanceMetadata();
// set up streaming for notifications
streamingSocket("user");
}
void Account::writeToSettings(QSettings &settings)
......@@ -580,6 +583,69 @@ void Account::invalidatePost(Post *p)
emit invalidatedPost(p);
}
QUrl Account::streamingUrl(QString stream)
{
QUrl url = apiUrl ("/api/v1/streaming");
QUrlQuery q;
q.addQueryItem ("access_token", m_token);
q.addQueryItem ("stream", stream);
url.setQuery (q);
url.setScheme ("wss");
return QUrl (url);
}
QWebSocket *Account::streamingSocket(QString stream)
{
if (m_websockets.contains (stream))
return m_websockets[stream];
auto socket = new QWebSocket ();
socket->setParent (this);
auto url = streamingUrl (stream);
QObject::connect(socket, &QWebSocket::textMessageReceived, [=] (const QString &message) {
QString target_tl = stream;
auto env = QJsonDocument::fromJson(message.toLocal8Bit ());
if (stream == "user")
target_tl = "home";
auto event = env.object()["event"].toString();
if (event == "update")
{
QJsonDocument doc = QJsonDocument::fromJson(env.object()["payload"].toString().toLocal8Bit());
handleUpdate(doc, target_tl);
return;
}
qDebug() << socket << "UNHANDLED IN:" << env;
});
socket->open (url);
qDebug() << "[WEBSOCKET] Connecting to" << url;
m_websockets[stream] = socket;
return socket;
}
void Account::handleUpdate(QJsonDocument doc, QString target)
{
QList<std::shared_ptr<Post>> posts;
auto obj = doc.object();
auto p = std::make_shared<Post>(this, obj);
posts.push_back(p);
emit fetchedTimeline(target, posts);
}
void Identity::fromSourceData(QJsonObject doc)
{
m_display_name = doc["display_name"].toString();
......
......@@ -13,6 +13,7 @@
#include <QHttpPart>
#include <QHttpMultiPart>
#include <QFile>
#include <QWebSocket>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
......@@ -107,6 +108,10 @@ public:
void upload(std::shared_ptr<Post> p, QFile *file, QString filename);
void updateAttachment(Attachment *a);
// streaming
QUrl streamingUrl(QString stream);
QWebSocket *streamingSocket(QString stream);
// post refresh
void invalidatePost(Post *p);
......@@ -144,12 +149,14 @@ private:
QString m_instance_name;
Identity m_identity;
AllowedContentType m_allowed_content_types;
QMap<QString, QWebSocket *> m_websockets;
QMap<QString, std::shared_ptr<Identity>> m_identity_cache;
// OAuth authorization
QUrlQuery buildOAuthQuery();
void mutatePost(std::shared_ptr<Post> p, const QString verb, bool deliver_home = false);
void handleUpdate(QJsonDocument doc, QString target);
};
}
......
......@@ -4,9 +4,7 @@
#
#-------------------------------------------------
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
QT += core gui network websockets widgets
TARGET = Michabo
TEMPLATE = app
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment