cancelSend method

Future<void> cancelSend()

Removes an unsent or yet-to-send event from the database and timeline. These are events marked with the status SENDING or ERROR. Throws an exception if used for an already sent event!

Implementation

Future<void> cancelSend() async {
  if (status.isSent) {
    throw Exception('Can only delete events which are not sent yet!');
  }

  await room.client.database.removeEvent(eventId, room.id);

  if (room.lastEvent != null && room.lastEvent!.eventId == eventId) {
    final redactedBecause = Event.fromMatrixEvent(
      MatrixEvent(
        type: EventTypes.Redaction,
        content: {'redacts': eventId},
        redacts: eventId,
        senderId: senderId,
        eventId: '${eventId}_cancel_send',
        originServerTs: DateTime.now(),
      ),
      room,
    );

    await room.client.handleSync(
      SyncUpdate(
        nextBatch: '',
        rooms: RoomsUpdate(
          join: {
            room.id: JoinedRoomUpdate(
              timeline: TimelineUpdate(events: [redactedBecause]),
            ),
          },
        ),
      ),
    );
  }
  room.client.onCancelSendEvent.add(eventId);
}