Manage room user attributes
Overview
The In-app Chat SDK provides the feature of configuring user attributes of users in a room, that is, customizing user attributes of users in a room. This feature applies to the following scenarios:
- Configure roles of room members, such as setting the room owner, administrator, participant, and participant level.
- Turn on/off the camera, turn on/off the microphone, mute/unmute a user, and enable/disable whiteboard sharing.
Implementation process
-
User attributes of up to 500 users can be configured in each room, and the user attributes are stored in
Key-Value
format. To increase the maximum number of user attributes that can be configured in each room, developers can contact ZEGOCLOUD technical support. -
The total length of user attribute
Key-Value
pairs of each user in a room cannot exceed 144 bytes, and the number ofKey-Value
pairs cannot exceed 30. In a singleKey-Value
pair, the key cannot exceed 8 bytes, and the value cannot exceed 64 bytes. To increase the maximum length of aKey-Value
pair, developers can contact ZEGOCLOUD technical support. -
After a room is destroyed, the configured customized user attributes are also deleted.
Configure user attributes
The operator and the user whose attributes are configured or modified must be users in the room.
Developers can call the setRoomMembersAttributes API to configure user attributes of users in a room. Configured user attributes are stored in Key-Value
format.
- When a key does not exist, configuring user attributes indicates adding a user attribute.
- When a key already exists, configuring user attributes indicates updating the value of an existing attribute.
Map<String, String> attributes = {"key1": "value1"};
List<String> userIDs = ["userID1"];
ZIM
.getInstance()
?.setRoomMembersAttributes(
attributes, userIDs, 'roomID', ZIMRoomMemberAttributesSetConfig())
.then((value) {
// The logic after the configuration is successful.
})
.catchError((onError) {
// The logic after the configuration fails.
});
Query user attributes of some users
Users in a room can call the queryRoomMembersAttributes API to query the user attributes of some users in the room.
Map<String, String> attributes = {"key1": "value1"};
List<String> userIDs = ["userID1"];
ZIM
.getInstance()
?.queryRoomMembersAttributes(userIDs, 'roomID')
.then((value) {
// The logic after the configuration is successful.
})
.catchError((onError) {
// The logic after the configuration fails.
});
Query user attributes of all users
Users in a room can call the queryRoomMemberAttributesList API to query the user attributes of all users in the room.
ZIM
.getInstance()
?.queryRoomMemberAttributesList(
'roomID', ZIMRoomMemberAttributesQueryConfig())
.then((value) {
// The logic after the configuration is successful.
})
.catchError((onError) {
// The logic after the configuration fails.
});
User attribute change notification
When user attributes of members in a room are changed, developers can register the onRoomMemberAttributesUpdated method of the setEventHandler callback API to listen for user attribute change notifications of members in the room.
ZIMEventHandler.onRoomMemberAttributesUpdated = (
ZIM zim,List<ZIMRoomMemberAttributesUpdateInfo> infos,ZIMRoomOperatedInfo operatedInfo, String roomID){
//The service logic after user attributes of members in a room are changed.
};
When a user in a room calls the setRoomMembersAttributes API:
-
If the number of users in the room is less than or equal to 500, all users in the room will receive the user attribute change notification.
-
If the number of users in the room exceeds 500, only the operator and the user whose attribute is configured or modified will receive the user attribute change notification.
To increase the maximum number of users in the room who can receive the user attribute change notification, developers can contact ZEGOCLOUD technical support.
Batch query room user information
Developers can use the queryRoomMembers interface to query the information of users in a room in bulk by passing the roomID and an array of userIDs. After a successful API call, the callback will return the userList of successfully queried users (which can be used to confirm if the target users are in the room), as well as the errorUserList of failed queries (which can be used to confirm if the target users are not in the room).
The default limit for the number of users that can be queried in bulk is 10.
The sample code is as follows:
ZIM.getInstance().queryRoomMembers(["userA","userB"], "roomID")
.then((ZIMRoomMembersQueriedResult value) {
// Map of userID mapped to ZIMErrorUserInfo, if there is a need to pay attention to the failure reason, you can read this data structure
Map<String, ZIMErrorUserInfo> targetErrorUserInfoMap = {};
Map<String, ZIMRoomMemberInfo> targetRoomMemberMap = {};
for (ZIMRoomMemberInfo currentMemberInfo in value.memberList) {
targetRoomMemberMap[currentMemberInfo.userID] = currentMemberInfo;
}
for (ZIMErrorUserInfo currentErrorUserInfo in value.errorUserList) {
targetErrorUserInfoMap[currentErrorUserInfo.userID] = currentErrorUserInfo;
}
// Check if the target member is in the room
bool isTargetUserIdAInTheRoom = targetRoomMemberMap.containsKey(userIdA);
}).catchError((onError){
// Handle failure
});