This commit is contained in:
withchao 2023-02-14 15:02:45 +08:00
parent 8435508f35
commit 1ae1f77a14
4 changed files with 108 additions and 177 deletions

View File

@ -5,18 +5,23 @@ import (
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/constant"
"Open_IM/pkg/common/http"
"Open_IM/pkg/common/log"
"Open_IM/pkg/common/tracelog"
pbChat "Open_IM/pkg/proto/msg"
"Open_IM/pkg/utils"
http2 "net/http"
"context"
)
func copyCallbackCommonReqStruct(msg *pbChat.SendMsgReq) cbapi.CommonCallbackReq {
req := cbapi.CommonCallbackReq{
func cbURL() string {
return config.Config.Callback.CallbackUrl
}
func toCommonCallback(ctx context.Context, msg *pbChat.SendMsgReq, command string) cbapi.CommonCallbackReq {
return cbapi.CommonCallbackReq{
SendID: msg.MsgData.SendID,
ServerMsgID: msg.MsgData.ServerMsgID,
CallbackCommand: command,
ClientMsgID: msg.MsgData.ClientMsgID,
OperationID: msg.OperationID,
OperationID: tracelog.GetOperationID(ctx),
SenderPlatformID: msg.MsgData.SenderPlatformID,
SenderNickname: msg.MsgData.SenderNickname,
SessionType: msg.MsgData.SessionType,
@ -30,198 +35,86 @@ func copyCallbackCommonReqStruct(msg *pbChat.SendMsgReq) cbapi.CommonCallbackReq
Seq: msg.MsgData.Seq,
Ex: msg.MsgData.Ex,
}
return req
}
func callbackBeforeSendSingleMsg(msg *pbChat.SendMsgReq) (err error) {
callbackResp := cbapi.CommonCallbackResp{OperationID: msg.OperationID}
func CallbackBeforeSendSingleMsg(ctx context.Context, msg *pbChat.SendMsgReq) error {
if !config.Config.Callback.CallbackBeforeSendSingleMsg.Enable {
return callbackResp
return nil
}
log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), msg)
commonCallbackReq := copyCallbackCommonReqStruct(msg)
commonCallbackReq.CallbackCommand = constant.CallbackBeforeSendSingleMsgCommand
req := cbapi.CallbackBeforeSendSingleMsgReq{
CommonCallbackReq: commonCallbackReq,
req := &cbapi.CallbackBeforeSendSingleMsgReq{
CommonCallbackReq: toCommonCallback(ctx, msg, constant.CallbackBeforeSendSingleMsgCommand),
RecvID: msg.MsgData.RecvID,
}
resp := &cbapi.CallbackBeforeSendSingleMsgResp{
CommonCallbackResp: &callbackResp,
}
//utils.CopyStructFields(req, msg.MsgData)
defer log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), req, *resp)
if err := http.CallBackPostReturn(config.Config.Callback.CallbackUrl, constant.CallbackBeforeSendSingleMsgCommand,
req, resp, config.Config.Callback.CallbackBeforeSendSingleMsg); err != nil {
callbackResp.ErrCode = http2.StatusInternalServerError
callbackResp.ErrMsg = err.Error()
if !*config.Config.Callback.CallbackBeforeSendSingleMsg.CallbackFailedContinue {
callbackResp.ActionCode = constant.ActionForbidden
return callbackResp
} else {
callbackResp.ActionCode = constant.ActionAllow
return callbackResp
}
}
return callbackResp
resp := &cbapi.CallbackBeforeSendSingleMsgResp{}
return http.CallBackPostReturnV2(cbURL(), req, resp, config.Config.Callback.CallbackBeforeSendSingleMsg)
}
func callbackAfterSendSingleMsg(msg *pbChat.SendMsgReq) error {
callbackResp := cbapi.CommonCallbackResp{OperationID: msg.OperationID}
func CallbackAfterSendSingleMsg(ctx context.Context, msg *pbChat.SendMsgReq) error {
if !config.Config.Callback.CallbackAfterSendSingleMsg.Enable {
return callbackResp
return nil
}
log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), msg)
commonCallbackReq := copyCallbackCommonReqStruct(msg)
commonCallbackReq.CallbackCommand = constant.CallbackAfterSendSingleMsgCommand
req := cbapi.CallbackAfterSendSingleMsgReq{
CommonCallbackReq: commonCallbackReq,
req := &cbapi.CallbackAfterSendSingleMsgReq{
CommonCallbackReq: toCommonCallback(ctx, msg, constant.CallbackBeforeSendSingleMsgCommand),
RecvID: msg.MsgData.RecvID,
}
resp := &cbapi.CallbackAfterSendSingleMsgResp{CommonCallbackResp: &callbackResp}
defer log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), req, *resp)
if err := http.CallBackPostReturn(config.Config.Callback.CallbackUrl, constant.CallbackAfterSendSingleMsgCommand,
req, resp, config.Config.Callback.CallbackAfterSendSingleMsg); err != nil {
callbackResp.ErrCode = http2.StatusInternalServerError
callbackResp.ErrMsg = err.Error()
return callbackResp
}
return callbackResp
resp := &cbapi.CallbackAfterSendSingleMsgResp{}
return http.CallBackPostReturnV2(cbURL(), req, resp, config.Config.Callback.CallbackAfterSendSingleMsg)
}
func callbackBeforeSendGroupMsg(msg *pbChat.SendMsgReq) error {
callbackResp := cbapi.CommonCallbackResp{OperationID: msg.OperationID}
if !config.Config.Callback.CallbackBeforeSendGroupMsg.Enable {
return callbackResp
func CallbackBeforeSendGroupMsg(ctx context.Context, msg *pbChat.SendMsgReq) error {
if !config.Config.Callback.CallbackAfterSendSingleMsg.Enable {
return nil
}
log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), msg)
commonCallbackReq := copyCallbackCommonReqStruct(msg)
commonCallbackReq.CallbackCommand = constant.CallbackBeforeSendGroupMsgCommand
req := cbapi.CallbackAfterSendGroupMsgReq{
CommonCallbackReq: commonCallbackReq,
req := &cbapi.CallbackAfterSendGroupMsgReq{
CommonCallbackReq: toCommonCallback(ctx, msg, constant.CallbackBeforeSendSingleMsgCommand),
GroupID: msg.MsgData.GroupID,
}
resp := &cbapi.CallbackBeforeSendGroupMsgResp{CommonCallbackResp: &callbackResp}
defer log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), req, *resp)
if err := http.CallBackPostReturn(config.Config.Callback.CallbackUrl, constant.CallbackBeforeSendGroupMsgCommand,
req, resp, config.Config.Callback.CallbackBeforeSendGroupMsg); err != nil {
callbackResp.ErrCode = http2.StatusInternalServerError
callbackResp.ErrMsg = err.Error()
if !*config.Config.Callback.CallbackBeforeSendGroupMsg.CallbackFailedContinue {
callbackResp.ActionCode = constant.ActionForbidden
return callbackResp
} else {
callbackResp.ActionCode = constant.ActionAllow
return callbackResp
}
}
return callbackResp
resp := &cbapi.CallbackBeforeSendGroupMsgResp{}
return http.CallBackPostReturnV2(cbURL(), req, resp, config.Config.Callback.CallbackAfterSendSingleMsg)
}
func callbackAfterSendGroupMsg(msg *pbChat.SendMsgReq) error {
callbackResp := cbapi.CommonCallbackResp{OperationID: msg.OperationID}
func CallbackAfterSendGroupMsg(ctx context.Context, msg *pbChat.SendMsgReq) error {
if !config.Config.Callback.CallbackAfterSendGroupMsg.Enable {
return callbackResp
return nil
}
log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), msg)
commonCallbackReq := copyCallbackCommonReqStruct(msg)
commonCallbackReq.CallbackCommand = constant.CallbackAfterSendGroupMsgCommand
req := cbapi.CallbackAfterSendGroupMsgReq{
CommonCallbackReq: commonCallbackReq,
req := &cbapi.CallbackAfterSendGroupMsgReq{
CommonCallbackReq: toCommonCallback(ctx, msg, constant.CallbackAfterSendGroupMsgCommand),
GroupID: msg.MsgData.GroupID,
}
resp := &cbapi.CallbackAfterSendGroupMsgResp{CommonCallbackResp: &callbackResp}
defer log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), req, *resp)
if err := http.CallBackPostReturn(config.Config.Callback.CallbackUrl, constant.CallbackAfterSendGroupMsgCommand, req, resp,
config.Config.Callback.CallbackAfterSendGroupMsg); err != nil {
callbackResp.ErrCode = http2.StatusInternalServerError
callbackResp.ErrMsg = err.Error()
return callbackResp
}
return callbackResp
resp := &cbapi.CallbackAfterSendGroupMsgResp{}
return http.CallBackPostReturnV2(cbURL(), req, resp, config.Config.Callback.CallbackAfterSendGroupMsg)
}
func callbackMsgModify(msg *pbChat.SendMsgReq) (err error) {
log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), msg)
callbackResp := cbapi.CommonCallbackResp{OperationID: msg.OperationID}
func CallbackMsgModify(ctx context.Context, msg *pbChat.SendMsgReq) error {
if !config.Config.Callback.CallbackMsgModify.Enable || msg.MsgData.ContentType != constant.Text {
return callbackResp
return nil
}
commonCallbackReq := copyCallbackCommonReqStruct(msg)
commonCallbackReq.CallbackCommand = constant.CallbackMsgModifyCommand
req := cbapi.CallbackMsgModifyCommandReq{
CommonCallbackReq: commonCallbackReq,
req := &cbapi.CallbackMsgModifyCommandReq{
CommonCallbackReq: toCommonCallback(ctx, msg, constant.CallbackMsgModifyCommand),
}
resp := &cbapi.CallbackMsgModifyCommandResp{CommonCallbackResp: &callbackResp}
defer log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), req, *resp)
if err := http.CallBackPostReturn(config.Config.Callback.CallbackUrl, constant.CallbackMsgModifyCommand, req, resp,
config.Config.Callback.CallbackMsgModify); err != nil {
callbackResp.ErrCode = http2.StatusInternalServerError
callbackResp.ErrMsg = err.Error()
if !*config.Config.Callback.CallbackMsgModify.CallbackFailedContinue {
callbackResp.ActionCode = constant.ActionForbidden
return callbackResp
} else {
callbackResp.ActionCode = constant.ActionAllow
return callbackResp
resp := &cbapi.CallbackMsgModifyCommandResp{}
if err := http.CallBackPostReturnV2(cbURL(), req, resp, config.Config.Callback.CallbackAfterSendGroupMsg); err != nil {
return err
}
}
if resp.ErrCode == constant.CallbackHandleSuccess && resp.ActionCode == constant.ActionAllow {
if resp.Content != nil {
msg.MsgData.Content = []byte(*resp.Content)
}
if resp.RecvID != nil {
msg.MsgData.RecvID = *resp.RecvID
}
if resp.GroupID != nil {
msg.MsgData.GroupID = *resp.GroupID
}
if resp.ClientMsgID != nil {
msg.MsgData.ClientMsgID = *resp.ClientMsgID
}
if resp.ServerMsgID != nil {
msg.MsgData.ServerMsgID = *resp.ServerMsgID
}
if resp.SenderPlatformID != nil {
msg.MsgData.SenderPlatformID = *resp.SenderPlatformID
}
if resp.SenderNickname != nil {
msg.MsgData.SenderNickname = *resp.SenderNickname
}
if resp.SenderFaceURL != nil {
msg.MsgData.SenderFaceURL = *resp.SenderFaceURL
}
if resp.SessionType != nil {
msg.MsgData.SessionType = *resp.SessionType
}
if resp.MsgFrom != nil {
msg.MsgData.MsgFrom = *resp.MsgFrom
}
if resp.ContentType != nil {
msg.MsgData.ContentType = *resp.ContentType
}
if resp.Status != nil {
msg.MsgData.Status = *resp.Status
}
if resp.Options != nil {
msg.MsgData.Options = *resp.Options
}
if resp.OfflinePushInfo != nil {
msg.MsgData.OfflinePushInfo = resp.OfflinePushInfo
}
if resp.AtUserIDList != nil {
msg.MsgData.AtUserIDList = *resp.AtUserIDList
}
if resp.MsgDataList != nil {
msg.MsgData.MsgDataList = *resp.MsgDataList
}
if resp.AttachedInfo != nil {
msg.MsgData.AttachedInfo = *resp.AttachedInfo
}
if resp.Ex != nil {
msg.MsgData.Ex = *resp.Ex
}
}
log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), string(msg.MsgData.Content))
return callbackResp
utils.NotNilReplace(msg.MsgData.OfflinePushInfo, resp.OfflinePushInfo)
utils.NotNilReplace(&msg.MsgData.RecvID, resp.RecvID)
utils.NotNilReplace(&msg.MsgData.GroupID, resp.GroupID)
utils.NotNilReplace(&msg.MsgData.ClientMsgID, resp.ClientMsgID)
utils.NotNilReplace(&msg.MsgData.ServerMsgID, resp.ServerMsgID)
utils.NotNilReplace(&msg.MsgData.SenderPlatformID, resp.SenderPlatformID)
utils.NotNilReplace(&msg.MsgData.SenderNickname, resp.SenderNickname)
utils.NotNilReplace(&msg.MsgData.SenderFaceURL, resp.SenderFaceURL)
utils.NotNilReplace(&msg.MsgData.SessionType, resp.SessionType)
utils.NotNilReplace(&msg.MsgData.MsgFrom, resp.MsgFrom)
utils.NotNilReplace(&msg.MsgData.ContentType, resp.ContentType)
utils.NotNilReplace(&msg.MsgData.Status, resp.Status)
utils.NotNilReplace(&msg.MsgData.Options, resp.Options)
utils.NotNilReplace(&msg.MsgData.AtUserIDList, resp.AtUserIDList)
utils.NotNilReplace(&msg.MsgData.MsgDataList, resp.MsgDataList)
utils.NotNilReplace(&msg.MsgData.AttachedInfo, resp.AttachedInfo)
utils.NotNilReplace(&msg.MsgData.Ex, resp.Ex)
return nil
}

