getOrCreateConversation method

Future<Conversation> getOrCreateConversation(
  1. String currentUserId,
  2. String peerUserId
)

Implementation

Future<Conversation> getOrCreateConversation(String currentUserId, String peerUserId) async {
  // Step 1: Get all conversations of current user
  if (kDebugMode) {
    print("REQUEST FROM Current UserID $currentUserId");
  }
  final response = await client.getConversations(UserIdRequest()..userId = currentUserId);

  // Step 2: Look for existing 1-on-1 conversation
  for (final convo in response.conversations) {
    if (!convo.isGroup && convo.memberIds.contains(peerUserId)) {
      return convo; // ✅ return the whole Conversation object
    }
  }

  // Step 3: If not found, create a new one
  final createReq = CreateConverationRequest()
    ..name = peerUserId
    ..isGroup = false
    ..createBy = currentUserId
    ..memberIds.addAll([currentUserId, peerUserId]);

  final newConvo = await client.createConversation(createReq);
  return newConvo; // ✅ already a Conversation type
}