getConversationFromMessage static method

  1. @Deprecated('This function is deprecated and moved into CometChatHelper class. Use CometChatHelper.getConversationFromMessage() instead.')
Future<Conversation?> getConversationFromMessage(
  1. BaseMessage message, {
  2. required dynamic onSuccess(
    1. Conversation conversation
    )?,
  3. required dynamic onError(
    1. CometChatException excep
    )?,
})

get the conversation object from message object.

method could throw PlatformException with error codes specifying the cause

Implementation

@Deprecated(
    'This function is deprecated and moved into CometChatHelper class. Use CometChatHelper.getConversationFromMessage() instead.')
static Future<Conversation?> getConversationFromMessage(BaseMessage message,
    {required Function(Conversation conversation)? onSuccess,
    required Function(CometChatException excep)? onError}) async {
  try {
    String conversationType = message.receiverType;
    User? loggedInUser = await getLoggedInUser();
    String? conversationId = message.conversationId;
    if (loggedInUser != null) {
      AppEntity appEntity = AppEntity();
      if (conversationType == CometChatReceiverType.user) {
        if (loggedInUser.uid == message.sender!.uid) {
          appEntity = message.receiver!;
        } else {
          appEntity = message.sender!;
        }
      } else {
        appEntity = message.receiver!;
      }
      Conversation conversation = Conversation(
          conversationId: conversationId,
          conversationType: conversationType,
          conversationWith: appEntity,
          lastMessage: message,
          updatedAt: message.updatedAt);
      if (onSuccess != null) onSuccess(conversation);
      return conversation;
    } else {
      _errorCallbackHandler(
          CometChatException(
              ErrorCode.errorUserNotLoggedIn,
              ErrorMessage.errorMessageUserNotLoggedIn,
              ErrorMessage.errorMessageUserNotLoggedIn),
          null,
          null,
          onError);
      return null;
    }
  } on PlatformException catch (p) {
    _errorCallbackHandler(null, p, null, onError);
  } catch (e) {
    _errorCallbackHandler(null, null, e, onError);
  }
  return null;
}