diff --git a/internal/rpc/friend/black.go b/internal/rpc/friend/black.go index fd840c5a4..f77e797b9 100644 --- a/internal/rpc/friend/black.go +++ b/internal/rpc/friend/black.go @@ -12,7 +12,7 @@ import ( ) func (s *friendServer) GetPaginationBlacks(ctx context.Context, req *pbFriend.GetPaginationBlacksReq) (resp *pbFriend.GetPaginationBlacksResp, err error) { - if err := s.userCheck.Access(ctx, req.UserID); err != nil { + if err := s.userRpcClient.Access(ctx, req.UserID); err != nil { return nil, err } var pageNumber, showNumber int32 @@ -45,13 +45,13 @@ func (s *friendServer) IsBlack(ctx context.Context, req *pbFriend.IsBlackReq) (* } func (s *friendServer) RemoveBlack(ctx context.Context, req *pbFriend.RemoveBlackReq) (*pbFriend.RemoveBlackResp, error) { - if err := s.userCheck.Access(ctx, req.OwnerUserID); err != nil { + if err := s.userRpcClient.Access(ctx, req.OwnerUserID); err != nil { return nil, err } if err := s.blackDatabase.Delete(ctx, []*relation.BlackModel{{OwnerUserID: req.OwnerUserID, BlockUserID: req.BlackUserID}}); err != nil { return nil, err } - s.notification.BlackDeletedNotification(ctx, req) + s.notificationSender.BlackDeletedNotification(ctx, req) return &pbFriend.RemoveBlackResp{}, nil } @@ -59,7 +59,7 @@ func (s *friendServer) AddBlack(ctx context.Context, req *pbFriend.AddBlackReq) if err := tokenverify.CheckAccessV3(ctx, req.OwnerUserID); err != nil { return nil, err } - _, err := s.userCheck.GetUsersInfos(ctx, []string{req.OwnerUserID, req.BlackUserID}, true) + _, err := s.userRpcClient.GetUsersInfo(ctx, []string{req.OwnerUserID, req.BlackUserID}) if err != nil { return nil, err } @@ -67,6 +67,6 @@ func (s *friendServer) AddBlack(ctx context.Context, req *pbFriend.AddBlackReq) if err := s.blackDatabase.Create(ctx, []*relation.BlackModel{&black}); err != nil { return nil, err } - s.notification.BlackAddedNotification(ctx, req) + s.notificationSender.BlackAddedNotification(ctx, req) return &pbFriend.AddBlackResp{}, nil } diff --git a/internal/rpc/friend/friend.go b/internal/rpc/friend/friend.go index 3337b3089..cedc7fb45 100644 --- a/internal/rpc/friend/friend.go +++ b/internal/rpc/friend/friend.go @@ -2,7 +2,9 @@ package friend import ( "context" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/log" + "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache" @@ -14,19 +16,18 @@ import ( registry "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry" "github.com/OpenIMSDK/Open-IM-Server/pkg/errs" pbfriend "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/friend" - "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient/check" "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient/convert" - "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient/notification" + notification "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient/notification2" "github.com/OpenIMSDK/Open-IM-Server/pkg/utils" "google.golang.org/grpc" ) type friendServer struct { - friendDatabase controller.FriendDatabase - blackDatabase controller.BlackDatabase - notification *notification.Check - userCheck *check.UserCheck - RegisterCenter registry.SvcDiscoveryRegistry + friendDatabase controller.FriendDatabase + blackDatabase controller.BlackDatabase + userRpcClient *rpcclient.UserClient + notificationSender *notification.FriendNotificationSender + RegisterCenter registry.SvcDiscoveryRegistry } func Start(client registry.SvcDiscoveryRegistry, server *grpc.Server) error { @@ -43,12 +44,14 @@ func Start(client registry.SvcDiscoveryRegistry, server *grpc.Server) error { } blackDB := relation.NewBlackGorm(db) friendDB := relation.NewFriendGorm(db) + userRpcClient := rpcclient.NewUserClient(client) + notificationSender := notification.NewFriendNotificationSender(client, notification.WithRpcFunc(userRpcClient.GetUsersInfo)) pbfriend.RegisterFriendServer(server, &friendServer{ - friendDatabase: controller.NewFriendDatabase(friendDB, relation.NewFriendRequestGorm(db), cache.NewFriendCacheRedis(rdb, friendDB, cache.GetDefaultOpt()), tx.NewGorm(db)), - blackDatabase: controller.NewBlackDatabase(blackDB, cache.NewBlackCacheRedis(rdb, blackDB, cache.GetDefaultOpt())), - notification: notification.NewCheck(client), - userCheck: check.NewUserCheck(client), - RegisterCenter: client, + friendDatabase: controller.NewFriendDatabase(friendDB, relation.NewFriendRequestGorm(db), cache.NewFriendCacheRedis(rdb, friendDB, cache.GetDefaultOpt()), tx.NewGorm(db)), + blackDatabase: controller.NewBlackDatabase(blackDB, cache.NewBlackCacheRedis(rdb, blackDB, cache.GetDefaultOpt())), + userRpcClient: userRpcClient, + notificationSender: notificationSender, + RegisterCenter: client, }) return nil } @@ -66,7 +69,7 @@ func (s *friendServer) ApplyToAddFriend(ctx context.Context, req *pbfriend.Apply if req.ToUserID == req.FromUserID { return nil, errs.ErrCanNotAddYourself.Wrap() } - if _, err := s.userCheck.GetUsersInfoMap(ctx, []string{req.ToUserID, req.FromUserID}, true); err != nil { + if _, err := s.userRpcClient.GetUsersInfoMap(ctx, []string{req.ToUserID, req.FromUserID}); err != nil { return nil, err } in1, in2, err := s.friendDatabase.CheckIn(ctx, req.FromUserID, req.ToUserID) @@ -79,7 +82,7 @@ func (s *friendServer) ApplyToAddFriend(ctx context.Context, req *pbfriend.Apply if err = s.friendDatabase.AddFriendRequest(ctx, req.FromUserID, req.ToUserID, req.ReqMsg, req.Ex); err != nil { return nil, err } - s.notification.FriendApplicationAddNotification(ctx, req) + s.notificationSender.FriendApplicationAddNotification(ctx, req) return resp, nil } @@ -89,7 +92,7 @@ func (s *friendServer) ImportFriends(ctx context.Context, req *pbfriend.ImportFr if err := tokenverify.CheckAdmin(ctx); err != nil { return nil, err } - if _, err := s.userCheck.GetUsersInfos(ctx, append([]string{req.OwnerUserID}, req.FriendUserIDs...), true); err != nil { + if _, err := s.userRpcClient.GetUsersInfo(ctx, append([]string{req.OwnerUserID}, req.FriendUserIDs...)); err != nil { return nil, err } @@ -120,7 +123,7 @@ func (s *friendServer) RespondFriendApply(ctx context.Context, req *pbfriend.Res if err != nil { return nil, err } - s.notification.FriendApplicationAgreedNotification(ctx, req) + s.notificationSender.FriendApplicationAgreedNotification(ctx, req) return resp, nil } if req.HandleResult == constant.FriendResponseRefuse { @@ -128,7 +131,7 @@ func (s *friendServer) RespondFriendApply(ctx context.Context, req *pbfriend.Res if err != nil { return nil, err } - s.notification.FriendApplicationRefusedNotification(ctx, req) + s.notificationSender.FriendApplicationRefusedNotification(ctx, req) return resp, nil } return nil, errs.ErrArgs.Wrap("req.HandleResult != -1/1") @@ -138,7 +141,7 @@ func (s *friendServer) RespondFriendApply(ctx context.Context, req *pbfriend.Res func (s *friendServer) DeleteFriend(ctx context.Context, req *pbfriend.DeleteFriendReq) (resp *pbfriend.DeleteFriendResp, err error) { defer log.ZInfo(ctx, utils.GetFuncName()+" Return") resp = &pbfriend.DeleteFriendResp{} - if err := s.userCheck.Access(ctx, req.OwnerUserID); err != nil { + if err := s.userRpcClient.Access(ctx, req.OwnerUserID); err != nil { return nil, err } _, err = s.friendDatabase.FindFriendsWithError(ctx, req.OwnerUserID, []string{req.FriendUserID}) @@ -148,7 +151,7 @@ func (s *friendServer) DeleteFriend(ctx context.Context, req *pbfriend.DeleteFri if err := s.friendDatabase.Delete(ctx, req.OwnerUserID, []string{req.FriendUserID}); err != nil { return nil, err } - s.notification.FriendDeletedNotification(ctx, req) + s.notificationSender.FriendDeletedNotification(ctx, req) return resp, nil } @@ -156,7 +159,7 @@ func (s *friendServer) DeleteFriend(ctx context.Context, req *pbfriend.DeleteFri func (s *friendServer) SetFriendRemark(ctx context.Context, req *pbfriend.SetFriendRemarkReq) (resp *pbfriend.SetFriendRemarkResp, err error) { defer log.ZInfo(ctx, utils.GetFuncName()+" Return") resp = &pbfriend.SetFriendRemarkResp{} - if err := s.userCheck.Access(ctx, req.OwnerUserID); err != nil { + if err := s.userRpcClient.Access(ctx, req.OwnerUserID); err != nil { return nil, err } _, err = s.friendDatabase.FindFriendsWithError(ctx, req.OwnerUserID, []string{req.FriendUserID}) @@ -166,7 +169,7 @@ func (s *friendServer) SetFriendRemark(ctx context.Context, req *pbfriend.SetFri if err := s.friendDatabase.UpdateRemark(ctx, req.OwnerUserID, req.FriendUserID, req.Remark); err != nil { return nil, err } - s.notification.FriendRemarkSetNotification(ctx, req.OwnerUserID, req.FriendUserID) + s.notificationSender.FriendRemarkSetNotification(ctx, req.OwnerUserID, req.FriendUserID) return resp, nil } @@ -181,7 +184,7 @@ func (s *friendServer) GetDesignatedFriends(ctx context.Context, req *pbfriend.G if err != nil { return nil, err } - if resp.FriendsInfo, err = (*convert.NewDBFriend(nil, s.RegisterCenter)).DB2PB(ctx, friends); err != nil { + if resp.FriendsInfo, err = convert.FriendsDB2Pb(ctx, friends, s.userRpcClient.GetUsersInfoMap); err != nil { return nil, err } return resp, nil @@ -191,7 +194,7 @@ func (s *friendServer) GetDesignatedFriends(ctx context.Context, req *pbfriend.G func (s *friendServer) GetPaginationFriendsApplyTo(ctx context.Context, req *pbfriend.GetPaginationFriendsApplyToReq) (resp *pbfriend.GetPaginationFriendsApplyToResp, err error) { defer log.ZInfo(ctx, utils.GetFuncName()+" Return") resp = &pbfriend.GetPaginationFriendsApplyToResp{} - if err := s.userCheck.Access(ctx, req.UserID); err != nil { + if err := s.userRpcClient.Access(ctx, req.UserID); err != nil { return nil, err } pageNumber, showNumber := utils.GetPage(req.Pagination) @@ -199,7 +202,7 @@ func (s *friendServer) GetPaginationFriendsApplyTo(ctx context.Context, req *pbf if err != nil { return nil, err } - resp.FriendRequests, err = (*convert.NewDBFriendRequest(nil, s.RegisterCenter)).DB2PB(ctx, friendRequests) + resp.FriendRequests, err = convert.FriendRequestDB2Pb(ctx, friendRequests, s.userRpcClient.GetUsersInfoMap) if err != nil { return nil, err } @@ -211,7 +214,7 @@ func (s *friendServer) GetPaginationFriendsApplyTo(ctx context.Context, req *pbf func (s *friendServer) GetPaginationFriendsApplyFrom(ctx context.Context, req *pbfriend.GetPaginationFriendsApplyFromReq) (resp *pbfriend.GetPaginationFriendsApplyFromResp, err error) { defer log.ZInfo(ctx, utils.GetFuncName()+" Return") resp = &pbfriend.GetPaginationFriendsApplyFromResp{} - if err := s.userCheck.Access(ctx, req.UserID); err != nil { + if err := s.userRpcClient.Access(ctx, req.UserID); err != nil { return nil, err } pageNumber, showNumber := utils.GetPage(req.Pagination) @@ -219,7 +222,7 @@ func (s *friendServer) GetPaginationFriendsApplyFrom(ctx context.Context, req *p if err != nil { return nil, err } - resp.FriendRequests, err = (*convert.NewDBFriendRequest(nil, s.RegisterCenter)).DB2PB(ctx, friendRequests) + resp.FriendRequests, err = convert.FriendRequestDB2Pb(ctx, friendRequests, s.userRpcClient.GetUsersInfoMap) if err != nil { return nil, err } @@ -241,7 +244,7 @@ func (s *friendServer) IsFriend(ctx context.Context, req *pbfriend.IsFriendReq) // ok func (s *friendServer) GetPaginationFriends(ctx context.Context, req *pbfriend.GetPaginationFriendsReq) (resp *pbfriend.GetPaginationFriendsResp, err error) { defer log.ZInfo(ctx, utils.GetFuncName()+" Return") - if err := s.userCheck.Access(ctx, req.UserID); err != nil { + if err := s.userRpcClient.Access(ctx, req.UserID); err != nil { return nil, err } pageNumber, showNumber := utils.GetPage(req.Pagination) @@ -250,7 +253,7 @@ func (s *friendServer) GetPaginationFriends(ctx context.Context, req *pbfriend.G return nil, err } resp = &pbfriend.GetPaginationFriendsResp{} - resp.FriendsInfo, err = (*convert.NewDBFriend(nil, s.RegisterCenter)).DB2PB(ctx, friends) + resp.FriendsInfo, err = convert.FriendsDB2Pb(ctx, friends, s.userRpcClient.GetUsersInfoMap) if err != nil { return nil, err } @@ -260,7 +263,7 @@ func (s *friendServer) GetPaginationFriends(ctx context.Context, req *pbfriend.G func (s *friendServer) GetFriendIDs(ctx context.Context, req *pbfriend.GetFriendIDsReq) (resp *pbfriend.GetFriendIDsResp, err error) { defer log.ZInfo(ctx, utils.GetFuncName()+" Return") - if err := s.userCheck.Access(ctx, req.UserID); err != nil { + if err := s.userRpcClient.Access(ctx, req.UserID); err != nil { return nil, err } resp = &pbfriend.GetFriendIDsResp{} diff --git a/pkg/rpcclient/convert/friend.go b/pkg/rpcclient/convert/friend.go index 904620bac..f18acd416 100644 --- a/pkg/rpcclient/convert/friend.go +++ b/pkg/rpcclient/convert/friend.go @@ -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 +} diff --git a/pkg/rpcclient/notification2/friend.go b/pkg/rpcclient/notification2/friend.go index cc0931fc4..41166b194 100644 --- a/pkg/rpcclient/notification2/friend.go +++ b/pkg/rpcclient/notification2/friend.go @@ -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) } diff --git a/pkg/rpcclient/user_client.go b/pkg/rpcclient/user_client.go index 0ff0370c9..d2465f82f 100644 --- a/pkg/rpcclient/user_client.go +++ b/pkg/rpcclient/user_client.go @@ -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) +}