refactor: friend rpc config change.

pull/2100/head
Gordon 2 years ago
parent e6f1452974
commit f7e5aab805

@ -138,7 +138,8 @@ func newGinRouter(disCov discoveryregistry.SvcDiscoveryRegistry, rdb redis.Unive
} }
r.Use(gin.Recovery(), mw.CorsHandler(), mw.GinParseOperationID()) r.Use(gin.Recovery(), mw.CorsHandler(), mw.GinParseOperationID())
// init rpc client here // 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) groupRpc := rpcclient.NewGroup(disCov, config.RpcRegisterName.OpenImGroupName)
friendRpc := rpcclient.NewFriend(disCov, config.RpcRegisterName.OpenImFriendName) friendRpc := rpcclient.NewFriend(disCov, config.RpcRegisterName.OpenImFriendName)
messageRpc := rpcclient.NewMessage(disCov, config.RpcRegisterName.OpenImMsgName) messageRpc := rpcclient.NewMessage(disCov, config.RpcRegisterName.OpenImMsgName)

@ -96,7 +96,7 @@ type kickHandler struct {
func (ws *WsServer) SetDiscoveryRegistry(disCov discoveryregistry.SvcDiscoveryRegistry, config *config.GlobalConfig) { func (ws *WsServer) SetDiscoveryRegistry(disCov discoveryregistry.SvcDiscoveryRegistry, config *config.GlobalConfig) {
ws.MessageHandler = NewGrpcHandler(ws.validate, disCov, &config.RpcRegisterName) 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.userClient = &u
ws.disCov = disCov ws.disCov = disCov
} }

@ -47,7 +47,7 @@ func Start(config *config.GlobalConfig, client discoveryregistry.SvcDiscoveryReg
if err != nil { if err != nil {
return err return err
} }
userRpcClient := rpcclient.NewUserRpcClient(client, config.RpcRegisterName.OpenImUserName) userRpcClient := rpcclient.NewUserRpcClient(client, config.RpcRegisterName.OpenImUserName, &config.Manager, &config.IMAdmin)
pbauth.RegisterAuthServer(server, &authServer{ pbauth.RegisterAuthServer(server, &authServer{
userRpcClient: &userRpcClient, userRpcClient: &userRpcClient,
RegisterCenter: client, RegisterCenter: client,

@ -63,7 +63,7 @@ func Start(config *config.GlobalConfig, client discoveryregistry.SvcDiscoveryReg
} }
groupRpcClient := rpcclient.NewGroupRpcClient(client, config.RpcRegisterName.OpenImGroupName) groupRpcClient := rpcclient.NewGroupRpcClient(client, config.RpcRegisterName.OpenImGroupName)
msgRpcClient := rpcclient.NewMessageRpcClient(client, config.RpcRegisterName.OpenImMsgName) 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{ pbconversation.RegisterConversationServer(server, &conversationServer{
msgRpcClient: &msgRpcClient, msgRpcClient: &msgRpcClient,
user: &userRpcClient, user: &userRpcClient,

@ -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) { 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 return nil, err
} }
_, err := s.userRpcClient.GetUsersInfo(ctx, []string{req.OwnerUserID, req.BlackUserID}) _, err := s.userRpcClient.GetUsersInfo(ctx, []string{req.OwnerUserID, req.BlackUserID})

@ -25,8 +25,8 @@ import (
"github.com/openimsdk/open-im-server/v3/pkg/common/http" "github.com/openimsdk/open-im-server/v3/pkg/common/http"
) )
func CallbackBeforeAddFriend(ctx context.Context, globalConfig *config.GlobalConfig, req *pbfriend.ApplyToAddFriendReq) error { func CallbackBeforeAddFriend(ctx context.Context, callback *config.Callback, req *pbfriend.ApplyToAddFriendReq) error {
if !globalConfig.Callback.CallbackBeforeAddFriend.Enable { if !callback.CallbackBeforeAddFriend.Enable {
return nil return nil
} }
cbReq := &cbapi.CallbackBeforeAddFriendReq{ cbReq := &cbapi.CallbackBeforeAddFriendReq{
@ -37,14 +37,14 @@ func CallbackBeforeAddFriend(ctx context.Context, globalConfig *config.GlobalCon
Ex: req.Ex, Ex: req.Ex,
} }
resp := &cbapi.CallbackBeforeAddFriendResp{} 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 err
} }
return nil return nil
} }
func CallbackBeforeSetFriendRemark(ctx context.Context, globalConfig *config.GlobalConfig, req *pbfriend.SetFriendRemarkReq) error { func CallbackBeforeSetFriendRemark(ctx context.Context, callback *config.Callback, req *pbfriend.SetFriendRemarkReq) error {
if !globalConfig.Callback.CallbackBeforeSetFriendRemark.Enable { if !callback.CallbackBeforeSetFriendRemark.Enable {
return nil return nil
} }
cbReq := &cbapi.CallbackBeforeSetFriendRemarkReq{ cbReq := &cbapi.CallbackBeforeSetFriendRemarkReq{
@ -54,15 +54,15 @@ func CallbackBeforeSetFriendRemark(ctx context.Context, globalConfig *config.Glo
Remark: req.Remark, Remark: req.Remark,
} }
resp := &cbapi.CallbackBeforeSetFriendRemarkResp{} 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 return err
} }
utils.NotNilReplace(&req.Remark, &resp.Remark) utils.NotNilReplace(&req.Remark, &resp.Remark)
return nil return nil
} }
func CallbackAfterSetFriendRemark(ctx context.Context, globalConfig *config.GlobalConfig, req *pbfriend.SetFriendRemarkReq) error { func CallbackAfterSetFriendRemark(ctx context.Context, callback *config.Callback, req *pbfriend.SetFriendRemarkReq) error {
if !globalConfig.Callback.CallbackAfterSetFriendRemark.Enable { if !callback.CallbackAfterSetFriendRemark.Enable {
return nil return nil
} }
cbReq := &cbapi.CallbackAfterSetFriendRemarkReq{ cbReq := &cbapi.CallbackAfterSetFriendRemarkReq{
@ -72,13 +72,13 @@ func CallbackAfterSetFriendRemark(ctx context.Context, globalConfig *config.Glob
Remark: req.Remark, Remark: req.Remark,
} }
resp := &cbapi.CallbackAfterSetFriendRemarkResp{} 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 err
} }
return nil return nil
} }
func CallbackBeforeAddBlack(ctx context.Context, globalConfig *config.GlobalConfig, req *pbfriend.AddBlackReq) error { func CallbackBeforeAddBlack(ctx context.Context, callback *config.Callback, req *pbfriend.AddBlackReq) error {
if !globalConfig.Callback.CallbackBeforeAddBlack.Enable { if !callback.CallbackBeforeAddBlack.Enable {
return nil return nil
} }
cbReq := &cbapi.CallbackBeforeAddBlackReq{ cbReq := &cbapi.CallbackBeforeAddBlackReq{
@ -87,13 +87,13 @@ func CallbackBeforeAddBlack(ctx context.Context, globalConfig *config.GlobalConf
BlackUserID: req.BlackUserID, BlackUserID: req.BlackUserID,
} }
resp := &cbapi.CallbackBeforeAddBlackResp{} 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 err
} }
return nil return nil
} }
func CallbackAfterAddFriend(ctx context.Context, globalConfig *config.GlobalConfig, req *pbfriend.ApplyToAddFriendReq) error { func CallbackAfterAddFriend(ctx context.Context, callback *config.Callback, req *pbfriend.ApplyToAddFriendReq) error {
if !globalConfig.Callback.CallbackAfterAddFriend.Enable { if !callback.CallbackAfterAddFriend.Enable {
return nil return nil
} }
cbReq := &cbapi.CallbackAfterAddFriendReq{ cbReq := &cbapi.CallbackAfterAddFriendReq{
@ -103,14 +103,14 @@ func CallbackAfterAddFriend(ctx context.Context, globalConfig *config.GlobalConf
ReqMsg: req.ReqMsg, ReqMsg: req.ReqMsg,
} }
resp := &cbapi.CallbackAfterAddFriendResp{} 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 err
} }
return nil return nil
} }
func CallbackBeforeAddFriendAgree(ctx context.Context, globalConfig *config.GlobalConfig, req *pbfriend.RespondFriendApplyReq) error { func CallbackBeforeAddFriendAgree(ctx context.Context, callback *config.Callback, req *pbfriend.RespondFriendApplyReq) error {
if !globalConfig.Callback.CallbackBeforeAddFriendAgree.Enable { if !callback.CallbackBeforeAddFriendAgree.Enable {
return nil return nil
} }
cbReq := &cbapi.CallbackBeforeAddFriendAgreeReq{ cbReq := &cbapi.CallbackBeforeAddFriendAgreeReq{
@ -121,13 +121,13 @@ func CallbackBeforeAddFriendAgree(ctx context.Context, globalConfig *config.Glob
HandleResult: req.HandleResult, HandleResult: req.HandleResult,
} }
resp := &cbapi.CallbackBeforeAddFriendAgreeResp{} 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 err
} }
return nil return nil
} }
func CallbackAfterDeleteFriend(ctx context.Context, globalConfig *config.GlobalConfig, req *pbfriend.DeleteFriendReq) error { func CallbackAfterDeleteFriend(ctx context.Context, callback *config.Callback, req *pbfriend.DeleteFriendReq) error {
if !globalConfig.Callback.CallbackAfterDeleteFriend.Enable { if !callback.CallbackAfterDeleteFriend.Enable {
return nil return nil
} }
cbReq := &cbapi.CallbackAfterDeleteFriendReq{ cbReq := &cbapi.CallbackAfterDeleteFriendReq{
@ -136,13 +136,13 @@ func CallbackAfterDeleteFriend(ctx context.Context, globalConfig *config.GlobalC
FriendUserID: req.FriendUserID, FriendUserID: req.FriendUserID,
} }
resp := &cbapi.CallbackAfterDeleteFriendResp{} 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 err
} }
return nil return nil
} }
func CallbackBeforeImportFriends(ctx context.Context, globalConfig *config.GlobalConfig, req *pbfriend.ImportFriendReq) error { func CallbackBeforeImportFriends(ctx context.Context, callback *config.Callback, req *pbfriend.ImportFriendReq) error {
if !globalConfig.Callback.CallbackBeforeImportFriends.Enable { if !callback.CallbackBeforeImportFriends.Enable {
return nil return nil
} }
cbReq := &cbapi.CallbackBeforeImportFriendsReq{ cbReq := &cbapi.CallbackBeforeImportFriendsReq{
@ -151,7 +151,7 @@ func CallbackBeforeImportFriends(ctx context.Context, globalConfig *config.Globa
FriendUserIDs: req.FriendUserIDs, FriendUserIDs: req.FriendUserIDs,
} }
resp := &cbapi.CallbackBeforeImportFriendsResp{} 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 return err
} }
if len(resp.FriendUserIDs) != 0 { if len(resp.FriendUserIDs) != 0 {
@ -159,8 +159,8 @@ func CallbackBeforeImportFriends(ctx context.Context, globalConfig *config.Globa
} }
return nil return nil
} }
func CallbackAfterImportFriends(ctx context.Context, globalConfig *config.GlobalConfig, req *pbfriend.ImportFriendReq) error { func CallbackAfterImportFriends(ctx context.Context, callback *config.Callback, req *pbfriend.ImportFriendReq) error {
if !globalConfig.Callback.CallbackAfterImportFriends.Enable { if !callback.CallbackAfterImportFriends.Enable {
return nil return nil
} }
cbReq := &cbapi.CallbackAfterImportFriendsReq{ cbReq := &cbapi.CallbackAfterImportFriendsReq{
@ -169,14 +169,14 @@ func CallbackAfterImportFriends(ctx context.Context, globalConfig *config.Global
FriendUserIDs: req.FriendUserIDs, FriendUserIDs: req.FriendUserIDs,
} }
resp := &cbapi.CallbackAfterImportFriendsResp{} 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 err
} }
return nil return nil
} }
func CallbackAfterRemoveBlack(ctx context.Context, globalConfig *config.GlobalConfig, req *pbfriend.RemoveBlackReq) error { func CallbackAfterRemoveBlack(ctx context.Context, callback *config.Callback, req *pbfriend.RemoveBlackReq) error {
if !globalConfig.Callback.CallbackAfterRemoveBlack.Enable { if !callback.CallbackAfterRemoveBlack.Enable {
return nil return nil
} }
cbReq := &cbapi.CallbackAfterRemoveBlackReq{ cbReq := &cbapi.CallbackAfterRemoveBlackReq{
@ -185,7 +185,7 @@ func CallbackAfterRemoveBlack(ctx context.Context, globalConfig *config.GlobalCo
BlackUserID: req.BlackUserID, BlackUserID: req.BlackUserID,
} }
resp := &cbapi.CallbackAfterRemoveBlackResp{} 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 err
} }
return nil return nil

@ -22,7 +22,6 @@ import (
"github.com/OpenIMSDK/protocol/sdkws" "github.com/OpenIMSDK/protocol/sdkws"
registry "github.com/OpenIMSDK/tools/discoveryregistry" registry "github.com/OpenIMSDK/tools/discoveryregistry"
"github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/errs"
"github.com/OpenIMSDK/tools/log"
"github.com/OpenIMSDK/tools/tx" "github.com/OpenIMSDK/tools/tx"
"github.com/OpenIMSDK/tools/utils" "github.com/OpenIMSDK/tools/utils"
"google.golang.org/grpc" "google.golang.org/grpc"
@ -51,13 +50,13 @@ type friendServer struct {
func Start(config *config.GlobalConfig, client registry.SvcDiscoveryRegistry, server *grpc.Server) error { func Start(config *config.GlobalConfig, client registry.SvcDiscoveryRegistry, server *grpc.Server) error {
// Initialize MongoDB // Initialize MongoDB
mongo, err := unrelation.NewMongo(config) mongo, err := unrelation.NewMongo(&config.Mongo)
if err != nil { if err != nil {
return err return err
} }
// Initialize Redis // Initialize Redis
rdb, err := cache.NewRedis(config) rdb, err := cache.NewRedis(&config.Redis)
if err != nil { if err != nil {
return err return err
} }
@ -78,12 +77,12 @@ func Start(config *config.GlobalConfig, client registry.SvcDiscoveryRegistry, se
} }
// Initialize RPC clients // Initialize RPC clients
userRpcClient := rpcclient.NewUserRpcClient(client, config) userRpcClient := rpcclient.NewUserRpcClient(client, config.RpcRegisterName.OpenImUserName, &config.Manager, &config.IMAdmin)
msgRpcClient := rpcclient.NewMessageRpcClient(client, config) msgRpcClient := rpcclient.NewMessageRpcClient(client, config.RpcRegisterName.OpenImMsgName)
// Initialize notification sender // Initialize notification sender
notificationSender := notification.NewFriendNotificationSender( notificationSender := notification.NewFriendNotificationSender(
config, &config.Notification,
&msgRpcClient, &msgRpcClient,
notification.WithRpcFunc(userRpcClient.GetUsersInfo), notification.WithRpcFunc(userRpcClient.GetUsersInfo),
) )
@ -102,7 +101,7 @@ func Start(config *config.GlobalConfig, client registry.SvcDiscoveryRegistry, se
userRpcClient: &userRpcClient, userRpcClient: &userRpcClient,
notificationSender: notificationSender, notificationSender: notificationSender,
RegisterCenter: client, RegisterCenter: client,
conversationRpcClient: rpcclient.NewConversationRpcClient(client, config), conversationRpcClient: rpcclient.NewConversationRpcClient(client, config.RpcRegisterName.OpenImConversationName),
config: config, config: config,
}) })
@ -112,13 +111,13 @@ func Start(config *config.GlobalConfig, client registry.SvcDiscoveryRegistry, se
// ok. // ok.
func (s *friendServer) ApplyToAddFriend(ctx context.Context, req *pbfriend.ApplyToAddFriendReq) (resp *pbfriend.ApplyToAddFriendResp, err error) { func (s *friendServer) ApplyToAddFriend(ctx context.Context, req *pbfriend.ApplyToAddFriendReq) (resp *pbfriend.ApplyToAddFriendResp, err error) {
resp = &pbfriend.ApplyToAddFriendResp{} 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 return nil, err
} }
if req.ToUserID == req.FromUserID { if req.ToUserID == req.FromUserID {
return nil, errs.ErrCanNotAddYourself.Wrap("req.ToUserID", req.ToUserID) 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 return nil, err
} }
if _, err := s.userRpcClient.GetUsersInfoMap(ctx, []string{req.ToUserID, req.FromUserID}); err != nil { 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 return nil, err
} }
if in1 && in2 { 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 { if err = s.friendDatabase.AddFriendRequest(ctx, req.FromUserID, req.ToUserID, req.ReqMsg, req.Ex); err != nil {
return nil, err return nil, err
} }
s.notificationSender.FriendApplicationAddNotification(ctx, req) 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 nil, err
} }
return resp, nil return resp, nil
@ -143,20 +142,19 @@ func (s *friendServer) ApplyToAddFriend(ctx context.Context, req *pbfriend.Apply
// ok. // ok.
func (s *friendServer) ImportFriends(ctx context.Context, req *pbfriend.ImportFriendReq) (resp *pbfriend.ImportFriendResp, err error) { 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.Manager, &s.config.IMAdmin); err != nil {
if err := authverify.CheckAdmin(ctx, s.config); err != nil {
return nil, err return nil, err
} }
if _, err := s.userRpcClient.GetUsersInfo(ctx, append([]string{req.OwnerUserID}, req.FriendUserIDs...)); err != nil { if _, err := s.userRpcClient.GetUsersInfo(ctx, append([]string{req.OwnerUserID}, req.FriendUserIDs...)); err != nil {
return nil, err return nil, err
} }
if utils.Contain(req.OwnerUserID, req.FriendUserIDs...) { 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) { if utils.Duplicate(req.FriendUserIDs) {
return nil, errs.ErrArgs.Wrap("friend userID repeated") 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 return nil, err
} }
@ -170,7 +168,7 @@ func (s *friendServer) ImportFriends(ctx context.Context, req *pbfriend.ImportFr
HandleResult: constant.FriendResponseAgree, 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 nil, err
} }
return &pbfriend.ImportFriendResp{}, nil return &pbfriend.ImportFriendResp{}, nil
@ -178,9 +176,8 @@ func (s *friendServer) ImportFriends(ctx context.Context, req *pbfriend.ImportFr
// ok. // ok.
func (s *friendServer) RespondFriendApply(ctx context.Context, req *pbfriend.RespondFriendApplyReq) (resp *pbfriend.RespondFriendApplyResp, err error) { 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{} 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 return nil, err
} }
@ -191,7 +188,7 @@ func (s *friendServer) RespondFriendApply(ctx context.Context, req *pbfriend.Res
HandleResult: req.HandleResult, HandleResult: req.HandleResult,
} }
if req.HandleResult == constant.FriendResponseAgree { 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 return nil, err
} }
err := s.friendDatabase.AgreeFriendRequest(ctx, &friendRequest) err := s.friendDatabase.AgreeFriendRequest(ctx, &friendRequest)
@ -214,7 +211,6 @@ func (s *friendServer) RespondFriendApply(ctx context.Context, req *pbfriend.Res
// ok. // ok.
func (s *friendServer) DeleteFriend(ctx context.Context, req *pbfriend.DeleteFriendReq) (resp *pbfriend.DeleteFriendResp, err error) { 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{} resp = &pbfriend.DeleteFriendResp{}
if err := s.userRpcClient.Access(ctx, req.OwnerUserID); err != nil { if err := s.userRpcClient.Access(ctx, req.OwnerUserID); err != nil {
return nil, err return nil, err
@ -227,7 +223,7 @@ func (s *friendServer) DeleteFriend(ctx context.Context, req *pbfriend.DeleteFri
return nil, err return nil, err
} }
s.notificationSender.FriendDeletedNotification(ctx, req) 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 nil, err
} }
return resp, nil return resp, nil
@ -235,9 +231,8 @@ func (s *friendServer) DeleteFriend(ctx context.Context, req *pbfriend.DeleteFri
// ok. // ok.
func (s *friendServer) SetFriendRemark(ctx context.Context, req *pbfriend.SetFriendRemarkReq) (resp *pbfriend.SetFriendRemarkResp, err error) { 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 return nil, err
} }
resp = &pbfriend.SetFriendRemarkResp{} 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 { if err := s.friendDatabase.UpdateRemark(ctx, req.OwnerUserID, req.FriendUserID, req.Remark); err != nil {
return nil, err 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 return nil, err
} }
s.notificationSender.FriendRemarkSetNotification(ctx, req.OwnerUserID, req.FriendUserID) s.notificationSender.FriendRemarkSetNotification(ctx, req.OwnerUserID, req.FriendUserID)
@ -260,7 +255,6 @@ func (s *friendServer) SetFriendRemark(ctx context.Context, req *pbfriend.SetFri
// ok. // ok.
func (s *friendServer) GetDesignatedFriends(ctx context.Context, req *pbfriend.GetDesignatedFriendsReq) (resp *pbfriend.GetDesignatedFriendsResp, err error) { 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{} resp = &pbfriend.GetDesignatedFriendsResp{}
if utils.Duplicate(req.FriendUserIDs) { if utils.Duplicate(req.FriendUserIDs) {
return nil, errs.ErrArgs.Wrap("friend userID repeated") 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). // 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) { 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 { if err := s.userRpcClient.Access(ctx, req.UserID); err != nil {
return nil, err 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) { 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{} resp = &pbfriend.GetPaginationFriendsApplyFromResp{}
if err := s.userRpcClient.Access(ctx, req.UserID); err != nil { if err := s.userRpcClient.Access(ctx, req.UserID); err != nil {
return nil, err return nil, err
@ -329,7 +321,6 @@ func (s *friendServer) GetPaginationFriendsApplyFrom(ctx context.Context, req *p
// ok. // ok.
func (s *friendServer) IsFriend(ctx context.Context, req *pbfriend.IsFriendReq) (resp *pbfriend.IsFriendResp, err error) { 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 = &pbfriend.IsFriendResp{}
resp.InUser1Friends, resp.InUser2Friends, err = s.friendDatabase.CheckIn(ctx, req.UserID1, req.UserID2) resp.InUser1Friends, resp.InUser2Friends, err = s.friendDatabase.CheckIn(ctx, req.UserID1, req.UserID2)
if err != nil { 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) { 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 { if err := s.userRpcClient.Access(ctx, req.UserID); err != nil {
return nil, err 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) { 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 { if err := s.userRpcClient.Access(ctx, req.UserID); err != nil {
return nil, err return nil, err
} }

@ -73,7 +73,7 @@ func Start(config *config.GlobalConfig, client discoveryregistry.SvcDiscoveryReg
if err != nil { if err != nil {
return err return err
} }
userRpcClient := rpcclient.NewUserRpcClient(client, config) userRpcClient := rpcclient.NewUserRpcClient(client, config.RpcRegisterName.OpenImUserName, &config.Manager, &config.IMAdmin)
msgRpcClient := rpcclient.NewMessageRpcClient(client, config) msgRpcClient := rpcclient.NewMessageRpcClient(client, config)
conversationRpcClient := rpcclient.NewConversationRpcClient(client, config) conversationRpcClient := rpcclient.NewConversationRpcClient(client, config)
var gs groupServer var gs groupServer

@ -75,7 +75,7 @@ func Start(config *config.GlobalConfig, client discoveryregistry.SvcDiscoveryReg
cacheModel := cache.NewMsgCacheModel(rdb, config) cacheModel := cache.NewMsgCacheModel(rdb, config)
msgDocModel := unrelation.NewMsgMongoDriver(mongo.GetDatabase(config.Mongo.Database)) msgDocModel := unrelation.NewMsgMongoDriver(mongo.GetDatabase(config.Mongo.Database))
conversationClient := rpcclient.NewConversationRpcClient(client, config) 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) groupRpcClient := rpcclient.NewGroupRpcClient(client, config)
friendRpcClient := rpcclient.NewFriendRpcClient(client, config) friendRpcClient := rpcclient.NewFriendRpcClient(client, config)
msgDatabase, err := controller.NewCommonMsgDatabase(msgDocModel, cacheModel, config) msgDatabase, err := controller.NewCommonMsgDatabase(msgDocModel, cacheModel, config)

@ -84,7 +84,7 @@ func Start(config *config.GlobalConfig, client discoveryregistry.SvcDiscoveryReg
third.RegisterThirdServer(server, &thirdServer{ third.RegisterThirdServer(server, &thirdServer{
apiURL: apiURL, apiURL: apiURL,
thirdDatabase: controller.NewThirdDatabase(cache.NewMsgCacheModel(rdb, config), logdb), 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), s3dataBase: controller.NewS3Database(rdb, o, s3db),
defaultExpire: time.Hour * 24 * 7, defaultExpire: time.Hour * 24 * 7,
config: config, config: config,

@ -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) 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 return nil
} }
if utils.IsContain(opUserID, config.IMAdmin.UserID) { if utils.Contain(opUserID, imAdmin.UserID...) {
return nil return nil
} }
if opUserID == ownerUserID { 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 { func IsAppManagerUid(ctx context.Context, manager *config.Manager, imAdmin *config.IMAdmin) bool {
return (len(manager.UserID) > 0 && utils.IsContain(mcontext.GetOpUserID(ctx), manager.UserID)) || return (len(manager.UserID) > 0 && utils.Contain(mcontext.GetOpUserID(ctx), manager.UserID...)) ||
utils.IsContain(mcontext.GetOpUserID(ctx), imAdmin.UserID) utils.Contain(mcontext.GetOpUserID(ctx), imAdmin.UserID...)
} }
func CheckAdmin(ctx context.Context, manager *config.Manager, imAdmin *config.IMAdmin) error { 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 return nil
} }
if utils.IsContain(mcontext.GetOpUserID(ctx), imAdmin.UserID) { if utils.Contain(mcontext.GetOpUserID(ctx), imAdmin.UserID...) {
return nil return nil
} }
return errs.ErrNoPermission.Wrap(fmt.Sprintf("user %s is not admin userID", mcontext.GetOpUserID(ctx))) return errs.ErrNoPermission.Wrap(fmt.Sprintf("user %s is not admin userID", mcontext.GetOpUserID(ctx)))
} }
func CheckIMAdmin(ctx context.Context, config *config.GlobalConfig) error { 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 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 nil
} }
return errs.ErrNoPermission.Wrap(fmt.Sprintf("user %s is not CheckIMAdmin userID", mcontext.GetOpUserID(ctx))) 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 { 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 { func WsVerifyToken(token, userID, secret string, platformID int) error {

@ -82,12 +82,12 @@ func WithRpcFunc(
} }
func NewFriendNotificationSender( func NewFriendNotificationSender(
config *config.GlobalConfig, conf *config.Notification,
msgRpcClient *rpcclient.MessageRpcClient, msgRpcClient *rpcclient.MessageRpcClient,
opts ...friendNotificationSenderOptions, opts ...friendNotificationSenderOptions,
) *FriendNotificationSender { ) *FriendNotificationSender {
f := &FriendNotificationSender{ f := &FriendNotificationSender{
NotificationSender: rpcclient.NewNotificationSender(config, rpcclient.WithRpcClient(msgRpcClient)), NotificationSender: rpcclient.NewNotificationSender(conf, rpcclient.WithRpcClient(msgRpcClient)),
} }
for _, opt := range opts { for _, opt := range opts {
opt(f) opt(f)

@ -16,6 +16,8 @@ package rpcclient
import ( import (
"context" "context"
"github.com/openimsdk/open-im-server/v3/pkg/authverify"
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
"strings" "strings"
"github.com/OpenIMSDK/protocol/sdkws" "github.com/OpenIMSDK/protocol/sdkws"
@ -34,16 +36,23 @@ type User struct {
Client user.UserClient Client user.UserClient
Discov discoveryregistry.SvcDiscoveryRegistry Discov discoveryregistry.SvcDiscoveryRegistry
MessageGateWayRpcName string MessageGateWayRpcName string
manager *config.Manager
imAdmin *config.IMAdmin
} }
// NewUser initializes and returns a User instance based on the provided service discovery registry. // 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) conn, err := discov.GetConn(context.Background(), rpcRegisterName)
if err != nil { if err != nil {
util.ExitWithError(err) util.ExitWithError(err)
} }
client := user.NewUserClient(conn) 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. // 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. // NewUserRpcClient initializes a UserRpcClient based on the provided service discovery registry.
func NewUserRpcClient(client discoveryregistry.SvcDiscoveryRegistry, rpcRegisterName string) UserRpcClient { func NewUserRpcClient(client discoveryregistry.SvcDiscoveryRegistry, rpcRegisterName string,
return UserRpcClient(*NewUser(client, rpcRegisterName, "")) 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. // 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 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. // GetAllUserIDs retrieves all user IDs with pagination options.
func (u *UserRpcClient) GetAllUserIDs(ctx context.Context, pageNumber, showNumber int32) ([]string, error) { 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}}) resp, err := u.Client.GetAllUserID(ctx, &user.GetAllUserIDReq{Pagination: &sdkws.RequestPagination{PageNumber: pageNumber, ShowNumber: showNumber}})

Loading…
Cancel
Save