mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-05 01:25:58 +08:00
friend
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
||||
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient"
|
||||
utils "github.com/OpenIMSDK/open_utils"
|
||||
)
|
||||
|
||||
@@ -17,17 +16,59 @@ func FriendPb2DB(friend *sdkws.FriendInfo) *relation.FriendModel {
|
||||
return dbFriend
|
||||
}
|
||||
|
||||
func FriendDB2Pb(ctx context.Context, friendDB *relation.FriendModel, getUser func(ctx context.Context, userIDs []string) ([]rpcclient.CommonUser, error)) (*sdkws.FriendInfo, error) {
|
||||
func FriendDB2Pb(ctx context.Context, friendDB *relation.FriendModel, getUsers func(ctx context.Context, userIDs []string) (map[string]*sdkws.UserInfo, error)) (*sdkws.FriendInfo, error) {
|
||||
pbfriend := &sdkws.FriendInfo{FriendUser: &sdkws.UserInfo{}}
|
||||
utils.CopyStructFields(pbfriend, friendDB)
|
||||
users, err := getUser(ctx, []string{friendDB.FriendUserID})
|
||||
users, err := getUsers(ctx, []string{friendDB.FriendUserID})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pbfriend.FriendUser.UserID = users[0].GetUserID()
|
||||
pbfriend.FriendUser.Nickname = users[0].GetNickname()
|
||||
pbfriend.FriendUser.FaceURL = users[0].GetFaceURL()
|
||||
pbfriend.FriendUser.Ex = users[0].GetEx()
|
||||
pbfriend.FriendUser.UserID = users[friendDB.FriendUserID].UserID
|
||||
pbfriend.FriendUser.Nickname = users[friendDB.FriendUserID].Nickname
|
||||
pbfriend.FriendUser.FaceURL = users[friendDB.FriendUserID].FaceURL
|
||||
pbfriend.FriendUser.Ex = users[friendDB.FriendUserID].Ex
|
||||
pbfriend.CreateTime = friendDB.CreateTime.Unix()
|
||||
return pbfriend, nil
|
||||
}
|
||||
|
||||
func FriendsDB2Pb(ctx context.Context, friendsDB []*relation.FriendModel, getUsers func(ctx context.Context, userIDs []string) (map[string]*sdkws.UserInfo, error)) (friendsPb []*sdkws.FriendInfo, err error) {
|
||||
var userID []string
|
||||
for _, friendDB := range friendsDB {
|
||||
userID = append(userID, friendDB.FriendUserID)
|
||||
}
|
||||
users, err := getUsers(ctx, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, friend := range friendsDB {
|
||||
friendPb := &sdkws.FriendInfo{FriendUser: &sdkws.UserInfo{}}
|
||||
utils.CopyStructFields(friendPb, friend)
|
||||
friendPb.FriendUser.UserID = users[friend.FriendUserID].UserID
|
||||
friendPb.FriendUser.Nickname = users[friend.FriendUserID].Nickname
|
||||
friendPb.FriendUser.FaceURL = users[friend.FriendUserID].FaceURL
|
||||
friendPb.FriendUser.Ex = users[friend.FriendUserID].Ex
|
||||
friendPb.CreateTime = friend.CreateTime.Unix()
|
||||
friendsPb = append(friendsPb, friendPb)
|
||||
}
|
||||
return friendsPb, nil
|
||||
}
|
||||
|
||||
func FriendRequestDB2Pb(ctx context.Context, friendRequests []*relation.FriendRequestModel, getUsers func(ctx context.Context, userIDs []string) (map[string]*sdkws.UserInfo, error)) (PBFriendRequests []*sdkws.FriendRequest, err error) {
|
||||
var userID []string
|
||||
for _, friendRequest := range friendRequests {
|
||||
userID = append(userID, friendRequest.FromUserID)
|
||||
}
|
||||
users, err := getUsers(ctx, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, friendRequest := range friendRequests {
|
||||
friendRequestPb := &sdkws.FriendRequest{}
|
||||
utils.CopyStructFields(friendRequestPb, friendRequest)
|
||||
friendRequestPb.FromFaceURL = users[friendRequest.FromUserID].FaceURL
|
||||
friendRequestPb.FromNickname = users[friendRequest.FromUserID].Nickname
|
||||
friendRequestPb.ToFaceURL = users[friendRequest.ToUserID].FaceURL
|
||||
friendRequestPb.ToNickname = users[friendRequest.ToUserID].Nickname
|
||||
}
|
||||
return PBFriendRequests, nil
|
||||
}
|
||||
|
||||
@@ -24,12 +24,6 @@ type FriendNotificationSender struct {
|
||||
db controller.FriendDatabase
|
||||
}
|
||||
|
||||
func Test_New() {
|
||||
var c controller.UserDatabase
|
||||
noti := NewFriendNotificationSender(client, WithDBFunc(c.FindWithError))
|
||||
noti.BlackAddedNotification(ctx, pb)
|
||||
}
|
||||
|
||||
type friendNotificationSenderOptions func(*FriendNotificationSender)
|
||||
|
||||
func WithDBFunc(fn func(ctx context.Context, userIDs []string) (users []*relationTb.UserModel, err error)) friendNotificationSenderOptions {
|
||||
@@ -190,7 +184,7 @@ func (c *FriendNotificationSender) FriendAddedNotification(ctx context.Context,
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
friendAddedTips.Friend, err = convert.FriendDB2Pb(ctx, friends[0], c.getUsersInfo)
|
||||
friendAddedTips.Friend, err = convert.FriendDB2Pb(ctx, friends[0], c.getUsersInfoMap)
|
||||
c.friendNotification(ctx, fromUserID, toUserID, constant.FriendAddedNotification, &friendAddedTips)
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/tokenverify"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws"
|
||||
@@ -16,8 +17,8 @@ type UserClient struct {
|
||||
MetaClient
|
||||
}
|
||||
|
||||
func NewUserClient(client discoveryregistry.SvcDiscoveryRegistry) *GroupClient {
|
||||
return &GroupClient{
|
||||
func NewUserClient(client discoveryregistry.SvcDiscoveryRegistry) *UserClient {
|
||||
return &UserClient{
|
||||
MetaClient: MetaClient{
|
||||
client: client,
|
||||
rpcRegisterName: config.Config.RpcRegisterName.OpenImUserName,
|
||||
@@ -25,7 +26,7 @@ func NewUserClient(client discoveryregistry.SvcDiscoveryRegistry) *GroupClient {
|
||||
}
|
||||
}
|
||||
|
||||
func (u *UserClient) GetUsersInfos(ctx context.Context, userIDs []string, complete bool) ([]*sdkws.UserInfo, error) {
|
||||
func (u *UserClient) GetUsersInfo(ctx context.Context, userIDs []string) ([]*sdkws.UserInfo, error) {
|
||||
cc, err := u.getConn()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -36,26 +37,24 @@ func (u *UserClient) GetUsersInfos(ctx context.Context, userIDs []string, comple
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if complete {
|
||||
if ids := utils.Single(userIDs, utils.Slice(resp.UsersInfo, func(e *sdkws.UserInfo) string {
|
||||
return e.UserID
|
||||
})); len(ids) > 0 {
|
||||
return nil, errs.ErrUserIDNotFound.Wrap(strings.Join(ids, ","))
|
||||
}
|
||||
if ids := utils.Single(userIDs, utils.Slice(resp.UsersInfo, func(e *sdkws.UserInfo) string {
|
||||
return e.UserID
|
||||
})); len(ids) > 0 {
|
||||
return nil, errs.ErrUserIDNotFound.Wrap(strings.Join(ids, ","))
|
||||
}
|
||||
return resp.UsersInfo, nil
|
||||
}
|
||||
|
||||
func (u *UserClient) GetUserInfo(ctx context.Context, userID string) (*sdkws.UserInfo, error) {
|
||||
users, err := u.GetUsersInfos(ctx, []string{userID}, true)
|
||||
users, err := u.GetUsersInfo(ctx, []string{userID})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return users[0], nil
|
||||
}
|
||||
|
||||
func (u *UserClient) GetUsersInfoMap(ctx context.Context, userIDs []string, complete bool) (map[string]*sdkws.UserInfo, error) {
|
||||
users, err := u.GetUsersInfos(ctx, userIDs, complete)
|
||||
func (u *UserClient) GetUsersInfoMap(ctx context.Context, userIDs []string) (map[string]*sdkws.UserInfo, error) {
|
||||
users, err := u.GetUsersInfo(ctx, userIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -65,7 +64,7 @@ func (u *UserClient) GetUsersInfoMap(ctx context.Context, userIDs []string, comp
|
||||
}
|
||||
|
||||
func (u *UserClient) GetPublicUserInfos(ctx context.Context, userIDs []string, complete bool) ([]*sdkws.PublicUserInfo, error) {
|
||||
users, err := u.GetUsersInfos(ctx, userIDs, complete)
|
||||
users, err := u.GetUsersInfo(ctx, userIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -110,3 +109,11 @@ func (u *UserClient) GetUserGlobalMsgRecvOpt(ctx context.Context, userID string)
|
||||
}
|
||||
return resp.GlobalRecvMsgOpt, err
|
||||
}
|
||||
|
||||
func (u *UserClient) Access(ctx context.Context, ownerUserID string) error {
|
||||
_, err := u.GetUserInfo(ctx, ownerUserID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return tokenverify.CheckAccessV3(ctx, ownerUserID)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user