From f87b8fe817c390ced781726422098b5a4af2b15a Mon Sep 17 00:00:00 2001 From: krombel Date: Sat, 13 Jan 2018 13:49:51 +0100 Subject: [PATCH] Upload filter automatically and use filter_id (#201) When a custom filter is inserted into nheko.conf or there was no filter defined yet the default filter gets automatically uploaded. After a successful upload the server-side generated filter-id is used. This is done async as it is just an enhancement and it is not required to upload the filter before the first request. --- include/MatrixClient.h | 1 + src/MatrixClient.cc | 56 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/include/MatrixClient.h b/include/MatrixClient.h index 1a8ff12c..826b4e18 100644 --- a/include/MatrixClient.h +++ b/include/MatrixClient.h @@ -64,6 +64,7 @@ public: void uploadAudio(const QString &roomid, const QSharedPointer data, const QString &filename); + void uploadFilter(const QString &filter) noexcept; void joinRoom(const QString &roomIdOrAlias); void leaveRoom(const QString &roomId); void sendTypingNotification(const QString &roomid, int timeoutInMillis = 20000); diff --git a/src/MatrixClient.cc b/src/MatrixClient.cc index 019e6141..53f6a32e 100644 --- a/src/MatrixClient.cc +++ b/src/MatrixClient.cc @@ -226,6 +226,13 @@ MatrixClient::registerUser(const QString &user, const QString &pass, const QStri void MatrixClient::sync() noexcept { + // the filter is not uploaded yet (so it is a json with { at the beginning) + // ignore for now that the filter might be uploaded multiple times as we expect + // servers to do deduplication + if (filter_.startsWith("{")) { + uploadFilter(filter_); + } + QUrlQuery query; query.addQueryItem("set_presence", "online"); query.addQueryItem("filter", filter_); @@ -967,6 +974,55 @@ MatrixClient::uploadAudio(const QString &roomid, }); } +void +MatrixClient::uploadFilter(const QString &filter) noexcept +{ + // validate that filter is a Json-String + QJsonDocument doc = QJsonDocument::fromJson(filter.toUtf8()); + if (doc.isNull() || !doc.isObject()) { + qWarning() << "Input which should be uploaded as filter is no JsonObject"; + return; + } + + QSettings settings; + auto userid = settings.value("auth/user_id", "").toString(); + + QUrlQuery query; + query.addQueryItem("access_token", token_); + + QUrl endpoint(server_); + endpoint.setPath(clientApiUrl_ + QString("/user/%1/filter").arg(userid)); + endpoint.setQuery(query); + + QNetworkRequest request(endpoint); + request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json"); + + auto reply = post(request, doc.toJson(QJsonDocument::Compact)); + + connect(reply, &QNetworkReply::finished, this, [this, reply]() { + reply->deleteLater(); + + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + if (status == 0 || status >= 400) { + qWarning() << reply->errorString() << "42"; + return; + } + + auto data = reply->readAll(); + auto response = QJsonDocument::fromJson(data); + auto filter_id = response.object()["filter_id"].toString(); + + qDebug() << "Filter with ID" << filter_id << "created."; + QSettings settings; + settings.setValue("client/sync_filter", filter_id); + settings.sync(); + + // set the filter_ var so following syncs will use it + filter_ = filter_id; + }); +} + void MatrixClient::joinRoom(const QString &roomIdOrAlias) {