Host user relationship

Update time: 2021/12/03 09:36:57

Hosting user relationship data

The 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.

Notification of add/added, request/requested, delete/deleted, and multi-client synchronization can be sent to the app through callback of the registered friend data change notification:

For example:

C++

C++    void OnFriendListChange(const nim::FriendChangeEvent& change_event)
    {
    	switch (change_event.type_)
    	{
    	case nim::kNIMFriendChangeTypeDel:
    	{
    		break;
    	}
    	case nim::kNIMFriendChangeTypeRequest:
    	{
    		break;
    	}
    	case nim::kNIMFriendChangeTypeSyncList:
    	{
    		break;
    	}
    	case nim::kNIMFriendChangeTypeUpdate:
    	{
    		break;
    	}
    	default:
    		break;
    	}
    }
    
    void foo()
    {
    	// register with SDK to observe friend list change
    	nim::Friend::RegChangeCb(&OnFriendListChange);
    }

C#

C#    void OnFriendChanged(object sender, NIM.Friend.NIMFriendProfileChangedArgs args)
    {
        if (args.ChangedInfo == null)
            return;
    
        if (args.ChangedInfo.ChangedType == NIM.Friend.NIMFriendChangeType.kNIMFriendChangeTypeDel)
        {
                
        }
        if (args.ChangedInfo.ChangedType == NIM.Friend.NIMFriendChangeType.kNIMFriendChangeTypeRequest)
        {
                
        }
        if (args.ChangedInfo.ChangedType == NIM.Friend.NIMFriendChangeType.kNIMFriendChangeTypeSyncList)
        {
                
        }
        if (args.ChangedInfo.ChangedType == NIM.Friend.NIMFriendChangeType.kNIMFriendChangeTypeUpdate)
        {
                
        }
    }
    
    void foo()
    {
    	NIM.Friend.FriendAPI.FriendProfileChangedHandler += OnFriendChanged;
    }

