From 2a19783f994db579eea685b2c3d681d1612423cc Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Tue, 12 Jan 2021 00:02:18 +0100 Subject: [PATCH] Fix timeline becoming corrupted on backfill Fixes #273 --- CMakeLists.txt | 2 +- io.github.NhekoReborn.Nheko.json | 2 +- src/Cache.cpp | 15 +++++++++------ src/timeline/EventStore.cpp | 32 +++++++++++++++++++++++++------- src/timeline/EventStore.h | 2 +- 5 files changed, 37 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1f8b0036..fcf40d4b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -354,7 +354,7 @@ if(USE_BUNDLED_MTXCLIENT) FetchContent_Declare( MatrixClient GIT_REPOSITORY https://github.com/Nheko-Reborn/mtxclient.git - GIT_TAG d44158e17e8eb872bee762a81fc04bfad117f0c5 + GIT_TAG f364a5e83db3f1978b6c3d3453accae02c9267c3 ) set(BUILD_LIB_EXAMPLES OFF CACHE INTERNAL "") set(BUILD_LIB_TESTS OFF CACHE INTERNAL "") diff --git a/io.github.NhekoReborn.Nheko.json b/io.github.NhekoReborn.Nheko.json index 8a74a71a..6760c816 100644 --- a/io.github.NhekoReborn.Nheko.json +++ b/io.github.NhekoReborn.Nheko.json @@ -152,7 +152,7 @@ "name": "mtxclient", "sources": [ { - "commit": "d44158e17e8eb872bee762a81fc04bfad117f0c5", + "commit": "f364a5e83db3f1978b6c3d3453accae02c9267c3", "type": "git", "url": "https://github.com/Nheko-Reborn/mtxclient.git" } diff --git a/src/Cache.cpp b/src/Cache.cpp index 17b55144..389df0a3 100644 --- a/src/Cache.cpp +++ b/src/Cache.cpp @@ -1208,10 +1208,11 @@ Cache::saveState(const mtx::responses::Sync &res) for (const auto &ev : res.account_data.events) std::visit( [&txn, &accountDataDb](const auto &event) { + auto j = json(event); lmdb::dbi_put(txn, accountDataDb, - lmdb::val(to_string(event.type)), - lmdb::val(json(event).dump())); + lmdb::val(j["type"].get()), + lmdb::val(j.dump())); }, ev); } @@ -1242,10 +1243,11 @@ Cache::saveState(const mtx::responses::Sync &res) for (const auto &evt : room.second.account_data.events) { std::visit( [&txn, &accountDataDb](const auto &event) { + auto j = json(event); lmdb::dbi_put(txn, accountDataDb, - lmdb::val(to_string(event.type)), - lmdb::val(json(event).dump())); + lmdb::val(j["type"].get()), + lmdb::val(j.dump())); }, evt); @@ -1391,10 +1393,11 @@ Cache::saveInvite(lmdb::txn &txn, } else { std::visit( [&txn, &statesdb](auto msg) { + auto j = json(msg); bool res = lmdb::dbi_put(txn, statesdb, - lmdb::val(to_string(msg.type)), - lmdb::val(json(msg).dump())); + lmdb::val(j["type"].get()), + lmdb::val(j.dump())); if (!res) nhlog::db()->warn("couldn't save data: {}", 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::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::max(); + this->last = std::numeric_limits::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::max() + return (last != std::numeric_limits::max() && last >= first) ? static_cast(last - first) + 1 : 0; }