diff --git a/lib/src/client.dart b/lib/src/client.dart index dc904a6f..f5dfdbaf 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -2421,11 +2421,8 @@ class Client extends MatrixApi { final event = Event.fromJson(eventUpdate.content, room); // Update the room state: - if (!room.partial || - // make sure we do overwrite events we have already loaded. - room.states[event.type]?.containsKey(event.stateKey ?? '') == - true || - importantStateEvents.contains(event.type)) { + if (event.stateKey != null && + (!room.partial || importantStateEvents.contains(event.type))) { room.setState(event); } if (eventUpdate.type != EventUpdateType.timeline) break; diff --git a/lib/src/room.dart b/lib/src/room.dart index cb2cd94f..4f9574c8 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -176,8 +176,9 @@ class Room { // the room ID: if (state is Event) { final roomId = state.roomId; - if (roomId == null || roomId != id) { + if (roomId != id) { Logs().wtf('Tried to set state event for wrong room!'); + assert(roomId == id); return; } } @@ -186,6 +187,7 @@ class Room { Logs().w( 'Tried to set a non state event with type "${state.type}" as state event for a room', ); + assert(stateKey != null); return; } diff --git a/test/image_pack_test.dart b/test/image_pack_test.dart index bcb7e87e..ed1503ce 100644 --- a/test/image_pack_test.dart +++ b/test/image_pack_test.dart @@ -52,7 +52,7 @@ void main() { room2.setState(Event( type: 'm.room.power_levels', content: {}, - room: room, + room: room2, stateKey: '', senderId: client.userID!, eventId: '\$fakeid3:fakeServer.notExisting', @@ -61,7 +61,7 @@ void main() { room2.setState(Event( type: 'm.room.member', content: {'membership': 'join'}, - room: room, + room: room2, stateKey: client.userID, senderId: '@fakeuser:fakeServer.notExisting', eventId: '\$fakeid4:fakeServer.notExisting', diff --git a/test/room_test.dart b/test/room_test.dart index 67c19f42..8b4deb2c 100644 --- a/test/room_test.dart +++ b/test/room_test.dart @@ -496,6 +496,7 @@ void main() { eventId: '12', originServerTs: DateTime.now(), content: {'body': 'brainfarts'}, + stateKey: '', ), ); expect(room.lastEvent?.body, '* BBB'); @@ -1079,18 +1080,20 @@ void main() { test('setState', () async { // not set non-state-events - room.setState(Event.fromJson( - { - 'content': {'history_visibility': 'shared'}, - 'event_id': '\$143273582443PhrSn:example.org', - 'origin_server_ts': 1432735824653, - 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', - 'sender': '@example:example.org', - 'type': 'm.custom', - 'unsigned': {'age': 1234} - }, - room, - )); + try { + room.setState(Event.fromJson( + { + 'content': {'history_visibility': 'shared'}, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'type': 'm.custom', + 'unsigned': {'age': 1234}, + }, + room, + )); + } catch (_) {} expect(room.getState('m.custom') != null, false); // set state events @@ -1110,18 +1113,20 @@ void main() { expect(room.getState('m.custom') != null, true); // sets messages as state events - room.setState(Event.fromJson( - { - 'content': {'history_visibility': 'shared'}, - 'event_id': '\$143273582443PhrSn:example.org', - 'origin_server_ts': 1432735824653, - 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', - 'sender': '@example:example.org', - 'type': 'm.room.message', - 'unsigned': {'age': 1234} - }, - room, - )); + try { + room.setState(Event.fromJson( + { + 'content': {'history_visibility': 'shared'}, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'type': 'm.room.message', + 'unsigned': {'age': 1234} + }, + room, + )); + } catch (_) {} expect(room.getState('m.room.message') == null, true); });