From 7b61f8eb01ec4ff104ec41be7afa81441f3190ce Mon Sep 17 00:00:00 2001 From: henri2h Date: Mon, 12 Sep 2022 17:09:29 +0200 Subject: [PATCH] fix: don't assume redacts attribute from content to be valid --- lib/src/client.dart | 22 ++++++++++--------- lib/src/database/fluffybox_database.dart | 4 +++- .../database/hive_collections_database.dart | 4 +++- lib/src/database/hive_database.dart | 4 +++- lib/src/timeline.dart | 3 ++- 5 files changed, 23 insertions(+), 14 deletions(-) diff --git a/lib/src/client.dart b/lib/src/client.dart index e0ed96bd..927179d3 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -2061,16 +2061,18 @@ class Client extends MatrixApi { case EventUpdateType.inviteState: final stateEvent = Event.fromJson(eventUpdate.content, room); if (stateEvent.type == EventTypes.Redaction) { - final String redacts = eventUpdate.content['redacts']; - room.states.forEach( - (String key, Map states) => states.forEach( - (String key, Event state) { - if (state.eventId == redacts) { - state.setRedactionEvent(stateEvent); - } - }, - ), - ); + final String? redacts = eventUpdate.content.tryGet('redacts'); + if (redacts != null) { + room.states.forEach( + (String key, Map states) => states.forEach( + (String key, Event state) { + if (state.eventId == redacts) { + state.setRedactionEvent(stateEvent); + } + }, + ), + ); + } } else { // We want to set state the in-memory cache for the room with the new event. // To do this, we have to respect to not save edits, unless they edit the diff --git a/lib/src/database/fluffybox_database.dart b/lib/src/database/fluffybox_database.dart index 226fa68b..b5113d9e 100644 --- a/lib/src/database/fluffybox_database.dart +++ b/lib/src/database/fluffybox_database.dart @@ -937,7 +937,9 @@ class FluffyBoxDatabase extends DatabaseApi { // In case of this is a redaction event if (eventUpdate.content['type'] == EventTypes.Redaction) { - final event = await getEventById(eventUpdate.content['redacts'], tmpRoom); + final eventId = eventUpdate.content.tryGet('redacts'); + final event = + eventId != null ? await getEventById(eventId, tmpRoom) : null; if (event != null) { event.setRedactionEvent(Event.fromJson(eventUpdate.content, tmpRoom)); await _eventsBox.put( diff --git a/lib/src/database/hive_collections_database.dart b/lib/src/database/hive_collections_database.dart index 55de1fa1..94427453 100644 --- a/lib/src/database/hive_collections_database.dart +++ b/lib/src/database/hive_collections_database.dart @@ -963,7 +963,9 @@ class HiveCollectionsDatabase extends DatabaseApi { // In case of this is a redaction event if (eventUpdate.content['type'] == EventTypes.Redaction) { - final event = await getEventById(eventUpdate.content['redacts'], tmpRoom); + final eventId = eventUpdate.content.tryGet('redacts'); + final event = + eventId != null ? await getEventById(eventId, tmpRoom) : null; if (event != null) { event.setRedactionEvent(Event.fromJson(eventUpdate.content, tmpRoom)); await _eventsBox.put( diff --git a/lib/src/database/hive_database.dart b/lib/src/database/hive_database.dart index 5795ecab..16549bc8 100644 --- a/lib/src/database/hive_database.dart +++ b/lib/src/database/hive_database.dart @@ -910,7 +910,9 @@ class FamedlySdkHiveDatabase extends DatabaseApi { // In case of this is a redaction event if (eventUpdate.content['type'] == EventTypes.Redaction) { final tmpRoom = Room(id: eventUpdate.roomID, client: client); - final event = await getEventById(eventUpdate.content['redacts'], tmpRoom); + final eventId = eventUpdate.content.tryGet('redacts'); + final event = + eventId != null ? await getEventById(eventId, tmpRoom) : null; if (event != null) { event.setRedactionEvent(Event.fromJson(eventUpdate.content, tmpRoom)); await _eventsBox.put( diff --git a/lib/src/timeline.dart b/lib/src/timeline.dart index 781ce8a7..102838d3 100644 --- a/lib/src/timeline.dart +++ b/lib/src/timeline.dart @@ -504,7 +504,8 @@ class Timeline { // Handle redaction events if (eventUpdate.content['type'] == EventTypes.Redaction) { - final index = _findEvent(event_id: eventUpdate.content['redacts']); + final index = + _findEvent(event_id: eventUpdate.content.tryGet('redacts')); if (index < events.length) { removeAggregatedEvent(events[index]);