partialUpdateMessage method

Future<UpdateMessageResponse> partialUpdateMessage(
  1. Message message, {
  2. Map<String, Object?>? set,
  3. List<String>? unset,
  4. bool skipEnrichUrl = false,
})

Partially updates the message in this channel.

Use set to define values to be set.

Use unset to define values to be unset.

Implementation

Future<UpdateMessageResponse> partialUpdateMessage(
  Message message, {
  Map<String, Object?>? set,
  List<String>? unset,
  bool skipEnrichUrl = false,
}) async {
  _checkInitialized();
  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(const StreamChatError('Message cancelled'));

  // ignore: parameter_assignments
  message = message.copyWith(
    state: MessageState.updating,
    localUpdatedAt: DateTime.now(),
  );

  state?.updateMessage(message);

  try {
    // Wait for the previous update call to finish. Otherwise, the order of
    // messages will not be maintained.
    final response = await _updateMessageLock.synchronized(
      () => _client.partialUpdateMessage(
        message.id,
        set: set,
        unset: unset,
        skipEnrichUrl: skipEnrichUrl,
      ),
    );

    final updatedMessage = response.message.syncWith(message).copyWith(
          // Update the message state to updated.
          state: MessageState.updated,
          ownReactions: message.ownReactions,
        );

    state?.updateMessage(updatedMessage);

    return response;
  } catch (e) {
    if (e is StreamChatNetworkError) {
      if (e.isRetriable) {
        state!._retryQueue.add([
          message.copyWith(
            // Update the message state to failed.
            state: MessageState.updatingFailed,
          ),
        ]);
      } else {
        // Reset the message to original state if the update fails and is not
        // retriable.
        state?.updateMessage(originalMessage.copyWith(
          state: MessageState.updatingFailed,
        ));
      }
    }

    rethrow;
  }
}