getDisplayEvent method

Event getDisplayEvent(
  1. Timeline timeline
)

Fetches the event to be rendered, taking into account all the edits and the like. It needs a timeline for that.

Implementation

Event getDisplayEvent(Timeline timeline) {
  if (redacted) {
    return this;
  }
  if (hasAggregatedEvents(timeline, RelationshipTypes.edit)) {
    // alright, we have an edit
    final allEditEvents = aggregatedEvents(timeline, RelationshipTypes.edit)
        // we only allow edits made by the original author themself
        .where((e) => e.senderId == senderId && e.type == EventTypes.Message)
        .toList();
    // we need to check again if it isn't empty, as we potentially removed all
    // aggregated edits
    if (allEditEvents.isNotEmpty) {
      allEditEvents.sort((a, b) => a.originServerTs.millisecondsSinceEpoch -
                  b.originServerTs.millisecondsSinceEpoch >
              0
          ? 1
          : -1);
      final rawEvent = allEditEvents.last.toJson();
      // update the content of the new event to render
      if (rawEvent['content']['m.new_content'] is Map) {
        rawEvent['content'] = rawEvent['content']['m.new_content'];
      }
      return Event.fromJson(rawEvent, room);
    }
  }
  return this;
}