Quick Reference for AI Agents & Developers
- Kotlin
- Java
Block Users
As a logged-in user, how do I block a user from sending me messages? You can block users using theblockUsers() method. Once any user is blocked, all communication to and from that user will be completely blocked. You can block multiple users in a single operation. The blockUsers() method takes a List<String> as a parameter which holds the list of UIDs to be blocked.
- Java
- Kotlin
onSuccess() callback, you receive a HashMap which contains UIDs as the keys and success or fail as the value based on whether the block operation for the UID was successful or not.
Unblock Users
As a logged-in user, how do I unblock a user I previously blocked? You can unblock previously blocked users using theunblockUsers() method. You can unblock multiple users in a single operation. The unblockUsers() method takes a List<String> as a parameter which holds the list of UIDs to be unblocked.
- Java
- Kotlin
onSuccess() callback, you receive a HashMap which contains UIDs as the keys and success or fail as the value based on whether the unblock operation for the UID was successful or not.
Get list of blocked users
As a logged-in user, how do I get a list of all users I’ve blocked? To fetch the list of blocked users, you can use theBlockedUsersRequest class. 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.
The BlockedUsersRequestBuilder class allows you to set the following parameters:
Set Limit
This method sets the limit i.e. the number of blocked users that should be fetched in a single iteration.- Java
- Kotlin
Set Search Keyword
This method allows you to set the search string based on which the blocked users are to be fetched.- Java
- Kotlin
Set Direction
CometChat.BlockedUsersRequest.DIRECTION.BLOCKED_BY_ME- Ensures that the list of blocked users only contains users blocked by the logged-in user.CometChat.BlockedUsersRequest.DIRECTION.HAS_BLOCKED_ME- Ensures that the list of blocked users only contains users that have blocked the logged-in user.CometChat.BlockedUsersRequest.DIRECTION.BOTH- Ensures the list of users includes both of the above cases. This is the default value for the direction variable if it is not set.
- Java
- Kotlin
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.
- Java
- Kotlin
Best Practices
Batch Block Operations
Batch Block Operations
When blocking multiple users, use a single
blockUsers() call with a list of UIDs instead of multiple individual calls. This reduces network overhead and improves performance.Handle Partial Failures
Handle Partial Failures
The
onSuccess() callback returns a HashMap with individual success/fail status for each UID. Always check the result map to handle cases where some users were blocked successfully while others failed.Update UI After Blocking
Update UI After Blocking
After blocking a user, update your UI to reflect the change immediately. Remove blocked users from conversation lists and prevent message sending to blocked users on the client side for better UX.