updateMessage method

Future<UpdateMessageResponse> updateMessage(
  1. Message message
)

Updates the message in this channel.

Waits for a _messageAttachmentsUploadCompleter to complete before actually updating the message.

Implementation

Future<UpdateMessageResponse> updateMessage(Message message) async {
  final originalMessage = message;

  // Cancelling previous completer in case it's called again in the process
  // Eg. Updating the message while the previous call is in progress.
  _messageAttachmentsUploadCompleter
      .remove(message.id)
      ?.completeError('Message Cancelled');

  // ignore: parameter_assignments
  message = message.copyWith(
    status: MessageSendingStatus.updating,
    updatedAt: message.updatedAt,
    attachments: message.attachments.map(
      (it) {
        if (it.uploadState.isSuccess) return it;
        return it.copyWith(uploadState: const UploadState.preparing());
      },
    ).toList(),
  );

  state?.updateMessage(message);

  try {
    if (message.attachments.any((it) => !it.uploadState.isSuccess)) {
      final attachmentsUploadCompleter = Completer<Message>();
      _messageAttachmentsUploadCompleter[message.id] =
          attachmentsUploadCompleter;

      _uploadAttachments(
        message.id,
        message.attachments.map((it) => it.id),
      );

      // ignore: parameter_assignments
      message = await attachmentsUploadCompleter.future;
    }

    final response = await _client.updateMessage(message);

    final m = response.message.copyWith(
      ownReactions: message.ownReactions,
    );

    state?.updateMessage(m);

    return response;
  } catch (e) {
    if (e is StreamChatNetworkError) {
      if (e.isRetriable) {
        state!._retryQueue.add([message]);
      } else {
        state?.updateMessage(originalMessage);
      }
    }
    rethrow;
  }
}