diff --git a/config/config.yaml b/config/config.yaml index 966c613f4..474832f34 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -345,7 +345,7 @@ callback: enable: false callbackTimeOut: 2 callbackFailedContinue: true # 回调超时是否继续 - callbackBeforeExtendMsgModify: + callbackBeforeSetGroupMemberInfo: enable: false callbackTimeOut: 2 callbackFailedContinue: true # 回调超时是否继续 diff --git a/internal/rpc/group/callback.go b/internal/rpc/group/callback.go index 90dc63af0..5bf450a69 100644 --- a/internal/rpc/group/callback.go +++ b/internal/rpc/group/callback.go @@ -10,6 +10,8 @@ import ( pbGroup "Open_IM/pkg/proto/group" "Open_IM/pkg/utils" http2 "net/http" + + "google.golang.org/protobuf/types/known/wrapperspb" ) func callbackBeforeCreateGroup(req *pbGroup.CreateGroupReq) cbApi.CommonCallbackResp { @@ -126,3 +128,56 @@ func CallbackBeforeMemberJoinGroup(operationID string, groupMember *db.GroupMemb } return callbackResp } + +func CallbackBeforeSetGroupMemberInfo(req *pbGroup.SetGroupMemberInfoReq) cbApi.CommonCallbackResp { + callbackResp := cbApi.CommonCallbackResp{OperationID: req.OperationID} + if !config.Config.Callback.CallbackBeforeSetGroupMemberInfo.Enable { + return callbackResp + } + callbackReq := cbApi.CallbackBeforeSetGroupMemberInfoReq{ + CallbackCommand: constant.CallbackBeforeSetGroupMemberInfoCommand, + OperationID: req.OperationID, + GroupID: req.GroupID, + UserID: req.UserID, + } + if req.Nickname != nil { + callbackReq.Nickname = req.Nickname.Value + } + if req.FaceURL != nil { + callbackReq.FaceURL = req.FaceURL.Value + } + if req.RoleLevel != nil { + callbackReq.RoleLevel = req.RoleLevel.Value + } + if req.Ex != nil { + callbackReq.Ex = req.Ex.Value + } + resp := &cbApi.CallbackBeforeSetGroupMemberInfoResp{ + CommonCallbackResp: &callbackResp, + } + + if err := http.CallBackPostReturn(config.Config.Callback.CallbackUrl, constant.CallbackBeforeSetGroupMemberInfoCommand, callbackReq, resp, config.Config.Callback.CallbackBeforeSetGroupMemberInfo.CallbackTimeOut); err != nil { + callbackResp.ErrCode = http2.StatusInternalServerError + callbackResp.ErrMsg = err.Error() + if !config.Config.Callback.CallbackBeforeSetGroupMemberInfo.CallbackFailedContinue { + callbackResp.ActionCode = constant.ActionForbidden + return callbackResp + } else { + callbackResp.ActionCode = constant.ActionAllow + return callbackResp + } + } + if resp.FaceURL != nil { + req.FaceURL = &wrapperspb.StringValue{Value: *resp.FaceURL} + } + if resp.Nickname != nil { + req.Nickname = &wrapperspb.StringValue{Value: *resp.Nickname} + } + if resp.RoleLevel != nil { + req.RoleLevel = &wrapperspb.Int32Value{Value: *resp.RoleLevel} + } + if resp.Ex != nil { + req.Ex = &wrapperspb.StringValue{Value: *resp.Ex} + } + return callbackResp +} diff --git a/internal/rpc/group/group.go b/internal/rpc/group/group.go index 64cddba02..4d68582e5 100644 --- a/internal/rpc/group/group.go +++ b/internal/rpc/group/group.go @@ -30,6 +30,7 @@ import ( grpcPrometheus "github.com/grpc-ecosystem/go-grpc-prometheus" "google.golang.org/grpc" + "google.golang.org/protobuf/types/known/wrapperspb" "gorm.io/gorm" ) @@ -1765,11 +1766,35 @@ func (s *groupServer) SetGroupMemberNickname(ctx context.Context, req *pbGroup.S log.Error(req.OperationID, errMsg) return &pbGroup.SetGroupMemberNicknameResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil } + cbReq := &pbGroup.SetGroupMemberInfoReq{ + GroupID: req.GroupID, + UserID: req.UserID, + OperationID: req.OperationID, + OpUserID: req.OpUserID, + Nickname: &wrapperspb.StringValue{Value: req.Nickname}, + } + callbackResp := CallbackBeforeSetGroupMemberInfo(cbReq) + if callbackResp.ErrCode != 0 { + log.NewError(req.OperationID, utils.GetSelfFuncName(), "CallbackBeforeMemberJoinGroup resp: ", callbackResp) + } + if callbackResp.ActionCode != constant.ActionAllow { + if callbackResp.ErrCode == 0 { + callbackResp.ErrCode = 201 + } + log.NewDebug(req.OperationID, utils.GetSelfFuncName(), "CallbackBeforeMemberJoinGroup result", "end rpc and return", callbackResp) + return &pbGroup.SetGroupMemberNicknameResp{ + CommonResp: &pbGroup.CommonResp{ + ErrCode: int32(callbackResp.ErrCode), + ErrMsg: callbackResp.ErrMsg, + }, + }, nil + } + nickName := cbReq.Nickname.Value groupMemberInfo := db.GroupMember{} groupMemberInfo.UserID = req.UserID groupMemberInfo.GroupID = req.GroupID - if req.Nickname == "" { + if nickName == "" { userNickname, err := imdb.GetUserNameByUserID(groupMemberInfo.UserID) if err != nil { errMsg := req.OperationID + " GetUserNameByUserID failed " + err.Error() @@ -1778,7 +1803,7 @@ func (s *groupServer) SetGroupMemberNickname(ctx context.Context, req *pbGroup.S } groupMemberInfo.Nickname = userNickname } else { - groupMemberInfo.Nickname = req.Nickname + groupMemberInfo.Nickname = nickName } if err := rocksCache.DelGroupMemberInfoFromCache(req.GroupID, req.UserID); err != nil { @@ -1805,6 +1830,23 @@ func (s *groupServer) SetGroupMemberInfo(ctx context.Context, req *pbGroup.SetGr resp.CommonResp.ErrMsg = err.Error() return resp, nil } + callbackResp := CallbackBeforeSetGroupMemberInfo(req) + if callbackResp.ErrCode != 0 { + log.NewError(req.OperationID, utils.GetSelfFuncName(), "CallbackBeforeMemberJoinGroup resp: ", callbackResp) + } + if callbackResp.ActionCode != constant.ActionAllow { + if callbackResp.ErrCode == 0 { + callbackResp.ErrCode = 201 + } + log.NewDebug(req.OperationID, utils.GetSelfFuncName(), "CallbackBeforeMemberJoinGroup result", "end rpc and return", callbackResp) + return &pbGroup.SetGroupMemberInfoResp{ + CommonResp: &pbGroup.CommonResp{ + ErrCode: int32(callbackResp.ErrCode), + ErrMsg: callbackResp.ErrMsg, + }, + }, nil + } + groupMember := db.GroupMember{ GroupID: req.GroupID, UserID: req.UserID, diff --git a/pkg/call_back_struct/group.go b/pkg/call_back_struct/group.go index 33a18d9c0..a040adcf0 100644 --- a/pkg/call_back_struct/group.go +++ b/pkg/call_back_struct/group.go @@ -46,3 +46,22 @@ type CallbackBeforeMemberJoinGroupResp struct { MuteEndTime *int64 `json:"muteEndTime"` Ex *string `json:"ex"` } + +type CallbackBeforeSetGroupMemberInfoReq struct { + CallbackCommand string `json:"callbackCommand"` + OperationID string `json:"operationID"` + GroupID string `json:"groupID"` + UserID string `json:"userID"` + Nickname string `json:"nickName"` + FaceURL string `json:"faceURL"` + RoleLevel int32 `json:"roleLevel"` + Ex string `json:"ex"` +} + +type CallbackBeforeSetGroupMemberInfoResp struct { + *CommonCallbackResp + Ex *string `json:"ex"` + Nickname *string `json:"nickName"` + FaceURL *string `json:"faceURL"` + RoleLevel *int32 `json:"roleLevel"` +} diff --git a/pkg/common/config/config.go b/pkg/common/config/config.go index 855d5c6fb..32ad9391b 100644 --- a/pkg/common/config/config.go +++ b/pkg/common/config/config.go @@ -292,7 +292,7 @@ type config struct { CallbackBeforeAddFriend callBackConfig `yaml:"callbackBeforeAddFriend"` CallbackBeforeCreateGroup callBackConfig `yaml:"callbackBeforeCreateGroup"` CallbackBeforeMemberJoinGroup callBackConfig `yaml:"callbackBeforeMemberJoinGroup"` - CallbackBeforeExtendMsgModify callBackConfig `yaml:"callbackBeforeExtendMsgModify"` + CallbackBeforeSetGroupMemberInfo callBackConfig `yaml:"callbackBeforeSetGroupMemberInfo"` } `yaml:"callback"` Notification struct { ///////////////////////group///////////////////////////// diff --git a/pkg/common/constant/constant.go b/pkg/common/constant/constant.go index ff2e0097f..250bf5856 100644 --- a/pkg/common/constant/constant.go +++ b/pkg/common/constant/constant.go @@ -202,20 +202,21 @@ const ( VerificationCodeForResetSuffix = "_forReset" //callbackCommand - CallbackBeforeSendSingleMsgCommand = "callbackBeforeSendSingleMsgCommand" - CallbackAfterSendSingleMsgCommand = "callbackAfterSendSingleMsgCommand" - CallbackBeforeSendGroupMsgCommand = "callbackBeforeSendGroupMsgCommand" - CallbackAfterSendGroupMsgCommand = "callbackAfterSendGroupMsgCommand" - CallbackMsgModifyCommand = "callbackMsgModifyCommand" - CallbackUserOnlineCommand = "callbackUserOnlineCommand" - CallbackUserOfflineCommand = "callbackUserOfflineCommand" - CallbackUserKickOffCommand = "callbackUserKickOffCommand" - CallbackOfflinePushCommand = "callbackOfflinePushCommand" - CallbackOnlinePushCommand = "callbackOnlinePushCommand" - CallbackSuperGroupOnlinePushCommand = "callbackSuperGroupOnlinePushCommand" - CallbackBeforeAddFriendCommand = "callbackBeforeAddFriendCommand" - CallbackBeforeCreateGroupCommand = "callbackBeforeCreateGroupCommand" - CallbackBeforeMemberJoinGroupCommand = "callbackBeforeMemberJoinGroupCommand" + CallbackBeforeSendSingleMsgCommand = "callbackBeforeSendSingleMsgCommand" + CallbackAfterSendSingleMsgCommand = "callbackAfterSendSingleMsgCommand" + CallbackBeforeSendGroupMsgCommand = "callbackBeforeSendGroupMsgCommand" + CallbackAfterSendGroupMsgCommand = "callbackAfterSendGroupMsgCommand" + CallbackMsgModifyCommand = "callbackMsgModifyCommand" + CallbackUserOnlineCommand = "callbackUserOnlineCommand" + CallbackUserOfflineCommand = "callbackUserOfflineCommand" + CallbackUserKickOffCommand = "callbackUserKickOffCommand" + CallbackOfflinePushCommand = "callbackOfflinePushCommand" + CallbackOnlinePushCommand = "callbackOnlinePushCommand" + CallbackSuperGroupOnlinePushCommand = "callbackSuperGroupOnlinePushCommand" + CallbackBeforeAddFriendCommand = "callbackBeforeAddFriendCommand" + CallbackBeforeCreateGroupCommand = "callbackBeforeCreateGroupCommand" + CallbackBeforeMemberJoinGroupCommand = "callbackBeforeMemberJoinGroupCommand" + CallbackBeforeSetGroupMemberInfoCommand = "CallbackBeforeSetGroupMemberInfoCommand" //callback actionCode ActionAllow = 0