markMessageAsUnread static method

Future<Conversation?> markMessageAsUnread(
  1. BaseMessage baseMessage, {
  2. required dynamic onSuccess(
    1. Conversation conversation
    )?,
  3. required dynamic onError(
    1. CometChatException excep
    )?,
})

Marks a BaseMessage as unread.

The markMessageAsUnread function takes in a BaseMessage object, which represents the message you want to mark as unread.

Returns a Conversation object on success. Marks a message as unread and returns the updated conversation.

Migration Note: Migrated from platform channels to native Dart implementation. Uses MessageRepository for marking messages as unread.

Android Reference: CometChat.markMessageAsUnread()

Implementation

static Future<Conversation?> markMessageAsUnread(
  BaseMessage baseMessage, {
  required Function(Conversation)? onSuccess,
  required Function(CometChatException excep)? onError,
}) async {
  try {
    // Get SDK instance
    final sdk = SdkRegistry.getInstance();

    // The conversation the message belongs to: the group, or — for a 1:1 —
    // the OTHER party (the receiver of a message I sent, the sender of one I
    // received), as markAsRead resolves it. `receiverUid` alone is the
    // logged-in user for a received message, which marked (and re-fetched)
    // the wrong conversation. Keyed as BEH-READ-16..19.
    final selfUid = sdk.auth.getLoggedInUser()?.uid;
    final senderUid = baseMessage.sender?.uid;
    final String peerId =
        baseMessage.receiverType == 'group' ||
            senderUid == null ||
            senderUid == selfUid
        ? baseMessage.receiverUid
        : senderUid;

    // Captured as the request goes out: an answer received after this
    // session ended is never written (BEH-SYNC-06).
    final issuedIn = OfflineRuntime.instance.readSession;

    // Call native Dart message repository
    final answered = await sdk.messages.markAsUnread(
      baseMessage.id,
      peerId,
      baseMessage.receiverType,
    );

    // BEH-CONV-13: the conversation the server ANSWERED with is written back
    // through the getConversation LWW path (unread count, read marker, tags,
    // and hasBlockedMe — this endpoint forces withBlockedInfo, BEH-CONV-16).
    // No conversation in the answer (unread-message-count mode) → the cache
    // is left untouched: no estimate. No-op when offline is disabled.
    if (answered != null &&
        OfflineRuntime.instance.storeFor(issuedIn) != null) {
      try {
        final fromAnswer = sdk.conversations.parseConversation(answered);
        await OfflineRuntime.instance.writeIn(
          issuedIn,
          (store) => ConversationCacheWriter.writeThrough(
            store,
            fromAnswer,
            withBlockedInfo: true,
            // chat-api's mark-unread answer is built without the pin
            // annotation (V3_0 ConversationCore
            // markConversationMessagesUnread), so it says nothing about
            // the pin: keep the stored one.
            pinAuthoritative: false,
          ),
        );
      } catch (_) {
        /* best-effort — never fail a successful server op */
      }
    }

    // Get updated conversation for the callback (unchanged public contract:
    // onSuccess receives the re-fetched conversation).
    final conversation = baseMessage.receiverType == 'user'
        ? await sdk.conversations.getUserConversation(peerId)
        : await sdk.conversations.getGroupConversation(peerId);

    // Call success callback
    if (onSuccess != null) onSuccess(conversation);
    return conversation;
  } on SdkException catch (sdkEx) {
    // Convert SdkException to CometChatException
    final cometChatEx = CometChatException(
      sdkEx.code,
      sdkEx.details ?? sdkEx.message,
      sdkEx.message,
    );
    _errorCallbackHandler(cometChatEx, null, null, onError);
  } catch (e) {
    _errorCallbackHandler(null, null, e, onError);
  }
  return null;
}