summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorNicolas Werner <nicolas.werner@hotmail.de>2023-11-01 16:15:46 +0100
committerNicolas Werner <nicolas.werner@hotmail.de>2023-11-01 16:15:46 +0100
commitaeda42111bb3f2345af8b2b6bdbef06c88e3b7b2 (patch)
treeaab419b4710b968acdd732a72267487f77ce748a /src
parentSave profile when explicitly specified (diff)
downloadnheko-aeda42111bb3f2345af8b2b6bdbef06c88e3b7b2.tar.xz
Throttle sync processing if the window is unfocused for a while
Diffstat (limited to 'src')
-rw-r--r--src/ChatPage.cpp22
-rw-r--r--src/ChatPage.h7
2 files changed, 26 insertions, 3 deletions
diff --git a/src/ChatPage.cpp b/src/ChatPage.cpp
index b756f486..a75b7ef5 100644
--- a/src/ChatPage.cpp
+++ b/src/ChatPage.cpp
@@ -844,7 +844,10 @@ ChatPage::handleSyncResponse(const mtx::responses::Sync &res, const std::string
         nhlog::db()->error("saving sync response: {}", e.what());
     }
 
-    emit trySyncCb();
+    if (shouldThrottleSync())
+        QTimer::singleShot(1000, this, &ChatPage::trySyncCb);
+    else
+        emit trySyncCb();
 }
 
 void
@@ -1161,6 +1164,20 @@ ChatPage::setStatus(const QString &status)
       });
 }
 
+bool
+ChatPage::shouldBeUnavailable() const
+{
+    return lastWindowActive.isValid() &&
+           lastWindowActive.addSecs(60 * 5) < QDateTime::currentDateTime();
+}
+
+bool
+ChatPage::shouldThrottleSync() const
+{
+    return lastWindowActive.isValid() &&
+           lastWindowActive.addSecs(6 * 5) < QDateTime::currentDateTime();
+}
+
 mtx::presence::PresenceState
 ChatPage::currentPresence() const
 {
@@ -1172,8 +1189,7 @@ ChatPage::currentPresence() const
     case UserSettings::Presence::Offline:
         return mtx::presence::offline;
     case UserSettings::Presence::AutomaticPresence:
-        if (lastWindowActive.isValid() &&
-            lastWindowActive.addSecs(60 * 5) < QDateTime::currentDateTime())
+        if (shouldBeUnavailable())
             return mtx::presence::unavailable;
         else
             return mtx::presence::online;
diff --git a/src/ChatPage.h b/src/ChatPage.h
index 13621b23..910ccd1e 100644
--- a/src/ChatPage.h
+++ b/src/ChatPage.h
@@ -203,6 +203,13 @@ private:
     void loadStateFromCache();
     void resetUI();
 
+    // returns if the user had no interaction with Nheko for quite a while, which means we set our
+    // presence to unavailable if automatic presence is enabled
+    bool shouldBeUnavailable() const;
+    // If we should throttle sync processing to reduce CPU load, if people are spamming messages and
+    // we aren't looking
+    bool shouldThrottleSync() const;
+
     template<class Collection>
     Memberships getMemberships(const std::vector<Collection> &events) const;