summary refs log tree commit diff
diff options
context:
space:
mode:
authorNicolas Werner <nicolas.werner@hotmail.de>2020-07-18 21:00:36 +0200
committerNicolas Werner <nicolas.werner@hotmail.de>2020-07-18 21:00:36 +0200
commit5695f004a25649f8955659ea57769af8e311be9b (patch)
tree2178d840c83eea58b14d1a5950edb909b6319fc3
parentMark own events as read again after sending (diff)
downloadnheko-5695f004a25649f8955659ea57769af8e311be9b.tar.xz
Fix race condition between /messages and /sync
-rw-r--r--src/ChatPage.cpp80
-rw-r--r--src/ChatPage.h2
2 files changed, 48 insertions, 34 deletions
diff --git a/src/ChatPage.cpp b/src/ChatPage.cpp
index 813b0c2a..c4376905 100644
--- a/src/ChatPage.cpp
+++ b/src/ChatPage.cpp
@@ -573,6 +573,12 @@ ChatPage::ChatPage(QSharedPointer<UserSettings> userSettings, QWidget *parent)
           [this]() { QTimer::singleShot(RETRY_TIMEOUT, this, &ChatPage::trySync); },
           Qt::QueuedConnection);
 
+        connect(this,
+                &ChatPage::newSyncResponse,
+                this,
+                &ChatPage::handleSyncResponse,
+                Qt::QueuedConnection);
+
         connect(this, &ChatPage::dropToLoginPageCb, this, &ChatPage::dropToLoginPage);
 
         instance_ = this;
@@ -1004,6 +1010,45 @@ ChatPage::startInitialSync()
 }
 
 void
+ChatPage::handleSyncResponse(mtx::responses::Sync res)
+{
+        nhlog::net()->debug("sync completed: {}", res.next_batch);
+
+        // Ensure that we have enough one-time keys available.
+        ensureOneTimeKeyCount(res.device_one_time_keys_count);
+
+        // TODO: fine grained error handling
+        try {
+                cache::saveState(res);
+                olm::handle_to_device_messages(res.to_device.events);
+
+                auto updates = cache::roomUpdates(res);
+
+                emit syncTopBar(updates);
+                emit syncRoomlist(updates);
+
+                emit syncUI(res.rooms);
+
+                emit syncTags(cache::roomTagUpdates(res));
+
+                // if we process a lot of syncs (1 every 200ms), this means we clean the
+                // db every 100s
+                static int syncCounter = 0;
+                if (syncCounter++ >= 500) {
+                        cache::deleteOldData();
+                        syncCounter = 0;
+                }
+        } catch (const lmdb::map_full_error &e) {
+                nhlog::db()->error("lmdb is full: {}", e.what());
+                cache::deleteOldData();
+        } catch (const lmdb::error &e) {
+                nhlog::db()->error("saving sync response: {}", e.what());
+        }
+
+        emit trySyncCb();
+}
+
+void
 ChatPage::trySync()
 {
         mtx::http::SyncOpts opts;
@@ -1042,40 +1087,7 @@ ChatPage::trySync()
                           return;
                   }
 
-                  nhlog::net()->debug("sync completed: {}", res.next_batch);
-
-                  // Ensure that we have enough one-time keys available.
-                  ensureOneTimeKeyCount(res.device_one_time_keys_count);
-
-                  // TODO: fine grained error handling
-                  try {
-                          cache::saveState(res);
-                          olm::handle_to_device_messages(res.to_device.events);
-
-                          auto updates = cache::roomUpdates(res);
-
-                          emit syncTopBar(updates);
-                          emit syncRoomlist(updates);
-
-                          emit syncUI(res.rooms);
-
-                          emit syncTags(cache::roomTagUpdates(res));
-
-                          // if we process a lot of syncs (1 every 200ms), this means we clean the
-                          // db every 100s
-                          static int syncCounter = 0;
-                          if (syncCounter++ >= 500) {
-                                  cache::deleteOldData();
-                                  syncCounter = 0;
-                          }
-                  } catch (const lmdb::map_full_error &e) {
-                          nhlog::db()->error("lmdb is full: {}", e.what());
-                          cache::deleteOldData();
-                  } catch (const lmdb::error &e) {
-                          nhlog::db()->error("saving sync response: {}", e.what());
-                  }
-
-                  emit trySyncCb();
+                  emit newSyncResponse(res);
           });
 }
 
diff --git a/src/ChatPage.h b/src/ChatPage.h
index c38d7717..18bed289 100644
--- a/src/ChatPage.h
+++ b/src/ChatPage.h
@@ -139,6 +139,7 @@ signals:
         void trySyncCb();
         void tryDelayedSyncCb();
         void tryInitialSyncCb();
+        void newSyncResponse(mtx::responses::Sync res);
         void leftRoom(const QString &room_id);
 
         void initializeRoomList(QMap<QString, RoomInfo>);
@@ -173,6 +174,7 @@ private slots:
 
         void joinRoom(const QString &room);
         void sendTypingNotifications();
+        void handleSyncResponse(mtx::responses::Sync res);
 
 private:
         static ChatPage *instance_;