From cec08b377587b24bcea306694d85b2a2d4bed321 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Wed, 25 Aug 2021 09:52:57 +0200 Subject: [PATCH] feat: cache archived rooms to access them with `getRoomById` getRoomById searches now in the local cache for the given room and returns null if not found. If you have loaded the [archive] before, it can also return archived rooms. This should make it much easier to display archived rooms in the client. --- lib/src/client.dart | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/src/client.dart b/lib/src/client.dart index 686177be..f7141b76 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -275,10 +275,14 @@ class Client extends MatrixApi { return null; } + /// Searches in the local cache for the given room and returns null if not + /// found. If you have loaded the [archive] before, it can also return + /// archived rooms. Room getRoomById(String id) { - for (final room in rooms) { + for (final room in [...rooms, ..._archivedRooms]) { if (room.id == id) return room; } + return null; } @@ -645,8 +649,10 @@ class Client extends MatrixApi { avatarUrl: profile.avatarUrl); } + final List _archivedRooms = []; + Future> get archive async { - final archiveList = []; + _archivedRooms.clear(); final syncResp = await sync( filter: '{"room":{"include_leave":true,"timeline":{"limit":10}}}', timeout: 0, @@ -681,10 +687,10 @@ class Client extends MatrixApi { )); } } - archiveList.add(leftRoom); + _archivedRooms.add(leftRoom); } } - return archiveList; + return _archivedRooms; } /// Uploads a file and automatically caches it in the database, if it is small enough