Skip to main content
Quick Reference for AI Agents & Developers
  • Block users: CometChat.blockUsers(_:onSuccess:onError:) — pass array of UIDs
  • Unblock users: CometChat.unblockUsers(_:onSuccess:onError:) — pass array of UIDs
  • Get blocked users: BlockedUserRequest.BlockedUserRequestBuilder.set(limit:).build()fetchNext(onSuccess:onError:)
  • Filter direction: .set(direction:).byMe, .me, .both
  • Related: Retrieve Users · Users Overview

Block Users

In other words, as a logged-in user, how do I block a user from sending me messages? You can block users using the blockUsers() method. Once any user is blocked, all the communication to and from the respective user will be completely blocked. You can block multiple users in a single operation. TheblockUsers() method takes an array of string as a parameter that holds the list of UIDs to be blocked.
let blockUsers = ["UID1", "UID2", "UID3"]

CometChat.blockUsers(blockUsers, onSuccess: {
                
   print("Blocked user successfully.")
                
}, onError: { (error) in
                
   print("Blocked user failed with error: \\(error?.errorDescription)")
                
})
In the onSuccess() callback, you receive a dictionary which contains UIDs as the keys and “success” or “fail” as the value based on if the block operation for the UID was successful or not.
Request Parameters:
ParameterTypeDescription
UIDs[String]Array of user UIDs to block. Example: ["cometchat-uid-2"]
Success Response (Dictionary [String: Any]):
ParameterTypeDescription
BoolKey is the UID, value indicates success (true) or failure (false). Example: "cometchat-uid-2": true
Error Response (CometChatException):
ParameterTypeDescription
errorCodeStringUnique error code identifying the error type. Example: "ERR_UID_NOT_FOUND"
errorDescriptionStringHuman-readable description of the error. Example: "User with the specified UID does not exist"

Unblock Users

You can unblock the already blocked users using the unblockUsers() method. You can unblock multiple users in a single operation. TheunblockUsers() method takes an array of string as a parameter that holds the list of UIDs to be unblocked.
let unblockUsers = ["UID1", "UID2", "UID3"]

CometChat.unblockUsers(unblockUsers, onSuccess: { (users) in
                
   print("Unblocked user successfully.")
                
}, onError: { (error) in
                
   print("Unblocked user failed with error: \\(error?.errorDescription)")
                
})
In the onSuccess() callback, you receive a HashMap which contains UIDs as the keys and “success” or “fail” as the value based on if the unblock operation for the UID was successful or not.
Request Parameters:
ParameterTypeDescription
UIDs[String]Array of user UIDs to unblock. Example: ["cometchat-uid-2"]
Success Response (Dictionary [String: Any]):
ParameterTypeDescription
BoolKey is the UID, value indicates success (true) or failure (false). Example: "cometchat-uid-2": true
Error Response (CometChatException):
ParameterTypeDescription
errorCodeStringUnique error code identifying the error type. Example: "ERR_UID_NOT_FOUND"
errorDescriptionStringHuman-readable description of the error. Example: "User with the specified UID does not exist"

Get list of blocked users

In order to fetch the list of blocked users, you can use the BlockedUsersRequest class. To use this class i.e to create an object of the BlockedUsersRequest class, you need to use the BlockedUsersRequestBuilder class. The BlockedUsersRequestBuilder class allows you to set the parameters based on which the blocked users are to be fetched.

BlockedUserRequestBuilder Methods

MethodTypeDescription
set(limit:)IntNumber of blocked users to fetch per request (1-100)
set(searchKeyword:)StringSearch string to filter blocked users
set(direction:)CometChat.BlockedFilter direction: .byMe, .me, .both
Builder Configuration:
ParameterTypeDescription
limitIntMaximum number of blocked users to fetch per request. Range: 1-100. Example: 20
searchKeywordStringSearch string to filter blocked users by UID or name. Example: "john"
directionCometChat.BlockedFilter direction for blocked users list. Example: .byMe
Direction Values:
ValueDescription
.byMeReturns only users blocked by the logged-in user
.meReturns only users who have blocked the logged-in user
.bothReturns both users blocked by me and users who blocked me (default)
Common Filter Combinations:
Use CaseBuilder Configuration
Fetch users I blocked.set(direction: .byMe).set(limit: 20)
Fetch users who blocked me.set(direction: .me).set(limit: 20)
Fetch all blocked relationships.set(direction: .both).set(limit: 20)
Search blocked users by name.set(searchKeyword: "john").set(direction: .byMe).set(limit: 20)
The BlockedUsersRequestBuilder class allows you to set the below parameters:
  1. set(limit: Int) - This method sets the limit i.e. the number of blocked users that should be fetched in a single iteration.
