✨ 初始化项目
This commit is contained in:
901
sns/api.go
Normal file
901
sns/api.go
Normal file
@@ -0,0 +1,901 @@
|
||||
/**
|
||||
* @Author: Echo
|
||||
* @Author:1711788888@qq.com
|
||||
* @Date: 2021/5/27 20:45
|
||||
* @Desc: 关系链管理
|
||||
*/
|
||||
|
||||
package sns
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.echol.cn/loser/tencent-im/internal/core"
|
||||
"git.echol.cn/loser/tencent-im/internal/enum"
|
||||
"git.echol.cn/loser/tencent-im/internal/types"
|
||||
)
|
||||
|
||||
const (
|
||||
service = "sns"
|
||||
commandAddFriend = "friend_add"
|
||||
commandImportFriend = "friend_import"
|
||||
commandUpdateFriend = "friend_update"
|
||||
commandDeleteFriend = "friend_delete"
|
||||
commandDeleteAllFriend = "friend_delete_all"
|
||||
commandCheckFriend = "friend_check"
|
||||
commandGetFriend = "friend_get_list"
|
||||
commandFetchFriend = "friend_get"
|
||||
commandAddBlackList = "black_list_add"
|
||||
commandDeleteBlackList = "black_list_delete"
|
||||
commandGetBlackList = "black_list_get"
|
||||
commandCheckBlackList = "black_list_check"
|
||||
commandAddGroup = "group_add"
|
||||
commandDeleteGroup = "group_delete"
|
||||
commandGetGroup = "group_get"
|
||||
|
||||
batchCheckFriendsLimit = 100 // 批量校验好友限制
|
||||
batchGetFriendsLimit = 100 // 批量获取好友限制
|
||||
batchAddBlacklistLimit = 1000 // 批量添加黑名单限制
|
||||
batchDeleteBlacklistLimit = 1000 // 批量删除黑名单限制
|
||||
batchCheckBlacklistLimit = 1000 // 批量校验黑名单限制
|
||||
batchAddGroupsLimit = 100 // 批量添加分组限制
|
||||
batchJoinGroupsLimit = 1000 // 批量加入群组账号限制
|
||||
batchDeleteGroupsLimit = 100 // 批量删除分组限制
|
||||
batchGetGroupsLimit = 100 // 批量获取分组限制
|
||||
)
|
||||
|
||||
type API interface {
|
||||
// AddFriend 添加单个好友
|
||||
// 本方法拓展于“添加多个好友(AddFriends)”方法。
|
||||
// 添加好友,仅支持添加单个好友
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/1643
|
||||
AddFriend(userId string, isBothAdd, isForceAdd bool, friend *Friend) (err error)
|
||||
|
||||
// AddFriends 添加多个好友
|
||||
// 添加好友,支持批量添加好友
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/1643
|
||||
AddFriends(userId string, isBothAdd, isForceAdd bool, friends ...*Friend) (results []*Result, err error)
|
||||
|
||||
// ImportFriend 导入单个好友
|
||||
// 本方法拓展于“添加多个好友(ImportFriends)”方法。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/8301
|
||||
ImportFriend(userId string, friend *Friend) (err error)
|
||||
|
||||
// ImportFriends 导入多个好友
|
||||
// 支持批量导入单向好友。
|
||||
// 往同一个用户导入好友时建议采用批量导入的方式,避免并发写导致的写冲突。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/8301
|
||||
ImportFriends(userId string, friends ...*Friend) (results []*Result, err error)
|
||||
|
||||
// UpdateFriend 更新单个好友
|
||||
// 本方法拓展于“更新多个好友(UpdateFriends)”方法。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/12525
|
||||
UpdateFriend(userId string, friend *Friend) (err error)
|
||||
|
||||
// UpdateFriends 更新多个好友
|
||||
// 支持批量更新同一用户的多个好友的关系链数据。
|
||||
// 更新一个用户多个好友时,建议采用批量方式,避免并发写导致的写冲突。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/12525
|
||||
UpdateFriends(userId string, friends ...*Friend) (results []*Result, err error)
|
||||
|
||||
// DeleteFriend 删除单个好友
|
||||
// 本方法拓展于“删除多个好友(DeleteFriends)”方法。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/1644
|
||||
DeleteFriend(userId string, isBothDelete bool, deletedUserId string) (err error)
|
||||
|
||||
// DeleteFriends 删除多个好友
|
||||
// 删除好友,支持单向删除好友和双向删除好友。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/1644
|
||||
DeleteFriends(userId string, isBothDelete bool, deletedUserIds ...string) (results []*Result, err error)
|
||||
|
||||
// DeleteAllFriends 删除所有好友
|
||||
// 清除指定用户的标配好友数据和自定义好友数据。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/1645
|
||||
DeleteAllFriends(userId string, deleteType ...DeleteType) (err error)
|
||||
|
||||
// CheckFriend 校验单个好友
|
||||
// 本方法拓展于“校验多个好友(CheckFriends)”方法。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/1646
|
||||
CheckFriend(userId string, checkType CheckType, checkedUserId string) (relation string, err error)
|
||||
|
||||
// CheckFriends 校验多个好友
|
||||
// 支持批量校验好友关系。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/1646
|
||||
CheckFriends(userId string, checkType CheckType, checkedUserIds ...string) (results []*CheckResult, err error)
|
||||
|
||||
// GetFriend 拉取单个指定好友
|
||||
// 本方法拓展于“拉取多个指定好友(GetFriends)”方法。
|
||||
// 支持拉取指定好友的好友数据和资料数据。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/8609
|
||||
GetFriend(userId string, tagList []string, friendUserId string) (friend *Friend, err error)
|
||||
|
||||
// GetFriends 拉取多个指定好友
|
||||
// 支持拉取指定好友的好友数据和资料数据。
|
||||
// 建议每次拉取的好友数不超过100,避免因数据量太大导致回包失败。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/8609
|
||||
GetFriends(userId string, tagList []string, friendUserIds ...string) (friends []*Friend, err error)
|
||||
|
||||
// FetchFriends 拉取好友
|
||||
// 分页拉取全量好友数据。
|
||||
// 不支持资料数据的拉取。
|
||||
// 不需要指定请求拉取的字段,默认返回全量的标配好友数据和自定义好友数据。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/1647
|
||||
FetchFriends(userId string, startIndex int, sequence ...int) (ret *FetchFriendsRet, err error)
|
||||
|
||||
// PullFriends 续拉取好友
|
||||
// 本API是借助"拉取好友"API进行扩展实现
|
||||
// 分页拉取全量好友数据。
|
||||
// 不支持资料数据的拉取。
|
||||
// 不需要指定请求拉取的字段,默认返回全量的标配好友数据和自定义好友数据。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/1647
|
||||
PullFriends(userId string, fn func(ret *FetchFriendsRet)) (err error)
|
||||
|
||||
// AddBlacklist 添加黑名单
|
||||
// 添加黑名单,支持批量添加黑名单。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/3718
|
||||
AddBlacklist(userId string, blackedUserIds ...string) (results []*Result, err error)
|
||||
|
||||
// DeleteBlacklist 删除黑名单
|
||||
// 删除指定黑名单。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/3719
|
||||
DeleteBlacklist(userId string, deletedUserIds ...string) (results []*Result, err error)
|
||||
|
||||
// FetchBlacklist 拉取黑名单
|
||||
// 支持分页拉取所有黑名单。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/3722
|
||||
FetchBlacklist(userId string, maxLimited int, startIndexAndSequence ...int) (ret *FetchBlacklistRet, err error)
|
||||
|
||||
// PullBlacklist 拉取黑名单
|
||||
// 本API是借助"拉取黑名单"API进行扩展实现
|
||||
// 支持分页拉取所有黑名单。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/3722
|
||||
PullBlacklist(userId string, maxLimited int, fn func(ret *FetchBlacklistRet)) (err error)
|
||||
|
||||
// CheckBlacklist 校验黑名单
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/3725
|
||||
CheckBlacklist(userId string, checkType BlacklistCheckType, checkedUserIds ...string) (results []*CheckResult, err error)
|
||||
|
||||
// AddGroups 添加分组
|
||||
// 添加分组,支持批量添加分组,并将指定好友加入到新增分组中。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/10107
|
||||
AddGroups(userId string, groupNames []string, joinedUserIds ...[]string) (currentSequence int, results []*Result, err error)
|
||||
|
||||
// DeleteGroups 删除分组
|
||||
// 删除指定分组。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/10108
|
||||
DeleteGroups(userId string, groupNames ...string) (currentSequence int, err error)
|
||||
|
||||
// GetGroups 拉取分组
|
||||
// 拉取分组,支持指定分组以及拉取分组下的好友列表。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/54763
|
||||
GetGroups(userId string, lastSequence int, isGetFriends bool, groupNames ...string) (currentSequence int, results []*GroupResult, err error)
|
||||
}
|
||||
|
||||
type api struct {
|
||||
client core.Client
|
||||
}
|
||||
|
||||
func NewAPI(client core.Client) API {
|
||||
return &api{client: client}
|
||||
}
|
||||
|
||||
// AddFriend 添加单个好友
|
||||
// 本方法拓展于“添加多个好友(AddFriends)”方法。
|
||||
// 添加好友,仅支持添加单个好友
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/1643
|
||||
func (a *api) AddFriend(userId string, isBothAdd, isForceAdd bool, friend *Friend) (err error) {
|
||||
var results []*Result
|
||||
|
||||
if results, err = a.AddFriends(userId, isBothAdd, isForceAdd, friend); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, result := range results {
|
||||
if result.UserId == friend.GetUserId() && result.ResultCode != enum.SuccessCode {
|
||||
return core.NewError(result.ResultCode, result.ResultInfo)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// AddFriends 添加多个好友
|
||||
// 添加好友,支持批量添加好友
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/1643
|
||||
func (a *api) AddFriends(userId string, isBothAdd, isForceAdd bool, friends ...*Friend) (results []*Result, err error) {
|
||||
if len(friends) == 0 {
|
||||
err = core.NewError(enum.InvalidParamsCode, "the friends is not set")
|
||||
return
|
||||
}
|
||||
|
||||
req := &addFriendsReq{UserId: userId, Friends: make([]*addFriendItem, 0, len(friends))}
|
||||
|
||||
for _, friend := range friends {
|
||||
if err = friend.checkError(); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
item := new(addFriendItem)
|
||||
item.UserId = friend.GetUserId()
|
||||
item.Remark, _ = friend.GetRemark()
|
||||
item.AddWording, _ = friend.GetAddWording()
|
||||
item.AddSource, _ = friend.GetSrcAddSource()
|
||||
if groups, exist := friend.GetGroup(); exist {
|
||||
item.GroupName = groups[0]
|
||||
}
|
||||
|
||||
req.Friends = append(req.Friends, item)
|
||||
}
|
||||
|
||||
if isBothAdd {
|
||||
req.AddType = AddTypeBoth
|
||||
} else {
|
||||
req.AddType = AddTypeSingle
|
||||
}
|
||||
|
||||
if isForceAdd {
|
||||
req.ForceAddFlags = ForceAddYes
|
||||
} else {
|
||||
req.ForceAddFlags = ForceAddNo
|
||||
}
|
||||
|
||||
resp := &addFriendsResp{}
|
||||
|
||||
if err = a.client.Post(service, commandAddFriend, req, resp); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
results = resp.Results
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// ImportFriend 导入单个好友
|
||||
// 本方法拓展于“添加多个好友(ImportFriends)”方法。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/8301
|
||||
func (a *api) ImportFriend(userId string, friend *Friend) (err error) {
|
||||
var results []*Result
|
||||
|
||||
if results, err = a.ImportFriends(userId, friend); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, result := range results {
|
||||
if result.UserId == friend.GetUserId() && result.ResultCode != enum.SuccessCode {
|
||||
return core.NewError(result.ResultCode, result.ResultInfo)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// ImportFriends 导入多个好友
|
||||
// 支持批量导入单向好友。
|
||||
// 往同一个用户导入好友时建议采用批量导入的方式,避免并发写导致的写冲突。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/8301
|
||||
func (a *api) ImportFriends(userId string, friends ...*Friend) (results []*Result, err error) {
|
||||
if len(friends) == 0 {
|
||||
err = core.NewError(enum.InvalidParamsCode, "the friends is not set")
|
||||
return
|
||||
}
|
||||
|
||||
req := &importFriendsReq{UserId: userId, Friends: make([]*importFriendItem, 0, len(friends))}
|
||||
|
||||
for _, friend := range friends {
|
||||
if err = friend.checkError(); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
item := new(importFriendItem)
|
||||
item.UserId = friend.GetUserId()
|
||||
item.Remark, _ = friend.GetRemark()
|
||||
item.AddWording, _ = friend.GetAddWording()
|
||||
item.AddTime, _ = friend.GetAddTime()
|
||||
item.RemarkTime, _ = friend.GetRemarkTime()
|
||||
item.AddSource, _ = friend.GetSrcAddSource()
|
||||
item.GroupName, _ = friend.GetGroup()
|
||||
|
||||
if customAttrs := friend.GetSNSCustomAttrs(); len(customAttrs) > 0 {
|
||||
item.CustomData = make([]*types.TagPair, 0, len(customAttrs))
|
||||
for k, v := range customAttrs {
|
||||
item.CustomData = append(item.CustomData, &types.TagPair{
|
||||
Tag: k,
|
||||
Value: v,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
req.Friends = append(req.Friends, item)
|
||||
}
|
||||
|
||||
resp := &importFriendsResp{}
|
||||
|
||||
if err = a.client.Post(service, commandImportFriend, req, resp); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
results = resp.Results
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateFriend 更新单个好友
|
||||
// 本方法拓展于“更新多个好友(UpdateFriends)”方法。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/12525
|
||||
func (a *api) UpdateFriend(userId string, friend *Friend) (err error) {
|
||||
var results []*Result
|
||||
|
||||
if results, err = a.UpdateFriends(userId, friend); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, result := range results {
|
||||
if result.UserId == friend.GetUserId() && result.ResultCode != enum.SuccessCode {
|
||||
return core.NewError(result.ResultCode, result.ResultInfo)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateFriends 更新多个好友
|
||||
// 支持批量更新同一用户的多个好友的关系链数据。
|
||||
// 更新一个用户多个好友时,建议采用批量方式,避免并发写导致的写冲突。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/12525
|
||||
func (a *api) UpdateFriends(userId string, friends ...*Friend) (results []*Result, err error) {
|
||||
if len(friends) == 0 {
|
||||
err = core.NewError(enum.InvalidParamsCode, "the friends is not set")
|
||||
return
|
||||
}
|
||||
|
||||
req := &updateFriendsReq{UserId: userId, Friends: make([]*updateFriendItem, 0, len(friends))}
|
||||
|
||||
for _, friend := range friends {
|
||||
item := new(updateFriendItem)
|
||||
item.UserId = friend.GetUserId()
|
||||
|
||||
for k, v := range friend.GetSNSAttrs() {
|
||||
switch k {
|
||||
case FriendAttrAddSource, FriendAttrAddTime, FriendAttrRemarkTime, FriendAttrAddWording:
|
||||
default:
|
||||
item.Attrs = append(item.Attrs, &types.TagPair{
|
||||
Tag: k,
|
||||
Value: v,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
for k, v := range friend.GetSNSCustomAttrs() {
|
||||
item.Attrs = append(item.Attrs, &types.TagPair{
|
||||
Tag: k,
|
||||
Value: v,
|
||||
})
|
||||
}
|
||||
|
||||
req.Friends = append(req.Friends, item)
|
||||
}
|
||||
|
||||
resp := &updateFriendsResp{}
|
||||
|
||||
if err = a.client.Post(service, commandUpdateFriend, req, resp); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
results = resp.Results
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteFriend 删除单个好友
|
||||
// 本方法拓展于“删除多个好友(DeleteFriends)”方法。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/1644
|
||||
func (a *api) DeleteFriend(userId string, isBothDelete bool, deletedUserId string) (err error) {
|
||||
var results []*Result
|
||||
|
||||
if results, err = a.DeleteFriends(userId, isBothDelete, deletedUserId); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if results != nil && len(results) > 0 {
|
||||
for _, result := range results {
|
||||
if result.UserId == deletedUserId && result.ResultCode != enum.SuccessCode {
|
||||
return core.NewError(result.ResultCode, result.ResultInfo)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteFriends 删除多个好友
|
||||
// 删除好友,支持单向删除好友和双向删除好友。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/1644
|
||||
func (a *api) DeleteFriends(userId string, isBothDelete bool, deletedUserIds ...string) (results []*Result, err error) {
|
||||
req := &deleteFriendsReq{UserId: userId, DeletedUserIds: deletedUserIds}
|
||||
resp := &deleteFriendsResp{}
|
||||
|
||||
if isBothDelete {
|
||||
req.DeleteType = DeleteTypeBoth
|
||||
} else {
|
||||
req.DeleteType = DeleteTypeSingle
|
||||
}
|
||||
|
||||
if err = a.client.Post(service, commandDeleteFriend, req, resp); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
results = resp.Results
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteAllFriends 删除所有好友
|
||||
// 清除指定用户的标配好友数据和自定义好友数据。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/1645
|
||||
func (a *api) DeleteAllFriends(userId string, deleteType ...DeleteType) (err error) {
|
||||
req := &deleteAllFriendsReq{UserId: userId}
|
||||
|
||||
if len(deleteType) > 0 {
|
||||
req.DeleteType = deleteType[0]
|
||||
} else {
|
||||
req.DeleteType = DeleteTypeSingle
|
||||
}
|
||||
|
||||
if err = a.client.Post(service, commandDeleteAllFriend, req, &types.ActionBaseResp{}); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// CheckFriend 校验单个好友
|
||||
// 本方法拓展于“校验多个好友(CheckFriends)”方法。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/1646
|
||||
func (a *api) CheckFriend(userId string, checkType CheckType, checkedUserId string) (relation string, err error) {
|
||||
var results []*CheckResult
|
||||
|
||||
if results, err = a.CheckFriends(userId, checkType, checkedUserId); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if results != nil && len(results) > 0 {
|
||||
for _, result := range results {
|
||||
if result.UserId == checkedUserId {
|
||||
if result.ResultCode != enum.SuccessCode {
|
||||
err = core.NewError(result.ResultCode, result.ResultInfo)
|
||||
return
|
||||
}
|
||||
|
||||
relation = result.Relation
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// CheckFriends 校验多个好友
|
||||
// 支持批量校验好友关系。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/1646
|
||||
func (a *api) CheckFriends(userId string, checkType CheckType, checkedUserIds ...string) (results []*CheckResult, err error) {
|
||||
if c := len(checkedUserIds); c == 0 {
|
||||
err = core.NewError(enum.InvalidParamsCode, "the accounts is not set")
|
||||
return
|
||||
} else if c > batchCheckFriendsLimit {
|
||||
err = core.NewError(enum.InvalidParamsCode, fmt.Sprintf("the number of checked accounts cannot exceed %d", batchCheckFriendsLimit))
|
||||
return
|
||||
}
|
||||
|
||||
req := &checkFriendsReq{UserId: userId, CheckedUserIds: checkedUserIds, CheckType: checkType}
|
||||
resp := &checkFriendsResp{}
|
||||
|
||||
if err = a.client.Post(service, commandCheckFriend, req, resp); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
results = resp.Results
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetFriend 拉取单个指定好友
|
||||
// 本方法拓展于“拉取多个指定好友(GetFriends)”方法。
|
||||
// 支持拉取指定好友的好友数据和资料数据。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/8609
|
||||
func (a *api) GetFriend(userId string, tagList []string, friendUserId string) (friend *Friend, err error) {
|
||||
var friends []*Friend
|
||||
|
||||
if friends, err = a.GetFriends(userId, tagList, friendUserId); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if len(friends) > 0 {
|
||||
friend = friends[0]
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetFriends 拉取多个指定好友
|
||||
// 支持拉取指定好友的好友数据和资料数据。
|
||||
// 建议每次拉取的好友数不超过100,避免因数据量太大导致回包失败。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/8609
|
||||
func (a *api) GetFriends(userId string, tagList []string, friendUserIds ...string) (friends []*Friend, err error) {
|
||||
if c := len(friendUserIds); c == 0 {
|
||||
err = core.NewError(enum.InvalidParamsCode, "the account of friends is not set")
|
||||
return
|
||||
} else if c > batchGetFriendsLimit {
|
||||
err = core.NewError(enum.InvalidParamsCode, fmt.Sprintf("the number of friend's account cannot exceed %d", batchGetFriendsLimit))
|
||||
return
|
||||
}
|
||||
|
||||
req := &getFriendsReq{UserId: userId, FriendUserIds: friendUserIds}
|
||||
resp := &getFriendsResp{}
|
||||
|
||||
for _, tag := range tagList {
|
||||
switch tag {
|
||||
case FriendAttrRemarkTime:
|
||||
default:
|
||||
req.TagList = append(req.TagList, tag)
|
||||
}
|
||||
}
|
||||
|
||||
if err = a.client.Post(service, commandGetFriend, req, resp); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
friends = make([]*Friend, 0, len(resp.Friends))
|
||||
|
||||
for _, item := range resp.Friends {
|
||||
friend := NewFriend(item.UserId)
|
||||
friend.SetError(item.ResultCode, item.ResultInfo)
|
||||
for _, v := range item.Profiles {
|
||||
friend.SetAttr(v.Tag, v.Value)
|
||||
}
|
||||
friends = append(friends, friend)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// FetchFriends 拉取好友
|
||||
// 分页拉取全量好友数据。
|
||||
// 不支持资料数据的拉取。
|
||||
// 不需要指定请求拉取的字段,默认返回全量的标配好友数据和自定义好友数据。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/1647
|
||||
func (a *api) FetchFriends(userId string, startIndex int, sequence ...int) (ret *FetchFriendsRet, err error) {
|
||||
req := &fetchFriendsReq{UserId: userId, StartIndex: startIndex}
|
||||
resp := &fetchFriendsResp{}
|
||||
|
||||
if len(sequence) > 0 {
|
||||
req.StandardSequence = sequence[0]
|
||||
}
|
||||
|
||||
if len(sequence) > 1 {
|
||||
req.CustomSequence = sequence[1]
|
||||
}
|
||||
|
||||
if err = a.client.Post(service, commandFetchFriend, req, resp); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
ret = &FetchFriendsRet{
|
||||
StandardSequence: resp.StandardSequence,
|
||||
CustomSequence: resp.CustomSequence,
|
||||
StartIndex: resp.NextStartIndex,
|
||||
Total: resp.FriendNum,
|
||||
HasMore: resp.CompleteFlag == 0,
|
||||
List: make([]*Friend, 0, len(resp.Friends)),
|
||||
}
|
||||
|
||||
for _, item := range resp.Friends {
|
||||
friend := NewFriend(item.UserId)
|
||||
for _, v := range item.Values {
|
||||
friend.SetAttr(v.Tag, v.Value)
|
||||
}
|
||||
ret.List = append(ret.List, friend)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// PullFriends 续拉取好友
|
||||
// 本API是借助"拉取好友"API进行扩展实现
|
||||
// 分页拉取全量好友数据。
|
||||
// 不支持资料数据的拉取。
|
||||
// 不需要指定请求拉取的字段,默认返回全量的标配好友数据和自定义好友数据。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/1647
|
||||
func (a *api) PullFriends(userId string, fn func(ret *FetchFriendsRet)) (err error) {
|
||||
var (
|
||||
ret *FetchFriendsRet
|
||||
startIndex int
|
||||
standardSequence int
|
||||
customSequence int
|
||||
)
|
||||
|
||||
for ret == nil || ret.HasMore {
|
||||
ret, err = a.FetchFriends(userId, startIndex, standardSequence, customSequence)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
fn(ret)
|
||||
|
||||
if ret.HasMore {
|
||||
startIndex = ret.StartIndex
|
||||
standardSequence = ret.StandardSequence
|
||||
customSequence = ret.CustomSequence
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// AddBlacklist 添加黑名单
|
||||
// 添加黑名单,支持批量添加黑名单。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/3718
|
||||
func (a *api) AddBlacklist(userId string, blackedUserIds ...string) (results []*Result, err error) {
|
||||
if c := len(blackedUserIds); c == 0 {
|
||||
err = core.NewError(enum.InvalidParamsCode, "the blacked accounts is not set")
|
||||
return
|
||||
} else if c > batchAddBlacklistLimit {
|
||||
err = core.NewError(enum.InvalidParamsCode, fmt.Sprintf("the number of blacked accounts cannot exceed %d", batchAddBlacklistLimit))
|
||||
return
|
||||
}
|
||||
|
||||
req := &addBlacklistReq{UserId: userId, BlackedUserIds: blackedUserIds}
|
||||
resp := &addBlacklistResp{}
|
||||
|
||||
if err = a.client.Post(service, commandAddBlackList, req, resp); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
results = resp.Results
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteBlacklist 删除黑名单
|
||||
// 删除指定黑名单。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/3719
|
||||
func (a *api) DeleteBlacklist(userId string, deletedUserIds ...string) (results []*Result, err error) {
|
||||
if c := len(deletedUserIds); c == 0 {
|
||||
err = core.NewError(enum.InvalidParamsCode, "the deleted accounts is not set")
|
||||
return
|
||||
} else if c > batchDeleteBlacklistLimit {
|
||||
err = core.NewError(enum.InvalidParamsCode, fmt.Sprintf("the number of deleted accounts cannot exceed %d", batchDeleteBlacklistLimit))
|
||||
return
|
||||
}
|
||||
|
||||
req := &deleteBlacklistReq{UserId: userId, DeletedUserIds: deletedUserIds}
|
||||
resp := &deleteBlacklistResp{}
|
||||
|
||||
if err = a.client.Post(service, commandDeleteBlackList, req, resp); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
results = resp.Results
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// FetchBlacklist 拉取黑名单
|
||||
// 支持分页拉取所有黑名单。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/3722
|
||||
func (a *api) FetchBlacklist(userId string, maxLimited int, startIndexAndSequence ...int) (ret *FetchBlacklistRet, err error) {
|
||||
req := &fetchBlacklistReq{UserId: userId, MaxLimited: maxLimited}
|
||||
|
||||
if len(startIndexAndSequence) > 0 {
|
||||
req.StartIndex = startIndexAndSequence[0]
|
||||
}
|
||||
|
||||
if len(startIndexAndSequence) > 1 {
|
||||
req.LastSequence = startIndexAndSequence[1]
|
||||
}
|
||||
|
||||
resp := &fetchBlacklistResp{}
|
||||
|
||||
if err = a.client.Post(service, commandGetBlackList, req, resp); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
ret = &FetchBlacklistRet{
|
||||
StartIndex: resp.StartIndex,
|
||||
StandardSequence: resp.CurrentSequence,
|
||||
List: resp.Blacklists,
|
||||
HasMore: resp.StartIndex != 0,
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// PullBlacklist 拉取黑名单
|
||||
// 本API是借助"拉取黑名单"API进行扩展实现
|
||||
// 支持分页拉取所有黑名单。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/3722
|
||||
func (a *api) PullBlacklist(userId string, maxLimited int, fn func(ret *FetchBlacklistRet)) (err error) {
|
||||
var (
|
||||
ret *FetchBlacklistRet
|
||||
startIndex = 0
|
||||
standardSequence = 0
|
||||
)
|
||||
|
||||
for ret == nil || ret.HasMore {
|
||||
ret, err = a.FetchBlacklist(userId, maxLimited, startIndex, standardSequence)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
fn(ret)
|
||||
|
||||
if ret.HasMore {
|
||||
startIndex = ret.StartIndex
|
||||
standardSequence = ret.StandardSequence
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// CheckBlacklist 校验黑名单
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/3725
|
||||
func (a *api) CheckBlacklist(userId string, checkType BlacklistCheckType, checkedUserIds ...string) (results []*CheckResult, err error) {
|
||||
if c := len(checkedUserIds); c == 0 {
|
||||
err = core.NewError(enum.InvalidParamsCode, "the checked accounts is not set")
|
||||
return
|
||||
} else if c > batchCheckBlacklistLimit {
|
||||
err = core.NewError(enum.InvalidParamsCode, fmt.Sprintf("the number of checked accounts cannot exceed %d", batchCheckBlacklistLimit))
|
||||
return
|
||||
}
|
||||
|
||||
req := &checkBlacklistReq{UserId: userId, CheckedUserIds: checkedUserIds, CheckType: checkType}
|
||||
resp := &checkBlacklistResp{}
|
||||
|
||||
if err = a.client.Post(service, commandCheckBlackList, req, resp); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
results = resp.Results
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// AddGroups 添加分组
|
||||
// 添加分组,支持批量添加分组,并将指定好友加入到新增分组中。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/10107
|
||||
func (a *api) AddGroups(userId string, groupNames []string, joinedUserIds ...[]string) (currentSequence int, results []*Result, err error) {
|
||||
if c := len(groupNames); c == 0 {
|
||||
err = core.NewError(enum.InvalidParamsCode, "the added groups is not set")
|
||||
return
|
||||
} else if c > batchAddGroupsLimit {
|
||||
err = core.NewError(enum.InvalidParamsCode, fmt.Sprintf("the number of added groups cannot exceed %d", batchAddGroupsLimit))
|
||||
return
|
||||
}
|
||||
|
||||
req := &addGroupsReq{UserId: userId, GroupNames: groupNames}
|
||||
|
||||
if len(joinedUserIds) > 0 {
|
||||
if c := len(joinedUserIds[0]); c == 0 {
|
||||
err = core.NewError(enum.InvalidParamsCode, "the added groups is not set")
|
||||
return
|
||||
} else if c > batchJoinGroupsLimit {
|
||||
err = core.NewError(enum.InvalidParamsCode, fmt.Sprintf("the number of accounts joining the group cannot exceed %d", batchJoinGroupsLimit))
|
||||
return
|
||||
}
|
||||
|
||||
req.JoinedUserIds = joinedUserIds[0]
|
||||
}
|
||||
|
||||
resp := &addGroupsResp{}
|
||||
|
||||
if err = a.client.Post(service, commandAddGroup, req, resp); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
currentSequence = resp.CurrentSequence
|
||||
results = resp.Results
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteGroups 删除分组
|
||||
// 删除指定分组。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/10108
|
||||
func (a *api) DeleteGroups(userId string, groupNames ...string) (currentSequence int, err error) {
|
||||
if c := len(groupNames); c == 0 {
|
||||
err = core.NewError(enum.InvalidParamsCode, "the deleted groups is not set")
|
||||
return
|
||||
} else if c > batchDeleteGroupsLimit {
|
||||
err = core.NewError(enum.InvalidParamsCode, fmt.Sprintf("the number of deleted groups cannot exceed %d", batchDeleteGroupsLimit))
|
||||
return
|
||||
}
|
||||
|
||||
req := &deleteGroupsReq{UserId: userId, GroupNames: groupNames}
|
||||
resp := &deleteGroupsResp{}
|
||||
|
||||
if err = a.client.Post(service, commandDeleteGroup, req, resp); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
currentSequence = resp.CurrentSequence
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetGroups 拉取分组
|
||||
// 拉取分组,支持指定分组以及拉取分组下的好友列表。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/54763
|
||||
func (a *api) GetGroups(userId string, lastSequence int, isGetFriends bool, groupNames ...string) (currentSequence int, results []*GroupResult, err error) {
|
||||
if c := len(groupNames); c == 0 {
|
||||
err = core.NewError(enum.InvalidParamsCode, "the gotten groups is not set")
|
||||
return
|
||||
} else if c > batchGetGroupsLimit {
|
||||
err = core.NewError(enum.InvalidParamsCode, fmt.Sprintf("the number of gotten groups cannot exceed %d", batchGetGroupsLimit))
|
||||
return
|
||||
}
|
||||
|
||||
req := &getGroupsReq{UserId: userId, LastSequence: lastSequence, GroupNames: groupNames}
|
||||
resp := &getGroupsResp{}
|
||||
|
||||
if isGetFriends {
|
||||
req.NeedFriend = NeedFriendYes
|
||||
} else {
|
||||
req.NeedFriend = NeedFriendNo
|
||||
}
|
||||
|
||||
if err = a.client.Post(service, commandGetGroup, req, resp); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
currentSequence = resp.CurrentSequence
|
||||
results = resp.Results
|
||||
|
||||
return
|
||||
}
|
90
sns/enum.go
Normal file
90
sns/enum.go
Normal file
@@ -0,0 +1,90 @@
|
||||
/**
|
||||
* @Author: Echo
|
||||
* @Author:1711788888@qq.com
|
||||
* @Date: 2021/9/7 14:40
|
||||
* @Desc: TODO
|
||||
*/
|
||||
|
||||
package sns
|
||||
|
||||
import "git.echol.cn/loser/tencent-im/internal/enum"
|
||||
|
||||
type (
|
||||
// AddType 添加类型
|
||||
AddType string
|
||||
|
||||
// DeleteType 删除类型
|
||||
DeleteType string
|
||||
|
||||
// CheckType 校验模式
|
||||
CheckType string
|
||||
|
||||
// ForceAddType 强制添加类型
|
||||
ForceAddType int
|
||||
|
||||
// BlacklistCheckType 黑名单校验模式
|
||||
BlacklistCheckType string
|
||||
|
||||
// NeedFriendType 是否需要拉取分组下的User列表
|
||||
NeedFriendType string
|
||||
)
|
||||
|
||||
const (
|
||||
// 标准资料字段
|
||||
StandardAttrNickname = enum.StandardAttrNickname // 昵称
|
||||
StandardAttrGender = enum.StandardAttrGender // 性别
|
||||
StandardAttrBirthday = enum.StandardAttrBirthday // 生日
|
||||
StandardAttrLocation = enum.StandardAttrLocation // 所在地
|
||||
StandardAttrSignature = enum.StandardAttrSignature // 个性签名
|
||||
StandardAttrAllowType = enum.StandardAttrAllowType // 加好友验证方式
|
||||
StandardAttrLanguage = enum.StandardAttrLanguage // 语言
|
||||
StandardAttrAvatar = enum.StandardAttrAvatar // 头像URL
|
||||
StandardAttrMsgSettings = enum.StandardAttrMsgSettings // 消息设置
|
||||
StandardAttrAdminForbidType = enum.StandardAttrAdminForbidType // 管理员禁止加好友标识
|
||||
StandardAttrLevel = enum.StandardAttrLevel // 等级
|
||||
StandardAttrRole = enum.StandardAttrRole // 角色
|
||||
|
||||
// 好友属性
|
||||
FriendAttrAddSource = "Tag_SNS_IM_AddSource" // 添加源
|
||||
FriendAttrRemark = "Tag_SNS_IM_Remark" // 备注
|
||||
FriendAttrGroup = "Tag_SNS_IM_Group" // 分组
|
||||
FriendAttrAddWording = "Tag_SNS_IM_AddWording" // 附言信息
|
||||
FriendAttrAddTime = "Tag_SNS_IM_AddTime" // 添加时间
|
||||
FriendAttrRemarkTime = "Tag_SNS_IM_RemarkTime" // 备注时间
|
||||
|
||||
// 添加类型
|
||||
AddTypeSingle AddType = "Add_Type_Single" // 单向添加
|
||||
AddTypeBoth AddType = "Add_Type_Both" // 双向添加
|
||||
|
||||
// 删除类型
|
||||
DeleteTypeSingle DeleteType = "Delete_Type_Single" // 单向删除
|
||||
DeleteTypeBoth DeleteType = "Delete_Type_Both" // 双向删除
|
||||
|
||||
// 校验模式
|
||||
CheckTypeSingle CheckType = "CheckResult_Type_Single" // 单向校验好友关系
|
||||
CheckTypeBoth CheckType = "CheckResult_Type_Both" // 双向校验好友关系
|
||||
|
||||
// 黑名单校验模式
|
||||
BlacklistCheckTypeSingle BlacklistCheckType = "BlackCheckResult_Type_Single" // 单向校验黑名单关系
|
||||
BlacklistCheckTypeBoth BlacklistCheckType = "BlackCheckResult_Type_Both" // 双向校验黑名单关系
|
||||
|
||||
// 强制加好友类型
|
||||
ForceAddYes ForceAddType = 1 // 强制加好友
|
||||
ForceAddNo ForceAddType = 0 // 常规加好友
|
||||
|
||||
// 是否需要拉取分组下的 User 列表
|
||||
NeedFriendYes NeedFriendType = "Need_Friend_Type_Yes" // 需要拉取
|
||||
NeedFriendNo NeedFriendType = "Need_Friend_Type_No" // 不需要拉取
|
||||
|
||||
// 好友关系结果
|
||||
CheckResultTypeNoRelation = "CheckResult_Type_NoRelation" // From_Account 的好友表中没有 To_Account,但无法确定 To_Account 的好友表中是否有 From_Account
|
||||
CheckResultTypeAWithB = "CheckResult_Type_AWithB" // From_Account 的好友表中有 To_Account,但无法确定 To_Account 的好友表中是否有 From_Account
|
||||
CheckResultTypeBWithA = "CheckResult_Type_BWithA" // From_Account 的好友表中没有 To_Account,但 To_Account 的好友表中有 From_Account
|
||||
CheckResultTypeBothWay = "CheckResult_Type_BothWay" // From_Account 的好友表中有 To_Account,To_Account 的好友表中也有 From_Account
|
||||
|
||||
// 黑名单关系结果
|
||||
BlackCheckResultTypeNO = "BlackCheckResult_Type_NO" // From_Account 的黑名单中没有 To_Account,但无法确定 To_Account 的黑名单中是否有 From_Account
|
||||
BlackCheckResultTypeAWithB = "BlackCheckResult_Type_AWithB" // From_Account 的黑名单中有 To_Account,但无法确定 To_Account 的黑名单中是否有 From_Account
|
||||
BlackCheckResultTypeBWithA = "BlackCheckResult_Type_BWithA" // From_Account 的黑名单中没有 To_Account,但 To_Account 的黑名单中有 From_Account
|
||||
BlackCheckResultTypeBothWay = "BlackCheckResult_Type_BothWay" // From_Account 的黑名单中有 To_Account,To_Account 的黑名单中也有 From_Account
|
||||
)
|
206
sns/friend.go
Normal file
206
sns/friend.go
Normal file
@@ -0,0 +1,206 @@
|
||||
/**
|
||||
* @Author: Echo
|
||||
* @Email:1711788888@qq.com
|
||||
* @Date: 2021/8/30 2:55 下午
|
||||
* @Desc: 好友关系
|
||||
*/
|
||||
|
||||
package sns
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.echol.cn/loser/tencent-im/internal/entity"
|
||||
)
|
||||
|
||||
var (
|
||||
errNotSetAccount = errors.New("the friend's account is not set")
|
||||
errNotSetAddSource = errors.New("the friend's add source is not set")
|
||||
)
|
||||
|
||||
type Friend struct {
|
||||
entity.User
|
||||
customAttrs map[string]interface{}
|
||||
}
|
||||
|
||||
func NewFriend(userId ...string) *Friend {
|
||||
f := new(Friend)
|
||||
f.customAttrs = make(map[string]interface{})
|
||||
|
||||
if len(userId) > 0 {
|
||||
f.SetUserId(userId[0])
|
||||
}
|
||||
|
||||
return f
|
||||
}
|
||||
|
||||
// SetAddSource 设置添加来源
|
||||
func (f *Friend) SetAddSource(addSource string) {
|
||||
f.SetAttr(FriendAttrAddSource, "AddSource_Type_"+addSource)
|
||||
}
|
||||
|
||||
// GetAddSource 获取添加来源
|
||||
func (f *Friend) GetAddSource() (addSource string, exist bool) {
|
||||
var v interface{}
|
||||
if v, exist = f.GetAttr(FriendAttrAddSource); exist {
|
||||
addSource = strings.TrimLeft(v.(string), "AddSource_Type_")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetSrcAddSource 获取添加来源(原始的)
|
||||
func (f *Friend) GetSrcAddSource() (addSource string, exist bool) {
|
||||
var v interface{}
|
||||
if v, exist = f.GetAttr(FriendAttrAddSource); exist {
|
||||
addSource = v.(string)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// SetRemark 设置备注
|
||||
func (f *Friend) SetRemark(remark string) {
|
||||
f.SetAttr(FriendAttrRemark, remark)
|
||||
}
|
||||
|
||||
// GetRemark 获取备注
|
||||
func (f *Friend) GetRemark() (remark string, exist bool) {
|
||||
var v interface{}
|
||||
if v, exist = f.GetAttr(FriendAttrRemark); exist {
|
||||
remark = v.(string)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// SetGroup 设置分组
|
||||
func (f *Friend) SetGroup(groupName ...string) {
|
||||
f.SetAttr(FriendAttrGroup, groupName)
|
||||
}
|
||||
|
||||
// GetGroup 获取分组
|
||||
func (f *Friend) GetGroup() (groups []string, exist bool) {
|
||||
var v interface{}
|
||||
if v, exist = f.GetAttr(FriendAttrGroup); exist && v != nil {
|
||||
if vv, ok := v.([]interface{}); ok {
|
||||
for _, group := range vv {
|
||||
groups = append(groups, group.(string))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// SetAddWording 设置形成好友关系时的附言信息
|
||||
func (f *Friend) SetAddWording(addWording string) {
|
||||
f.SetAttr(FriendAttrAddWording, addWording)
|
||||
}
|
||||
|
||||
// GetAddWording 获取形成好友关系时的附言信息
|
||||
func (f *Friend) GetAddWording() (addWording string, exist bool) {
|
||||
var v interface{}
|
||||
if v, exist = f.GetAttr(FriendAttrAddWording); exist {
|
||||
addWording = v.(string)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// SetAddTime 设置添加时间(忽略)
|
||||
func (f *Friend) SetAddTime(addTime int64) {
|
||||
f.SetAttr(FriendAttrAddTime, addTime)
|
||||
}
|
||||
|
||||
// GetAddTime 获取添加时间
|
||||
func (f *Friend) GetAddTime() (addTime int64, exist bool) {
|
||||
var v interface{}
|
||||
if v, exist = f.GetAttr(FriendAttrAddTime); exist {
|
||||
addTime = v.(int64)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// SetRemarkTime 设置备注时间
|
||||
func (f *Friend) SetRemarkTime(remarkTime int64) {
|
||||
f.SetAttr(FriendAttrRemarkTime, remarkTime)
|
||||
}
|
||||
|
||||
// GetRemarkTime 获取备注时间
|
||||
func (f *Friend) GetRemarkTime() (remarkTime int64, exist bool) {
|
||||
var v interface{}
|
||||
if v, exist = f.GetAttr(FriendAttrRemarkTime); exist {
|
||||
remarkTime = v.(int64)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// SetSNSCustomAttr 设置SNS自定义关系数据(自定义字段需要单独申请,请在 IM 控制台 >应用配置>功能配置申请自定义好友字段,申请提交后,自定义好友字段将在5分钟内生效)
|
||||
func (f *Friend) SetSNSCustomAttr(name string, value interface{}) {
|
||||
if f.customAttrs == nil {
|
||||
f.customAttrs = make(map[string]interface{})
|
||||
}
|
||||
|
||||
f.customAttrs[fmt.Sprintf("%s_%s", "Tag_SNS_Custom", name)] = value
|
||||
}
|
||||
|
||||
// GetSNSCustomAttr 设置SNS自定义关系数据 (自定义字段需要单独申请,请在 IM 控制台 >应用配置>功能配置申请自定义好友字段,申请提交后,自定义好友字段将在5分钟内生效)
|
||||
func (f *Friend) GetSNSCustomAttr(name string) (value interface{}, exist bool) {
|
||||
if f.customAttrs == nil {
|
||||
return
|
||||
}
|
||||
|
||||
value, exist = f.customAttrs[fmt.Sprintf("%s_%s", "Tag_SNS_Custom", name)]
|
||||
return
|
||||
}
|
||||
|
||||
// GetSNSAttrs 获取SNS标准关系数据
|
||||
func (f *Friend) GetSNSAttrs() (attrs map[string]interface{}) {
|
||||
attrs = make(map[string]interface{})
|
||||
|
||||
for k, v := range f.GetAttrs() {
|
||||
switch k {
|
||||
case FriendAttrAddSource, FriendAttrRemark, FriendAttrGroup, FriendAttrAddWording, FriendAttrAddTime, FriendAttrRemarkTime:
|
||||
attrs[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetSNSCustomAttrs 获取SNS自定义关系数据(自定义字段需要单独申请,请在 IM 控制台 >应用配置>功能配置申请自定义好友字段,申请提交后,自定义好友字段将在5分钟内生效)
|
||||
func (f *Friend) GetSNSCustomAttrs() (attrs map[string]interface{}) {
|
||||
attrs = make(map[string]interface{})
|
||||
|
||||
if f.customAttrs == nil {
|
||||
return
|
||||
}
|
||||
|
||||
for k, v := range f.customAttrs {
|
||||
switch k {
|
||||
case FriendAttrAddSource, FriendAttrRemark, FriendAttrGroup, FriendAttrAddWording, FriendAttrAddTime, FriendAttrRemarkTime:
|
||||
default:
|
||||
attrs[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// checkError 检测参数错误
|
||||
func (f *Friend) checkError() error {
|
||||
if f.GetUserId() == "" {
|
||||
return errNotSetAccount
|
||||
}
|
||||
|
||||
if _, exist := f.GetSrcAddSource(); !exist {
|
||||
return errNotSetAddSource
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
301
sns/types.go
Normal file
301
sns/types.go
Normal file
@@ -0,0 +1,301 @@
|
||||
/**
|
||||
* @Author: Echo
|
||||
* @Author:1711788888@qq.com
|
||||
* @Date: 2021/5/29 17:34
|
||||
* @Desc: 关系链管理数据类型
|
||||
*/
|
||||
|
||||
package sns
|
||||
|
||||
import "git.echol.cn/loser/tencent-im/internal/types"
|
||||
|
||||
type (
|
||||
// 添加的好友信息
|
||||
addFriendItem struct {
|
||||
UserId string `json:"To_Account"` // (必填)好友的UserID
|
||||
AddSource string `json:"AddSource"` // (必填)加好友来源
|
||||
Remark string `json:"Remark,omitempty"` // (选填)加好友备注
|
||||
GroupName string `json:"GroupName,omitempty"` // (选填)分组信息,添加好友时只允许设置一个分组,因此使用 String 类型即可
|
||||
AddWording string `json:"AddWording,omitempty"` // (选填)好友关系时的附言信息
|
||||
}
|
||||
|
||||
// 添加好友(请求)
|
||||
addFriendsReq struct {
|
||||
UserId string `json:"From_Account"` // (必填)需要为该UserID添加好友
|
||||
Friends []*addFriendItem `json:"AddFriendItem"` // (必填)好友结构体对象
|
||||
AddType AddType `json:"AddType"` // (选填)加好友方式(默认双向加好友方式)Add_Type_Single:表示单向加好友;Add_Type_Both:表示双向加好友
|
||||
ForceAddFlags ForceAddType `json:"ForceAddFlags"` // (选填)管理员强制加好友标记:1表示强制加好友,0表示常规加好友方式
|
||||
}
|
||||
|
||||
// 添加好友(响应)
|
||||
addFriendsResp struct {
|
||||
types.ActionBaseResp
|
||||
Results []*Result `json:"ResultItem"` // 批量加好友的结果对象数组
|
||||
}
|
||||
|
||||
// Result 添加结果
|
||||
Result struct {
|
||||
UserId string `json:"To_Account"` // 请求添加的好友的UserID
|
||||
ResultCode int `json:"ResultCode"` // 处理结果,0表示成功,非0表示失败
|
||||
ResultInfo string `json:"ResultInfo"` // 错误描述信息
|
||||
}
|
||||
|
||||
// 导入好友(请求)
|
||||
importFriendsReq struct {
|
||||
UserId string `json:"From_Account"` // 需要为该UserID添加好友
|
||||
Friends []*importFriendItem `json:"AddFriendItem"` // 好友结构体对象
|
||||
}
|
||||
|
||||
// 导入好友(响应)
|
||||
importFriendsResp struct {
|
||||
types.ActionBaseResp
|
||||
Results []*Result `json:"ResultItem"` // 结果对象数组
|
||||
FailUserIds []string `json:"Fail_Account"` // 返回处理失败的用户列表,仅当存在失败用户时才返回该字段
|
||||
}
|
||||
|
||||
// 导入好友
|
||||
importFriendItem struct {
|
||||
UserId string `json:"To_Account"` // (必填)好友的UserID
|
||||
AddSource string `json:"AddSource"` // (必填)加好友来源
|
||||
Remark string `json:"Remark,omitempty"` // (选填)加好友备注
|
||||
GroupName []string `json:"GroupName,omitempty"` // (选填)分组信息
|
||||
AddWording string `json:"AddWording,omitempty"` // (选填)好友关系时的附言信息
|
||||
AddTime int64 `json:"AddTime,omitempty"` // (选填)形成好友关系的时间
|
||||
RemarkTime int64 `json:"RemarkTime,omitempty"` // (选填)好友备注时间
|
||||
CustomData []*types.TagPair `json:"CustomItem,omitempty"` // (选填)自定义好友数据
|
||||
}
|
||||
|
||||
// 更新好友(请求)
|
||||
updateFriendsReq struct {
|
||||
UserId string `json:"From_Account"` // (必填)需要更新该UserID的关系链数据
|
||||
Friends []*updateFriendItem `json:"UpdateItem"` // (必填)需要更新的好友对象数组
|
||||
}
|
||||
|
||||
// 更新好友(响应)
|
||||
updateFriendsResp struct {
|
||||
types.ActionBaseResp
|
||||
Results []*Result `json:"ResultItem"` // 结果对象数组
|
||||
FailUserIds []string `json:"Fail_Account"` // 返回处理失败的用户列表,仅当存在失败用户时才返回该字段
|
||||
}
|
||||
|
||||
// 更新好友
|
||||
updateFriendItem struct {
|
||||
UserId string `json:"To_Account"` // (必填)好友的UserID
|
||||
Attrs []*types.TagPair `json:"SnsItem"` // (必填)需要更新的关系链数据对象数组
|
||||
}
|
||||
|
||||
// 删除好友(请求)
|
||||
deleteFriendsReq struct {
|
||||
UserId string `json:"From_Account"` // (必填)需要删除该UserID的好友
|
||||
DeletedUserIds []string `json:"To_Account"` // (必填)待删除的好友的 UserID 列表,单次请求的 To_Account 数不得超过1000
|
||||
DeleteType DeleteType `json:"DeleteType"` // (选填)删除模式
|
||||
}
|
||||
|
||||
// 删除好友(响应)
|
||||
deleteFriendsResp struct {
|
||||
types.ActionBaseResp
|
||||
ErrorDisplay string `json:"ErrorDisplay"` // 详细的客户端展示信息
|
||||
Results []*Result `json:"ResultItem"` // 返回处理失败的用户列表,仅当存在失败用户时才返回该字段
|
||||
}
|
||||
|
||||
// 删除所有好友(请求)
|
||||
deleteAllFriendsReq struct {
|
||||
UserId string `json:"From_Account"` // (必填)需要删除该UserID的好友
|
||||
DeleteType DeleteType `json:"DeleteType"` // (选填)删除类型
|
||||
}
|
||||
|
||||
// 校验好友(请求)
|
||||
checkFriendsReq struct {
|
||||
UserId string `json:"From_Account"` // (必填)需要校验该 UserID 的好友
|
||||
CheckedUserIds []string `json:"To_Account"` // (必填)请求校验的好友的 UserID 列表,单次请求的 To_Account 数不得超过1000
|
||||
CheckType CheckType `json:"CheckType"` // (必填)校验模式
|
||||
}
|
||||
|
||||
// 校验好友(响应)
|
||||
checkFriendsResp struct {
|
||||
types.ActionBaseResp
|
||||
Results []*CheckResult `json:"InfoItem"` // 结果对象数组
|
||||
FailUserIds []string `json:"Fail_Account"` // 返回处理失败的用户列表,仅当存在失败用户时才返回该字段
|
||||
}
|
||||
|
||||
// CheckResult 校验结果
|
||||
CheckResult struct {
|
||||
UserId string `json:"To_Account"` // 请求校验的用户的 UserID
|
||||
Relation string `json:"Relation"` // 校验成功时 To_Account 与 From_Account 之间的好友关系
|
||||
ResultCode int `json:"ResultCode"` // 处理结果,0表示成功,非0表示失败
|
||||
ResultInfo string `json:"ResultInfo"` // 描述信息,成功时该字段为空
|
||||
}
|
||||
|
||||
// 拉取指定好友(请求)
|
||||
getFriendsReq struct {
|
||||
UserId string `json:"From_Account"` // (必填)指定要拉取好友数据的用户的 UserID
|
||||
FriendUserIds []string `json:"To_Account"` // (必填)好友的 UserID 列表,建议每次请求的好友数不超过100,避免因数据量太大导致回包失败
|
||||
TagList []string `json:"TagList"` // (必填)指定要拉取的资料字段及好友字段
|
||||
}
|
||||
|
||||
// 拉取指定好友(响应)
|
||||
getFriendsResp struct {
|
||||
types.ActionBaseResp
|
||||
FailUserIds []string `json:"Fail_Account"` // 返回处理失败的用户列表,仅当存在失败用户时才返回该字段
|
||||
Friends []friendData `json:"InfoItem"` // 好友对象数组
|
||||
}
|
||||
|
||||
// 拉取好友(请求)
|
||||
fetchFriendsReq struct {
|
||||
UserId string `json:"From_Account"` // (必填)需要拉取该 UserID 的黑名单
|
||||
StartIndex int `json:"StartIndex"` // (必填)拉取的起始位置
|
||||
StandardSequence int `json:"StandardSequence"` // (选填)上次拉好友数据时返回的 StandardSequence,如果 StandardSequence 字段的值与后台一致,后台不会返回标配好友数据
|
||||
CustomSequence int `json:"CustomSequence"` // (选填)上次拉好友数据时返回的 CustomSequence,如果 CustomSequence 字段的值与后台一致,后台不会返回自定义好友数据
|
||||
}
|
||||
|
||||
// 拉取好友(响应)
|
||||
fetchFriendsResp struct {
|
||||
types.ActionBaseResp
|
||||
Friends []friendData `json:"UserDataItem"` // 好友对象数组
|
||||
StandardSequence int `json:"StandardSequence"` // 标配好友数据的 Sequence,客户端可以保存该 Sequence,下次请求时通过请求的 StandardSequence 字段返回给后台
|
||||
CustomSequence int `json:"CustomSequence"` // 自定义好友数据的 Sequence,客户端可以保存该 Sequence,下次请求时通过请求的 CustomSequence 字段返回给后台
|
||||
FriendNum int `json:"FriendNum"` // 好友总数
|
||||
CompleteFlag int `json:"CompleteFlag"` // 分页的结束标识,非0值表示已完成全量拉取
|
||||
NextStartIndex int `json:"NextStartIndex"` // 分页接口下一页的起始位置
|
||||
}
|
||||
|
||||
// 好友信息
|
||||
friendData struct {
|
||||
UserId string `json:"To_Account"` // 好友的 UserID
|
||||
Values []types.TagPair `json:"ValueItem"` // 好友数据的数组
|
||||
Profiles []types.TagPair `json:"SnsProfileItem"` // 好友数据的数组
|
||||
ResultCode int `json:"ResultCode"` // 处理结果,0表示成功,非0表示失败
|
||||
ResultInfo string `json:"ResultInfo"` // 错误描述信息
|
||||
}
|
||||
|
||||
// FetchFriendsRet 好友列表
|
||||
FetchFriendsRet struct {
|
||||
StandardSequence int // 标准排序
|
||||
CustomSequence int // 自定义排序
|
||||
StartIndex int // 分页接口下一页的起始位置
|
||||
Total int // 好友总数
|
||||
HasMore bool // 是否还有更多数据
|
||||
List []*Friend // 好友列表
|
||||
}
|
||||
|
||||
// 添加黑名单(请求)
|
||||
addBlacklistReq struct {
|
||||
UserId string `json:"From_Account"` // (必填)请求为该 UserID 添加黑名单
|
||||
BlackedUserIds []string `json:"To_Account"` // (必填)待添加为黑名单的用户 UserID 列表,单次请求的 To_Account 数不得超过1000
|
||||
}
|
||||
|
||||
// 添加黑名单(响应)
|
||||
addBlacklistResp struct {
|
||||
types.ActionBaseResp
|
||||
FailUserIds []string `json:"Fail_Account"` // 返回处理失败的用户列表,仅当存在失败用户时才返回该字段
|
||||
Results []*Result `json:"ResultItem"` // 批量添加黑名单的结果对象数组
|
||||
}
|
||||
|
||||
// 删除黑名单(请求)
|
||||
deleteBlacklistReq struct {
|
||||
UserId string `json:"From_Account"` // (必填)需要删除该 UserID 的黑名单
|
||||
DeletedUserIds []string `json:"To_Account"` // (必填)待删除的黑名单的 UserID 列表,单次请求的 To_Account 数不得超过1000
|
||||
}
|
||||
|
||||
// 删除黑名单(响应)
|
||||
deleteBlacklistResp struct {
|
||||
types.ActionBaseResp
|
||||
FailUserIds []string `json:"Fail_Account"` // 返回处理失败的用户列表,仅当存在失败用户时才返回该字段
|
||||
Results []*Result `json:"ResultItem"` // 批量添加黑名单的结果对象数组
|
||||
}
|
||||
|
||||
// 拉取黑名单(请求)
|
||||
fetchBlacklistReq struct {
|
||||
UserId string `json:"From_Account"` // (必填)需要拉取该 UserID 的黑名单
|
||||
StartIndex int `json:"StartIndex"` // (必填)拉取的起始位置
|
||||
MaxLimited int `json:"MaxLimited"` // (必填)每页最多拉取的黑名单数
|
||||
LastSequence int `json:"LastSequence"` // (必填)上一次拉黑名单时后台返回给客户端的 Seq,初次拉取时为0
|
||||
}
|
||||
|
||||
// 拉取黑名单(响应)
|
||||
fetchBlacklistResp struct {
|
||||
types.ActionBaseResp
|
||||
ErrorDisplay string `json:"ErrorDisplay"` // 详细的客户端展示信息
|
||||
StartIndex int `json:"StartIndex"` // 下页拉取的起始位置,0表示已拉完
|
||||
CurrentSequence int `json:"CurrentSequence"` // 黑名单最新的 Seq
|
||||
Blacklists []*Blacklist `json:"BlackListItem"` // 黑名单对象数组
|
||||
}
|
||||
|
||||
// FetchBlacklistRet 拉取黑名单结果
|
||||
FetchBlacklistRet struct {
|
||||
StandardSequence int // 标准排序
|
||||
StartIndex int // 分页接口下一页的起始位置
|
||||
HasMore bool // 是否还有数据
|
||||
List []*Blacklist // 黑名单列表
|
||||
}
|
||||
|
||||
// Blacklist 黑名单
|
||||
Blacklist struct {
|
||||
UserId string `json:"To_Account"` // 黑名单的 UserID
|
||||
Time int `json:"AddBlackTimeStamp"` // 添加黑名单的时间
|
||||
}
|
||||
|
||||
// 校验黑名单(请求)
|
||||
checkBlacklistReq struct {
|
||||
UserId string `json:"From_Account"` // (必填)需要校验该 UserID 的黑名单
|
||||
CheckedUserIds []string `json:"To_Account"` // (必填)待校验的黑名单的 UserID 列表,单次请求的 To_Account 数不得超过1000
|
||||
CheckType BlacklistCheckType `json:"CheckType"` // (必填)校验模式
|
||||
}
|
||||
|
||||
// 校验黑名单(响应)
|
||||
checkBlacklistResp struct {
|
||||
types.ActionBaseResp
|
||||
FailUserIds []string `json:"Fail_Account"` // 返回处理失败的用户列表,仅当存在失败用户时才返回该字段
|
||||
Results []*CheckResult `json:"BlackListCheckItem"` // 校验结果对象数组
|
||||
}
|
||||
|
||||
// 添加分组(请求)
|
||||
addGroupsReq struct {
|
||||
UserId string `json:"From_Account"` // (必填)需要为该 UserID 添加新分组
|
||||
GroupNames []string `json:"GroupName"` // (必填)新增分组列表
|
||||
JoinedUserIds []string `json:"To_Account"` // (必填)需要加入新增分组的好友的 UserID 列表
|
||||
}
|
||||
|
||||
// 添加分组(响应)
|
||||
addGroupsResp struct {
|
||||
types.ActionBaseResp
|
||||
ErrorDisplay string `json:"ErrorDisplay"` // 详细的客户端展示信息
|
||||
FailUserIds []string `json:"Fail_Account"` // 返回处理失败的用户列表,仅当存在失败用户时才返回该字段
|
||||
CurrentSequence int `json:"CurrentSequence"` // 返回最新的分组 Sequence
|
||||
Results []*Result `json:"ResultItem"` // 好友加入新增分组的结果对象数组
|
||||
}
|
||||
|
||||
// 删除分组(请求)
|
||||
deleteGroupsReq struct {
|
||||
UserId string `json:"From_Account"` // (必填)需要删除该 UserID 的分组
|
||||
GroupNames []string `json:"GroupName"` // (必填)要删除的分组列表
|
||||
}
|
||||
|
||||
// 删除分组(响应)
|
||||
deleteGroupsResp struct {
|
||||
types.ActionBaseResp
|
||||
CurrentSequence int `json:"CurrentSequence"` // 返回最新的分组 Sequence
|
||||
}
|
||||
|
||||
// 拉取分组(请求)
|
||||
getGroupsReq struct {
|
||||
UserId string `json:"From_Account"` // (必填)指定要拉取分组的用户的 UserID
|
||||
LastSequence int `json:"LastSequence"` // (必填)上一次拉取分组时后台返回给客户端的 Seq,初次拉取时为0,只有 GroupName 为空时有效
|
||||
NeedFriend NeedFriendType `json:"NeedFriend"` // (选填)是否需要拉取分组下的 User 列表
|
||||
GroupNames []string `json:"GroupName"` // (选填)要拉取的分组名称
|
||||
}
|
||||
|
||||
// 拉取分组(响应)
|
||||
getGroupsResp struct {
|
||||
types.ActionBaseResp
|
||||
CurrentSequence int `json:"CurrentSequence"` // 返回最新的分组 Sequence
|
||||
Results []*GroupResult `json:"ResultItem"` // 拉取分组的结果对象数组
|
||||
}
|
||||
|
||||
// GroupResult 分组结果
|
||||
GroupResult struct {
|
||||
GroupName string `json:"GroupName"` // 分组名
|
||||
FriendNumber int `json:"FriendNumber"` // 该分组下的好友数量
|
||||
UserIds []string `json:"To_Account"` // 该分组下的好友的 UserID
|
||||
}
|
||||
)
|
Reference in New Issue
Block a user