summary refs log tree commit diff
diff options
context:
space:
mode:
authorNicolas Werner <nicolas.werner@hotmail.de>2021-03-16 21:01:14 +0100
committerNicolas Werner <nicolas.werner@hotmail.de>2021-03-16 21:01:14 +0100
commitcc3d32c65efca1f7dad2314a9e0d36bcd0c9d127 (patch)
tree4975b1a90b90349f22203be6863bea360f763616
parentConvert flatpak file to yaml to be able to add comments (diff)
downloadnheko-cc3d32c65efca1f7dad2314a9e0d36bcd0c9d127.tar.xz
Move check for duplicate /sync responses to the same thread
-rw-r--r--src/ChatPage.cpp24
-rw-r--r--src/ChatPage.h5
2 files changed, 15 insertions, 14 deletions
diff --git a/src/ChatPage.cpp b/src/ChatPage.cpp
index 7c018aff..e1f631d7 100644
--- a/src/ChatPage.cpp
+++ b/src/ChatPage.cpp
@@ -805,8 +805,17 @@ ChatPage::startInitialSync()
 }
 
 void
-ChatPage::handleSyncResponse(const mtx::responses::Sync &res)
+ChatPage::handleSyncResponse(const mtx::responses::Sync &res, const std::string &prev_batch_token)
 {
+        try {
+                if (prev_batch_token != cache::nextBatchToken()) {
+                        nhlog::net()->warn("Duplicate sync, dropping");
+                        return;
+                }
+        } catch (const lmdb::error &e) {
+                nhlog::db()->warn("Logged out in the mean time, dropping sync");
+        }
+
         nhlog::net()->debug("sync completed: {}", res.next_batch);
 
         // Ensure that we have enough one-time keys available.
@@ -861,15 +870,6 @@ ChatPage::trySync()
         http::client()->sync(
           opts,
           [this, since = opts.since](const mtx::responses::Sync &res, mtx::http::RequestErr err) {
-                  try {
-                          if (since != cache::nextBatchToken()) {
-                                  nhlog::net()->warn("Duplicate sync, dropping");
-                                  return;
-                          }
-                  } catch (const lmdb::error &e) {
-                          nhlog::db()->warn("Logged out in the mean time, dropping sync");
-                  }
-
                   if (err) {
                           const auto error      = QString::fromStdString(err->matrix_error.error);
                           const auto msg        = tr("Please try to login again: %1").arg(error);
@@ -895,7 +895,7 @@ ChatPage::trySync()
                           return;
                   }
 
-                  emit newSyncResponse(res);
+                  emit newSyncResponse(res, since);
           });
 }
 
@@ -1448,4 +1448,4 @@ void
 ChatPage::highlightRoom(const QString &room_id)
 {
         room_list_->highlightSelectedRoom(room_id);
-}
\ No newline at end of file
+}
diff --git a/src/ChatPage.h b/src/ChatPage.h
index 17a4827f..84e7cdff 100644
--- a/src/ChatPage.h
+++ b/src/ChatPage.h
@@ -141,7 +141,7 @@ signals:
         void trySyncCb();
         void tryDelayedSyncCb();
         void tryInitialSyncCb();
-        void newSyncResponse(const mtx::responses::Sync &res);
+        void newSyncResponse(const mtx::responses::Sync &res, const std::string &prev_batch_token);
         void leftRoom(const QString &room_id);
         void newRoom(const QString &room_id);
 
@@ -194,7 +194,8 @@ private slots:
         void changeRoom(const QString &room_id);
         void dropToLoginPage(const QString &msg);
 
-        void handleSyncResponse(const mtx::responses::Sync &res);
+        void handleSyncResponse(const mtx::responses::Sync &res,
+                                const std::string &prev_batch_token);
 
 private:
         static ChatPage *instance_;