summary refs log tree commit diff
path: root/src/timeline
diff options
context:
space:
mode:
authorNicolas Werner <nicolas.werner@hotmail.de>2021-01-12 00:02:18 +0100
committerNicolas Werner <nicolas.werner@hotmail.de>2021-01-12 00:02:18 +0100
commit2a19783f994db579eea685b2c3d681d1612423cc (patch)
tree27274a54b5d695a44a1bb0edb68c6b95037ae531 /src/timeline
parentAllow joins via other servers from a matrix: uri (diff)
downloadnheko-2a19783f994db579eea685b2c3d681d1612423cc.tar.xz
Fix timeline becoming corrupted on backfill
Fixes #273
Diffstat (limited to 'src/timeline')
-rw-r--r--src/timeline/EventStore.cpp32
-rw-r--r--src/timeline/EventStore.h2
2 files changed, 26 insertions, 8 deletions
diff --git a/src/timeline/EventStore.cpp b/src/timeline/EventStore.cpp
index e561d099..6cf8e602 100644
--- a/src/timeline/EventStore.cpp
+++ b/src/timeline/EventStore.cpp
@@ -67,11 +67,25 @@ EventStore::EventStore(std::string room_id, QObject *)
                   if (newFirst == first)
                           fetchMore();
                   else {
-                          emit beginInsertRows(toExternalIdx(newFirst),
-                                               toExternalIdx(this->first - 1));
-                          this->first = newFirst;
-                          emit endInsertRows();
-                          emit fetchedMore();
+                          if (this->last != std::numeric_limits<uint64_t>::max()) {
+                                  emit beginInsertRows(toExternalIdx(newFirst),
+                                                       toExternalIdx(this->first - 1));
+                                  this->first = newFirst;
+                                  emit endInsertRows();
+                                  emit fetchedMore();
+                          } else {
+                                  auto range = cache::client()->getTimelineRange(room_id_);
+
+                                  if (range && range->last - range->first != 0) {
+                                          emit beginInsertRows(0, int(range->last - range->first));
+                                          this->first = range->first;
+                                          this->last  = range->last;
+                                          emit endInsertRows();
+                                          emit fetchedMore();
+                                  } else {
+                                          fetchMore();
+                                  }
+                          }
                   }
           },
           Qt::QueuedConnection);
@@ -247,15 +261,19 @@ EventStore::handleSync(const mtx::responses::Timeline &events)
                 nhlog::db()->warn("{} called from a different thread!", __func__);
 
         auto range = cache::client()->getTimelineRange(room_id_);
-        if (!range)
+        if (!range) {
+                emit beginResetModel();
+                this->first = std::numeric_limits<uint64_t>::max();
+                this->last  = std::numeric_limits<uint64_t>::max();
+                emit endResetModel();
                 return;
+        }
 
         if (events.limited) {
                 emit beginResetModel();
                 this->last  = range->last;
                 this->first = range->first;
                 emit endResetModel();
-
         } else if (range->last > this->last) {
                 emit beginInsertRows(toExternalIdx(this->last + 1), toExternalIdx(range->last));
                 this->last = range->last;
diff --git a/src/timeline/EventStore.h b/src/timeline/EventStore.h
index c6b39742..f8eff9a9 100644
--- a/src/timeline/EventStore.h
+++ b/src/timeline/EventStore.h
@@ -74,7 +74,7 @@ public:
 
         int size() const
         {
-                return last != std::numeric_limits<uint64_t>::max()
+                return (last != std::numeric_limits<uint64_t>::max() && last >= first)
                          ? static_cast<int>(last - first) + 1
                          : 0;
         }