@@ -5,9 +5,33 @@ import '../../common/event_x.dart';
55import '../../common/logging.dart' ;
66
77class TimelineModel extends SafeChangeNotifier {
8+ Future <Timeline > loadTimeline (
9+ Room room, {
10+ void Function (int )? onChange,
11+ void Function (int )? onRemove,
12+ void Function (int )? onInsert,
13+ void Function ()? onNewEvent,
14+ void Function ()? onUpdate,
15+ String ? eventContextId,
16+ int ? limit = Room .defaultHistoryCount,
17+ }) async {
18+ // This calls await postLoad() and then creates a new Timeline object
19+ final timeline = await room.getTimeline (
20+ onChange: onChange,
21+ onRemove: onRemove,
22+ onInsert: onInsert,
23+ onNewEvent: onNewEvent,
24+ onUpdate: onUpdate,
25+ eventContextId: eventContextId,
26+ limit: limit,
27+ );
28+
29+ _setTimeline (timeline: timeline);
30+ return timeline;
31+ }
32+
833 Future <void > postTimelineLoad (Timeline timeline) async {
9- await loadRoomStates (timeline.room);
10- await loadAllKeysFromRoom (timeline);
34+ _requestKeysForUndecryptableEvents (timeline);
1135 await requestHistory (timeline, historyCount: 500 );
1236 await trySetReadMarker (timeline);
1337 }
@@ -71,26 +95,10 @@ class TimelineModel extends SafeChangeNotifier {
7195 }
7296 }
7397
74- Future <void > loadAllKeysFromRoom (Timeline timeline) async {
98+ // Request the keys for undecryptable events of this timeline
99+ void _requestKeysForUndecryptableEvents (Timeline timeline) {
75100 try {
76- for (final event
77- in timeline.events
78- .toList (growable: false )
79- .where (
80- (e) =>
81- e.isEncryptedAndCouldDecrypt &&
82- ! _hasLoadedKeys.contains (e.eventId),
83- )) {
84- await event.requestKey ();
85- await timeline.room.client.encryption? .decryptRoomEvent (
86- event,
87- store: event.stateKey != null ,
88- );
89- _hasLoadedKeys.add (event.eventId);
90- printMessageInDebugMode (
91- 'Decrypted event ${event .eventId } in room ${timeline .room .getLocalizedDisplayname ()}' ,
92- );
93- }
101+ timeline.requestKeys (onlineKeyBackupOnly: false );
94102 } on Exception catch (e, s) {
95103 printMessageInDebugMode (e, s);
96104 }
0 commit comments