fix: Do not set messages as state events anymore

We had this as a workaround and forgot to remove it after the database migration to the new way how to store the last message.
This commit is contained in:
td 2024-05-22 22:22:56 +05:30
parent 763bb0ba00
commit 79960d1be3
No known key found for this signature in database
GPG Key ID: 62A30523D4D6CE28
4 changed files with 36 additions and 32 deletions

View File

@ -2421,11 +2421,8 @@ class Client extends MatrixApi {
final event = Event.fromJson(eventUpdate.content, room); final event = Event.fromJson(eventUpdate.content, room);
// Update the room state: // Update the room state:
if (!room.partial || if (event.stateKey != null &&
// make sure we do overwrite events we have already loaded. (!room.partial || importantStateEvents.contains(event.type))) {
room.states[event.type]?.containsKey(event.stateKey ?? '') ==
true ||
importantStateEvents.contains(event.type)) {
room.setState(event); room.setState(event);
} }
if (eventUpdate.type != EventUpdateType.timeline) break; if (eventUpdate.type != EventUpdateType.timeline) break;

View File

@ -176,8 +176,9 @@ class Room {
// the room ID: // the room ID:
if (state is Event) { if (state is Event) {
final roomId = state.roomId; final roomId = state.roomId;
if (roomId == null || roomId != id) { if (roomId != id) {
Logs().wtf('Tried to set state event for wrong room!'); Logs().wtf('Tried to set state event for wrong room!');
assert(roomId == id);
return; return;
} }
} }
@ -186,6 +187,7 @@ class Room {
Logs().w( Logs().w(
'Tried to set a non state event with type "${state.type}" as state event for a room', 'Tried to set a non state event with type "${state.type}" as state event for a room',
); );
assert(stateKey != null);
return; return;
} }

View File

@ -52,7 +52,7 @@ void main() {
room2.setState(Event( room2.setState(Event(
type: 'm.room.power_levels', type: 'm.room.power_levels',
content: {}, content: {},
room: room, room: room2,
stateKey: '', stateKey: '',
senderId: client.userID!, senderId: client.userID!,
eventId: '\$fakeid3:fakeServer.notExisting', eventId: '\$fakeid3:fakeServer.notExisting',
@ -61,7 +61,7 @@ void main() {
room2.setState(Event( room2.setState(Event(
type: 'm.room.member', type: 'm.room.member',
content: {'membership': 'join'}, content: {'membership': 'join'},
room: room, room: room2,
stateKey: client.userID, stateKey: client.userID,
senderId: '@fakeuser:fakeServer.notExisting', senderId: '@fakeuser:fakeServer.notExisting',
eventId: '\$fakeid4:fakeServer.notExisting', eventId: '\$fakeid4:fakeServer.notExisting',

View File

@ -496,6 +496,7 @@ void main() {
eventId: '12', eventId: '12',
originServerTs: DateTime.now(), originServerTs: DateTime.now(),
content: {'body': 'brainfarts'}, content: {'body': 'brainfarts'},
stateKey: '',
), ),
); );
expect(room.lastEvent?.body, '* BBB'); expect(room.lastEvent?.body, '* BBB');
@ -1079,6 +1080,7 @@ void main() {
test('setState', () async { test('setState', () async {
// not set non-state-events // not set non-state-events
try {
room.setState(Event.fromJson( room.setState(Event.fromJson(
{ {
'content': {'history_visibility': 'shared'}, 'content': {'history_visibility': 'shared'},
@ -1087,10 +1089,11 @@ void main() {
'room_id': '!jEsUZKDJdhlrceRyVU:example.org', 'room_id': '!jEsUZKDJdhlrceRyVU:example.org',
'sender': '@example:example.org', 'sender': '@example:example.org',
'type': 'm.custom', 'type': 'm.custom',
'unsigned': {'age': 1234} 'unsigned': {'age': 1234},
}, },
room, room,
)); ));
} catch (_) {}
expect(room.getState('m.custom') != null, false); expect(room.getState('m.custom') != null, false);
// set state events // set state events
@ -1110,6 +1113,7 @@ void main() {
expect(room.getState('m.custom') != null, true); expect(room.getState('m.custom') != null, true);
// sets messages as state events // sets messages as state events
try {
room.setState(Event.fromJson( room.setState(Event.fromJson(
{ {
'content': {'history_visibility': 'shared'}, 'content': {'history_visibility': 'shared'},
@ -1122,6 +1126,7 @@ void main() {
}, },
room, room,
)); ));
} catch (_) {}
expect(room.getState('m.room.message') == null, true); expect(room.getState('m.room.message') == null, true);
}); });