Refactor avatar fetching in one function

This commit is contained in:
Konstantinos Sideris 2018-01-21 20:28:38 +02:00
parent fc890f572c
commit c123bada94
7 changed files with 23 additions and 74 deletions

View file

@ -83,7 +83,6 @@ private slots:
void updateTopBarAvatar(const QString &roomid, const QPixmap &img); void updateTopBarAvatar(const QString &roomid, const QPixmap &img);
void updateOwnProfileInfo(const QUrl &avatar_url, const QString &display_name); void updateOwnProfileInfo(const QUrl &avatar_url, const QString &display_name);
void updateOwnCommunitiesInfo(const QList<QString> &own_communities); void updateOwnCommunitiesInfo(const QList<QString> &own_communities);
void setOwnAvatar(const QPixmap &img);
void initialSyncCompleted(const mtx::responses::Sync &response); void initialSyncCompleted(const mtx::responses::Sync &response);
void syncCompleted(const mtx::responses::Sync &response); void syncCompleted(const mtx::responses::Sync &response);
void changeTopRoomInfo(const QString &room_id); void changeTopRoomInfo(const QString &room_id);

View file

@ -19,9 +19,6 @@ public:
inline QString getLongDescription() const; inline QString getLongDescription() const;
inline const QList<QString> getRoomList() const; inline const QList<QString> getRoomList() const;
signals:
void roomsChanged(QList<QString> &rooms);
private: private:
QUrl avatar_; QUrl avatar_;
QString name_; QString name_;

View file

@ -47,11 +47,13 @@ public:
const QString &server) noexcept; const QString &server) noexcept;
void versions() noexcept; void versions() noexcept;
void fetchRoomAvatar(const QString &roomid, const QUrl &avatar_url); 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<void(QImage)> onSuccess,
std::function<void(QString)> onError);
void fetchCommunityAvatar(const QString &communityId, const QUrl &avatarUrl); void fetchCommunityAvatar(const QString &communityId, const QUrl &avatarUrl);
void fetchCommunityProfile(const QString &communityId); void fetchCommunityProfile(const QString &communityId);
void fetchCommunityRooms(const QString &communityId); void fetchCommunityRooms(const QString &communityId);
void fetchOwnAvatar(const QUrl &avatar_url);
void downloadImage(const QString &event_id, const QUrl &url); void downloadImage(const QString &event_id, const QUrl &url);
void downloadFile(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; 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 communityAvatarRetrieved(const QString &communityId, const QPixmap &img);
void communityProfileRetrieved(const QString &communityId, const QJsonObject &profile); void communityProfileRetrieved(const QString &communityId, const QJsonObject &profile);
void communityRoomsRetrieved(const QString &communityId, const QJsonObject &rooms); void communityRoomsRetrieved(const QString &communityId, const QJsonObject &rooms);
void ownAvatarRetrieved(const QPixmap &img);
void imageDownloaded(const QString &event_id, const QPixmap &img); void imageDownloaded(const QString &event_id, const QPixmap &img);
void fileDownloaded(const QString &event_id, const QByteArray &data); void fileDownloaded(const QString &event_id, const QByteArray &data);

View file

@ -27,8 +27,6 @@ void
AvatarProvider::init(QSharedPointer<MatrixClient> client) AvatarProvider::init(QSharedPointer<MatrixClient> client)
{ {
client_ = client; client_ = client;
connect(client_.data(), &MatrixClient::userAvatarRetrieved, &AvatarProvider::updateAvatar);
} }
void void
@ -65,7 +63,13 @@ AvatarProvider::resolve(const QString &userId, std::function<void(QImage)> callb
// Add the current timeline item to the waiting list for this avatar. // Add the current timeline item to the waiting list for this avatar.
if (!toBeResolved_.contains(userId)) { 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<std::function<void(QImage)>> items; QList<std::function<void(QImage)>> items;
items.push_back(callback); items.push_back(callback);

View file

@ -349,7 +349,6 @@ ChatPage::ChatPage(QSharedPointer<MatrixClient> client,
} }
}); });
connect(client_.data(), &MatrixClient::ownAvatarRetrieved, this, &ChatPage::setOwnAvatar);
connect(client_.data(), &MatrixClient::joinedRoom, this, [=](const QString &room_id) { connect(client_.data(), &MatrixClient::joinedRoom, this, [=](const QString &room_id) {
emit showNotification("You joined the room."); emit showNotification("You joined the room.");
removeInvite(room_id); removeInvite(room_id);
@ -493,12 +492,6 @@ ChatPage::bootstrap(QString userid, QString homeserver, QString token)
client_->initialSync(); client_->initialSync();
} }
void
ChatPage::setOwnAvatar(const QPixmap &img)
{
user_info_widget_->setAvatar(img.toImage());
}
void void
ChatPage::syncCompleted(const mtx::responses::Sync &response) 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); user_info_widget_->setDisplayName(display_name);
if (avatar_url.isValid()) 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 void

View file

@ -39,6 +39,4 @@ Community::parseRooms(const QJsonObject &rooms)
for (auto i = 0; i < rooms["chunk"].toArray().size(); i++) { for (auto i = 0; i < rooms["chunk"].toArray().size(); i++) {
rooms_.append(rooms["chunk"].toArray()[i].toObject()["room_id"].toString()); rooms_.append(rooms["chunk"].toArray()[i].toObject()["room_id"].toString());
} }
emit roomsChanged(rooms_);
} }

View file

@ -681,7 +681,9 @@ MatrixClient::fetchCommunityRooms(const QString &communityId)
} }
void void
MatrixClient::fetchUserAvatar(const QString &userId, const QUrl &avatarUrl) MatrixClient::fetchUserAvatar(const QUrl &avatarUrl,
std::function<void(QImage)> onSuccess,
std::function<void(QString)> onError)
{ {
QList<QString> url_parts = avatarUrl.toString().split("mxc://"); QList<QString> url_parts = avatarUrl.toString().split("mxc://");
@ -704,25 +706,23 @@ MatrixClient::fetchUserAvatar(const QString &userId, const QUrl &avatarUrl)
QNetworkRequest avatar_request(endpoint); QNetworkRequest avatar_request(endpoint);
auto reply = get(avatar_request); auto reply = get(avatar_request);
connect(reply, &QNetworkReply::finished, this, [this, reply, userId]() { connect(reply, &QNetworkReply::finished, this, [this, reply, onSuccess, onError]() {
reply->deleteLater(); reply->deleteLater();
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if (status == 0 || status >= 400) { if (status == 0 || status >= 400)
qWarning() << reply->errorString(); return onError(reply->errorString());
return;
}
auto data = reply->readAll(); auto data = reply->readAll();
if (data.size() == 0) if (data.size() == 0)
return; return onError("received avatar with no data");
QImage img; QImage img;
img.loadFromData(data); 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<QString> 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 void
MatrixClient::messages(const QString &roomid, const QString &from_token, int limit) noexcept MatrixClient::messages(const QString &roomid, const QString &from_token, int limit) noexcept
{ {