CometChat Allows you to fetch the list of conversations the logged-in user is a part of. This list of conversations consists of both user and group conversations.
In other words, as a logged-in user, how do I retrieve the latest conversations that I’ve been a part of?To fetch the list of conversations, you can use the ConversationsRequest class. To use this class i.e. to create an object of the ConversationsRequest class, you need to use the ConversationsRequestBuilder class. The ConversationsRequestBuilder class allows you to set the parameters based on which the conversations are to be fetched.
This method sets the limit i.e. the number of conversations that should be fetched in a single iteration.
Swift
Report incorrect code
Copy
Ask AI
var conversationRequest = ConversationRequest.ConversationRequestBuilder(limit: 30) .build()
A Maximum of only 50 Conversations can be fetched at once. If you want to fetch more conversations, you can use the fetchNext() on the same conversationsRequest object.
This method makes sure that the tags associated with the conversations are returned along with the other details of the conversations. The default value for this parameter is false.
Swift
Report incorrect code
Copy
Ask AI
var conversationRequest = ConversationRequest.ConversationRequestBuilder(limit: 50) .withTags(true) .build()
This method helps you search a conversation based on User or Group name.
This feature is only available with Conversation & Advanced Search. The Conversation & Advanced Search is only available in Advanced & Customplans. If you’re already on one of these plans, please enable the Conversation & Advanced Search from CometChat Dashboard (Open your app, navigate to Chats -> Settings -> General Configuration)
Swift
Report incorrect code
Copy
Ask AI
var conversationRequest = ConversationRequest.ConversationRequestBuilder(limit: 50) .setSearchKeyword("Hiking") .build()
This feature is only available with Conversation & Advanced Search. The Conversation & Advanced Search is only available in Advanced & Customplans. If you’re already on one of these plans, please enable the Conversation & Advanced Search from CometChat Dashboard (Open your app, navigate to Chats -> Settings -> General Configuration)
Swift
Report incorrect code
Copy
Ask AI
var conversationRequest = ConversationRequest.ConversationRequestBuilder(limit: 50) .setUnread(true) .build()
Finally, once all the parameters are set to the builder class, you need to call the build() method to get the object of the ConversationsRequest class.Once you have the object of the ConversationsRequest class, you need to call the fetchNext() method. Calling this method will return a list of Conversation objects containing X number of conversations depending on the limit set.
Swift (User)
Swift (Group)
Report incorrect code
Copy
Ask AI
let convRequest = ConversationRequest.ConversationRequestBuilder(limit: 20) .setConversationType(conversationType: .user) .build()convRequest.fetchNext(onSuccess: { (conversationList) in print("success of convRequest \(conversationList)") }) { (exception) in print("here exception \(String(describing: exception?.errorDescription))")}
Report incorrect code
Copy
Ask AI
let convRequest = ConversationRequest.ConversationRequestBuilder(limit: 20) .setConversationType(conversationType: .group) .build()convRequest.fetchNext(onSuccess: { (conversationList) in print("success of convRequest \(conversationList)") }) { (exception) in print("here exception \(String(describing: exception?.errorDescription))")}
The Conversation object consists of the following fields:
Field
Information
conversationId
ID of the conversation
conversationType
Type of conversation (user/group)
lastMessage
Last message in the conversation
conversationWith
User or Group object containing the details of the user or group
In other words, as a logged-in user, how do I tag a conversation?To tag a specific conversation, you can use the tagConversation() method. The tagConversation() method accepts three parameters:
conversationWith: UID/GUID of the user/group whose conversation you want to tag.
conversationType: The conversationType variable can hold one of the below two values:
user - Only fetches user conversation.
group - Only fetches group conversations.
tags: The tags variable will be a list of tags you want to add to a conversation.
Swift
Report incorrect code
Copy
Ask AI
let id = "cometchat-uid-1" // id of the user/grouplet tags = ["pinned"]CometChat.tagConversation(conversationWith: id, conversationType: .user, tags: tags, onSuccess: { conversation in print("conversation", conversation)}, onError: { error in print("error", error)})
The tags for conversations are one-way. This means that if user A tags a conversation with user B, that tag will be applied to that conversation only for user A.
In other words, as a logged-in user, how do I retrieve a specific conversation?To fetch a specific conversation, you can use the getConversation method. The getConversation method accepts two parameters:
conversationWith: UID/GUID of the user/group whose conversation you want to fetch.
conversationType: The conversationType variable can hold one of the below two values:
user - Only fetches user conversation.
group - Only fetches group conversations.
Swift (User)
Swift (Group)
Report incorrect code
Copy
Ask AI
CometChat.getConversation(conversationWith: "cometchat-uid-3", conversationType: .user, onSuccess: { conversation in print("success \(String(describing: conversation?.stringValue()))")}) { error in print("error \(String(describing: error?.errorDescription))")}
Report incorrect code
Copy
Ask AI
CometChat.getConversation(conversationWith: "cometchat-guid-101", conversationType: .group, onSuccess: { conversation in print("success \(String(describing: conversation?.stringValue()))")}) { error in print("error \(String(describing: error?.errorDescription))")}
For real-time events, you will always receive Message objects and not Conversation objects. Thus, you will need a mechanism to convert the Message object to a Conversation object. You can use the getConversationFromMessage() method of the CometChat class.
Swift
Report incorrect code
Copy
Ask AI
let myConversation = CometChat.getConversationFromMessage(message)
While converting a Message object to a Conversation object, the unreadMessageCount & tags will not be available in the Conversation object. The unread message count needs to be managed in your client-side code.