diff --git a/internal/api/route.go b/internal/api/route.go index 9d3c5c9f9..abae166ba 100644 --- a/internal/api/route.go +++ b/internal/api/route.go @@ -138,7 +138,8 @@ func newGinRouter(disCov discoveryregistry.SvcDiscoveryRegistry, rdb redis.Unive } r.Use(gin.Recovery(), mw.CorsHandler(), mw.GinParseOperationID()) // init rpc client here - userRpc := rpcclient.NewUser(disCov, config.RpcRegisterName.OpenImUserName, config.RpcRegisterName.OpenImMessageGatewayName) + userRpc := rpcclient.NewUser(disCov, config.RpcRegisterName.OpenImUserName, config.RpcRegisterName.OpenImMessageGatewayName, + &config.Manager, &config.IMAdmin) groupRpc := rpcclient.NewGroup(disCov, config.RpcRegisterName.OpenImGroupName) friendRpc := rpcclient.NewFriend(disCov, config.RpcRegisterName.OpenImFriendName) messageRpc := rpcclient.NewMessage(disCov, config.RpcRegisterName.OpenImMsgName) diff --git a/internal/msggateway/n_ws_server.go b/internal/msggateway/n_ws_server.go index 1fd651f22..b16968fb2 100644 --- a/internal/msggateway/n_ws_server.go +++ b/internal/msggateway/n_ws_server.go @@ -96,7 +96,7 @@ type kickHandler struct { func (ws *WsServer) SetDiscoveryRegistry(disCov discoveryregistry.SvcDiscoveryRegistry, config *config.GlobalConfig) { ws.MessageHandler = NewGrpcHandler(ws.validate, disCov, &config.RpcRegisterName) - u := rpcclient.NewUserRpcClient(disCov, config.RpcRegisterName.OpenImUserName) + u := rpcclient.NewUserRpcClient(disCov, config.RpcRegisterName.OpenImUserName, &config.Manager, &config.IMAdmin) ws.userClient = &u ws.disCov = disCov } diff --git a/internal/rpc/auth/auth.go b/internal/rpc/auth/auth.go index f631e24b1..a031ed38c 100644 --- a/internal/rpc/auth/auth.go +++ b/internal/rpc/auth/auth.go @@ -47,7 +47,7 @@ func Start(config *config.GlobalConfig, client discoveryregistry.SvcDiscoveryReg if err != nil { return err } - userRpcClient := rpcclient.NewUserRpcClient(client, config.RpcRegisterName.OpenImUserName) + userRpcClient := rpcclient.NewUserRpcClient(client, config.RpcRegisterName.OpenImUserName, &config.Manager, &config.IMAdmin) pbauth.RegisterAuthServer(server, &authServer{ userRpcClient: &userRpcClient, RegisterCenter: client, diff --git a/internal/rpc/conversation/conversaion.go b/internal/rpc/conversation/conversaion.go index 453eaac2b..0052e1209 100644 --- a/internal/rpc/conversation/conversaion.go +++ b/internal/rpc/conversation/conversaion.go @@ -63,7 +63,7 @@ func Start(config *config.GlobalConfig, client discoveryregistry.SvcDiscoveryReg } groupRpcClient := rpcclient.NewGroupRpcClient(client, config.RpcRegisterName.OpenImGroupName) msgRpcClient := rpcclient.NewMessageRpcClient(client, config.RpcRegisterName.OpenImMsgName) - userRpcClient := rpcclient.NewUserRpcClient(client, config.RpcRegisterName.OpenImUserName) + userRpcClient := rpcclient.NewUserRpcClient(client, config.RpcRegisterName.OpenImUserName, &config.Manager, &config.IMAdmin) pbconversation.RegisterConversationServer(server, &conversationServer{ msgRpcClient: &msgRpcClient, user: &userRpcClient, diff --git a/internal/rpc/friend/black.go b/internal/rpc/friend/black.go index 67578e948..f934e1dbb 100644 --- a/internal/rpc/friend/black.go +++ b/internal/rpc/friend/black.go @@ -66,7 +66,7 @@ func (s *friendServer) RemoveBlack(ctx context.Context, req *pbfriend.RemoveBlac } func (s *friendServer) AddBlack(ctx context.Context, req *pbfriend.AddBlackReq) (*pbfriend.AddBlackResp, error) { - if err := authverify.CheckAccessV3(ctx, req.OwnerUserID, s.config); err != nil { + if err := authverify.CheckAccessV3(ctx, req.OwnerUserID, &s.config.Manager, &s.config.IMAdmin); err != nil { return nil, err } _, err := s.userRpcClient.GetUsersInfo(ctx, []string{req.OwnerUserID, req.BlackUserID}) diff --git a/internal/rpc/friend/callback.go b/internal/rpc/friend/callback.go index 8db5c5141..c6b4d99c5 100644 --- a/internal/rpc/friend/callback.go +++ b/internal/rpc/friend/callback.go @@ -25,8 +25,8 @@ import ( "github.com/openimsdk/open-im-server/v3/pkg/common/http" ) -func CallbackBeforeAddFriend(ctx context.Context, globalConfig *config.GlobalConfig, req *pbfriend.ApplyToAddFriendReq) error { - if !globalConfig.Callback.CallbackBeforeAddFriend.Enable { +func CallbackBeforeAddFriend(ctx context.Context, callback *config.Callback, req *pbfriend.ApplyToAddFriendReq) error { + if !callback.CallbackBeforeAddFriend.Enable { return nil } cbReq := &cbapi.CallbackBeforeAddFriendReq{ @@ -37,14 +37,14 @@ func CallbackBeforeAddFriend(ctx context.Context, globalConfig *config.GlobalCon Ex: req.Ex, } resp := &cbapi.CallbackBeforeAddFriendResp{} - if err := http.CallBackPostReturn(ctx, globalConfig.Callback.CallbackUrl, cbReq, resp, globalConfig.Callback.CallbackBeforeAddFriend); err != nil { + if err := http.CallBackPostReturn(ctx, callback.CallbackUrl, cbReq, resp, callback.CallbackBeforeAddFriend); err != nil { return err } return nil } -func CallbackBeforeSetFriendRemark(ctx context.Context, globalConfig *config.GlobalConfig, req *pbfriend.SetFriendRemarkReq) error { - if !globalConfig.Callback.CallbackBeforeSetFriendRemark.Enable { +func CallbackBeforeSetFriendRemark(ctx context.Context, callback *config.Callback, req *pbfriend.SetFriendRemarkReq) error { + if !callback.CallbackBeforeSetFriendRemark.Enable { return nil } cbReq := &cbapi.CallbackBeforeSetFriendRemarkReq{ @@ -54,15 +54,15 @@ func CallbackBeforeSetFriendRemark(ctx context.Context, globalConfig *config.Glo Remark: req.Remark, } resp := &cbapi.CallbackBeforeSetFriendRemarkResp{} - if err := http.CallBackPostReturn(ctx, globalConfig.Callback.CallbackUrl, cbReq, resp, globalConfig.Callback.CallbackBeforeAddFriend); err != nil { + if err := http.CallBackPostReturn(ctx, callback.CallbackUrl, cbReq, resp, callback.CallbackBeforeAddFriend); err != nil { return err } utils.NotNilReplace(&req.Remark, &resp.Remark) return nil } -func CallbackAfterSetFriendRemark(ctx context.Context, globalConfig *config.GlobalConfig, req *pbfriend.SetFriendRemarkReq) error { - if !globalConfig.Callback.CallbackAfterSetFriendRemark.Enable { +func CallbackAfterSetFriendRemark(ctx context.Context, callback *config.Callback, req *pbfriend.SetFriendRemarkReq) error { + if !callback.CallbackAfterSetFriendRemark.Enable { return nil } cbReq := &cbapi.CallbackAfterSetFriendRemarkReq{ @@ -72,13 +72,13 @@ func CallbackAfterSetFriendRemark(ctx context.Context, globalConfig *config.Glob Remark: req.Remark, } resp := &cbapi.CallbackAfterSetFriendRemarkResp{} - if err := http.CallBackPostReturn(ctx, globalConfig.Callback.CallbackUrl, cbReq, resp, globalConfig.Callback.CallbackBeforeAddFriend); err != nil { + if err := http.CallBackPostReturn(ctx, callback.CallbackUrl, cbReq, resp, callback.CallbackBeforeAddFriend); err != nil { return err } return nil } -func CallbackBeforeAddBlack(ctx context.Context, globalConfig *config.GlobalConfig, req *pbfriend.AddBlackReq) error { - if !globalConfig.Callback.CallbackBeforeAddBlack.Enable { +func CallbackBeforeAddBlack(ctx context.Context, callback *config.Callback, req *pbfriend.AddBlackReq) error { + if !callback.CallbackBeforeAddBlack.Enable { return nil } cbReq := &cbapi.CallbackBeforeAddBlackReq{ @@ -87,13 +87,13 @@ func CallbackBeforeAddBlack(ctx context.Context, globalConfig *config.GlobalConf BlackUserID: req.BlackUserID, } resp := &cbapi.CallbackBeforeAddBlackResp{} - if err := http.CallBackPostReturn(ctx, globalConfig.Callback.CallbackUrl, cbReq, resp, globalConfig.Callback.CallbackBeforeAddBlack); err != nil { + if err := http.CallBackPostReturn(ctx, callback.CallbackUrl, cbReq, resp, callback.CallbackBeforeAddBlack); err != nil { return err } return nil } -func CallbackAfterAddFriend(ctx context.Context, globalConfig *config.GlobalConfig, req *pbfriend.ApplyToAddFriendReq) error { - if !globalConfig.Callback.CallbackAfterAddFriend.Enable { +func CallbackAfterAddFriend(ctx context.Context, callback *config.Callback, req *pbfriend.ApplyToAddFriendReq) error { + if !callback.CallbackAfterAddFriend.Enable { return nil } cbReq := &cbapi.CallbackAfterAddFriendReq{ @@ -103,14 +103,14 @@ func CallbackAfterAddFriend(ctx context.Context, globalConfig *config.GlobalConf ReqMsg: req.ReqMsg, } resp := &cbapi.CallbackAfterAddFriendResp{} - if err := http.CallBackPostReturn(ctx, globalConfig.Callback.CallbackUrl, cbReq, resp, globalConfig.Callback.CallbackAfterAddFriend); err != nil { + if err := http.CallBackPostReturn(ctx, callback.CallbackUrl, cbReq, resp, callback.CallbackAfterAddFriend); err != nil { return err } return nil } -func CallbackBeforeAddFriendAgree(ctx context.Context, globalConfig *config.GlobalConfig, req *pbfriend.RespondFriendApplyReq) error { - if !globalConfig.Callback.CallbackBeforeAddFriendAgree.Enable { +func CallbackBeforeAddFriendAgree(ctx context.Context, callback *config.Callback, req *pbfriend.RespondFriendApplyReq) error { + if !callback.CallbackBeforeAddFriendAgree.Enable { return nil } cbReq := &cbapi.CallbackBeforeAddFriendAgreeReq{ @@ -121,13 +121,13 @@ func CallbackBeforeAddFriendAgree(ctx context.Context, globalConfig *config.Glob HandleResult: req.HandleResult, } resp := &cbapi.CallbackBeforeAddFriendAgreeResp{} - if err := http.CallBackPostReturn(ctx, globalConfig.Callback.CallbackUrl, cbReq, resp, globalConfig.Callback.CallbackBeforeAddFriendAgree); err != nil { + if err := http.CallBackPostReturn(ctx, callback.CallbackUrl, cbReq, resp, callback.CallbackBeforeAddFriendAgree); err != nil { return err } return nil } -func CallbackAfterDeleteFriend(ctx context.Context, globalConfig *config.GlobalConfig, req *pbfriend.DeleteFriendReq) error { - if !globalConfig.Callback.CallbackAfterDeleteFriend.Enable { +func CallbackAfterDeleteFriend(ctx context.Context, callback *config.Callback, req *pbfriend.DeleteFriendReq) error { + if !callback.CallbackAfterDeleteFriend.Enable { return nil } cbReq := &cbapi.CallbackAfterDeleteFriendReq{ @@ -136,13 +136,13 @@ func CallbackAfterDeleteFriend(ctx context.Context, globalConfig *config.GlobalC FriendUserID: req.FriendUserID, } resp := &cbapi.CallbackAfterDeleteFriendResp{} - if err := http.CallBackPostReturn(ctx, globalConfig.Callback.CallbackUrl, cbReq, resp, globalConfig.Callback.CallbackAfterDeleteFriend); err != nil { + if err := http.CallBackPostReturn(ctx, callback.CallbackUrl, cbReq, resp, callback.CallbackAfterDeleteFriend); err != nil { return err } return nil } -func CallbackBeforeImportFriends(ctx context.Context, globalConfig *config.GlobalConfig, req *pbfriend.ImportFriendReq) error { - if !globalConfig.Callback.CallbackBeforeImportFriends.Enable { +func CallbackBeforeImportFriends(ctx context.Context, callback *config.Callback, req *pbfriend.ImportFriendReq) error { + if !callback.CallbackBeforeImportFriends.Enable { return nil } cbReq := &cbapi.CallbackBeforeImportFriendsReq{ @@ -151,7 +151,7 @@ func CallbackBeforeImportFriends(ctx context.Context, globalConfig *config.Globa FriendUserIDs: req.FriendUserIDs, } resp := &cbapi.CallbackBeforeImportFriendsResp{} - if err := http.CallBackPostReturn(ctx, globalConfig.Callback.CallbackUrl, cbReq, resp, globalConfig.Callback.CallbackBeforeImportFriends); err != nil { + if err := http.CallBackPostReturn(ctx, callback.CallbackUrl, cbReq, resp, callback.CallbackBeforeImportFriends); err != nil { return err } if len(resp.FriendUserIDs) != 0 { @@ -159,8 +159,8 @@ func CallbackBeforeImportFriends(ctx context.Context, globalConfig *config.Globa } return nil } -func CallbackAfterImportFriends(ctx context.Context, globalConfig *config.GlobalConfig, req *pbfriend.ImportFriendReq) error { - if !globalConfig.Callback.CallbackAfterImportFriends.Enable { +func CallbackAfterImportFriends(ctx context.Context, callback *config.Callback, req *pbfriend.ImportFriendReq) error { + if !callback.CallbackAfterImportFriends.Enable { return nil } cbReq := &cbapi.CallbackAfterImportFriendsReq{ @@ -169,14 +169,14 @@ func CallbackAfterImportFriends(ctx context.Context, globalConfig *config.Global FriendUserIDs: req.FriendUserIDs, } resp := &cbapi.CallbackAfterImportFriendsResp{} - if err := http.CallBackPostReturn(ctx, globalConfig.Callback.CallbackUrl, cbReq, resp, globalConfig.Callback.CallbackAfterImportFriends); err != nil { + if err := http.CallBackPostReturn(ctx, callback.CallbackUrl, cbReq, resp, callback.CallbackAfterImportFriends); err != nil { return err } return nil } -func CallbackAfterRemoveBlack(ctx context.Context, globalConfig *config.GlobalConfig, req *pbfriend.RemoveBlackReq) error { - if !globalConfig.Callback.CallbackAfterRemoveBlack.Enable { +func CallbackAfterRemoveBlack(ctx context.Context, callback *config.Callback, req *pbfriend.RemoveBlackReq) error { + if !callback.CallbackAfterRemoveBlack.Enable { return nil } cbReq := &cbapi.CallbackAfterRemoveBlackReq{ @@ -185,7 +185,7 @@ func CallbackAfterRemoveBlack(ctx context.Context, globalConfig *config.GlobalCo BlackUserID: req.BlackUserID, } resp := &cbapi.CallbackAfterRemoveBlackResp{} - if err := http.CallBackPostReturn(ctx, globalConfig.Callback.CallbackUrl, cbReq, resp, globalConfig.Callback.CallbackAfterRemoveBlack); err != nil { + if err := http.CallBackPostReturn(ctx, callback.CallbackUrl, cbReq, resp, callback.CallbackAfterRemoveBlack); err != nil { return err } return nil diff --git a/internal/rpc/friend/friend.go b/internal/rpc/friend/friend.go index f8252cead..c76dddc28 100644 --- a/internal/rpc/friend/friend.go +++ b/internal/rpc/friend/friend.go @@ -22,7 +22,6 @@ import ( "github.com/OpenIMSDK/protocol/sdkws" registry "github.com/OpenIMSDK/tools/discoveryregistry" "github.com/OpenIMSDK/tools/errs" - "github.com/OpenIMSDK/tools/log" "github.com/OpenIMSDK/tools/tx" "github.com/OpenIMSDK/tools/utils" "google.golang.org/grpc" @@ -51,13 +50,13 @@ type friendServer struct { func Start(config *config.GlobalConfig, client registry.SvcDiscoveryRegistry, server *grpc.Server) error { // Initialize MongoDB - mongo, err := unrelation.NewMongo(config) + mongo, err := unrelation.NewMongo(&config.Mongo) if err != nil { return err } // Initialize Redis - rdb, err := cache.NewRedis(config) + rdb, err := cache.NewRedis(&config.Redis) if err != nil { return err } @@ -78,12 +77,12 @@ func Start(config *config.GlobalConfig, client registry.SvcDiscoveryRegistry, se } // Initialize RPC clients - userRpcClient := rpcclient.NewUserRpcClient(client, config) - msgRpcClient := rpcclient.NewMessageRpcClient(client, config) + userRpcClient := rpcclient.NewUserRpcClient(client, config.RpcRegisterName.OpenImUserName, &config.Manager, &config.IMAdmin) + msgRpcClient := rpcclient.NewMessageRpcClient(client, config.RpcRegisterName.OpenImMsgName) // Initialize notification sender notificationSender := notification.NewFriendNotificationSender( - config, + &config.Notification, &msgRpcClient, notification.WithRpcFunc(userRpcClient.GetUsersInfo), ) @@ -102,7 +101,7 @@ func Start(config *config.GlobalConfig, client registry.SvcDiscoveryRegistry, se userRpcClient: &userRpcClient, notificationSender: notificationSender, RegisterCenter: client, - conversationRpcClient: rpcclient.NewConversationRpcClient(client, config), + conversationRpcClient: rpcclient.NewConversationRpcClient(client, config.RpcRegisterName.OpenImConversationName), config: config, }) @@ -112,13 +111,13 @@ func Start(config *config.GlobalConfig, client registry.SvcDiscoveryRegistry, se // ok. func (s *friendServer) ApplyToAddFriend(ctx context.Context, req *pbfriend.ApplyToAddFriendReq) (resp *pbfriend.ApplyToAddFriendResp, err error) { resp = &pbfriend.ApplyToAddFriendResp{} - if err := authverify.CheckAccessV3(ctx, req.FromUserID, s.config); err != nil { + if err := authverify.CheckAccessV3(ctx, req.FromUserID, &s.config.Manager, &s.config.IMAdmin); err != nil { return nil, err } if req.ToUserID == req.FromUserID { return nil, errs.ErrCanNotAddYourself.Wrap("req.ToUserID", req.ToUserID) } - if err = CallbackBeforeAddFriend(ctx, s.config, req); err != nil && err != errs.ErrCallbackContinue { + if err = CallbackBeforeAddFriend(ctx, &s.config.Callback, req); err != nil && err != errs.ErrCallbackContinue { return nil, err } if _, err := s.userRpcClient.GetUsersInfoMap(ctx, []string{req.ToUserID, req.FromUserID}); err != nil { @@ -129,13 +128,13 @@ func (s *friendServer) ApplyToAddFriend(ctx context.Context, req *pbfriend.Apply return nil, err } if in1 && in2 { - return nil, errs.ErrRelationshipAlready.Wrap() + return nil, errs.ErrRelationshipAlready.Wrap("has f") } if err = s.friendDatabase.AddFriendRequest(ctx, req.FromUserID, req.ToUserID, req.ReqMsg, req.Ex); err != nil { return nil, err } s.notificationSender.FriendApplicationAddNotification(ctx, req) - if err = CallbackAfterAddFriend(ctx, s.config, req); err != nil && err != errs.ErrCallbackContinue { + if err = CallbackAfterAddFriend(ctx, &s.config.Callback, req); err != nil && err != errs.ErrCallbackContinue { return nil, err } return resp, nil @@ -143,20 +142,19 @@ func (s *friendServer) ApplyToAddFriend(ctx context.Context, req *pbfriend.Apply // ok. func (s *friendServer) ImportFriends(ctx context.Context, req *pbfriend.ImportFriendReq) (resp *pbfriend.ImportFriendResp, err error) { - defer log.ZInfo(ctx, utils.GetFuncName()+" Return") - if err := authverify.CheckAdmin(ctx, s.config); err != nil { + if err := authverify.CheckAdmin(ctx, &s.config.Manager, &s.config.IMAdmin); err != nil { return nil, err } if _, err := s.userRpcClient.GetUsersInfo(ctx, append([]string{req.OwnerUserID}, req.FriendUserIDs...)); err != nil { return nil, err } if utils.Contain(req.OwnerUserID, req.FriendUserIDs...) { - return nil, errs.ErrCanNotAddYourself.Wrap() + return nil, errs.ErrCanNotAddYourself.Wrap("can not add yourself") } if utils.Duplicate(req.FriendUserIDs) { return nil, errs.ErrArgs.Wrap("friend userID repeated") } - if err := CallbackBeforeImportFriends(ctx, s.config, req); err != nil { + if err := CallbackBeforeImportFriends(ctx, &s.config.Callback, req); err != nil { return nil, err } @@ -170,7 +168,7 @@ func (s *friendServer) ImportFriends(ctx context.Context, req *pbfriend.ImportFr HandleResult: constant.FriendResponseAgree, }) } - if err := CallbackAfterImportFriends(ctx, s.config, req); err != nil { + if err := CallbackAfterImportFriends(ctx, &s.config.Callback, req); err != nil { return nil, err } return &pbfriend.ImportFriendResp{}, nil @@ -178,9 +176,8 @@ func (s *friendServer) ImportFriends(ctx context.Context, req *pbfriend.ImportFr // ok. func (s *friendServer) RespondFriendApply(ctx context.Context, req *pbfriend.RespondFriendApplyReq) (resp *pbfriend.RespondFriendApplyResp, err error) { - defer log.ZInfo(ctx, utils.GetFuncName()+" Return") resp = &pbfriend.RespondFriendApplyResp{} - if err := authverify.CheckAccessV3(ctx, req.ToUserID, s.config); err != nil { + if err := authverify.CheckAccessV3(ctx, req.ToUserID, &s.config.Manager, &s.config.IMAdmin); err != nil { return nil, err } @@ -191,7 +188,7 @@ func (s *friendServer) RespondFriendApply(ctx context.Context, req *pbfriend.Res HandleResult: req.HandleResult, } if req.HandleResult == constant.FriendResponseAgree { - if err := CallbackBeforeAddFriendAgree(ctx, s.config, req); err != nil && err != errs.ErrCallbackContinue { + if err := CallbackBeforeAddFriendAgree(ctx, &s.config.Callback, req); err != nil && err != errs.ErrCallbackContinue { return nil, err } err := s.friendDatabase.AgreeFriendRequest(ctx, &friendRequest) @@ -214,7 +211,6 @@ func (s *friendServer) RespondFriendApply(ctx context.Context, req *pbfriend.Res // ok. 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.userRpcClient.Access(ctx, req.OwnerUserID); err != nil { return nil, err @@ -227,7 +223,7 @@ func (s *friendServer) DeleteFriend(ctx context.Context, req *pbfriend.DeleteFri return nil, err } s.notificationSender.FriendDeletedNotification(ctx, req) - if err := CallbackAfterDeleteFriend(ctx, s.config, req); err != nil { + if err := CallbackAfterDeleteFriend(ctx, &s.config.Callback, req); err != nil { return nil, err } return resp, nil @@ -235,9 +231,8 @@ func (s *friendServer) DeleteFriend(ctx context.Context, req *pbfriend.DeleteFri // ok. func (s *friendServer) SetFriendRemark(ctx context.Context, req *pbfriend.SetFriendRemarkReq) (resp *pbfriend.SetFriendRemarkResp, err error) { - defer log.ZInfo(ctx, utils.GetFuncName()+" Return") - if err = CallbackBeforeSetFriendRemark(ctx, s.config, req); err != nil && err != errs.ErrCallbackContinue { + if err = CallbackBeforeSetFriendRemark(ctx, &s.config.Callback, req); err != nil && err != errs.ErrCallbackContinue { return nil, err } resp = &pbfriend.SetFriendRemarkResp{} @@ -251,7 +246,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 } - if err := CallbackAfterSetFriendRemark(ctx, s.config, req); err != nil && err != errs.ErrCallbackContinue { + if err := CallbackAfterSetFriendRemark(ctx, &s.config.Callback, req); err != nil && err != errs.ErrCallbackContinue { return nil, err } s.notificationSender.FriendRemarkSetNotification(ctx, req.OwnerUserID, req.FriendUserID) @@ -260,7 +255,6 @@ func (s *friendServer) SetFriendRemark(ctx context.Context, req *pbfriend.SetFri // ok. func (s *friendServer) GetDesignatedFriends(ctx context.Context, req *pbfriend.GetDesignatedFriendsReq) (resp *pbfriend.GetDesignatedFriendsResp, err error) { - defer log.ZInfo(ctx, utils.GetFuncName()+" Return") resp = &pbfriend.GetDesignatedFriendsResp{} if utils.Duplicate(req.FriendUserIDs) { return nil, errs.ErrArgs.Wrap("friend userID repeated") @@ -292,7 +286,6 @@ func (s *friendServer) GetDesignatedFriendsApply(ctx context.Context, // Get received friend requests (i.e., those initiated by others). func (s *friendServer) GetPaginationFriendsApplyTo(ctx context.Context, req *pbfriend.GetPaginationFriendsApplyToReq) (resp *pbfriend.GetPaginationFriendsApplyToResp, err error) { - defer log.ZInfo(ctx, utils.GetFuncName()+" Return") if err := s.userRpcClient.Access(ctx, req.UserID); err != nil { return nil, err } @@ -310,7 +303,6 @@ 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.userRpcClient.Access(ctx, req.UserID); err != nil { return nil, err @@ -329,7 +321,6 @@ func (s *friendServer) GetPaginationFriendsApplyFrom(ctx context.Context, req *p // ok. func (s *friendServer) IsFriend(ctx context.Context, req *pbfriend.IsFriendReq) (resp *pbfriend.IsFriendResp, err error) { - defer log.ZInfo(ctx, utils.GetFuncName()+" Return") resp = &pbfriend.IsFriendResp{} resp.InUser1Friends, resp.InUser2Friends, err = s.friendDatabase.CheckIn(ctx, req.UserID1, req.UserID2) if err != nil { @@ -339,7 +330,6 @@ func (s *friendServer) IsFriend(ctx context.Context, req *pbfriend.IsFriendReq) } 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.userRpcClient.Access(ctx, req.UserID); err != nil { return nil, err } @@ -357,7 +347,6 @@ 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.userRpcClient.Access(ctx, req.UserID); err != nil { return nil, err } diff --git a/internal/rpc/group/group.go b/internal/rpc/group/group.go index 3785c7af8..6c672e97a 100644 --- a/internal/rpc/group/group.go +++ b/internal/rpc/group/group.go @@ -73,7 +73,7 @@ func Start(config *config.GlobalConfig, client discoveryregistry.SvcDiscoveryReg if err != nil { return err } - userRpcClient := rpcclient.NewUserRpcClient(client, config) + userRpcClient := rpcclient.NewUserRpcClient(client, config.RpcRegisterName.OpenImUserName, &config.Manager, &config.IMAdmin) msgRpcClient := rpcclient.NewMessageRpcClient(client, config) conversationRpcClient := rpcclient.NewConversationRpcClient(client, config) var gs groupServer diff --git a/internal/rpc/msg/server.go b/internal/rpc/msg/server.go index 9bf61bc5c..cc4792e87 100644 --- a/internal/rpc/msg/server.go +++ b/internal/rpc/msg/server.go @@ -75,7 +75,7 @@ func Start(config *config.GlobalConfig, client discoveryregistry.SvcDiscoveryReg cacheModel := cache.NewMsgCacheModel(rdb, config) msgDocModel := unrelation.NewMsgMongoDriver(mongo.GetDatabase(config.Mongo.Database)) conversationClient := rpcclient.NewConversationRpcClient(client, config) - userRpcClient := rpcclient.NewUserRpcClient(client, config) + userRpcClient := rpcclient.NewUserRpcClient(client, config.RpcRegisterName.OpenImUserName, &config.Manager, &config.IMAdmin) groupRpcClient := rpcclient.NewGroupRpcClient(client, config) friendRpcClient := rpcclient.NewFriendRpcClient(client, config) msgDatabase, err := controller.NewCommonMsgDatabase(msgDocModel, cacheModel, config) diff --git a/internal/rpc/third/third.go b/internal/rpc/third/third.go index b2de51c41..860eaa155 100644 --- a/internal/rpc/third/third.go +++ b/internal/rpc/third/third.go @@ -84,7 +84,7 @@ func Start(config *config.GlobalConfig, client discoveryregistry.SvcDiscoveryReg third.RegisterThirdServer(server, &thirdServer{ apiURL: apiURL, thirdDatabase: controller.NewThirdDatabase(cache.NewMsgCacheModel(rdb, config), logdb), - userRpcClient: rpcclient.NewUserRpcClient(client, config), + userRpcClient: rpcclient.NewUserRpcClient(client, config.RpcRegisterName.OpenImUserName, &config.Manager, &config.IMAdmin), s3dataBase: controller.NewS3Database(rdb, o, s3db), defaultExpire: time.Hour * 24 * 7, config: config, diff --git a/pkg/authverify/token.go b/pkg/authverify/token.go index fa710e947..14328e996 100644 --- a/pkg/authverify/token.go +++ b/pkg/authverify/token.go @@ -33,12 +33,12 @@ func Secret(secret string) jwt.Keyfunc { } } -func CheckAccessV3(ctx context.Context, ownerUserID string, config *config.GlobalConfig) (err error) { +func CheckAccessV3(ctx context.Context, ownerUserID string, manager *config.Manager, imAdmin *config.IMAdmin) (err error) { opUserID := mcontext.GetOpUserID(ctx) - if len(config.Manager.UserID) > 0 && utils.IsContain(opUserID, config.Manager.UserID) { + if len(manager.UserID) > 0 && utils.Contain(opUserID, manager.UserID...) { return nil } - if utils.IsContain(opUserID, config.IMAdmin.UserID) { + if utils.Contain(opUserID, imAdmin.UserID...) { return nil } if opUserID == ownerUserID { @@ -48,24 +48,24 @@ func CheckAccessV3(ctx context.Context, ownerUserID string, config *config.Globa } func IsAppManagerUid(ctx context.Context, manager *config.Manager, imAdmin *config.IMAdmin) bool { - return (len(manager.UserID) > 0 && utils.IsContain(mcontext.GetOpUserID(ctx), manager.UserID)) || - utils.IsContain(mcontext.GetOpUserID(ctx), imAdmin.UserID) + return (len(manager.UserID) > 0 && utils.Contain(mcontext.GetOpUserID(ctx), manager.UserID...)) || + utils.Contain(mcontext.GetOpUserID(ctx), imAdmin.UserID...) } func CheckAdmin(ctx context.Context, manager *config.Manager, imAdmin *config.IMAdmin) error { - if len(manager.UserID) > 0 && utils.IsContain(mcontext.GetOpUserID(ctx), manager.UserID) { + if len(manager.UserID) > 0 && utils.Contain(mcontext.GetOpUserID(ctx), manager.UserID...) { return nil } - if utils.IsContain(mcontext.GetOpUserID(ctx), imAdmin.UserID) { + if utils.Contain(mcontext.GetOpUserID(ctx), imAdmin.UserID...) { return nil } return errs.ErrNoPermission.Wrap(fmt.Sprintf("user %s is not admin userID", mcontext.GetOpUserID(ctx))) } func CheckIMAdmin(ctx context.Context, config *config.GlobalConfig) error { - if utils.IsContain(mcontext.GetOpUserID(ctx), config.IMAdmin.UserID) { + if utils.Contain(mcontext.GetOpUserID(ctx), config.IMAdmin.UserID...) { return nil } - if len(config.Manager.UserID) > 0 && utils.IsContain(mcontext.GetOpUserID(ctx), config.Manager.UserID) { + if len(config.Manager.UserID) > 0 && utils.Contain(mcontext.GetOpUserID(ctx), config.Manager.UserID...) { return nil } return errs.ErrNoPermission.Wrap(fmt.Sprintf("user %s is not CheckIMAdmin userID", mcontext.GetOpUserID(ctx))) @@ -76,7 +76,7 @@ func ParseRedisInterfaceToken(redisToken any, secret string) (*tokenverify.Claim } func IsManagerUserID(opUserID string, manager *config.Manager, imAdmin *config.IMAdmin) bool { - return (len(manager.UserID) > 0 && utils.IsContain(opUserID, manager.UserID)) || utils.IsContain(opUserID, imAdmin.UserID) + return (len(manager.UserID) > 0 && utils.Contain(opUserID, manager.UserID...)) || utils.Contain(opUserID, imAdmin.UserID...) } func WsVerifyToken(token, userID, secret string, platformID int) error { diff --git a/pkg/rpcclient/notification/friend.go b/pkg/rpcclient/notification/friend.go index 78e28be21..19fa41a3c 100644 --- a/pkg/rpcclient/notification/friend.go +++ b/pkg/rpcclient/notification/friend.go @@ -82,12 +82,12 @@ func WithRpcFunc( } func NewFriendNotificationSender( - config *config.GlobalConfig, + conf *config.Notification, msgRpcClient *rpcclient.MessageRpcClient, opts ...friendNotificationSenderOptions, ) *FriendNotificationSender { f := &FriendNotificationSender{ - NotificationSender: rpcclient.NewNotificationSender(config, rpcclient.WithRpcClient(msgRpcClient)), + NotificationSender: rpcclient.NewNotificationSender(conf, rpcclient.WithRpcClient(msgRpcClient)), } for _, opt := range opts { opt(f) diff --git a/pkg/rpcclient/user.go b/pkg/rpcclient/user.go index 5154f6f73..16218e94c 100644 --- a/pkg/rpcclient/user.go +++ b/pkg/rpcclient/user.go @@ -16,6 +16,8 @@ package rpcclient import ( "context" + "github.com/openimsdk/open-im-server/v3/pkg/authverify" + "github.com/openimsdk/open-im-server/v3/pkg/common/config" "strings" "github.com/OpenIMSDK/protocol/sdkws" @@ -34,16 +36,23 @@ type User struct { Client user.UserClient Discov discoveryregistry.SvcDiscoveryRegistry MessageGateWayRpcName string + manager *config.Manager + imAdmin *config.IMAdmin } // NewUser initializes and returns a User instance based on the provided service discovery registry. -func NewUser(discov discoveryregistry.SvcDiscoveryRegistry, rpcRegisterName, messageGateWayRpcName string) *User { +func NewUser(discov discoveryregistry.SvcDiscoveryRegistry, rpcRegisterName, messageGateWayRpcName string, + manager *config.Manager, imAdmin *config.IMAdmin) *User { conn, err := discov.GetConn(context.Background(), rpcRegisterName) if err != nil { util.ExitWithError(err) } client := user.NewUserClient(conn) - return &User{Discov: discov, Client: client, conn: conn, MessageGateWayRpcName: messageGateWayRpcName} + return &User{Discov: discov, Client: client, + conn: conn, + MessageGateWayRpcName: messageGateWayRpcName, + manager: manager, + imAdmin: imAdmin} } // UserRpcClient represents the structure for a User RPC client. @@ -56,8 +65,9 @@ func NewUserRpcClientByUser(user *User) *UserRpcClient { } // NewUserRpcClient initializes a UserRpcClient based on the provided service discovery registry. -func NewUserRpcClient(client discoveryregistry.SvcDiscoveryRegistry, rpcRegisterName string) UserRpcClient { - return UserRpcClient(*NewUser(client, rpcRegisterName, "")) +func NewUserRpcClient(client discoveryregistry.SvcDiscoveryRegistry, rpcRegisterName string, + manager *config.Manager, imAdmin *config.IMAdmin) UserRpcClient { + return UserRpcClient(*NewUser(client, rpcRegisterName, "", manager, imAdmin)) } // GetUsersInfo retrieves information for multiple users based on their user IDs. @@ -154,6 +164,15 @@ func (u *UserRpcClient) GetUserGlobalMsgRecvOpt(ctx context.Context, userID stri return resp.GlobalRecvMsgOpt, nil } +// Access verifies the access rights for the provided user ID. +func (u *UserRpcClient) Access(ctx context.Context, ownerUserID string) error { + _, err := u.GetUserInfo(ctx, ownerUserID) + if err != nil { + return err + } + return authverify.CheckAccessV3(ctx, ownerUserID, u.manager, u.imAdmin) +} + // GetAllUserIDs retrieves all user IDs with pagination options. func (u *UserRpcClient) GetAllUserIDs(ctx context.Context, pageNumber, showNumber int32) ([]string, error) { resp, err := u.Client.GetAllUserID(ctx, &user.GetAllUserIDReq{Pagination: &sdkws.RequestPagination{PageNumber: pageNumber, ShowNumber: showNumber}})