summary refs log tree commit diff
path: root/src/ChatPage.cpp
diff options
context:
space:
mode:
authorNicolas Werner <nicolas.werner@hotmail.de>2023-10-24 01:12:01 +0200
committerNicolas Werner <nicolas.werner@hotmail.de>2023-10-24 01:12:01 +0200
commita0a49b6c2a3cf48cb4e979a92cd2065a51bff775 (patch)
treef9316cc3a1c3ceca6c875bc166a528b9cab03385 /src/ChatPage.cpp
parentMerge branch 'ignore-users' of github.com:NepNep21/nheko into ignore-users (diff)
downloadnheko-a0a49b6c2a3cf48cb4e979a92cd2065a51bff775.tar.xz
Cleanup ignore user functionality slightly
Diffstat (limited to 'src/ChatPage.cpp')
-rw-r--r--src/ChatPage.cpp53
1 files changed, 50 insertions, 3 deletions
diff --git a/src/ChatPage.cpp b/src/ChatPage.cpp
index 90d542dd..e63135a9 100644
--- a/src/ChatPage.cpp
+++ b/src/ChatPage.cpp
@@ -6,6 +6,9 @@
 #include <QInputDialog>
 #include <QMessageBox>
 
+#include <algorithm>
+#include <unordered_set>
+
 #include <mtx/responses.hpp>
 
 #include "AvatarProvider.h"
@@ -21,7 +24,6 @@
 #include "encryption/DeviceVerificationFlow.h"
 #include "encryption/Olm.h"
 #include "ui/RoomSummary.h"
-#include "ui/Theme.h"
 #include "ui/UserProfile.h"
 #include "voip/CallManager.h"
 
@@ -29,8 +31,6 @@
 
 #include "timeline/TimelineViewManager.h"
 
-#include "blurhash.hpp"
-
 ChatPage *ChatPage::instance_                    = nullptr;
 static constexpr int CHECK_CONNECTIVITY_INTERVAL = 15'000;
 static constexpr int RETRY_TIMEOUT               = 5'000;
@@ -765,6 +765,23 @@ ChatPage::handleSyncResponse(const mtx::responses::Sync &res, const std::string
     // Ensure that we have enough one-time keys available.
     ensureOneTimeKeyCount(res.device_one_time_keys_count, res.device_unused_fallback_key_types);
 
+    std::optional<mtx::events::account_data::IgnoredUsers> oldIgnoredUsers;
+    if (auto ignoreEv = std::ranges::find_if(
+          res.account_data.events,
+          [](const mtx::events::collections::RoomAccountDataEvents &e) {
+              return std::holds_alternative<
+                mtx::events::AccountDataEvent<mtx::events::account_data::IgnoredUsers>>(e);
+          });
+        ignoreEv != res.account_data.events.end()) {
+        if (auto oldEv = cache::client()->getAccountData(mtx::events::EventType::IgnoredUsers))
+            oldIgnoredUsers =
+              std::get<mtx::events::AccountDataEvent<mtx::events::account_data::IgnoredUsers>>(
+                *oldEv)
+                .content;
+        else
+            oldIgnoredUsers = mtx::events::account_data::IgnoredUsers{};
+    }
+
     // TODO: fine grained error handling
     try {
         cache::client()->saveState(res);
@@ -773,6 +790,36 @@ ChatPage::handleSyncResponse(const mtx::responses::Sync &res, const std::string
         auto updates = cache::getRoomInfo(cache::client()->roomsWithStateUpdates(res));
 
         emit syncUI(std::move(res));
+
+        // if the ignored users changed, clear timeline of all affected rooms.
+        if (oldIgnoredUsers) {
+            if (auto newEv =
+                  cache::client()->getAccountData(mtx::events::EventType::IgnoredUsers)) {
+                std::vector<mtx::events::account_data::IgnoredUser> changedUsers{};
+                std::ranges::set_symmetric_difference(
+                  oldIgnoredUsers->users,
+                  std::get<mtx::events::AccountDataEvent<mtx::events::account_data::IgnoredUsers>>(
+                    *newEv)
+                    .content.users,
+                  std::back_inserter(changedUsers),
+                  {},
+                  &mtx::events::account_data::IgnoredUser::id,
+                  &mtx::events::account_data::IgnoredUser::id);
+
+                std::unordered_set<std::string> roomsToReload;
+                for (const auto &user : changedUsers) {
+                    auto commonRooms = cache::client()->getCommonRooms(user.id);
+                    for (const auto &room : commonRooms)
+                        roomsToReload.insert(room.first);
+                }
+
+                for (const auto &room : roomsToReload) {
+                    if (auto model =
+                          view_manager_->rooms()->getRoomById(QString::fromStdString(room)))
+                        model->clearTimeline();
+                }
+            }
+        }
     } catch (const lmdb::map_full_error &e) {
         nhlog::db()->error("lmdb is full: {}", e.what());
         cache::deleteOldData();