summary refs log tree commit diff
path: root/src/MatrixClient.cc
diff options
context:
space:
mode:
authorKonstantinos Sideris <sideris.konstantin@gmail.com>2017-06-05 02:14:05 +0300
committerKonstantinos Sideris <sideris.konstantin@gmail.com>2017-06-05 02:14:05 +0300
commit95c492bad837b999df409c01ea3253d52951c18c (patch)
treed2ed1f13db32071f3181abd6ab3b3e57d035dc13 /src/MatrixClient.cc
parentShow confirmation dialog on logout (diff)
downloadnheko-95c492bad837b999df409c01ea3253d52951c18c.tar.xz
Experimental support for user avatars in timeline
Diffstat (limited to 'src/MatrixClient.cc')
-rw-r--r--src/MatrixClient.cc52
1 files changed, 52 insertions, 0 deletions
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<int>(Endpoint::RoomAvatar));
 }
 
+void MatrixClient::fetchUserAvatar(const QString &userId, const QUrl &avatarUrl)
+{
+	QList<QString> 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<int>(Endpoint::UserAvatar));
+}
+
 void MatrixClient::downloadImage(const QString &event_id, const QUrl &url)
 {
 	QNetworkRequest image_request(url);