summary refs log tree commit diff
path: root/src/MatrixClient.cc
diff options
context:
space:
mode:
authorkrombel <krombel@krombel.de>2018-01-13 13:49:51 +0100
committermujx <mujx@users.noreply.github.com>2018-01-13 14:49:51 +0200
commitf87b8fe817c390ced781726422098b5a4af2b15a (patch)
tree7a148b4239e97c90b4bb8cdc0ac538f25eac38fc /src/MatrixClient.cc
parentProperly assign default value to QSharedPointer for Qt5.7 (diff)
downloadnheko-f87b8fe817c390ced781726422098b5a4af2b15a.tar.xz
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.


Diffstat (limited to 'src/MatrixClient.cc')
-rw-r--r--src/MatrixClient.cc56
1 files changed, 56 insertions, 0 deletions
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_); @@ -968,6 +975,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) { QUrlQuery query;