From c123bada94bfd21121d6d847c472c910b88fed65 Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Sun, 21 Jan 2018 20:28:38 +0200 Subject: [PATCH] Refactor avatar fetching in one function --- include/ChatPage.h | 1 - include/Community.h | 3 -- include/MatrixClient.h | 7 +++-- src/AvatarProvider.cc | 10 +++++-- src/ChatPage.cc | 12 +++----- src/Community.cc | 2 -- src/MatrixClient.cc | 62 ++++++------------------------------------ 7 files changed, 23 insertions(+), 74 deletions(-) diff --git a/include/ChatPage.h b/include/ChatPage.h index 0eae3838..3da84b33 100644 --- a/include/ChatPage.h +++ b/include/ChatPage.h @@ -83,7 +83,6 @@ private slots: void updateTopBarAvatar(const QString &roomid, const QPixmap &img); void updateOwnProfileInfo(const QUrl &avatar_url, const QString &display_name); void updateOwnCommunitiesInfo(const QList &own_communities); - void setOwnAvatar(const QPixmap &img); void initialSyncCompleted(const mtx::responses::Sync &response); void syncCompleted(const mtx::responses::Sync &response); void changeTopRoomInfo(const QString &room_id); diff --git a/include/Community.h b/include/Community.h index 0d70dee1..5f3adba0 100644 --- a/include/Community.h +++ b/include/Community.h @@ -19,9 +19,6 @@ public: inline QString getLongDescription() const; inline const QList getRoomList() const; -signals: - void roomsChanged(QList &rooms); - private: QUrl avatar_; QString name_; diff --git a/include/MatrixClient.h b/include/MatrixClient.h index 6847ab22..d3eebe49 100644 --- a/include/MatrixClient.h +++ b/include/MatrixClient.h @@ -47,11 +47,13 @@ public: const QString &server) noexcept; void versions() noexcept; void fetchRoomAvatar(const QString &roomid, const QUrl &avatar_url); - void fetchUserAvatar(const QString &userId, const QUrl &avatarUrl); + //! Download user's avatar. + void fetchUserAvatar(const QUrl &avatarUrl, + std::function onSuccess, + std::function onError); void fetchCommunityAvatar(const QString &communityId, const QUrl &avatarUrl); void fetchCommunityProfile(const QString &communityId); void fetchCommunityRooms(const QString &communityId); - void fetchOwnAvatar(const QUrl &avatar_url); void downloadImage(const QString &event_id, const QUrl &url); void downloadFile(const QString &event_id, const QUrl &url); void messages(const QString &room_id, const QString &from_token, int limit = 30) noexcept; @@ -120,7 +122,6 @@ signals: void communityAvatarRetrieved(const QString &communityId, const QPixmap &img); void communityProfileRetrieved(const QString &communityId, const QJsonObject &profile); void communityRoomsRetrieved(const QString &communityId, const QJsonObject &rooms); - void ownAvatarRetrieved(const QPixmap &img); void imageDownloaded(const QString &event_id, const QPixmap &img); void fileDownloaded(const QString &event_id, const QByteArray &data); diff --git a/src/AvatarProvider.cc b/src/AvatarProvider.cc index b8ea9e20..881ef2d5 100644 --- a/src/AvatarProvider.cc +++ b/src/AvatarProvider.cc @@ -27,8 +27,6 @@ void AvatarProvider::init(QSharedPointer client) { client_ = client; - - connect(client_.data(), &MatrixClient::userAvatarRetrieved, &AvatarProvider::updateAvatar); } void @@ -65,7 +63,13 @@ AvatarProvider::resolve(const QString &userId, std::function callb // Add the current timeline item to the waiting list for this avatar. if (!toBeResolved_.contains(userId)) { - client_->fetchUserAvatar(userId, avatars_[userId].url); + client_->fetchUserAvatar(avatars_[userId].url, + [userId](QImage image) { updateAvatar(userId, image); }, + [userId](QString error) { + qWarning() + << error << ": failed to retrieve user avatar" + << userId; + }); QList> items; items.push_back(callback); diff --git a/src/ChatPage.cc b/src/ChatPage.cc index 71552bcf..596f72dc 100644 --- a/src/ChatPage.cc +++ b/src/ChatPage.cc @@ -349,7 +349,6 @@ ChatPage::ChatPage(QSharedPointer client, } }); - connect(client_.data(), &MatrixClient::ownAvatarRetrieved, this, &ChatPage::setOwnAvatar); connect(client_.data(), &MatrixClient::joinedRoom, this, [=](const QString &room_id) { emit showNotification("You joined the room."); removeInvite(room_id); @@ -493,12 +492,6 @@ ChatPage::bootstrap(QString userid, QString homeserver, QString token) client_->initialSync(); } -void -ChatPage::setOwnAvatar(const QPixmap &img) -{ - user_info_widget_->setAvatar(img.toImage()); -} - void ChatPage::syncCompleted(const mtx::responses::Sync &response) { @@ -597,7 +590,10 @@ ChatPage::updateOwnProfileInfo(const QUrl &avatar_url, const QString &display_na user_info_widget_->setDisplayName(display_name); if (avatar_url.isValid()) - client_->fetchOwnAvatar(avatar_url); + client_->fetchUserAvatar( + avatar_url, + [=](QImage img) { user_info_widget_->setAvatar(img); }, + [=](QString error) { qWarning() << error << ": failed to fetch own avatar"; }); } void diff --git a/src/Community.cc b/src/Community.cc index df425e88..a68f2b18 100644 --- a/src/Community.cc +++ b/src/Community.cc @@ -39,6 +39,4 @@ Community::parseRooms(const QJsonObject &rooms) for (auto i = 0; i < rooms["chunk"].toArray().size(); i++) { rooms_.append(rooms["chunk"].toArray()[i].toObject()["room_id"].toString()); } - - emit roomsChanged(rooms_); } diff --git a/src/MatrixClient.cc b/src/MatrixClient.cc index fdc675b4..dd7c1c88 100644 --- a/src/MatrixClient.cc +++ b/src/MatrixClient.cc @@ -681,7 +681,9 @@ MatrixClient::fetchCommunityRooms(const QString &communityId) } void -MatrixClient::fetchUserAvatar(const QString &userId, const QUrl &avatarUrl) +MatrixClient::fetchUserAvatar(const QUrl &avatarUrl, + std::function onSuccess, + std::function onError) { QList url_parts = avatarUrl.toString().split("mxc://"); @@ -704,25 +706,23 @@ MatrixClient::fetchUserAvatar(const QString &userId, const QUrl &avatarUrl) QNetworkRequest avatar_request(endpoint); auto reply = get(avatar_request); - connect(reply, &QNetworkReply::finished, this, [this, reply, userId]() { + connect(reply, &QNetworkReply::finished, this, [this, reply, onSuccess, onError]() { reply->deleteLater(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - if (status == 0 || status >= 400) { - qWarning() << reply->errorString(); - return; - } + if (status == 0 || status >= 400) + return onError(reply->errorString()); auto data = reply->readAll(); if (data.size() == 0) - return; + return onError("received avatar with no data"); QImage img; img.loadFromData(data); - emit userAvatarRetrieved(userId, img); + onSuccess(std::move(img)); }); } @@ -780,52 +780,6 @@ MatrixClient::downloadFile(const QString &event_id, const QUrl &url) }); } -void -MatrixClient::fetchOwnAvatar(const QUrl &avatar_url) -{ - QList url_parts = avatar_url.toString().split("mxc://"); - - if (url_parts.size() != 2) { - qDebug() << "Invalid format for media " << 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); - - auto reply = get(avatar_request); - connect(reply, &QNetworkReply::finished, this, [this, reply]() { - 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 ownAvatarRetrieved(pixmap); - }); -} - void MatrixClient::messages(const QString &roomid, const QString &from_token, int limit) noexcept {