User Relationship

Update time: 2023/02/09 08:11:48

NIMUserManager is used to manage user relations and apply settings for messages in a session. If the user relation data is not stored on the CommsEase server, you must manage the user relation data on the app server.

Instant Messaging provides three built-in user relationships: friends, blocklist, and Do-Not-Disturb. The three relationships are independent of each other and do not affect each other.

Friend relationship

Get friend relationship

objc@protocol NIMUserManager <NSObject>
/**
 *  Return a friend list
 *
 *  @return NIMUser list
 */
- (nullable NSArray<NIMUser *> *)myFriends;
@end

The friend list has a local cache, and the cache will be automatically updated with the server after manual/automatic login. The interface returns a list of NIMUser. NIMUser wraps the friend ID managed on the CommsEase server, the friend's session settings (whether a message alert is required and whether the user is blocked.), and the user's detailed information NIMUserInfo (user information must be stored on the CommsEase server.

Manage friends

Prototype

objc@protocol NIMUserManager <NSObject>
/**
 *  Manage friends
 *
 *  @param request    The request to manage friends
 *  @param completion Callback for completion
 */
- (void)requestFriend:(NIMUserRequest *)request
            completion:(NIMUserBlock)block;
@end            

NIMUserRequest properties

Parameter Type Description
userId NSString Target user ID
operation NIMUserOperation Operation type
message NSString Additional message

Operation types:

Type Description
NIMUserOperationAdd Add a friend without verification
NIMUserOperationRequest Send a friend request
NIMUserOperationVerify Verify a friend request
NIMUserOperationReject Reject a friend request

Friend requests include add a friend without verification and approve/reject a friend request.

Example:

objcNIMUserRequest *request = [[NIMUserRequest alloc] init];
request.userId = self.user.usrId; //The user ID
request.operation = NIMUserOperationRequest; //The verification method
request.message         = @"make a friend";                                 //The custom message for verification.

Listen for friend operations

After performing friend operations, the target user will receive a system notification message NIMSystemNotification, which can be monitored by registering addDelegate: in NIMSystemNotificationManager. For more information, see System notifications.

  • Set the value of NIMSystemNotification - Type to NIMSystemNotificationTypeFriendAdd.

  • NIMSystemNotification - sourceID is the account that perform the operation.

  • NIMSystemNotification - targetID is the ID of a target account.

  • Set the value of NIMSystemNotification - attachment to NIMUserAddAttachment. Cast the received value type.

  • NIMUserAddAttachment - operationType have the following subtypes:

    • NIMUserOperationAdd: Add a friend without verification
    • NIMUserOperationRequest: Send a friend request
    • NIMUserOperationVerify: Verify a friend request
    • NIMUserOperationReject: Reject a friend request

Listen to changes of friend relations

If the friend relationship changes, a callback will be triggered:

objc@protocol NIMUserManagerDelegate <NSObject>

@optional
/**
 * Friends status changes (online)
 *
 *  @param user User object
 */
- (void)onFriendChanged:(NIMUser *)user;
@end

Delete a friend

The friend relationship between users can be canceled.

objc/**
 *  Delete a friend
 *
 *  @param userId      Friend ID
 *  @param remove      specify whether remarks are deleted
 *@param completion Completion callback
 */
- (void)deleteFriend:(NSString *)userId
         removeAlias:(BOOL)remove
          completion:(nullable NIMUserBlock)completion;
@end            

Properties

Parameter Type Description
userId NSString Target user ID
remove BOOL Whether remarks are deleted.
NIMUserBlock NIMUserBlock Callback for completion.

Check for friend relationship

objc@protocol NIMUserManager <NSObject>
/**
 *  Check if the user is included in the friend list
 *
 *  @param userId User ID
 *
 *  @return relationship
 */
- (BOOL)isMyFriend:(NSString *)userId;

Properties

Parameter Type Description
userId NSString Target user ID

Edit the nickname of a friend

objc@protocol NIMUserManager <NSObject>
/**
 *  @param user Target user 
 */
- (void)updateUser:(NIMUser *)user 
        completion:(nullable NIMUserBlock)completion;

Blocklist

You cannot receive messages from users in the blocklist.

Get the blocklist

objc@protocol NIMUserManager <NSObject>
/**
 *  Return all users included in the blacklist
 *
 *  @return List of NIMUser included in the blacklist
 */
- (nullable NSArray<NIMUser *> *)myBlackList;
@end

The blocklist list has a local cache, and the cache will be automatically updated with the server after manual/automatic login. The interface returns a list of NIMUser. NIMUser wraps the friend ID managed on the CommsEase server, the friend's session settings (whether a message alert is required and whether the user is blocked.), and the user's detailed information NIMUserInfo (user information must be stored on the CommsEase server.

Add a user to the blocklist

objc@protocol NIMUserManager <NSObject>
/**
 * Add a user to the blacklist
 *
 *  @param userId User ID
 *@param completion Completion callback
 */
- (void)addToBlackList:(NSString *)userId
            completion:(NIMUserBlock)completion;
@end            

Properties

Parameter Type Description
userId NSString Target user ID
NIMUserBlock NIMUserBlock Callback for completion.

Remove a user from the blocklist

objc@protocol NIMUserManager <NSObject>

/**
 * Remove a user from the blacklist
 *
 *  @param userId User ID
 *@param completion Completion callback
 */
- (void)removeFromBlackBlackList:(NSString *)userId
                      completion:(NIMUserBlock)completion;
@end                      

Properties

Parameter Type Description
userId NSString Target user ID
NIMUserBlock NIMUserBlock Callback for completion.

Listen to changes of the blocklist

If a target user is added to the blocklist, the local cache will be updated at the same time, and the callback will be triggered:

objc@protocol NIMUserManagerDelegate <NSObject>

@optional
/**
 *  Blacklist changes (online)
 */
- (void)onBlackListChanged;
@end

Check whether an account is included in the blocklist

objc@protocol NIMUserManager <NSObject>
/**
 *  Check if a user is included in the blacklist
 *
 *  @param userId User ID
 *
 *  @return result
 */
- (BOOL)isUserInBlackList:(NSString *)userId;
@end

Properties

Parameter Type Description
userId NSString Target user ID

Do-Not-Disturb

Get the Do-Not-Disturb list

objc@protocol NIMUserManager <NSObject>
/**
 * Do-Not-Disturb list
 *
 *  @return List of NIMUser with notifications muted
 */
- (NSArray<NIMUser *> *)myMuteUserList;
@end

The muted list has a local cache, and the cache will be automatically updated with the server after manual/automatic login. The interface returns a list of NIMUser. NIMUser wraps the friend ID managed on the CommsEase server, the friend's session settings (whether a message alert is required and whether the user is blocked.), and the user's detailed information NIMUserInfo (user information must be stored on the CommsEase server.

Set the mute status

objc@protocol NIMUserManager <NSObject>
/**
 *  Set the mute status
 *
 *  @param notify       enable or disable the alert
 *  @param userId User ID
 *  @param completion   Completion callback
 */
- (void)updateNotifyState:(BOOL)notify
                  forUser:(NSString *)userId
               completion:(nullable NIMUserBlock)completion
@end               

Properties

Parameter Type Description
notify BOOL Whether alerts are enabled for notifications
userId NSString Target user ID
NIMUserBlock NIMUserBlock Callback for completion.

After the setting is applied, the local cached data will be updated at the same time.

Listen to changes of the Do-Not-Disturb list

objc@protocol NIMUserManagerDelegate <NSObject>
/**
 *  The list of muted members changes (online)
 */
- (void)onMuteListChanged;
@end               

Check accounts for Do-Not-Disturb

objc@protocol NIMUserManager <NSObject>
/**
 *  Whether a user is muted for new messages
 *
 *  @param userId User ID
 *
 *  @return the result
 */
- (BOOL)notifyForNewMsg:(NSString *)userId;
@end

Properties

Parameter Type Description
userId NSString Target user ID
Was this page helpful?
Yes
No
  • Friend relationship
  • Get friend relationship
  • Manage friends
  • Listen for friend operations
  • Listen to changes of friend relations
  • Delete a friend
  • Check for friend relationship
  • Edit the nickname of a friend
  • Blocklist
  • Get the blocklist
  • Add a user to the blocklist
  • Remove a user from the blocklist
  • Listen to changes of the blocklist
  • Check whether an account is included in the blocklist
  • Do-Not-Disturb
  • Get the Do-Not-Disturb list
  • Set the mute status
  • Listen to changes of the Do-Not-Disturb list
  • Check accounts for Do-Not-Disturb