From ba38b1f97f6d103ee0d14d81773f61aff361eb1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Ku=C3=9Fowski?= Date: Wed, 28 May 2025 12:21:24 +0200 Subject: [PATCH] refactor: Do not store room update for leave rooms not cached anyway This adds a check before the storeRoomUpdate() call if the room is actually known. This has the effect that the call of forgetRoom() is skipped. The reason for this is an edge case in the database implementation when calling getAllKeys(). This somehow can corrupt the keys-cache and lead to some problems. I wasn't able to fix this problem yet so this refactoring is more a good-enough workaround for now to not trigger it on an initial sync. I plan to fix it with a different approach which completely removes the keys-cache in the future. However this change leads to some problems in the tests as they already rely on this edge case. --- lib/fake_matrix_api.dart | 10 ---------- lib/src/client.dart | 4 ++++ test/client_test.dart | 9 +++------ test/encryption/key_manager_test.dart | 1 + 4 files changed, 8 insertions(+), 16 deletions(-) diff --git a/lib/fake_matrix_api.dart b/lib/fake_matrix_api.dart index 95150547..657dc713 100644 --- a/lib/fake_matrix_api.dart +++ b/lib/fake_matrix_api.dart @@ -600,16 +600,6 @@ class FakeMatrixApi extends BaseClient { }, 'timeline': { 'events': [ - { - 'sender': '@bob:example.com', - 'type': 'm.room.member', - 'state_key': '@bob:example.com', - 'content': {'membership': 'join'}, - 'prev_content': {'membership': 'invite'}, - 'origin_server_ts': 1417731086795, - 'event_id': '\$7365636s6r6432:example.com', - 'unsigned': {'foo': 'bar'}, - }, { 'sender': '@alice:example.com', 'type': 'm.room.message', diff --git a/lib/src/client.dart b/lib/src/client.dart index 4cd518fd..bdb788a6 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -2736,6 +2736,10 @@ class Client extends MatrixApi { await _handleRoomEvents(room, state, EventUpdateType.inviteState); } } + if (syncRoomUpdate is LeftRoomUpdate && getRoomById(id) == null) { + Logs().d('Skip store LeftRoomUpdate for unknown room', id); + continue; + } await database.storeRoomUpdate(id, syncRoomUpdate, room.lastEvent, this); } } diff --git a/test/client_test.dart b/test/client_test.dart index b50999cb..f88a182d 100644 --- a/test/client_test.dart +++ b/test/client_test.dart @@ -282,16 +282,13 @@ void main() { final eventUpdateList = await eventUpdateListFuture; - expect(eventUpdateList.length, 3); + expect(eventUpdateList.length, 2); - expect(eventUpdateList[0].type, 'm.room.member'); + expect(eventUpdateList[0].type, 'm.room.message'); expect(eventUpdateList[0].roomId, '!726s6s6q:example.com'); expect(eventUpdateList[1].type, 'm.room.message'); - expect(eventUpdateList[1].roomId, '!726s6s6q:example.com'); - - expect(eventUpdateList[2].type, 'm.room.message'); - expect(eventUpdateList[2].roomId, '!726s6s6f:example.com'); + expect(eventUpdateList[1].roomId, '!726s6s6f:example.com'); expect( matrix diff --git a/test/encryption/key_manager_test.dart b/test/encryption/key_manager_test.dart index 4319aca7..300d97d3 100644 --- a/test/encryption/key_manager_test.dart +++ b/test/encryption/key_manager_test.dart @@ -186,6 +186,7 @@ void main() { sess = await client.encryption!.keyManager .createOutboundGroupSession(roomId); final room = client.getRoomById(roomId)!; + room.partial = false; final member = room.getState('m.room.member', '@alice:example.com'); member!.content['membership'] = 'leave'; room.summary.mJoinedMemberCount = room.summary.mJoinedMemberCount! - 1;