Event constructor
Event({
- EventStatus status = defaultStatus,
- required Map<
String, dynamic> content, - required String type,
- required String eventId,
- required String senderId,
- required DateTime originServerTs,
- Map<
String, dynamic> ? unsigned, - Map<
String, dynamic> ? prevContent, - String? stateKey,
- String? redacts,
- required Room room,
- MatrixEvent? originalSource,
Implementation
Event({
this.status = defaultStatus,
required Map<String, dynamic> super.content,
required super.type,
required String eventId,
required super.senderId,
required DateTime originServerTs,
Map<String, dynamic>? unsigned,
Map<String, dynamic>? prevContent,
String? stateKey,
super.redacts,
required this.room,
MatrixEvent? originalSource,
}) : _originalSource = originalSource,
super(
eventId: eventId,
originServerTs: originServerTs,
roomId: room.id,
) {
this.eventId = eventId;
this.unsigned = unsigned;
// synapse unfortunately isn't following the spec and tosses the prev_content
// into the unsigned block.
// Currently we are facing a very strange bug in web which is impossible to debug.
// It may be because of this line so we put this in try-catch until we can fix it.
try {
this.prevContent = (prevContent != null && prevContent.isNotEmpty)
? prevContent
: (unsigned != null &&
unsigned.containsKey('prev_content') &&
unsigned['prev_content'] is Map)
? unsigned['prev_content']
: null;
} catch (_) {
// A strange bug in dart web makes this crash
}
this.stateKey = stateKey;
// Mark event as failed to send if status is `sending` and event is older
// than the timeout. This should not happen with the deprecated Moor
// database!
if (status.isSending) {
// Age of this event in milliseconds
final age = DateTime.now().millisecondsSinceEpoch -
originServerTs.millisecondsSinceEpoch;
final room = this.room;
if (
// We don't want to mark the event as failed if it's the lastEvent in the room
// since that would be a race condition (with the same event from timeline)
// The `room.lastEvent` is null at the time this constructor is called for it,
// there's no other way to check this.
room.lastEvent?.eventId != null &&
// If the event is in the sending queue, then we don't mess with it.
!room.sendingQueueEventsByTxId.contains(transactionId) &&
// Else, if the event is older than the timeout, then we mark it as failed.
age > room.client.sendTimelineEventTimeout.inMilliseconds) {
// Update this event in database and open timelines
final json = toJson();
json['unsigned'] ??= <String, dynamic>{};
json['unsigned'][messageSendingStatusKey] = EventStatus.error.intValue;
// ignore: discarded_futures
room.client.handleSync(
SyncUpdate(
nextBatch: '',
rooms: RoomsUpdate(
join: {
room.id: JoinedRoomUpdate(
timeline: TimelineUpdate(
events: [MatrixEvent.fromJson(json)],
),
),
},
),
),
);
}
}
}