getConversationFromMessage static method

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

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{
      if(onError != null) onError(CometChatException(ErrorCode.errorUserNotLoggedIn, "User not logged in", "User not logged in"));
      return null;
    }
  } on PlatformException catch (platformException) {
    if(onError != null) onError(CometChatException(platformException.code, platformException.details, platformException.message));
  } catch (e) {
    debugPrint("Error: $e");
    if(onError != null) onError(CometChatException(ErrorCode.errorUnhandledException, e.toString() , e.toString()));
  }
  return null;
}