From 9aff62a18c0386585b1c62923c533d121778f5cb Mon Sep 17 00:00:00 2001 From: Philipp Grieshofer Date: Mon, 20 Mar 2023 13:12:47 +0000 Subject: [PATCH] feat: Expose a getter for all rooms including archived rooms --- lib/src/client.dart | 10 ++++++++-- lib/src/room.dart | 11 +++++++++++ test/client_test.dart | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/lib/src/client.dart b/lib/src/client.dart index 8e85102f..3d457be9 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -252,6 +252,12 @@ class Client extends MatrixApi { List get rooms => _rooms; List _rooms = []; + /// Get a list of the archived rooms + /// + /// Attention! Archived rooms are only returned if [loadArchive()] was called + /// beforehand! The state refers to the last retrieval via [loadArchive()]! + List get archivedRooms => _archivedRooms; + bool enableDehydratedDevices = false; /// Whether this client supports end-to-end encryption using olm. @@ -264,7 +270,7 @@ class Client extends MatrixApi { String get fingerprintKey => encryption?.fingerprintKey ?? ''; - /// Wheather this session is unknown to others + /// Whether this session is unknown to others bool get isUnknownSession => userDeviceKeys[userID]?.deviceKeys[deviceID]?.signed != true; @@ -1187,7 +1193,7 @@ class Client extends MatrixApi { /// full Room object with the sender User object in it. Returns null if this /// push notification is not corresponding to an existing event. /// The client does **not** need to be initialized first. If it is not - /// initalized, it will only fetch the necessary parts of the database. This + /// initialized, it will only fetch the necessary parts of the database. This /// should make it possible to run this parallel to another client with the /// same client name. /// This also checks if the given event has a readmarker and returns null diff --git a/lib/src/room.dart b/lib/src/room.dart index 7a84b6bc..fadc2208 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -1393,6 +1393,17 @@ class Room { } else { final archive = client.getArchiveRoomFromCache(id); events = archive?.timeline.events.toList() ?? []; + for (var i = 0; i < events.length; i++) { + // Try to decrypt encrypted events but don't update the database. + if (encrypted && client.encryptionEnabled) { + if (events[i].type == EventTypes.Encrypted) { + events[i] = await client.encryption!.decryptRoomEvent( + id, + events[i], + ); + } + } + } } var chunk = TimelineChunk(events: events); diff --git a/test/client_test.dart b/test/client_test.dart index 86c33917..763e32a2 100644 --- a/test/client_test.dart +++ b/test/client_test.dart @@ -21,6 +21,7 @@ import 'dart:convert'; import 'dart:typed_data'; import 'package:canonical_json/canonical_json.dart'; +import 'package:collection/collection.dart'; import 'package:olm/olm.dart' as olm; import 'package:test/test.dart'; @@ -1016,6 +1017,38 @@ void main() { expect(storedEvent2?.eventId, event?.eventId); }); + test('Rooms and archived rooms getter', () async { + final client = await getClient(); + await Future.delayed(Duration(milliseconds: 50)); + + expect(client.rooms.length, 2, + reason: + 'Count of invited+joined before loadArchive() rooms does not match'); + expect(client.archivedRooms.length, 0, + reason: + 'Count of archived rooms before loadArchive() does not match'); + + await client.loadArchive(); + + expect(client.rooms.length, 2, + reason: 'Count of invited+joined rooms does not match'); + expect(client.archivedRooms.length, 2, + reason: 'Count of archived rooms does not match'); + + expect( + client.archivedRooms.firstWhereOrNull( + (r) => r.room.id == '!5345234234:example.com') != + null, + true, + reason: '!5345234234:example.com not found as archived room'); + expect( + client.archivedRooms.firstWhereOrNull( + (r) => r.room.id == '!5345234235:example.com') != + null, + true, + reason: '!5345234235:example.com not found as archived room'); + }); + tearDown(() { matrix.dispose(closeDatabase: true); });