deleteChatItem method

Future<void> deleteChatItem(
  1. ChatItem item
)

Deletes the specified ChatItem posted by the authenticated MyUser.

Implementation

Future<void> deleteChatItem(ChatItem item) async {
  Log.debug('deleteChatItem($item)', '$runtimeType');

  if (item is! ChatMessage && item is! ChatForward) {
    throw UnimplementedError('Deletion of $item is not implemented.');
  }

  Chat? chat = chats[item.chatId]?.chat.value;

  if (item is ChatMessage) {
    if (item.status.value != SendingStatus.error) {
      if (item.author.id != me) {
        throw const DeleteChatMessageException(
          DeleteChatMessageErrorCode.notAuthor,
        );
      }

      if (me != null && chat?.isRead(item, me!) == true) {
        throw const DeleteChatMessageException(
          DeleteChatMessageErrorCode.read,
        );
      }
    }

    await _chatRepository.deleteChatMessage(item);
  } else if (item is ChatForward) {
    if (item.status.value != SendingStatus.error) {
      if (item.author.id != me) {
        throw const DeleteChatForwardException(
          DeleteChatForwardErrorCode.notAuthor,
        );
      }

      if (me != null && chat?.isRead(item, me!) == true) {
        throw const DeleteChatForwardException(
          DeleteChatForwardErrorCode.read,
        );
      }
    }

    await _chatRepository.deleteChatForward(item);
  }
}