removeMessage method

void removeMessage(
  1. Message message
)

Remove a message from this channelState.

Implementation

void removeMessage(Message message) async {
  await _channel._client.chatPersistenceClient?.deleteMessageById(message.id);

  final parentId = message.parentId;
  // i.e. it's a thread message, Remove it
  if (parentId != null) {
    final newThreads = {...threads};
    // Early return in case the thread is not available
    if (!newThreads.containsKey(parentId)) return;

    // Remove thread message shown in thread page.
    newThreads.update(
      parentId,
      (messages) => [...messages.where((e) => e.id != message.id)],
    );

    _threads = newThreads;

    // Early return if the thread message is not shown in channel.
    if (message.showInChannel == false) return;
  }

  // Remove regular message, thread message shown in channel
  var updatedMessages = [...messages]..removeWhere((e) => e.id == message.id);

  // Remove quoted message reference from every message if available.
  updatedMessages = [...updatedMessages].map((it) {
    // Early return if the message doesn't have a quoted message.
    if (it.quotedMessageId != message.id) return it;

    // Setting it to null will remove the quoted message from the message.
    return it.copyWith(
      quotedMessage: null,
      quotedMessageId: null,
    );
  }).toList();

  _channelState = _channelState.copyWith(
    messages: updatedMessages,
  );
}