Remove duplicated verification status calculation

This commit is contained in:
Nicolas Werner 2021-10-07 19:55:27 +02:00
parent 456a41bcdf
commit 47db1e5c65
No known key found for this signature in database
GPG key ID: C8D75E610773F2D9
2 changed files with 55 additions and 53 deletions

View file

@ -34,6 +34,7 @@ UserProfile::UserProfile(QString roomid,
this, this,
&UserProfile::setGlobalUsername, &UserProfile::setGlobalUsername,
Qt::QueuedConnection); Qt::QueuedConnection);
connect(this, &UserProfile::verificationStatiChanged, &UserProfile::updateVerificationStatus);
if (isGlobalUserProfile()) { if (isGlobalUserProfile()) {
getGlobalProfileData(); getGlobalProfileData();
@ -48,22 +49,7 @@ UserProfile::UserProfile(QString roomid,
if (user_id != this->userid_.toStdString()) if (user_id != this->userid_.toStdString())
return; return;
auto status = cache::verificationStatus(user_id); emit verificationStatiChanged();
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();
}); });
fetchDeviceList(this->userid_); fetchDeviceList(this->userid_);
} }
@ -170,20 +156,18 @@ UserProfile::fetchDeviceList(const QString &userID)
cache::client()->query_keys( cache::client()->query_keys(
userID.toStdString(), 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) { mtx::http::RequestErr err) {
if (err) { if (err) {
nhlog::net()->warn("failed to query device keys: {},{}", nhlog::net()->warn("failed to query device keys: {},{}",
mtx::errors::to_string(err->matrix_error.errcode), mtx::errors::to_string(err->matrix_error.errcode),
static_cast<int>(err->status_code)); static_cast<int>(err->status_code));
return;
} }
// Ensure local key cache is up to date // Ensure local key cache is up to date
cache::client()->query_keys( cache::client()->query_keys(
utils::localUser().toStdString(), utils::localUser().toStdString(),
[other_user_id, other_user_keys, this](const UserKeyCache &, [this](const UserKeyCache &, mtx::http::RequestErr err) {
mtx::http::RequestErr err) {
using namespace mtx; using namespace mtx;
std::string local_user_id = utils::localUser().toStdString(); 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: {},{}", nhlog::net()->warn("failed to query device keys: {},{}",
mtx::errors::to_string(err->matrix_error.errcode), mtx::errors::to_string(err->matrix_error.errcode),
static_cast<int>(err->status_code)); static_cast<int>(err->status_code));
return;
} }
this->hasMasterKey = !other_user_keys.master_keys.keys.empty(); emit verificationStatiChanged();
std::vector<DeviceInfo> 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();
}); });
}); });
} }
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> 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 void
UserProfile::banUser() UserProfile::banUser()
{ {

View file

@ -137,11 +137,15 @@ signals:
void globalUsernameRetrieved(const QString &globalUser); void globalUsernameRetrieved(const QString &globalUser);
void devicesChanged(); void devicesChanged();
// internal
void verificationStatiChanged();
public slots: public slots:
void updateAvatarUrl(); void updateAvatarUrl();
protected slots: private slots:
void setGlobalUsername(const QString &globalUser); void setGlobalUsername(const QString &globalUser);
void updateVerificationStatus();
private: private:
void updateRoomMemberState(mtx::events::state::Member member); void updateRoomMemberState(mtx::events::state::Member member);