C
```C
    void CallbackFriendChange(NIMFriendChangeType type, const char *result_json, const char *json_extension, const void *user_data)
    {
    	switch (type)
    	{
    		case kNIMFriendChangeTypeRequest:
    			// parse result_json
    			break;
    		case kNIMFriendChangeTypeDel:
    			// parse result_json
    			break;
    		...
    }
    
    typedef void(*nim_friend_reg_changed_cb)(const char *json_extension, nim_friend_change_cb_func cb, const void *user_data);
    
    void foo()
    {
    	nim_friend_reg_changed_cb func = (nim_friend_reg_changed_cb) GetProcAddress(hInst, "nim_friend_reg_changed_cb");
    	func("", &CallbackFriendChange, nullptr);
    }

Friends

  • Get a friend list

    For example:

    C++

      void OnGetFriendList(nim::NIMResCode res_code, const std::list<nim::FriendProfile>& user_profile_list)
      {
    
      }
    
      void foo()
      {
      	// register with SDK to observe friend list change
      	nim::Friend::GetList(&OnGetFriendList);
      }
    

    C#

      void OnFriendChanged(object sender, NIM.Friend.NIMFriendProfileChangedArgs args)
      {
          if (args.ChangedInfo == null)
              return;
    
          if (args.ChangedInfo.ChangedType == NIM.Friend.NIMFriendChangeType.kNIMFriendChangeTypeDel)
          {
    
          }
          if (args.ChangedInfo.ChangedType == NIM.Friend.NIMFriendChangeType.kNIMFriendChangeTypeRequest)
          {
    
          }
          if (args.ChangedInfo.ChangedType == NIM.Friend.NIMFriendChangeType.kNIMFriendChangeTypeSyncList)
          {
    
          }
          if (args.ChangedInfo.ChangedType == NIM.Friend.NIMFriendChangeType.kNIMFriendChangeTypeUpdate)
          {
    
          }
      }
    
      void foo()
      {
      	NIM.Friend.FriendAPI.FriendProfileChangedHandler += OnFriendChanged;
      }
    

    C

      void CallbackGetFriendsList(int res_code, const char *result_json, const char *json_extension, const void *user_data)
      {
      	// parse result_json
      }
    
      typedef void(*nim_friend_get_list)(const char *json_extension, nim_friend_get_list_cb_func cb, const void *user_data);
    
      void foo()
      {
      	nim_friend_get_list func = (nim_friend_get_list) GetProcAddress(hInst, "nim_friend_get_list");
      	func("", &CallbackGetFriendsList, nullptr);
      }
    
  • Friend request

    There are two types, namely friend request and accept/reject friend request.

    Friend verification covers add as friends (kNIMVerifyTypeAdd) and request to add friends (kNIMVerifyTypeAsk), and it can carry client-side custom messages (msg) to perform additional features such as add friend postscript.

    Use callback feature to get notified about the result of interface call. Take a friend request that does not require verification as an example:

    C++

      void OnFriendRequest(int res_code)
      {
      	if (res_code == kNIMResSuccess)
      		...
      	else
      		...	
      }
    
      void foo()
      {
      	nim::Friend::Request("id", nim::kNIMVerifyTypeAdd, "", &OnFriendRequest);
      }
    

    C#

      NIM.Friend.FriendAPI.ProcessFriendRequest("id", NIM.Friend.NIMVerifyType.kNIMVerifyTypeAdd, "add",
      (resCode, json, userdata) =>
      {
          if (resCode != 200)
          {
    
          }
      });
    

    C

      void CallbackFriendOpt(int res_code, const char *json_extension, const void *user_data)
      {
      	if (res_code == kNIMResSuccess)
      		...
      	else
      		...	
      }
    
      typedef void(*nim_friend_request)(const char *accid, NIMVerifyType verify_type, const char *msg, const char *json_extension, nim_friend_opt_cb_func cb, const void *user_data);
    
      void foo()
      {
      	nim_friend_request func = (nim_friend_request) GetProcAddress(hInst, "nim_friend_request");
      	func("id", kNIMVerifyTypeAdd, nullptr, nullptr, &CallbackFriendOpt, nullptr);
      }
    

    If the request requires verification (kNIMVerifyTypeAsk), after receiving the message, the other party can choose to accept (kNIMVerifyTypeAgree) or reject friend request (kNIMVerifyTypeReject). Similarly, at this time, the nim_friend_request interface can be also called to input the ID of the user rejected and the verification response type.

  • Delete a friend.

    C++

      void OnDeleteFriend(int res_code)
      {
      	if (res_code == kNIMResSuccess)
      		...
      	else
      		...	
      }
    
      void foo()
      {
      	nim::Friend::Delete("id", &OnDeleteFriend);
      }
    

    C#

      NIM.Friend.FriendAPI.DeleteFriend(id, (res_code, json, userdata) =>
      {
          if (res_code != 200)
          {
              MessageBox.Show("deleted unsuccessfully");
          }
      });
    

    C

      void CallbackFriendOpt(int res_code, const char *json_extension, const void *user_data)
      {
      	if (res_code == kNIMResSuccess)
      		...
      	else
      		...	
      }
    
      typedef void(*nim_friend_delete)(const char *accid, const char *json_extension, nim_friend_opt_cb_func cb, const void *user_data);
    
      void foo()
      {
      	nim_friend_delete func = (nim_friend_delete) GetProcAddress(hInst, "nim_friend_delete");
      	func("id", nullptr, &CallbackFriendOpt, nullptr);
      }
    
  • Update the friend profile

    C++

      void foo()
      {
      	nim::FriendProfile profile;
      	profile.SetAccId(m_uinfo.GetAccId());
      	profile.SetAlias(alias_edit->GetUTF8Text());
      	nim::Friend::Update(profile, nullptr);
      }
    

    C#

      NIM.Friend.NIMFriendProfile profile = new NIM.Friend.NIMFriendProfile;
      profile.Alias = "alias_name";
      profile.AccountId = "user_id";
      NIM.Friend.FriendAPI.UpdateFriendInfo(profile, (res_code, b, cc) =>
      {
          if (res_code == 200)
          {
              MessageBox.Show("updated successfully");
          }
          else
          {
              MessageBox.Show("updated unsuccessfully");
          }
      });
    

    C

      void CallbackFriendOpt(int res_code, const char *json_extension, const void *user_data)
      {
      	if (res_code == kNIMResSuccess)
      		...
      	else
      		...	
      }
    
      typedef void(*nim_friend_update)(const char *friend_json, const char *json_extension, nim_friend_opt_cb_func cb, const void *user_data);
    
      void foo()
      {
      	nim_friend_update func = (nim_friend_update) GetProcAddress(hInst, "nim_friend_update");
    
      	Json::Value friend_profile_json;
      	friend_profile_json[kNIMFriendKeyAccid] = "accid";
      	friend_profile_json[kNIMFriendKeyAlias] = "alias_name";
      	friend_profile_json[kNIMFriendKeyCreateTime] = create_timetag_;
      	friend_profile_json[kNIMFriendKeyUpdateTime] = update_timetag_;
      	Json::FastWriter fw;
    
      	func(fw.write(friend_profile_json).c_str(), nullptr, &CallbackFriendOpt, nullptr);
      }
    
  • Query user relationship

    Determine whether the user of the accid is your friend in the local cache data. Note that this is a synchronous interface that can block SDK thread, so use it with caution.

    Example:

    C++

      #include "nim_cpp_friend.h"
    
      bool IsFriend(const std::string& accid)
      {
      	return nim::Friend::QueryFriendshipBlock(accid);
      }
    

    C#

      bool IsFriend(string id)
      {
      	return NIM.Friend.FriendAPI.IsActiveFriend(id);
      }
    

    C

      #include "nim_friend.h"
    
      bool IsFriend(const std::string& accid)
      {
      	return nim_friend_query_friendship_block(accid.c_str(), nullptr);
      }
    

Blocklist

In CommsEase, blocklist and user relationship are independent of each other, so updating either of the two won't affect the other.

Local cache is available for blocklist, and will be synced and updated automatically with the server after manual/auto login. Get current data changes by callback of registered user relationship change notification:

For example:

C++

void OnMuteBlackEventCallback(const nim::SpecialRelationshipChangeEvent& change_event)
{
	switch (change_event.type_)
	{
	case nim::NIMUserSpecialRelationshipChangeType::kNIMUserSpecialRelationshipChangeTypeMarkBlack:
	{
		break;
	}
	case nim::NIMUserSpecialRelationshipChangeType::kNIMUserSpecialRelationshipChangeTypeMarkMute:
	{
		break;
	}
	case nim::NIMUserSpecialRelationshipChangeType::kNIMUserSpecialRelationshipChangeTypeSyncMuteAndBlocklist:
	{
		break;
	}
	}
}

void foo()
{
	nim::User::RegSpecialRelationshipChangedCb(&OnMuteBlackEventCallback);
}

C#

void OnUserRelationshipSync(object sender, UserRelationshipSyncArgs e)
{
    if (e.Items == null)
        return;
}

void OnUserRelationshipChanged(object sender, UserRelationshipChangedArgs e)
{
    if (e.ChangedType == NIMUserRelationshipChangeType.AddRemoveBlocklist)
    {
    }
}

void foo()
{
	NIM.User.UserAPI.UserRelationshipListSyncHander += OnUserRelationshipSync;
	NIM.User.UserAPI.UserRelationshipChangedHandler += OnUserRelationshipChanged;
}

C

void CallbackUserRelationshipChanged(NIMUserSpecialRelationshipChangeType type, const char *result_json ,const char *json_extension, const void *user_data)
{
	switch (type)
	{
		case kNIMUserSpecialRelationshipChangeTypeMarkBlack:
			// parse result_json
			break;
		case kNIMUserSpecialRelationshipChangeTypeMarkMute:
			// parse result_json
			break;
		...
	}
}

typedef	void (*nim_user_reg_special_relationship_changed_cb)(const char *json_extension, nim_user_special_relationship_change_cb_func cb, const void *user_data);

void foo()
{
	nim_user_reg_special_relationship_changed_cb func = (nim_user_reg_special_relationship_changed_cb) GetProcAddress(hInst, "nim_user_reg_special_relationship_changed_cb");
	func(nullptr, &CallbackUserRelationshipChanged, nullptr);
}
  • Add to/remove from blocklist

    C++

      void OnSetBlackCb(int res_code, const std::string& accid, bool opt)
      {
      	if (res_code != 200)
      		···
      }
    
      void foo()
      {
      	nim::User::SetBlack("accid", true, &OnSetBlackCb);
      }
    

    C#

      void foo()
      {
      	NIM.User.UserAPI.SetBlocklist("id", true, (response, accid, opt, jsonExtension, userData) =>
      	{
    
      	});
      }
    

    C

      void CallbackUserOpt(int res_code, const char *accid, bool opt, const char *json_extension, const void *user_data)
      {
      	if (res_code == kNIMResSuccess)
          	...
      	else
          	... 
      }
    
      typedef	void (*nim_user_set_black)(const char *accid, bool set_black, const char *json_extension, nim_user_opt_cb_func cb, const void *user_data);
    
      void foo()
      {
      	nim_user_set_black func = (nim_user_set_black) GetProcAddress(hInst, "nim_user_set_black");
      	func("id", true, nullptr, &CallbackUserOpt, nullptr);
      }
    
  • Get blocklist

    C++

      void OnGetBlocklistCallback(nim::NIMResCode res_code, const std::list<nim::BlocklistInfo>& lists)
      {
      	if (res_code == nim::kNIMResSuccess)
      	{
      		for (auto& info : lists)
      		{
    
      		}
      	}
      }
    
      void foo()
      {
      	nim::User::GetBlocklist(&OnGetBlocklistCallback);
      }
    

    C#

      void GetUserRelationCompleted(ResponseCode code, UserSpecialRelationshipItem[] list)
      {
    
      }
    
      void foo()
      {
      	NIM.User.UserAPI.GetRelationshipList(GetUserRelationCompleted);
      }
    

    C

      void CallbackGetBlocklist(int res_code, const char *mute_black_list_json, const char *json_extension, const void *callback)
      {
      	// parse mute_black_list_json
      }
    
      typedef	void (*nim_user_get_mute_Blocklist)(const char *json_extension, nim_user_sync_muteandBlocklist_cb_func cb, const void *user_data);
    
      void foo()
      {
      	nim_user_get_mute_blocklist func = (nim_user_get_mute_Blocklist) GetProcAddress(hInst, "nim_user_get_mute_Blocklist");
      	func(nullptr, &CallbackGetBlocklist, nullptr);
      }
    

Message alert

CommsEase contains setting option on whether to enable alert for individual user's message (i.e. mute or unmute the user). Mute and user relationship are mutually independent, so updating either of the two won't affect the other.

Local cache is available for mute list, and will be synced and updated automatically with the server after manual/auto login. Get current data changes by callback of registered user relationship change notification:

C++

void OnMuteBlackEventCallback(const nim::SpecialRelationshipChangeEvent& change_event)
{
	switch (change_event.type_)
	{
	case nim::NIMUserSpecialRelationshipChangeType::kNIMUserSpecialRelationshipChangeTypeMarkBlack:
	{
		break;
	}
	case nim::NIMUserSpecialRelationshipChangeType::kNIMUserSpecialRelationshipChangeTypeMarkMute:
	{
		break;
	}
	case nim::NIMUserSpecialRelationshipChangeType::kNIMUserSpecialRelationshipChangeTypeSyncMuteAndBlocklist:
	{
		break;
	}
	}
}

void foo()
{
	nim::User::RegSpecialRelationshipChangedCb(&OnMuteBlackEventCallback);
}

C#

void OnUserRelationshipSync(object sender, UserRelationshipSyncArgs e)
{
    if (e.Items == null)
        return;
}

void OnUserRelationshipChanged(object sender, UserRelationshipChangedArgs e)
{
    if (e.ChangedType == NIMUserRelationshipChangeType.AddRemoveBlocklist)
    {
    }
}

void foo()
{
	NIM.User.UserAPI.UserRelationshipListSyncHander += OnUserRelationshipSync;
	NIM.User.UserAPI.UserRelationshipChangedHandler += OnUserRelationshipChanged;
}

C

void CallbackUserRelationshipChanged(NIMUserSpecialRelationshipChangeType type, const char *result_json ,const char *json_extension, const void *user_data)
{
	switch (type)
	{
		case kNIMUserSpecialRelationshipChangeTypeMarkBlack:
			// parse result_json
			break;
		case kNIMUserSpecialRelationshipChangeTypeMarkMute:
			// parse result_json
			break;
		...
	}
}

typedef	void (*nim_user_reg_special_relationship_changed_cb)(const char *json_extension, nim_user_special_relationship_change_cb_func cb, const void *user_data);

void foo()
{
	nim_user_reg_special_relationship_changed_cb func = (nim_user_reg_special_relationship_changed_cb) GetProcAddress(hInst, "nim_user_reg_special_relationship_changed_cb");
	func(nullptr, &CallbackUserRelationshipChanged, nullptr);
}
  • Add to/remove from mute list

    C++

      void OnSetMuteCb(int res_code, const std::string& accid, bool opt)
      {
      	if (res_code != 200)
      		···
      }
    
      void foo()
      {
      	nim::User::SetMute("accid", true, &OnSetMuteCb);
      }
    

    C#

      void foo()
      {
      	NIM.User.UserAPI.SetUserMuted("id", true, (response, accid, opt, jsonExtension, userData) =>
      	{
    
      	});
      }
    

    C

      void CallbackSetRelation(int res_code, const char *accid, bool opt, const char *json_extension, const void *user_data)
      {
      	if (res_code == kNIMResSuccess)
          	...
      	else
          	... 
      }
    
      typedef	void (*nim_user_set_mute)(const char *accid, bool set_mute, const char *json_extension, nim_user_opt_cb_func cb, const void *user_data);
    
      void foo()
      {
      	nim_user_set_mute func = (nim_user_set_mute) GetProcAddress(hInst, "nim_user_set_mute");
      	func("id", true, nullptr, &CallbackSetRelation, nullptr);
      }
    
  • Get mute list

    C++

      void OnGetMuteListCallback(nim::NIMResCode res_code, const std::list<nim::BlocklistInfo>& lists)
      {
      	if (res_code == nim::kNIMResSuccess)
      	{
      		for (auto& info : lists)
      		{
    
      		}
      	}
      }
    
      void foo()
      {
      	nim::User::GetMutelist(&OnGetMuteListCallback);
      }
    

    C#

      void GetUserRelationCompleted(ResponseCode code, UserSpecialRelationshipItem[] list)
      {
    
      }
    
      void foo()
      {
      	NIM.User.UserAPI.GetRelationshipList(GetUserRelationCompleted);
      }
    

    C

      void CallbackGetMuteList(int res_code, const char *mute_black_list_json, const char *json_extension, const void *callback)
      {
      	// parse mute_black_list_json
      }
    
      typedef	void (*nim_user_get_mute_Blocklist)(const char *json_extension, nim_user_sync_muteandBlocklist_cb_func cb, const void *user_data);
    
      void foo()
      {
      	nim_user_get_mute_blocklist func = (nim_user_get_mute_Blocklist) GetProcAddress(hInst, "nim_user_get_mute_Blocklist");
      	func(nullptr, &CallbackGetMuteList, nullptr);
      }
    
Was this page helpful?
Yes
No
  • Hosting user relationship data
  • Friends
  • Blocklist
  • Message alert