From b7565af56ff12308d5bafa85a39c782310ba7c8d Mon Sep 17 00:00:00 2001 From: Krille Fear Date: Mon, 15 Nov 2021 10:57:55 +0100 Subject: [PATCH] fix: use originServerTs to check if state event is old Due to server bugs or whatever it sometimes happens that old state events appear in the setState method in the room class. Previously we checked if we already know this event ID, but for this we needed to check the timeline which is very fluid. Also this is a database operation in a non-async method which works in Hive but not in Sembast. Using originServerTs is not 100% safe as well but should be more stable because the chance that servers have veeery wrong time (which is necessary here) is much lower than the risk that the timeline is not long enough to know the old event. --- lib/src/database/database_api.dart | 2 -- lib/src/database/hive_database.dart | 4 ---- lib/src/room.dart | 3 ++- 3 files changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/src/database/database_api.dart b/lib/src/database/database_api.dart index 368f1ce8..7bdc6872 100644 --- a/lib/src/database/database_api.dart +++ b/lib/src/database/database_api.dart @@ -67,8 +67,6 @@ abstract class DatabaseApi { Future getEventById(String eventId, Room room); - bool eventIsKnown(String eventId, String roomId); - Future forgetRoom(String roomId); Future clearCache(); diff --git a/lib/src/database/hive_database.dart b/lib/src/database/hive_database.dart index 90891efb..884b2c53 100644 --- a/lib/src/database/hive_database.dart +++ b/lib/src/database/hive_database.dart @@ -371,10 +371,6 @@ class FamedlySdkHiveDatabase extends DatabaseApi { return Event.fromJson(convertToJson(raw), room); } - @override - bool eventIsKnown(String eventId, String roomId) => - _eventsBox.keys.contains(MultiKey(roomId, eventId).toString()); - /// Loads a whole list of events at once from the store for a specific room Future> _getEventsByIds(List eventIds, Room room) => Future.wait(eventIds diff --git a/lib/src/room.dart b/lib/src/room.dart index 43c98a16..b674bf81 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -203,7 +203,8 @@ class Room { final prevEvent = getState(state.type, stateKey); if (prevEvent != null && prevEvent.eventId != state.eventId && - client.database?.eventIsKnown(state.eventId, roomId) == true) { + prevEvent.originServerTs.millisecondsSinceEpoch > + state.originServerTs.millisecondsSinceEpoch) { return; }