set groupMemberInfo callback

This commit is contained in:
wangchuxiao 2022-12-16 16:54:18 +08:00
parent 4f2d543651
commit 920a7cfdac
6 changed files with 135 additions and 18 deletions

View File

@ -345,7 +345,7 @@ callback:
enable: false enable: false
callbackTimeOut: 2 callbackTimeOut: 2
callbackFailedContinue: true # 回调超时是否继续 callbackFailedContinue: true # 回调超时是否继续
callbackBeforeExtendMsgModify: callbackBeforeSetGroupMemberInfo:
enable: false enable: false
callbackTimeOut: 2 callbackTimeOut: 2
callbackFailedContinue: true # 回调超时是否继续 callbackFailedContinue: true # 回调超时是否继续

View File

@ -10,6 +10,8 @@ import (
pbGroup "Open_IM/pkg/proto/group" pbGroup "Open_IM/pkg/proto/group"
"Open_IM/pkg/utils" "Open_IM/pkg/utils"
http2 "net/http" http2 "net/http"
"google.golang.org/protobuf/types/known/wrapperspb"
) )
func callbackBeforeCreateGroup(req *pbGroup.CreateGroupReq) cbApi.CommonCallbackResp { func callbackBeforeCreateGroup(req *pbGroup.CreateGroupReq) cbApi.CommonCallbackResp {
@ -126,3 +128,56 @@ func CallbackBeforeMemberJoinGroup(operationID string, groupMember *db.GroupMemb
} }
return callbackResp 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
}

View File

@ -30,6 +30,7 @@ import (
grpcPrometheus "github.com/grpc-ecosystem/go-grpc-prometheus" grpcPrometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/protobuf/types/known/wrapperspb"
"gorm.io/gorm" "gorm.io/gorm"
) )
@ -1765,11 +1766,35 @@ func (s *groupServer) SetGroupMemberNickname(ctx context.Context, req *pbGroup.S
log.Error(req.OperationID, errMsg) log.Error(req.OperationID, errMsg)
return &pbGroup.SetGroupMemberNicknameResp{CommonResp: &pbGroup.CommonResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}}, nil 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 := db.GroupMember{}
groupMemberInfo.UserID = req.UserID groupMemberInfo.UserID = req.UserID
groupMemberInfo.GroupID = req.GroupID groupMemberInfo.GroupID = req.GroupID
if req.Nickname == "" { if nickName == "" {
userNickname, err := imdb.GetUserNameByUserID(groupMemberInfo.UserID) userNickname, err := imdb.GetUserNameByUserID(groupMemberInfo.UserID)
if err != nil { if err != nil {
errMsg := req.OperationID + " GetUserNameByUserID failed " + err.Error() errMsg := req.OperationID + " GetUserNameByUserID failed " + err.Error()
@ -1778,7 +1803,7 @@ func (s *groupServer) SetGroupMemberNickname(ctx context.Context, req *pbGroup.S
} }
groupMemberInfo.Nickname = userNickname groupMemberInfo.Nickname = userNickname
} else { } else {
groupMemberInfo.Nickname = req.Nickname groupMemberInfo.Nickname = nickName
} }
if err := rocksCache.DelGroupMemberInfoFromCache(req.GroupID, req.UserID); err != nil { 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() resp.CommonResp.ErrMsg = err.Error()
return resp, nil 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{ groupMember := db.GroupMember{
GroupID: req.GroupID, GroupID: req.GroupID,
UserID: req.UserID, UserID: req.UserID,

View File

@ -46,3 +46,22 @@ type CallbackBeforeMemberJoinGroupResp struct {
MuteEndTime *int64 `json:"muteEndTime"` MuteEndTime *int64 `json:"muteEndTime"`
Ex *string `json:"ex"` 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"`
}

View File

@ -292,7 +292,7 @@ type config struct {
CallbackBeforeAddFriend callBackConfig `yaml:"callbackBeforeAddFriend"` CallbackBeforeAddFriend callBackConfig `yaml:"callbackBeforeAddFriend"`
CallbackBeforeCreateGroup callBackConfig `yaml:"callbackBeforeCreateGroup"` CallbackBeforeCreateGroup callBackConfig `yaml:"callbackBeforeCreateGroup"`
CallbackBeforeMemberJoinGroup callBackConfig `yaml:"callbackBeforeMemberJoinGroup"` CallbackBeforeMemberJoinGroup callBackConfig `yaml:"callbackBeforeMemberJoinGroup"`
CallbackBeforeExtendMsgModify callBackConfig `yaml:"callbackBeforeExtendMsgModify"` CallbackBeforeSetGroupMemberInfo callBackConfig `yaml:"callbackBeforeSetGroupMemberInfo"`
} `yaml:"callback"` } `yaml:"callback"`
Notification struct { Notification struct {
///////////////////////group///////////////////////////// ///////////////////////group/////////////////////////////

View File

@ -202,20 +202,21 @@ const (
VerificationCodeForResetSuffix = "_forReset" VerificationCodeForResetSuffix = "_forReset"
//callbackCommand //callbackCommand
CallbackBeforeSendSingleMsgCommand = "callbackBeforeSendSingleMsgCommand" CallbackBeforeSendSingleMsgCommand = "callbackBeforeSendSingleMsgCommand"
CallbackAfterSendSingleMsgCommand = "callbackAfterSendSingleMsgCommand" CallbackAfterSendSingleMsgCommand = "callbackAfterSendSingleMsgCommand"
CallbackBeforeSendGroupMsgCommand = "callbackBeforeSendGroupMsgCommand" CallbackBeforeSendGroupMsgCommand = "callbackBeforeSendGroupMsgCommand"
CallbackAfterSendGroupMsgCommand = "callbackAfterSendGroupMsgCommand" CallbackAfterSendGroupMsgCommand = "callbackAfterSendGroupMsgCommand"
CallbackMsgModifyCommand = "callbackMsgModifyCommand" CallbackMsgModifyCommand = "callbackMsgModifyCommand"
CallbackUserOnlineCommand = "callbackUserOnlineCommand" CallbackUserOnlineCommand = "callbackUserOnlineCommand"
CallbackUserOfflineCommand = "callbackUserOfflineCommand" CallbackUserOfflineCommand = "callbackUserOfflineCommand"
CallbackUserKickOffCommand = "callbackUserKickOffCommand" CallbackUserKickOffCommand = "callbackUserKickOffCommand"
CallbackOfflinePushCommand = "callbackOfflinePushCommand" CallbackOfflinePushCommand = "callbackOfflinePushCommand"
CallbackOnlinePushCommand = "callbackOnlinePushCommand" CallbackOnlinePushCommand = "callbackOnlinePushCommand"
CallbackSuperGroupOnlinePushCommand = "callbackSuperGroupOnlinePushCommand" CallbackSuperGroupOnlinePushCommand = "callbackSuperGroupOnlinePushCommand"
CallbackBeforeAddFriendCommand = "callbackBeforeAddFriendCommand" CallbackBeforeAddFriendCommand = "callbackBeforeAddFriendCommand"
CallbackBeforeCreateGroupCommand = "callbackBeforeCreateGroupCommand" CallbackBeforeCreateGroupCommand = "callbackBeforeCreateGroupCommand"
CallbackBeforeMemberJoinGroupCommand = "callbackBeforeMemberJoinGroupCommand" CallbackBeforeMemberJoinGroupCommand = "callbackBeforeMemberJoinGroupCommand"
CallbackBeforeSetGroupMemberInfoCommand = "CallbackBeforeSetGroupMemberInfoCommand"
//callback actionCode //callback actionCode
ActionAllow = 0 ActionAllow = 0