editChatMessage method

Future<void> editChatMessage(
  1. ChatMessage item, {
  2. ChatMessageTextInput? text,
  3. ChatMessageAttachmentsInput? attachments,
  4. ChatMessageRepliesInput? repliesTo,
})

Edits the specified ChatMessage posted by the authenticated MyUser.

Implementation

Future<void> editChatMessage(
  ChatMessage item, {
  ChatMessageTextInput? text,
  ChatMessageAttachmentsInput? attachments,
  ChatMessageRepliesInput? repliesTo,
}) async {
  Log.debug('editChatMessage($item, $text)', '$runtimeType');

  if (text?.changed?.val.trim() == item.text?.val.trim()) {
    text = null;
  } else if (text != null) {
    text = ChatMessageTextInput(
      text.changed?.val.trim().isEmpty != false
          ? null
          : ChatMessageText(text.changed!.val.trim()),
    );
  }

  if (item.attachments
      .map((e) => e.id)
      .sameAs(attachments?.changed.map((e) => e.id))) {
    attachments = null;
  }

  if (item.repliesTo
      .map((e) => e.original?.id)
      .sameAs(repliesTo?.changed.map((e) => e))) {
    repliesTo = null;
  }

  if ((text?.changed ?? item.text)?.val.isEmpty != false &&
      (attachments?.changed ?? item.attachments).isEmpty &&
      (repliesTo?.changed ?? item.repliesTo).isNotEmpty) {
    text = const ChatMessageTextInput(ChatMessageText(' '));
  }

  if (text != null || attachments != null || repliesTo != null) {
    return _chatRepository.editChatMessage(
      item,
      text: text,
      attachments: attachments,
      repliesTo: repliesTo,
    );
  }
}