summary refs log tree commit diff
path: root/src/MatrixClient.cc
diff options
context:
space:
mode:
authorMax Sandholm <max@sandholm.org>2018-01-09 15:07:32 +0200
committermujx <mujx@users.noreply.github.com>2018-01-09 15:07:32 +0200
commit312df6f3bbcba0ad502864b13f9c51b4854ea2ce (patch)
treed43396836cb2ba21b13f218a6d25a7c82049338b /src/MatrixClient.cc
parentMake usernames in timeline less bold (diff)
downloadnheko-312df6f3bbcba0ad502864b13f9c51b4854ea2ce.tar.xz
Communities (#195)
Diffstat (limited to 'src/MatrixClient.cc')
-rw-r--r--src/MatrixClient.cc148
1 files changed, 147 insertions, 1 deletions
diff --git a/src/MatrixClient.cc b/src/MatrixClient.cc

index 1b2e020d..72467385 100644 --- a/src/MatrixClient.cc +++ b/src/MatrixClient.cc
@@ -112,7 +112,6 @@ MatrixClient::login(const QString &username, const QString &password) noexcept } }); } - void MatrixClient::logout() noexcept { @@ -445,6 +444,46 @@ MatrixClient::getOwnProfile() noexcept } void +MatrixClient::getOwnCommunities() noexcept +{ + QUrlQuery query; + query.addQueryItem("access_token", token_); + + QUrl endpoint(server_); + endpoint.setPath(clientApiUrl_ + "/joined_groups"); + endpoint.setQuery(query); + + QNetworkRequest request(QString(endpoint.toEncoded())); + + QNetworkReply *reply = get(request); + connect(reply, &QNetworkReply::finished, this, [this, reply]() { + reply->deleteLater(); + + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + if (status >= 400) { + qWarning() << reply->errorString(); + return; + } + + auto data = reply->readAll(); + auto json = QJsonDocument::fromJson(data).object(); + + try { + QList<QString> response; + for (auto it = json["groups"].toArray().constBegin(); + it != json["groups"].toArray().constEnd(); + it++) { + response.append(it->toString()); + } + emit getOwnCommunitiesResponse(response); + } catch (DeserializationException &e) { + qWarning() << "Own communities:" << e.what(); + } + }); +} + +void MatrixClient::fetchRoomAvatar(const QString &roomid, const QUrl &avatar_url) { QList<QString> url_parts = avatar_url.toString().split("mxc://"); @@ -491,6 +530,113 @@ MatrixClient::fetchRoomAvatar(const QString &roomid, const QUrl &avatar_url) } void +MatrixClient::fetchCommunityAvatar(const QString &communityId, const QUrl &avatar_url) +{ + QList<QString> url_parts = avatar_url.toString().split("mxc://"); + + if (url_parts.size() != 2) { + qDebug() << "Invalid format for community avatar " << avatar_url.toString(); + return; + } + + QUrlQuery query; + query.addQueryItem("width", "512"); + query.addQueryItem("height", "512"); + query.addQueryItem("method", "crop"); + + QString media_url = + QString("%1/_matrix/media/r0/thumbnail/%2").arg(getHomeServer().toString(), url_parts[1]); + + QUrl endpoint(media_url); + endpoint.setQuery(query); + + QNetworkRequest avatar_request(endpoint); + + QNetworkReply *reply = get(avatar_request); + connect(reply, &QNetworkReply::finished, this, [this, reply, communityId]() { + reply->deleteLater(); + + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + if (status == 0 || status >= 400) { + qWarning() << reply->errorString(); + return; + } + + auto img = reply->readAll(); + + if (img.size() == 0) + return; + + QPixmap pixmap; + pixmap.loadFromData(img); + + emit communityAvatarRetrieved(communityId, pixmap); + }); +} + +void +MatrixClient::fetchCommunityProfile(const QString &communityId) +{ + QUrlQuery query; + query.addQueryItem("access_token", token_); + + QUrl endpoint(server_); + endpoint.setPath(clientApiUrl_ + "/groups/" + communityId + "/profile"); + endpoint.setQuery(query); + + QNetworkRequest request(QString(endpoint.toEncoded())); + + QNetworkReply *reply = get(request); + + connect(reply, &QNetworkReply::finished, this, [this, reply, communityId]() { + reply->deleteLater(); + + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + if (status >= 400) { + qWarning() << reply->errorString(); + return; + } + + auto data = reply->readAll(); + const auto json = QJsonDocument::fromJson(data).object(); + + emit communityProfileRetrieved(communityId, json); + }); +} + +void +MatrixClient::fetchCommunityRooms(const QString &communityId) +{ + QUrlQuery query; + query.addQueryItem("access_token", token_); + + QUrl endpoint(server_); + endpoint.setPath(clientApiUrl_ + "/groups/" + communityId + "/rooms"); + endpoint.setQuery(query); + + QNetworkRequest request(QString(endpoint.toEncoded())); + + QNetworkReply *reply = get(request); + connect(reply, &QNetworkReply::finished, this, [this, reply, communityId]() { + reply->deleteLater(); + + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + if (status >= 400) { + qWarning() << reply->errorString(); + return; + } + + auto data = reply->readAll(); + const auto json = QJsonDocument::fromJson(data).object(); + + emit communityRoomsRetrieved(communityId, json); + }); +} + +void MatrixClient::fetchUserAvatar(const QString &userId, const QUrl &avatarUrl) { QList<QString> url_parts = avatarUrl.toString().split("mxc://");