View File

@ -25,6 +25,14 @@ type CommonCallbackReq struct {
Ex string `json:"ex"`
}
func (c *CommonCallbackReq) GetCallbackCommand() string {
return c.CallbackCommand
}
type CallbackReq interface {
GetCallbackCommand() string
}
type CallbackResp interface {
Parse() (err error)
}
@ -33,18 +41,20 @@ type CommonCallbackResp struct {
ActionCode int `json:"actionCode"`
ErrCode int32 `json:"errCode"`
ErrMsg string `json:"errMsg"`
OperationID string `json:"operationID"`
//OperationID string `json:"operationID"`
}
func (c *CommonCallbackResp) Parse() (err error) {
func (c *CommonCallbackResp) Parse() error {
if c == nil {
return constant.ErrData.Wrap("callback common is nil")
}
if c.ActionCode != constant.NoError || c.ErrCode != constant.NoError {
newErr := constant.ErrCallback
newErr.ErrCode = c.ErrCode
newErr.DetailErrMsg = fmt.Sprintf("callback response error actionCode is %d, errCode is %d, errMsg is %s", c.ActionCode, c.ErrCode, c.ErrMsg)
err = newErr
return
return newErr.Wrap()
}
return
return nil
}
type UserStatusBaseCallback struct {

View File

@ -78,3 +78,23 @@ func CallBackPostReturn(url, callbackCommand string, input interface{}, output c
}
return output.Parse()
}
func CallBackPostReturnV2(url string, req cbapi.CallbackReq, resp cbapi.CallbackResp, callbackConfig config.CallBackConfig) error {
v := urlLib.Values{}
v.Set("callbackCommand", req.GetCallbackCommand())
url = url + "?" + v.Encode()
b, err := Post(url, req, callbackConfig.CallbackTimeOut)
if err != nil {
if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue {
return constant.ErrCallbackContinue
}
return constant.NewErrNetwork(err)
}
if err = json.Unmarshal(b, resp); err != nil {
if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue {
return constant.ErrCallbackContinue
}
return constant.NewErrData(err)
}
return resp.Parse()
}

View File

@ -425,3 +425,11 @@ func Unwrap(err error) error {
}
return err
}
// NotNilReplace 当new_不为空时, 将old设置为new_
func NotNilReplace[T any](old, new_ *T) {
if old == nil || new_ == nil {
return
}
*old = *new_
}