From 47db1e5c65eb5cb8b9b0d8d50fb33f69f9665f30 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Thu, 7 Oct 2021 19:55:27 +0200 Subject: [PATCH] Remove duplicated verification status calculation --- src/ui/UserProfile.cpp | 102 ++++++++++++++++++++--------------------- src/ui/UserProfile.h | 6 ++- 2 files changed, 55 insertions(+), 53 deletions(-) diff --git a/src/ui/UserProfile.cpp b/src/ui/UserProfile.cpp index 62488954..5ddf011f 100644 --- a/src/ui/UserProfile.cpp +++ b/src/ui/UserProfile.cpp @@ -34,6 +34,7 @@ UserProfile::UserProfile(QString roomid, this, &UserProfile::setGlobalUsername, Qt::QueuedConnection); + connect(this, &UserProfile::verificationStatiChanged, &UserProfile::updateVerificationStatus); if (isGlobalUserProfile()) { getGlobalProfileData(); @@ -48,22 +49,7 @@ UserProfile::UserProfile(QString roomid, if (user_id != this->userid_.toStdString()) return; - auto status = cache::verificationStatus(user_id); - if (!status) - return; - this->isUserVerified = status->user_verified; - emit userStatusChanged(); - - for (auto &deviceInfo : deviceList_.deviceList_) { - deviceInfo.verification_status = - std::find(status->verified_devices.begin(), - status->verified_devices.end(), - deviceInfo.device_id.toStdString()) == status->verified_devices.end() - ? verification::UNVERIFIED - : verification::VERIFIED; - } - deviceList_.reset(deviceList_.deviceList_); - emit devicesChanged(); + emit verificationStatiChanged(); }); fetchDeviceList(this->userid_); } @@ -170,20 +156,18 @@ UserProfile::fetchDeviceList(const QString &userID) cache::client()->query_keys( userID.toStdString(), - [other_user_id = userID.toStdString(), this](const UserKeyCache &other_user_keys, + [other_user_id = userID.toStdString(), this](const UserKeyCache &, mtx::http::RequestErr err) { if (err) { nhlog::net()->warn("failed to query device keys: {},{}", mtx::errors::to_string(err->matrix_error.errcode), static_cast(err->status_code)); - return; } // Ensure local key cache is up to date cache::client()->query_keys( utils::localUser().toStdString(), - [other_user_id, other_user_keys, this](const UserKeyCache &, - mtx::http::RequestErr err) { + [this](const UserKeyCache &, mtx::http::RequestErr err) { using namespace mtx; std::string local_user_id = utils::localUser().toStdString(); @@ -191,44 +175,58 @@ UserProfile::fetchDeviceList(const QString &userID) nhlog::net()->warn("failed to query device keys: {},{}", mtx::errors::to_string(err->matrix_error.errcode), static_cast(err->status_code)); - return; } - this->hasMasterKey = !other_user_keys.master_keys.keys.empty(); - - std::vector deviceInfo; - auto devices = other_user_keys.device_keys; - auto verificationStatus = cache::client()->verificationStatus(other_user_id); - - isUserVerified = verificationStatus.user_verified; - emit userStatusChanged(); - - for (const auto &d : devices) { - auto device = d.second; - verification::Status verified = verification::Status::UNVERIFIED; - - if (std::find(verificationStatus.verified_devices.begin(), - verificationStatus.verified_devices.end(), - device.device_id) != verificationStatus.verified_devices.end() && - mtx::crypto::verify_identity_signature( - device, DeviceId(device.device_id), UserId(other_user_id))) - verified = verification::Status::VERIFIED; - - if (isSelf() && device.device_id == ::http::client()->device_id()) - verified = verification::Status::SELF; - - deviceInfo.push_back( - {QString::fromStdString(d.first), - QString::fromStdString(device.unsigned_info.device_display_name), - verified}); - } - - this->deviceList_.queueReset(std::move(deviceInfo)); - emit devicesChanged(); + emit verificationStatiChanged(); }); }); } +void +UserProfile::updateVerificationStatus() +{ + if (!cache::client() || !cache::client()->isDatabaseReady()) + return; + + auto user_keys = cache::client()->userKeys(userid_.toStdString()); + if (!user_keys) { + this->hasMasterKey = false; + this->isUserVerified = crypto::Trust::Unverified; + this->deviceList_.reset({}); + emit userStatusChanged(); + return; + } + + this->hasMasterKey = !user_keys->master_keys.keys.empty(); + + std::vector deviceInfo; + auto devices = user_keys->device_keys; + auto verificationStatus = cache::client()->verificationStatus(userid_.toStdString()); + + this->isUserVerified = verificationStatus.user_verified; + emit userStatusChanged(); + + for (const auto &d : devices) { + auto device = d.second; + verification::Status verified = + std::find(verificationStatus.verified_devices.begin(), + verificationStatus.verified_devices.end(), + device.device_id) == verificationStatus.verified_devices.end() + ? verification::UNVERIFIED + : verification::VERIFIED; + + if (isSelf() && device.device_id == ::http::client()->device_id()) + verified = verification::Status::SELF; + + deviceInfo.push_back({QString::fromStdString(d.first), + QString::fromStdString(device.unsigned_info.device_display_name), + verified}); + } + + this->deviceList_.queueReset(std::move(deviceInfo)); + emit devicesChanged(); +} + void UserProfile::banUser() { diff --git a/src/ui/UserProfile.h b/src/ui/UserProfile.h index c83ec01e..68f9c21b 100644 --- a/src/ui/UserProfile.h +++ b/src/ui/UserProfile.h @@ -137,11 +137,15 @@ signals: void globalUsernameRetrieved(const QString &globalUser); void devicesChanged(); + // internal + void verificationStatiChanged(); + public slots: void updateAvatarUrl(); -protected slots: +private slots: void setGlobalUsername(const QString &globalUser); + void updateVerificationStatus(); private: void updateRoomMemberState(mtx::events::state::Member member);