From 7e9bf3911e0457bf5af21672d4325882584b78ad Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sat, 16 Feb 2019 20:00:46 +0900 Subject: Room::canSwitchVersions() --- lib/room.cpp | 40 +++++++++++++++++++++++----------------- lib/room.h | 3 +++ 2 files changed, 26 insertions(+), 17 deletions(-) (limited to 'lib') diff --git a/lib/room.cpp b/lib/room.cpp index 3a37053d..538c1562 100644 --- a/lib/room.cpp +++ b/lib/room.cpp @@ -573,6 +573,26 @@ void Room::markAllMessagesAsRead() d->markMessagesAsRead(d->timeline.crbegin()); } +bool Room::canSwitchVersions() const +{ + // TODO, #276: m.room.power_levels + const auto* plEvt = + d->currentState.value({"m.room.power_levels", ""}); + if (!plEvt) + return true; + + const auto plJson = plEvt->contentJson(); + const auto currentUserLevel = + plJson.value("users"_ls).toObject() + .value(localUser()->id()).toInt( + plJson.value("users_default"_ls).toInt()); + const auto tombstonePowerLevel = + plJson.value("events").toObject() + .value("m.room.tombstone"_ls).toInt( + plJson.value("state_default"_ls).toInt()); + return currentUserLevel >= tombstonePowerLevel; +} + bool Room::hasUnreadMessages() const { return unreadCount() >= 0; @@ -1622,24 +1642,10 @@ void Room::checkVersion() { qCDebug(MAIN) << this << "version is" << version() << "which the server doesn't count as stable"; - // TODO, #276: m.room.power_levels - if (const auto* plEvt = - d->currentState.value({"m.room.power_levels", ""})) + if (canSwitchVersions()) { - const auto plJson = plEvt->contentJson(); - const auto currentUserLevel = - plJson.value("users"_ls).toObject() - .value(localUser()->id()).toInt( - plJson.value("users_default"_ls).toInt()); - const auto tombstonePowerLevel = - plJson.value("events").toObject() - .value("m.room.tombstone"_ls).toInt( - plJson.value("state_default"_ls).toInt()); - if (currentUserLevel >= tombstonePowerLevel) - { - qCDebug(MAIN) << "The current user has enough privileges to fix it"; - emit unstableVersion(defaultVersion, stableVersions); - } + qCDebug(MAIN) << "The current user has enough privileges to fix it"; + emit unstableVersion(defaultVersion, stableVersions); } } } diff --git a/lib/room.h b/lib/room.h index e09da94c..f12627f3 100644 --- a/lib/room.h +++ b/lib/room.h @@ -424,6 +424,9 @@ namespace QMatrixClient /// Mark all messages in the room as read void markAllMessagesAsRead(); + /// Whether the current user is allowed to upgrade the room + bool canSwitchVersions() const; + /// Switch the room's version (aka upgrade) void switchVersion(QString newVersion); -- cgit v1.2.3