User Relationship Management

Update time: 2021/09/25 14:33:06

SDK supports user relationship (friends) management and message settings for user sessions. CommsEase allows chats between users who are not friends. User relationship not managed by CommsEase needs to be maintained by the developer on the application server.

Friendship

Information of friends

The class NIM.Friend.NIMFriendProfile defines details of friends as follows:

Property Description
AccountId Friend account id
InitiativeRelationship Active friends
PassiveRelationship Passive friends
Source Source of friends
Alias Alias of friends
ExtensionalData Extension data
CreatedTimetag Creation timestamp of friends
UpdatedTimetag Update timestamp of friends

Get friend list

Get the collection of friend accounts, then you can use an account to get the corresponding user information, see User information hosting. Local cache is available for friend list, and will be synced and updated automatically with the server after manual/auto login. The function's callback function contains the NIM.Friend.NIMFriendProfileobject list.

  • API prototype
csharp/// <summary>
//Get cached list of friends
/// </summary>
/// <param name="cb"></param>
public static void GetFriendsList(GetFriendsListResultDelegate cb)
  • Example
csharpNIM.Friend.FriendAPI.GetFriendsList(FriendsListResultDelegate);

void FriendsListResultDelegate(NIMFriends result)
{
    if (result != null)
    {
        UnityEngine.Debug.Log(string.Format("GetFriendsList completed,total count:{0}", result.ProfileList == null ? 0 : result.ProfileList.Count());
    }
}

Add friends

Two verification types for adding friends: Directly add as friend or send friend request. When adding friends, you shall input account of the other party, friend verification type and remark (optional).

  • API prototype
csharp/// <summary>
///Add and verify friends
/// </summary>
/// <param name="accid">Account of the other party</param>
/// <param name="verifyType">Verification type</param>
/// <param name="msg"></param>
/// <param name="cb">Callback for operation results</param>
public static void ProcessFriendRequest(string accid, NIMVerifyType verifyType, string msg, FriendOperationDelegate cb)
  • Example
csharp//Directly add as friends, without verification of the other party
NIM.Friend.FriendAPI.ProcessFriendRequest("FRIEND ID",NIMVerifyType.kNIMVerifyTypeAdd, "", OnAddFriend);
//Add as friends, requiring verification of the other party
NIM.Friend.FriendAPI.ProcessFriendRequest("FRIEND ID",NIMVerifyType.kNIMVerifyTypeAsk, "Add friends, and request for accepting verification", OnAddFriend);

void OnAddFriend(int resCode, string jsonExtension, IntPtr userData)
{
    NIM.ResponseCode code = (ResponseCode)resCode;
    //Process operation results. If verification of the other party is required for adding friends, it does not mean that the friend is added successfully, but you need to wait for verification.
}

Accept/reject friend request of the other party

When receiving the system notification about request for friend verification, you can accept or reject the request. API is same with the one for adding friends. verifyType is filled with kNIMVerifyTypeAgree (agree) or kNIMVerifyTypeReject (reject).

The following codes demonstrate how to process friend request by system notification:

csharp//Register system notification
NIM.SysMessage.SysMsgAPI.ReceiveSysMsgHandler += OnReceiveSysMessage;

private void OnReceiveSysMessage(object sender, NIMSysMsgEventArgs e)
{
    var _sysMessage = e.Message;
    string _processFriendReqResult;
    //kNIMSysMsgTypeFriendAdd means friend request.
    if (_sysMessage.Content.MsgType == NIMSysMsgType.kNIMSysMsgTypeFriendAdd)
    {
        //Attachment field is json string {"vt":NIMVerifyType},key is "vt", value is enumeration type `NIMVerifyType`
        string attach = _sysMessage.Content.Attachment;
        string senderId = _sysMessage.Content.SenderId;
        FriendRequestVerify type = FriendRequestVerify.Deserialize(attach);
        if (type.VerifyType == NIMVerifyType.kNIMVerifyTypeAsk)
        {
            _processFriendReqResult = string.Format("{0} Add you as friends, and need to accept verification", senderId);
        }
        if(type.VerifyType == NIMVerifyType.kNIMVerifyTypeAdd)
        {
            _processFriendReqResult = string.Format("{0} Add you as friends", senderId);
        }
        if (type.VerifyType == NIMVerifyType.kNIMVerifyTypeReject)
        {
            _processFriendReqResult = string.Format("{0} Reject your friend request", senderId);
        }
        if (type.VerifyType == NIMVerifyType.kNIMVerifyTypeAgree)
        {
            _processFriendReqResult = string.Format("{0} Agree your friend request", senderId);
        }
    }
}

Delete friend

The relationship will be relieved automatically after deleting friends. Both parties will be disappeared from the list of friends. After friend is deleted, both parties may chat with each other.

  • API prototype
csharp/// <summary>
/// Delete friends
/// </summary>
/// <param name="accid">Account of the other party</param>
/// <param name="cb">Callback for operation results</param>
public static void DeleteFriend(string accid, FriendOperationDelegate cb)
  • Example
csharpNIM.Friend.FriendAPI.DeleteFriend("FRIEND ID", OnDeleteFriendCompleted);

void OnDeleteFriendCompleted(int resCode, string jsonExtension, IntPtr userData)
{
    if (resCode == 200)
    {
        //Delete friends successfully
    }
    else
    {
        UnityEngine.Debug.Log("Delete friend failed:{0}", (ResponseCode)resCode);
    }
}

Notification of friendship change

The event NIM.Friend.FriendAPI.FriendProfileChangedHandler is registered to receive related notification when friends have any change. Types of friendship changes include:

  • kNIMFriendChangeTypeRequest Add friends/process friend request
  • kNIMFriendChangeTypeDel Delete friend
  • kNIMFriendChangeTypeUpdate Update information of friends
  • kNIMFriendChangeTypeSyncList Synchronize the list of friends

Get friendship according to user account

  • API prototype
csharp/// <summary>
//Get cached friend information
/// </summary>
/// <param name="accountId"></param>
/// <param name="cb"></param>
public static void GetFriendProfile(string accountId, GetFriendProfileResultDelegate cb)
  • Example
csharpNIM.Friend.FriendAPI.GetFriendProfile("FRIEND ID", OnGetFriendProfileCompleted);

void OnGetFriendProfileCompleted(string accountId, NIMFriendProfile profile)
{
    if (profile == null)
    {
        UnityEngine.Debug.Log("GetFriendProfile {0} failed", accountId);
    }
    else
    {
        UnityEngine.Debug.Log("GetFriendProfile {0} sucess {1}", accountId, profile.Serialize());
    }
}

It determines that a user is my friend.

  • API prototype
csharp/// <summary>
/// Query that accid is my friend in local cache data
/// </summary>
/// <param name="accid">Friend id</param>
/// <returns>When positive and negative friends are all friends, "true" will be returned</returns>
public static bool IsActiveFriend(string accid)
  • Example
csharpvar ret = NIM.Friend.FriendAPI.IsActiveFriend("USER ID");
UnityEngine.Debug.Log(string.Format("{0} is a friend ? :{1}", "USER ID",ret));

Update friendship

Currently supports updating friend's remark

  • API prototype
csharp/// <summary>
/// Update friend information
/// </summary>
/// <param name="profile"></param>
/// <param name="cb"></param>
public static void UpdateFriendInfo(NIMFriendProfile profile, FriendOperationDelegate cb)
  • Example
csharpvar profile = new NIMFriendProfile();
profile.Alias = "NEW ALIAS";
NIM.Friend.FriendAPI.UpdateFriendInfo(profile, OnUpdateFriendProfile);

Blacklist

When a user is added to the blacklist, you will not receive any message or request from this user. For example, if user A blacklists user B, A will not receive any message from B. But B can read any message sent from A.

Add to/Remove from the blacklist

  • API prototype
csharp/// <summary>
/// Set and cancel blacklist
/// </summary>
/// <param name="accountId"> Friend id.</param>
/// <param name="inBlacklist">if set to <c>true</c> [set_black].</param>
/// <param name="cb">Callback for operation results</param>
public static void SetBlacklist(string accountId, bool inBlacklist, UserOperationDelegate cb)
  • Parameter Description
Parameter Description
accountId Operation acceptor ID
inBlacklist "True" is to add a user into blacklist; "false" is to remove a user from blacklist.
cb Operation result callback
  • Example
csharpNIM.User.UserAPI.SetBlacklist("USER ID", true, OnSetBlacklist);

private void OnSetBlacklist(ResponseCode response, string accid, bool opt, string jsonExtension, IntPtr userData)
{
    UnityEngine.Debug.Log(string.Format("Add {0} to blacklist:{1}", accid, response));
}

Monitor blacklist changes

The event NIM.User.UserAPI.UserRelationshipChangedHandler is registered to monitor blacklist change.

  • Event description
csharppublic static EventHandler<UserRelationshipChangedArgs> UserRelationshipChangedHandler;
  • Parameter Description

The event parameter UserRelationshipChangedArgs includes information as follows:

Property Description
AccountId User ID
IsSetted Configure or cancel configuration
ChangedType The type is NIMUserRelationshipChangeType, including "add/delete blacklist", "cancel message tip", and "information synchronization".
  • Example
csharpNIM.User.UserAPI.UserRelationshipChangedHandler += OnUserRelationshipChanged;

private void OnUserRelationshipChanged(object sender, UserRelationshipChangedArgs e)
{
    if(e.ChangedType == NIMUserRelationshipChangeType.AddRemoveBlacklist)
    {
        if(e.IsSetted)
        {
            //The account is added to blacklist.
        }
        else
        {
            //The account is removed from blacklist.
        }
    }
}

Get blacklist

  • API prototype
csharp/// <summary>
/// Get the list of user relationships (blacklist and mute list)
/// </summary>
/// <param name="resultDelegate"></param>
public static void GetRelationshipList(GetUserRelationshipResuleDelegate resultDelegate)
  • Example
csharpNIM.User.UserAPI.GetRelationshipList(OnGetRelationshipCompleted);

private void OnGetRelationshipCompleted(ResponseCode code, UserSpecialRelationshipItem[] list)
{
    //Get blacklist
    var blacklist = from c in list where c.IsBlacklist select c;
    //Get mute list
    var mutedlist = from c in list where c.IsMuted select c;
}

Message reminder

CommsEase supports to set or cancel message tip (mute) for a user. After the message tip is canceled, if a message is received from the user, there is not message tip in the notification bar. The configuration of message tip for a user supports roaming. The operation of message tip is similar to blacklist.

Set/mute message reminder

  • API prototype
csharp/// <summary>
///Set /cancel message tip
/// </summary>
/// <param name="accountId">Friend id</param>
/// <param name="isMuted">Cancel or configure</param>
/// <param name="cb">Callback for operation results</param>
public static void SetUserMuted(string accountId, bool isMuted, UserOperationDelegate cb)
  • Parameter Description
Parameter Description
accountId Operation acceptor ID
isMuted "True" is to set message tip, "false" is to cancel message tip of the user.
cb Operation result callback
  • Example
csharpNIM.User.UserAPI.SetUserMuted("USER ID", true, OnSetMuted);

private void OnSetMuted(ResponseCode response, string accid, bool opt, string jsonExtension, IntPtr userData)
{
    UnityEngine.Debug.Log(string.Format("Mute {0} {1}", accid, response));
}

Monitor messages reminder changes

The event NIM.User.UserAPI.UserRelationshipChangedHandler is registered to monitor change in message tip for a user.

  • Event description
csharppublic static EventHandler<UserRelationshipChangedArgs> UserRelationshipChangedHandler;
  • Parameter Description

The event parameter UserRelationshipChangedArgs includes information as follows:

Property Description
AccountId User ID
IsSetted Configure or cancel configuration
ChangedType The type is NIMUserRelationshipChangeType, including "add/delete blacklist", "cancel message tip", and "information synchronization".
  • Example
csharpNIM.User.UserAPI.UserRelationshipChangedHandler += OnUserRelationshipChanged;

private void OnUserRelationshipChanged(object sender, UserRelationshipChangedArgs e)
{
    if(e.ChangedType == NIMUserRelationshipChangeType.AddRemoveMute)
    {
        if(e.IsSetted)
        {
            //Set message tip for a user
        }
        else
        {
            //Cancel message tip for a user
        }
    }
}

Get muted account list

  • API prototype
csharp/// <summary>
/// Get the list of user relationships (blacklist and mute list)
/// </summary>
/// <param name="resultDelegate"></param>
public static void GetRelationshipList(GetUserRelationshipResuleDelegate resultDelegate)
  • Example
csharpNIM.User.UserAPI.GetRelationshipList(OnGetRelationshipCompleted);

private void OnGetRelationshipCompleted(ResponseCode code, UserSpecialRelationshipItem[] list)
{
    //Get blacklist
    var blacklist = from c in list where c.IsBlacklist select c;
    //Get mute list
    var mutedlist = from c in list where c.IsMuted select c;
}
Was this page helpful?
Yes
No
  • Friendship
  • Information of friends
  • Get friend list
  • Add friends
  • Accept/reject friend request of the other party
  • Delete friend
  • Notification of friendship change
  • Get friendship according to user account
  • It determines that a user is my friend.
  • Update friendship
  • Blacklist
  • Add to/Remove from the blacklist
  • Monitor blacklist changes
  • Get blacklist
  • Message reminder
  • Set/mute message reminder
  • Monitor messages reminder changes
  • Get muted account list