deleteMessage method

Future<void> deleteMessage(
  1. List<RCIMIWMessage> deleteMessage,
  2. BuildContext context
)

Implementation

Future<void> deleteMessage(
    List<RCIMIWMessage> deleteMessage, BuildContext context) async {
  final messages = List<RCIMIWMessage>.of(deleteMessage);
  if (messages.isEmpty ||
      _disposed ||
      _sessionInvalid ||
      !_conversationInitialized) {
    return;
  }
  stopPlayVoiceAndReference(messages, context);

  final targetEngine = _sessionEngine;
  if (targetEngine == null || !_isSessionEngineCurrent(targetEngine)) return;
  final operationConversationType = _con.conversationType;
  final operationTargetId = _con.targetId;
  final operationChannelId = _con.channelId;

  bool isCurrentOperation() {
    return _isSessionEngineCurrent(targetEngine) &&
        _con.conversationType == operationConversationType &&
        _con.targetId == operationTargetId &&
        _sameChannelId(_con.channelId, operationChannelId);
  }

  // Android Kit 对没有 UID 的未发送消息、聊天室消息只做本地删除;
  // 这些消息调用远端删除接口必然失败,且不会产生可用于刷新当前页的
  // 远端回调。
  final canDeleteRemotely =
      operationConversationType != RCIMIWConversationType.chatroom &&
          messages.every((message) =>
              message.messageUId != null && message.messageUId!.isNotEmpty);
  if (!canDeleteRemotely) {
    await _deleteMessagesLocally(
      targetEngine,
      messages,
      conversationType: operationConversationType,
      targetId: operationTargetId,
      channelId: operationChannelId,
    );
    return;
  }

  final immediateCode = await targetEngine.deleteMessages(
    operationConversationType ?? RCIMIWConversationType.invalid,
    operationTargetId ?? '',
    operationChannelId,
    messages,
    callback: IRCIMIWDeleteMessagesCallback(
      onMessagesDeleted: (code, returnedMessages) {
        final effectiveMessages =
            returnedMessages == null || returnedMessages.isEmpty
                ? messages
                : returnedMessages;
        if (code == 0) {
          if (_isSessionCurrent() &&
              identical(targetEngine, _sessionEngine)) {
            _rememberTerminalStateForMessages(
              <RCIMIWMessage>[...messages, ...effectiveMessages],
              RCIMIWReferenceMessageStatus.deleted,
              conversationType: operationConversationType,
              targetId: operationTargetId,
              channelId: operationChannelId,
            );
          }
          // 远端成功后同步本地库,避免退出重进时再次读到旧对象。
          if (isCurrentOperation()) {
            _applyDeletedMessages(
              effectiveMessages,
              conversationTypeHint: operationConversationType,
              targetIdHint: operationTargetId,
              channelIdHint: operationChannelId,
            );
          }
          unawaited(
            _deleteMessagesLocally(
              targetEngine,
              messages,
              conversationType: operationConversationType,
              targetId: operationTargetId,
              channelId: operationChannelId,
            ),
          );
        }
        RCIMWrapperPlatform.instance.writeLog(
          'RCKChatProvider deleteMessage',
          '',
          code ?? 0,
          'onMessagesDeleted: ${returnedMessages?.length}',
        );
      },
    ),
  );
  RCIMWrapperPlatform.instance.writeLog(
    'RCKChatProvider deleteMessage',
    '',
    immediateCode,
    'deleteMessages immediate result',
  );
}