Refactor avatar fetching in one function
This commit is contained in:
parent
fc890f572c
commit
c123bada94
7 changed files with 23 additions and 74 deletions
|
|
@ -27,8 +27,6 @@ void
|
|||
AvatarProvider::init(QSharedPointer<MatrixClient> client)
|
||||
{
|
||||
client_ = client;
|
||||
|
||||
connect(client_.data(), &MatrixClient::userAvatarRetrieved, &AvatarProvider::updateAvatar);
|
||||
}
|
||||
|
||||
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.
|
||||
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;
|
||||
items.push_back(callback);
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
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
|
||||
|
|
|
|||
|
|
@ -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_);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<void(QImage)> onSuccess,
|
||||
std::function<void(QString)> onError)
|
||||
{
|
||||
QList<QString> 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<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
|
||||
MatrixClient::messages(const QString &roomid, const QString &from_token, int limit) noexcept
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue