refactor: Simplify relates to and make it more type safe

Everything in content is user generated
so we should never expect correct
types and return null instead.
This commit is contained in:
Christian Pauly 2022-03-22 11:46:48 +01:00
parent 08e0284cef
commit 657a332b03
1 changed files with 5 additions and 11 deletions

View File

@ -673,17 +673,11 @@ class Event extends MatrixEvent {
/// Get the event ID that this relationship will reference. `null` if there is none
String? get relationshipEventId {
if (!(content['m.relates_to'] is Map)) {
return null;
}
if (content['m.relates_to'].containsKey('event_id')) {
return content['m.relates_to']['event_id'];
}
if (content['m.relates_to']['m.in_reply_to'] is Map &&
content['m.relates_to']['m.in_reply_to'].containsKey('event_id')) {
return content['m.relates_to']['m.in_reply_to']['event_id'];
}
return null;
final relatesToMap = content.tryGetMap<String, dynamic>('m.relates_to');
return relatesToMap?.tryGet<String>('event_id') ??
relatesToMap
?.tryGetMap<String, dynamic>('m.in_reply_to')
?.tryGet<String>('event_id');
}
/// Get whether this event has aggregated events from a certain [type]