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.
This commit is contained in:
krombel 2018-01-13 13:49:51 +01:00 committed by mujx
parent 396becbaa7
commit f87b8fe817
2 changed files with 57 additions and 0 deletions

View file

@ -64,6 +64,7 @@ public:
void uploadAudio(const QString &roomid, void uploadAudio(const QString &roomid,
const QSharedPointer<QIODevice> data, const QSharedPointer<QIODevice> data,
const QString &filename); const QString &filename);
void uploadFilter(const QString &filter) noexcept;
void joinRoom(const QString &roomIdOrAlias); void joinRoom(const QString &roomIdOrAlias);
void leaveRoom(const QString &roomId); void leaveRoom(const QString &roomId);
void sendTypingNotification(const QString &roomid, int timeoutInMillis = 20000); void sendTypingNotification(const QString &roomid, int timeoutInMillis = 20000);

View file

@ -226,6 +226,13 @@ MatrixClient::registerUser(const QString &user, const QString &pass, const QStri
void void
MatrixClient::sync() noexcept 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; QUrlQuery query;
query.addQueryItem("set_presence", "online"); query.addQueryItem("set_presence", "online");
query.addQueryItem("filter", filter_); 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 void
MatrixClient::joinRoom(const QString &roomIdOrAlias) MatrixClient::joinRoom(const QString &roomIdOrAlias)
{ {