feat: Expose a getter for all rooms including archived rooms

This commit is contained in:
Philipp Grieshofer 2023-03-20 13:12:47 +00:00 committed by Nicolas Werner
parent f2e1e52531
commit 9aff62a18c
3 changed files with 52 additions and 2 deletions

View File

@ -252,6 +252,12 @@ class Client extends MatrixApi {
List<Room> get rooms => _rooms; List<Room> get rooms => _rooms;
List<Room> _rooms = []; List<Room> _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<ArchivedRoom> get archivedRooms => _archivedRooms;
bool enableDehydratedDevices = false; bool enableDehydratedDevices = false;
/// Whether this client supports end-to-end encryption using olm. /// Whether this client supports end-to-end encryption using olm.
@ -264,7 +270,7 @@ class Client extends MatrixApi {
String get fingerprintKey => encryption?.fingerprintKey ?? ''; String get fingerprintKey => encryption?.fingerprintKey ?? '';
/// Wheather this session is unknown to others /// Whether this session is unknown to others
bool get isUnknownSession => bool get isUnknownSession =>
userDeviceKeys[userID]?.deviceKeys[deviceID]?.signed != true; 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 /// full Room object with the sender User object in it. Returns null if this
/// push notification is not corresponding to an existing event. /// push notification is not corresponding to an existing event.
/// The client does **not** need to be initialized first. If it is not /// 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 /// should make it possible to run this parallel to another client with the
/// same client name. /// same client name.
/// This also checks if the given event has a readmarker and returns null /// This also checks if the given event has a readmarker and returns null

View File

@ -1393,6 +1393,17 @@ class Room {
} else { } else {
final archive = client.getArchiveRoomFromCache(id); final archive = client.getArchiveRoomFromCache(id);
events = archive?.timeline.events.toList() ?? []; 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); var chunk = TimelineChunk(events: events);

View File

@ -21,6 +21,7 @@ import 'dart:convert';
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:canonical_json/canonical_json.dart'; import 'package:canonical_json/canonical_json.dart';
import 'package:collection/collection.dart';
import 'package:olm/olm.dart' as olm; import 'package:olm/olm.dart' as olm;
import 'package:test/test.dart'; import 'package:test/test.dart';
@ -1016,6 +1017,38 @@ void main() {
expect(storedEvent2?.eventId, event?.eventId); 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(() { tearDown(() {
matrix.dispose(closeDatabase: true); matrix.dispose(closeDatabase: true);
}); });