let blockedUserRequest = BlockedUserRequest.BlockedUserRequestBuilder.set(limit: 20).build();
  1. set(searchKeyword: String) - This method allows you to set the search string based on which the blocked users are to be fetched.
let blockedUserRequest = BlockedUserRequest.BlockedUserRequestBuilder
.set(searchKeyword: "abc")
.set(limit: 20)
.build();
  1. set(direction: CometChat.Blocked) - This can hold one of the below values: a. CometChat.Blocked.byMe - This will ensure that the list of blocked users only contains the users blocked by the logged-in user. b. CometChat.Blocked.me - setting the direction to this will return only the list of users that have blocked the logged-in user. c. CometChat.Blocked.both - this will make sure the list of users includes both the above cases. This is the default value for the direction variable if it is not set.

Direction Values

ValueDescription
.byMeUsers blocked by the logged-in user
.meUsers who have blocked the logged-in user
.bothBoth of the above (default)
let blockedUserRequest = BlockedUserRequest.BlockedUserRequestBuilder
.set(searchKeyword: "abc")
.set(limit: 20)
.set(direction: .both)
.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 BlockedUsersRequest class. Once you have the object of the BlockedUsersRequest class, you need to call the fetchNext() method. Calling this method will return a list of User objects containing n number of blocked users where n is the limit set in the builder class.
let blockedUserRequest = BlockedUserRequest.BlockedUserRequestBuilder(limit: 20).build();

blockedUserRequest.fetchNext(onSuccess : { (users) in
            
   print("Blocked users: \\(users)")
            
}, onError : { (error) in
            
   print("Error while fetching the blocked user request:  \\(error?.errorDescription)")

})
Request Parameters:
ParameterTypeDescription
limitIntMaximum number of blocked users to fetch. Example: 30
directionCometChat.BlockedFilter direction for blocked users. Example: .byMe
Success Response - Users Blocked By Me (Array of User Objects):
ParameterTypeDescription
uidString?Unique identifier of the blocked user. Example: "cometchat-uid-2"
nameString?Display name of the blocked user. Example: "George Alan"
avatarString?URL to the user’s avatar image. Example: "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"
linkString?URL to the user’s profile page. Example: nil
roleString?Role assigned to the user. Example: "default"
statusUserStatusCurrent online status of the user. Example: .offline
statusMessageString?Custom status message set by the user. Example: nil
lastActiveAtDoubleUnix timestamp of the user’s last activity. Example: 1771571894.0
hasBlockedMeBoolIndicates if this user has blocked the logged-in user. Example: false
blockedByMeBoolIndicates if the logged-in user has blocked this user. Example: true
deactivatedAtDoubleUnix timestamp when user was deactivated (0 if active). Example: 0.0
tags[String]Array of tags associated with the user. Example: []
metadata[String: Any]?Custom metadata dictionary. Example: [:]
Success Response - Users Who Blocked Me (Array of User Objects):
ParameterTypeDescription
uidString?Unique identifier of the user who blocked me. Example: "cometchat-uid-5"
nameString?Display name of the user. Example: "Mike Johnson"
avatarString?URL to the user’s avatar image. Example: "https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-5.webp"
linkString?URL to the user’s profile page. Example: nil
roleString?Role assigned to the user. Example: "default"
statusUserStatusCurrent online status of the user. Example: .online
statusMessageString?Custom status message set by the user. Example: nil
lastActiveAtDoubleUnix timestamp of the user’s last activity. Example: 1771572000.0
hasBlockedMeBoolIndicates if this user has blocked the logged-in user. Example: true
blockedByMeBoolIndicates if the logged-in user has blocked this user. Example: false
deactivatedAtDoubleUnix timestamp when user was deactivated (0 if active). Example: 0.0
tags[String]Array of tags associated with the user. Example: []
metadata[String: Any]?Custom metadata dictionary. Example: [:]
Error Response (CometChatException):
ParameterTypeDescription
errorCodeStringUnique error code identifying the error type. Example: "ERR_NOT_LOGGED_IN"
errorDescriptionStringHuman-readable description of the error. Example: "User is not logged in"

Common Error Codes

Error CodeDescriptionResolution
ERR_NOT_LOGGED_INUser is not logged inLogin first using CometChat.login()
ERR_UID_NOT_FOUNDUser UID not foundVerify the UID is correct
ERR_INVALID_LIMITInvalid limit valueUse a limit between 1-100