好友关系托管
更新时间: 2022/03/14 06:44:33
SDK 提供了用户好友关系管理,以及对用户会话的消息设置。在网易云信中,不是好友也允许聊天。好友关系如果不托管给网易云信,开发者需要自己在应用服务器维护。
添加/被添加,请求/被请求,删除/被删除的通知以及多端同步等通知通过注册的好友数据变更通知回调告知APP:
例:
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()
{
//向SDK注册监听好友列表变化
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
Cvoid CallbackFriendChange(NIMFriendChangeType type, const char *result_json, const char *json_extension, const void *user_data)
{
switch (type)
{
case kNIMFriendChangeTypeRequest:
// 解析result_json
break;
case kNIMFriendChangeTypeDel:
// 解析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);
}
好友关系
- 获取好友列表
例:
C++
C++void OnGetFriendList(nim::NIMResCode res_code, const std::list<nim::FriendProfile>& user_profile_list)
{
}
void foo()
{
//向SDK注册监听好友列表变化
nim::Friend::GetList(&OnGetFriendList);
}
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
Cvoid CallbackGetFriendsList(int res_code, const char *result_json, const char *json_extension, const void *user_data)
{
// 解析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);
}
-
好友请求
好友请求包括请求添加好友以及同意/拒绝好友请求两种。
好友验证包括直接添加为好友(kNIMVerifyTypeAdd),请求添加好友(kNIMVerifyTypeAsk),并且可以携带客户端自定义消息(msg)完成附加功能,例如添加好友附言功能。
回调函数告知接口调用是否成功,以不需要验证方式的好友请求为例:
C++
C++void OnFriendRequest(int res_code)
{
if (res_code == kNIMResSuccess)
...
else
...
}
void foo()
{
nim::Friend::Request("id", nim::kNIMVerifyTypeAdd, "", &OnFriendRequest);
}
C#
C#NIM.Friend.FriendAPI.ProcessFriendRequest("id", NIM.Friend.NIMVerifyType.kNIMVerifyTypeAdd, "加我加我",
(resCode, json, userdata) =>
{
if (resCode != 200)
{
}
});
C
Cvoid 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);
}
如果好友验证方式为需要验证(kNIMVerifyTypeAsk),对方收到消息之后,可以选择同意(kNIMVerifyTypeAgree)或者拒绝好友请求(kNIMVerifyTypeReject),此时同样调用nim_friend_request 接口,传入拒绝对象的ID和验证回复类型即可。
- 删除好友
C++
C++void OnDeleteFriend(int res_code)
{
if (res_code == kNIMResSuccess)
...
else
...
}
void foo()
{
nim::Friend::Delete("id", &OnDeleteFriend);
}
C#
C#NIM.Friend.FriendAPI.DeleteFriend(id, (res_code, json, userdata) =>
{
if (res_code != 200)
{
MessageBox.Show("删除失败");
}
});
C
Cvoid 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);
}
- 更新资料
C++
C++void foo()
{
nim::FriendProfile profile;
profile.SetAccId(m_uinfo.GetAccId());
profile.SetAlias(alias_edit->GetUTF8Text());
nim::Friend::Update(profile, nullptr);
}
C#
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("修改成功");
}
else
{
MessageBox.Show("修改失败");
}
});
C
Cvoid 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);
}
- 查询是否为好友
在本地缓存数据中查询accid是否为自己的好友,注意的是该接口为同步接口,会堵塞SDK线程,谨慎使用。
例子:
C++
C++#include "nim_cpp_friend.h"
bool IsFriend(const std::string& accid)
{
return nim::Friend::QueryFriendshipBlock(accid);
}
C#
C#bool IsFriend(string id)
{
return NIM.Friend.FriendAPI.IsActiveFriend(id);
}
C
C#include "nim_friend.h"
bool IsFriend(const std::string& accid)
{
return nim_friend_query_friendship_block(accid.c_str(), nullptr);
}
黑名单
网易云信中,黑名单和用户关系是互相独立的,即修改用户关系不会影响黑名单关系,同时,修改黑名单也不会对用户关系进行操作。
黑名单列表有本地缓存,缓存会在手动/自动登录后与服务器自动进行同步更新。通过注册用户关系变更通知回调获取当前数据变化:
例:
C++
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::kNIMUserSpecialRelationshipChangeTypeSyncMuteAndBlackList:
{
break;
}
}
}
void foo()
{
nim::User::RegSpecialRelationshipChangedCb(&OnMuteBlackEventCallback);
}
C#
C#void OnUserRelationshipSync(object sender, UserRelationshipSyncArgs e)
{
if (e.Items == null)
return;
}
void OnUserRelationshipChanged(object sender, UserRelationshipChangedArgs e)
{
if (e.ChangedType == NIMUserRelationshipChangeType.AddRemoveBlacklist)
{
}
}
void foo()
{
NIM.User.UserAPI.UserRelationshipListSyncHander += OnUserRelationshipSync;
NIM.User.UserAPI.UserRelationshipChangedHandler += OnUserRelationshipChanged;
}
C
Cvoid CallbackUserRelationshipChanged(NIMUserSpecialRelationshipChangeType type, const char *result_json ,const char *json_extension, const void *user_data)
{
switch (type)
{
case kNIMUserSpecialRelationshipChangeTypeMarkBlack:
//解析result_json
break;
case kNIMUserSpecialRelationshipChangeTypeMarkMute:
//解析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);
}
- 加入/移除黑名单
C++
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#
C#void foo()
{
NIM.User.UserAPI.SetBlacklist("id", true, (response, accid, opt, jsonExtension, userData) =>
{
});
}
C
Cvoid 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);
}
- 获取黑名单
C++
C++void OnGetBlackListCallback(nim::NIMResCode res_code, const std::list<nim::BlackListInfo>& lists)
{
if (res_code == nim::kNIMResSuccess)
{
for (auto& info : lists)
{
}
}
}
void foo()
{
nim::User::GetBlacklist(&OnGetBlackListCallback);
}
C#
C#void GetUserRelationCompleted(ResponseCode code, UserSpecialRelationshipItem[] list)
{
}
void foo()
{
NIM.User.UserAPI.GetRelationshipList(GetUserRelationCompleted);
}
C
Cvoid CallbackGetBlackList(int res_code, const char *mute_black_list_json, const char *json_extension, const void *callback)
{
//解析mute_black_list_json
}
typedef void (*nim_user_get_mute_blacklist)(const char *json_extension, nim_user_sync_muteandblacklist_cb_func cb, const void *user_data);
void foo()
{
nim_user_get_mute_blacklist func = (nim_user_get_mute_blacklist) GetProcAddress(hInst, "nim_user_get_mute_blacklist");
func(nullptr, &CallbackGetBlackList, nullptr);
}
消息提醒
网易云信中,可以单独设置是否开启某个用户的消息提醒,即对某个用户静音。静音关系和用户关系是互相独立的,修改用户关系不会影响静音关系,同时,修改静音关系也不会对用户关系进行操作。
静音名单列表有本地缓存,缓存会在手动/自动登录后与服务器自动进行同步更新。通过注册用户关系变更通知回调获取当前数据变化:
C++
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::kNIMUserSpecialRelationshipChangeTypeSyncMuteAndBlackList:
{
break;
}
}
}
void foo()
{
nim::User::RegSpecialRelationshipChangedCb(&OnMuteBlackEventCallback);
}
C#
C#void OnUserRelationshipSync(object sender, UserRelationshipSyncArgs e)
{
if (e.Items == null)
return;
}
void OnUserRelationshipChanged(object sender, UserRelationshipChangedArgs e)
{
if (e.ChangedType == NIMUserRelationshipChangeType.AddRemoveBlacklist)
{
}
}
void foo()
{
NIM.User.UserAPI.UserRelationshipListSyncHander += OnUserRelationshipSync;
NIM.User.UserAPI.UserRelationshipChangedHandler += OnUserRelationshipChanged;
}
C
Cvoid CallbackUserRelationshipChanged(NIMUserSpecialRelationshipChangeType type, const char *result_json ,const char *json_extension, const void *user_data)
{
switch (type)
{
case kNIMUserSpecialRelationshipChangeTypeMarkBlack:
//解析result_json
break;
case kNIMUserSpecialRelationshipChangeTypeMarkMute:
//解析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);
}
- 加入/移除静音单列表
C++
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#
C#void foo()
{
NIM.User.UserAPI.SetUserMuted("id", true, (response, accid, opt, jsonExtension, userData) =>
{
});
}
C
Cvoid 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);
}
- 获取静音名单列表
C++
C++void OnGetMuteListCallback(nim::NIMResCode res_code, const std::list<nim::BlackListInfo>& lists)
{
if (res_code == nim::kNIMResSuccess)
{
for (auto& info : lists)
{
}
}
}
void foo()
{
nim::User::GetMutelist(&OnGetMuteListCallback);
}
C#
C#void GetUserRelationCompleted(ResponseCode code, UserSpecialRelationshipItem[] list)
{
}
void foo()
{
NIM.User.UserAPI.GetRelationshipList(GetUserRelationCompleted);
}
C
Cvoid CallbackGetMuteList(int res_code, const char *mute_black_list_json, const char *json_extension, const void *callback)
{
//解析mute_black_list_json
}
typedef void (*nim_user_get_mute_blacklist)(const char *json_extension, nim_user_sync_muteandblacklist_cb_func cb, const void *user_data);
void foo()
{
nim_user_get_mute_blacklist func = (nim_user_get_mute_blacklist) GetProcAddress(hInst, "nim_user_get_mute_blacklist");
func(nullptr, &CallbackGetMuteList, nullptr);
}