summary refs log tree commit diff
path: root/src/ui/RoomSettings.cpp
diff options
context:
space:
mode:
authorNicolas Werner <nicolas.werner@hotmail.de>2023-06-24 01:04:54 +0200
committerNicolas Werner <nicolas.werner@hotmail.de>2023-06-24 01:04:54 +0200
commitfa653bc078ca80508b5de27ac02c77980dda4c45 (patch)
tree6e46f203e41eb592870e4ab9a8409ec0db1af04f /src/ui/RoomSettings.cpp
parentBump arm job count (diff)
downloadnheko-fa653bc078ca80508b5de27ac02c77980dda4c45.tar.xz
Allow viewing and changing the history visibility
Diffstat (limited to 'src/ui/RoomSettings.cpp')
-rw-r--r--src/ui/RoomSettings.cpp81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/ui/RoomSettings.cpp b/src/ui/RoomSettings.cpp
index 97c7cd61..769f2c8d 100644
--- a/src/ui/RoomSettings.cpp
+++ b/src/ui/RoomSettings.cpp
@@ -74,6 +74,11 @@ RoomSettings::RoomSettings(QString roomid, QObject *parent)
     guestRules_ = info_.guest_access ? AccessState::CanJoin : AccessState::Forbidden;
     emit accessJoinRulesChanged();
 
+    if (auto ev = cache::client()->getStateEvent<mtx::events::state::HistoryVisibility>(
+          roomid_.toStdString())) {
+        this->historyVisibility_ = ev->content.history_visibility;
+    }
+
     this->allowedRoomsModel = new RoomSettingsAllowedRoomsModel(this);
 }
 
@@ -151,6 +156,22 @@ RoomSettings::notifications()
     return notifications_;
 }
 
+RoomSettings::Visibility
+RoomSettings::historyVisibility() const
+{
+    switch (this->historyVisibility_) {
+    case mtx::events::state::Visibility::WorldReadable:
+        return WorldReadable;
+    case mtx::events::state::Visibility::Joined:
+        return Joined;
+    case mtx::events::state::Visibility::Invited:
+        return Invited;
+    case mtx::events::state::Visibility::Shared:
+        return Shared;
+    }
+    return Shared;
+}
+
 bool
 RoomSettings::privateAccess() const
 {
@@ -279,6 +300,20 @@ RoomSettings::canChangeAvatar() const
 }
 
 bool
+RoomSettings::canChangeHistoryVisibility() const
+{
+    try {
+        return cache::hasEnoughPowerLevel({EventType::RoomHistoryVisibility},
+                                          roomid_.toStdString(),
+                                          utils::localUser().toStdString());
+    } catch (const lmdb::error &e) {
+        nhlog::db()->warn("lmdb error: {}", e.what());
+    }
+
+    return false;
+}
+
+bool
 RoomSettings::isEncryptionEnabled() const
 {
     return usesEncryption_;
@@ -458,6 +493,52 @@ RoomSettings::changeName(const QString &name)
 }
 
 void
+RoomSettings::changeHistoryVisibility(Visibility value)
+{
+    auto tempVis = mtx::events::state::Visibility::Shared;
+
+    switch (value) {
+    case WorldReadable:
+        tempVis = mtx::events::state::Visibility::WorldReadable;
+        break;
+    case Joined:
+        tempVis = mtx::events::state::Visibility::Joined;
+        break;
+    case Invited:
+        tempVis = mtx::events::state::Visibility::Invited;
+        break;
+    case Shared:
+        tempVis = mtx::events::state::Visibility::Shared;
+        break;
+    default:
+        return;
+    }
+
+    using namespace mtx::events;
+    auto proxy = std::make_shared<ThreadProxy>();
+    connect(proxy.get(), &ThreadProxy::eventSent, this, [this, tempVis]() {
+        this->historyVisibility_ = tempVis;
+        emit historyVisibilityChanged();
+    });
+    connect(proxy.get(), &ThreadProxy::error, this, &RoomSettings::displayError);
+
+    state::HistoryVisibility body;
+    body.history_visibility = tempVis;
+
+    http::client()->send_state_event(
+      roomid_.toStdString(),
+      body,
+      [proxy](const mtx::responses::EventId &, mtx::http::RequestErr err) {
+          if (err) {
+              emit proxy->error(QString::fromStdString(err->matrix_error.error));
+              return;
+          }
+
+          emit proxy->eventSent();
+      });
+}
+
+void
 RoomSettings::changeTopic(const QString &topic)
 {
     // Check if the values are changed from the originals.