From 9d9865a1a1be9821c6a35204ccf3ba849da552dd Mon Sep 17 00:00:00 2001 From: Krille Date: Fri, 28 Jun 2024 09:11:43 +0200 Subject: [PATCH] fix: Do not update lastEvent with state events --- lib/src/client.dart | 3 +- lib/src/room.dart | 33 ++------------------ test/encryption/key_manager_test.dart | 45 +++++++++++++++------------ 3 files changed, 29 insertions(+), 52 deletions(-) diff --git a/lib/src/client.dart b/lib/src/client.dart index 45974ee6..ea1d6ce4 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -2435,8 +2435,7 @@ class Client extends MatrixApi { // If last event is null or not a valid room preview event anyway, // just use this: - if (room.lastEvent == null || - !roomPreviewLastEvents.contains(room.lastEvent?.type)) { + if (room.lastEvent == null) { room.lastEvent = event; break; } diff --git a/lib/src/room.dart b/lib/src/room.dart index 03483dff..e0eb11c6 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -98,7 +98,7 @@ class Room { summary: RoomSummary.fromJson(Map.from(json['summary'])), ); if (json['last_event'] != null) { - room._lastEvent = Event.fromJson(json['last_event'], room); + room.lastEvent = Event.fromJson(json['last_event'], room); } return room; } @@ -365,33 +365,7 @@ class Room { /// Wheither this is a direct chat or not bool get isDirectChat => directChatMatrixID != null; - Event? _lastEvent; - - set lastEvent(Event? event) { - _lastEvent = event; - } - - Event? get lastEvent { - if (_lastEvent != null) return _lastEvent; - - // Just pick the newest state event as an indicator for when the last - // activity was in this room. This is better than nothing: - var lastTime = DateTime.fromMillisecondsSinceEpoch(0); - Event? lastEvent; - - states.forEach((final String key, final entry) { - final state = entry['']; - if (state == null) return; - if (state is! Event) return; - if (state.originServerTs.millisecondsSinceEpoch > - lastTime.millisecondsSinceEpoch) { - lastTime = state.originServerTs; - lastEvent = state; - } - }); - - return lastEvent; - } + Event? lastEvent; /// Returns a list of all current typing users. List get typingUsers { @@ -416,9 +390,8 @@ class Room { required this.client, Map? roomAccountData, RoomSummary? summary, - Event? lastEvent, + this.lastEvent, }) : roomAccountData = roomAccountData ?? {}, - _lastEvent = lastEvent, summary = summary ?? RoomSummary.fromJson({ 'm.joined_member_count': 0, diff --git a/test/encryption/key_manager_test.dart b/test/encryption/key_manager_test.dart index 48d343a4..c4178526 100644 --- a/test/encryption/key_manager_test.dart +++ b/test/encryption/key_manager_test.dart @@ -323,26 +323,31 @@ void main() { .timeout(const Duration(seconds: 5)); client.rooms.add(room); // we build up an encrypted message so that we can test if it successfully decrypted afterwards - room.setState( - Event( - senderId: '@test:example.com', - type: 'm.room.encrypted', - room: room, - eventId: '12345', - originServerTs: DateTime.now(), - content: { - 'algorithm': AlgorithmTypes.megolmV1AesSha2, - 'ciphertext': session.encrypt(json.encode({ - 'type': 'm.room.message', - 'content': {'msgtype': 'm.text', 'body': 'foxies'}, - })), - 'device_id': client.deviceID, - 'sender_key': client.identityKey, - 'session_id': sessionId, - }, - stateKey: '', - ), - ); + await client.handleSync(SyncUpdate( + nextBatch: '', + rooms: RoomsUpdate(join: { + room.id: JoinedRoomUpdate( + timeline: TimelineUpdate(events: [ + Event( + senderId: '@test:example.com', + type: 'm.room.encrypted', + room: room, + eventId: '12345', + originServerTs: DateTime.now(), + content: { + 'algorithm': AlgorithmTypes.megolmV1AesSha2, + 'ciphertext': session.encrypt(json.encode({ + 'type': 'm.room.message', + 'content': {'msgtype': 'm.text', 'body': 'foxies'}, + })), + 'device_id': client.deviceID, + 'sender_key': client.identityKey, + 'session_id': sessionId, + }, + stateKey: '', + ) + ])) + }))); expect(room.lastEvent?.type, 'm.room.encrypted'); // set a payload... var sessionPayload = {