fix: deleting last message is edited

This commit is contained in:
Mohammad Reza Moradi 2025-04-10 11:49:35 +02:00
parent 0b4ee0e60e
commit defe47a198
No known key found for this signature in database
GPG Key ID: 5255BD653356ACD7
2 changed files with 22 additions and 5 deletions

View File

@ -2854,7 +2854,7 @@ class Client extends MatrixApi {
room.setState(user); room.setState(user);
} }
} }
_updateRoomsByEventUpdate(room, event, type); await _updateRoomsByEventUpdate(room, event, type);
if (store) { if (store) {
await database?.storeEventUpdate(room.id, event, type, this); await database?.storeEventUpdate(room.id, event, type, this);
} }
@ -3020,11 +3020,11 @@ class Client extends MatrixApi {
return room; return room;
} }
void _updateRoomsByEventUpdate( Future<void> _updateRoomsByEventUpdate(
Room room, Room room,
StrippedStateEvent eventUpdate, StrippedStateEvent eventUpdate,
EventUpdateType type, EventUpdateType type,
) { ) async {
if (type == EventUpdateType.history) return; if (type == EventUpdateType.history) return;
switch (type) { switch (type) {
@ -3060,13 +3060,29 @@ class Client extends MatrixApi {
if (event.type == EventTypes.Redaction && if (event.type == EventTypes.Redaction &&
({ ({
room.lastEvent?.eventId, room.lastEvent?.eventId,
room.lastEvent?.relationshipEventId,
}.contains( }.contains(
event.redacts ?? event.content.tryGet<String>('redacts'), event.redacts ?? event.content.tryGet<String>('redacts'),
))) { ))) {
room.lastEvent?.setRedactionEvent(event); room.lastEvent?.setRedactionEvent(event);
break; break;
} }
// Is this event redacting the last event which is a edited event.
final relationshipEventId = room.lastEvent?.relationshipEventId;
if (relationshipEventId != null &&
relationshipEventId ==
(event.redacts ?? event.content.tryGet<String>('redacts')) &&
event.type == EventTypes.Redaction &&
room.lastEvent?.relationshipType == RelationshipTypes.edit) {
final originalEvent = await database?.getEventById(
relationshipEventId,
room,
) ??
room.lastEvent;
// Manually remove the data as it's already in cache until relogin.
originalEvent?.setRedactionEvent(event);
room.lastEvent = originalEvent;
break;
}
// Is this event an edit of the last event? Otherwise ignore it. // Is this event an edit of the last event? Otherwise ignore it.
if (event.relationshipType == RelationshipTypes.edit) { if (event.relationshipType == RelationshipTypes.edit) {

View File

@ -509,7 +509,8 @@ void main() {
nextBatch: '', nextBatch: '',
), ),
); );
expect(room.lastEvent?.eventId, 'testLastEventAfterEdit'); // We do not delete the last edited event. Manually set the last event to original redacted event.
expect(room.lastEvent?.eventId, 'testLastEventBeforeEdit');
expect(room.lastEvent?.body, 'Redacted'); expect(room.lastEvent?.body, 'Redacted');
}); });