From 95c492bad837b999df409c01ea3253d52951c18c Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Mon, 5 Jun 2017 02:14:05 +0300 Subject: Experimental support for user avatars in timeline --- src/MatrixClient.cc | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'src/MatrixClient.cc') diff --git a/src/MatrixClient.cc b/src/MatrixClient.cc index a605623f..927db541 100644 --- a/src/MatrixClient.cc +++ b/src/MatrixClient.cc @@ -287,6 +287,29 @@ void MatrixClient::onRoomAvatarResponse(QNetworkReply *reply) emit roomAvatarRetrieved(roomid, pixmap); } +void MatrixClient::onUserAvatarResponse(QNetworkReply *reply) +{ + reply->deleteLater(); + + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + if (status == 0 || status >= 400) { + qWarning() << reply->errorString(); + return; + } + + auto data = reply->readAll(); + + if (data.size() == 0) + return; + + auto roomid = reply->property("userid").toString(); + + QImage img; + img.loadFromData(data); + + emit userAvatarRetrieved(roomid, img); +} void MatrixClient::onGetOwnAvatarResponse(QNetworkReply *reply) { reply->deleteLater(); @@ -392,6 +415,9 @@ void MatrixClient::onResponse(QNetworkReply *reply) case Endpoint::RoomAvatar: onRoomAvatarResponse(reply); break; + case Endpoint::UserAvatar: + onUserAvatarResponse(reply); + break; case Endpoint::GetOwnAvatar: onGetOwnAvatarResponse(reply); break; @@ -591,6 +617,32 @@ void MatrixClient::fetchRoomAvatar(const QString &roomid, const QUrl &avatar_url reply->setProperty("endpoint", static_cast(Endpoint::RoomAvatar)); } +void MatrixClient::fetchUserAvatar(const QString &userId, const QUrl &avatarUrl) +{ + QList url_parts = avatarUrl.toString().split("mxc://"); + + if (url_parts.size() != 2) { + qDebug() << "Invalid format for user avatar " << avatarUrl.toString(); + return; + } + + QUrlQuery query; + query.addQueryItem("width", "128"); + query.addQueryItem("height", "128"); + 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); + reply->setProperty("userid", userId); + reply->setProperty("endpoint", static_cast(Endpoint::UserAvatar)); +} + void MatrixClient::downloadImage(const QString &event_id, const QUrl &url) { QNetworkRequest image_request(url); -- cgit 1.5.1