summary refs log tree commit diff
path: root/src/ui/HiddenEvents.cpp
diff options
context:
space:
mode:
authorNicolas Werner <nicolas.werner@hotmail.de>2022-02-12 19:27:51 +0100
committerNicolas Werner <nicolas.werner@hotmail.de>2022-02-12 19:48:53 +0100
commit47b822604d81a6c603ce6c9e868456c8ccc2e18d (patch)
tree73f06796cff7941b9f0e7ab2b2a9e59054da0afa /src/ui/HiddenEvents.cpp
parentHidden events: Make reusable read function (diff)
downloadnheko-47b822604d81a6c603ce6c9e868456c8ccc2e18d.tar.xz
Hidden events: Refactor slightly for reusability
Diffstat (limited to 'src/ui/HiddenEvents.cpp')
-rw-r--r--src/ui/HiddenEvents.cpp93
1 files changed, 93 insertions, 0 deletions
diff --git a/src/ui/HiddenEvents.cpp b/src/ui/HiddenEvents.cpp
new file mode 100644
index 00000000..a8da812e
--- /dev/null
+++ b/src/ui/HiddenEvents.cpp
@@ -0,0 +1,93 @@
+// SPDX-FileCopyrightText: 2022 Nheko Contributors
+//
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+#include "HiddenEvents.h"
+
+#include "Cache_p.h"
+#include "MainWindow.h"
+#include "MatrixClient.h"
+
+void
+HiddenEvents::load()
+{
+    using namespace mtx::events;
+    mtx::events::account_data::nheko_extensions::HiddenEvents hiddenEvents;
+    hiddenEvents.hidden_event_types = std::vector{
+      EventType::Reaction,
+      EventType::CallCandidates,
+      EventType::Unsupported,
+    };
+
+    if (auto temp =
+          cache::client()->getAccountData(mtx::events::EventType::NhekoHiddenEvents, "")) {
+        auto h = std::get<
+          mtx::events::AccountDataEvent<mtx::events::account_data::nheko_extensions::HiddenEvents>>(
+          *temp);
+        if (h.content.hidden_event_types)
+            hiddenEvents = std::move(h.content);
+    }
+
+    if (!roomid_.isEmpty()) {
+        if (auto temp = cache::client()->getAccountData(mtx::events::EventType::NhekoHiddenEvents,
+                                                        roomid_.toStdString())) {
+            auto h = std::get<mtx::events::AccountDataEvent<
+              mtx::events::account_data::nheko_extensions::HiddenEvents>>(*temp);
+            if (h.content.hidden_event_types)
+                hiddenEvents = std::move(h.content);
+        }
+    }
+
+    hiddenEvents_.clear();
+    hiddenEvents_ = std::move(hiddenEvents.hidden_event_types.value());
+    emit hiddenEventsChanged();
+}
+
+Q_INVOKABLE void
+HiddenEvents::toggle(int type)
+{
+    auto t = qml_mtx_events::fromRoomEventType(static_cast<qml_mtx_events::EventType>(type));
+    if (auto it = std::find(begin(hiddenEvents_), end(hiddenEvents_), t); it != end(hiddenEvents_))
+        hiddenEvents_.erase(it);
+    else
+        hiddenEvents_.push_back(t);
+    emit hiddenEventsChanged();
+}
+
+QVariantList
+HiddenEvents::hiddenEvents() const
+{
+    QVariantList l;
+    for (const auto &e : hiddenEvents_) {
+        l.push_back(qml_mtx_events::toRoomEventType(e));
+    }
+
+    return l;
+}
+
+void
+HiddenEvents::save()
+{
+    mtx::events::account_data::nheko_extensions::HiddenEvents hiddenEvents;
+    hiddenEvents.hidden_event_types = hiddenEvents_;
+
+    if (roomid_.isEmpty())
+        http::client()->put_account_data(hiddenEvents, [](mtx::http::RequestErr e) {
+            if (e) {
+                nhlog::net()->error("Failed to set hidden events: {}", *e);
+                MainWindow::instance()->showNotification(
+                  tr("Failed to set hidden events: %1")
+                    .arg(QString::fromStdString(e->matrix_error.error)));
+            }
+        });
+    else
+        http::client()->put_room_account_data(
+          roomid_.toStdString(), hiddenEvents, [](mtx::http::RequestErr e) {
+              if (e) {
+                  nhlog::net()->error("Failed to set hidden events: {}", *e);
+                  MainWindow::instance()->showNotification(
+                    tr("Failed to set hidden events: %1")
+                      .arg(QString::fromStdString(e->matrix_error.error)));
+              }
+          });
+}