From 49b313e3b85911acfc8186e548608bd44324a986 Mon Sep 17 00:00:00 2001
From: tastytea <tastytea@tastytea.de>
Date: Thu, 13 Jan 2022 00:39:26 +0100
Subject: [PATCH] Hidden events: Make save-function reusable for global account
 data

---
 resources/qml/dialogs/HiddenEventsDialog.qml |  2 +-
 src/ui/RoomSettings.cpp                      | 24 +++++++++++++-------
 src/ui/RoomSettings.h                        |  3 ++-
 3 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/resources/qml/dialogs/HiddenEventsDialog.qml b/resources/qml/dialogs/HiddenEventsDialog.qml
index 004c8d301..8f2d01fdd 100644
--- a/resources/qml/dialogs/HiddenEventsDialog.qml
+++ b/resources/qml/dialogs/HiddenEventsDialog.qml
@@ -121,7 +121,7 @@ ApplicationWindow {
             if (!toggleSticker.checked) {
                 events.push("m.sticker");
             }
-            roomSettings.saveHiddenEventsSettings(events);
+            roomSettings.saveHiddenEventsSettings(events, roomSettings.roomId);
 
             hiddenEventsDialog.close();
         }
diff --git a/src/ui/RoomSettings.cpp b/src/ui/RoomSettings.cpp
index 0502b281d..3d68aebb5 100644
--- a/src/ui/RoomSettings.cpp
+++ b/src/ui/RoomSettings.cpp
@@ -427,9 +427,8 @@ RoomSettings::openEditModal()
 }
 
 void
-RoomSettings::saveHiddenEventsSettings(const QSet<QString> events)
+RoomSettings::saveHiddenEventsSettings(const QSet<QString> &events, const QString &roomId)
 {
-    // TODO: Make this reusable for global account settings.
     mtx::events::account_data::nheko_extensions::HiddenEvents hiddenEvents;
     hiddenEvents.hidden_event_types = {
       EventType::Reaction, EventType::CallCandidates, EventType::Unsupported};
@@ -438,12 +437,21 @@ RoomSettings::saveHiddenEventsSettings(const QSet<QString> events)
           mtx::events::getEventType(event.toStdString()));
     }
 
-    const auto roomid = roomid_.toStdString();
-    http::client()->put_room_account_data(roomid, hiddenEvents, [&roomid](mtx::http::RequestErr e) {
-        if (e) {
-            nhlog::net()->error("Failed to update room account data in {}: {}", roomid, *e);
-        }
-    });
+    if (!roomId.isEmpty()) {
+        const auto rid = roomId.toStdString();
+        http::client()->put_room_account_data(rid, hiddenEvents, [&rid](mtx::http::RequestErr e) {
+            if (e) {
+                nhlog::net()->error(
+                  "Failed to update room account data with hidden events in {}: {}", rid, *e);
+            }
+        });
+    } else {
+        http::client()->put_account_data(hiddenEvents, [](mtx::http::RequestErr e) {
+            if (e) {
+                nhlog::net()->error("Failed to update account data with hidden events: {}", *e);
+            }
+        });
+    }
 }
 
 void
diff --git a/src/ui/RoomSettings.h b/src/ui/RoomSettings.h
index d5d42ce04..c6f4e95a5 100644
--- a/src/ui/RoomSettings.h
+++ b/src/ui/RoomSettings.h
@@ -109,7 +109,8 @@ public:
     Q_INVOKABLE void enableEncryption();
     Q_INVOKABLE void updateAvatar();
     Q_INVOKABLE void openEditModal();
-    Q_INVOKABLE void saveHiddenEventsSettings(QSet<QString> events);
+    Q_INVOKABLE void
+    saveHiddenEventsSettings(const QSet<QString> &events, const QString &roomId = {});
     Q_INVOKABLE void changeAccessRules(int index);
     Q_INVOKABLE void changeNotifications(int currentIndex);
     Q_INVOKABLE bool eventHidden(QString event) const;
-- 
GitLab