You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/internal/rpc/msg/callback.go

161 lines
6.2 KiB

3 years ago
package msg
import (
cbApi "Open_IM/pkg/call_back_struct"
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/constant"
"Open_IM/pkg/common/http"
"Open_IM/pkg/common/log"
pbChat "Open_IM/pkg/proto/chat"
"Open_IM/pkg/utils"
)
3 years ago
func copyCallbackCommonReqStruct(msg *pbChat.SendMsgReq) cbApi.CommonCallbackReq {
return cbApi.CommonCallbackReq{
3 years ago
SendID: msg.MsgData.SendID,
ServerMsgID: msg.MsgData.ServerMsgID,
ClientMsgID: msg.MsgData.ClientMsgID,
OperationID: msg.OperationID,
3 years ago
SenderPlatformID: msg.MsgData.SenderPlatformID,
3 years ago
SenderNickname: msg.MsgData.SenderNickname,
SessionType: msg.MsgData.SessionType,
MsgFrom: msg.MsgData.MsgFrom,
ContentType: msg.MsgData.ContentType,
Status: msg.MsgData.Status,
CreateTime: msg.MsgData.CreateTime,
Content: string(msg.MsgData.Content),
3 years ago
}
}
3 years ago
func callbackBeforeSendSingleMsg(msg *pbChat.SendMsgReq) (canSend bool, err error) {
3 years ago
if !config.Config.Callback.CallbackBeforeSendSingleMsg.Enable {
3 years ago
return true, nil
}
3 years ago
log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), msg)
3 years ago
commonCallbackReq := copyCallbackCommonReqStruct(msg)
commonCallbackReq.CallbackCommand = constant.CallbackBeforeSendSingleMsgCommand
req := cbApi.CallbackBeforeSendSingleMsgReq{
CommonCallbackReq: commonCallbackReq,
3 years ago
RecvID: msg.MsgData.RecvID,
3 years ago
}
resp := &cbApi.CallbackBeforeSendSingleMsgResp{
CommonCallbackResp: cbApi.CommonCallbackResp{},
}
//utils.CopyStructFields(req, msg.MsgData)
defer log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), req, *resp)
3 years ago
if err := http.PostReturn(config.Config.Callback.CallbackUrl, req, resp, config.Config.Callback.CallbackBeforeSendSingleMsg.CallbackTimeOut); err != nil {
3 years ago
if !config.Config.Callback.CallbackBeforeSendSingleMsg.CallbackFailedContinue {
3 years ago
return false, err
3 years ago
} else {
return true, err
3 years ago
}
3 years ago
} else {
if resp.ActionCode == constant.ActionForbidden && resp.ErrCode == constant.CallbackHandleSuccess {
3 years ago
return false, nil
}
3 years ago
}
3 years ago
return true, err
3 years ago
}
func callbackAfterSendSingleMsg(msg *pbChat.SendMsgReq) error {
3 years ago
if !config.Config.Callback.CallbackAfterSendSingleMsg.Enable {
3 years ago
return nil
}
log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), msg)
3 years ago
commonCallbackReq := copyCallbackCommonReqStruct(msg)
commonCallbackReq.CallbackCommand = constant.CallbackAfterSendSingleMsgCommand
req := cbApi.CallbackAfterSendSingleMsgReq{
CommonCallbackReq: commonCallbackReq,
3 years ago
RecvID: msg.MsgData.RecvID,
3 years ago
}
3 years ago
resp := &cbApi.CallbackAfterSendSingleMsgResp{CommonCallbackResp: cbApi.CommonCallbackResp{}}
3 years ago
//utils.CopyStructFields(req, msg.MsgData)
defer log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), req, *resp)
3 years ago
if err := http.PostReturn(config.Config.Callback.CallbackUrl, req, resp, config.Config.Callback.CallbackAfterSendSingleMsg.CallbackTimeOut); err != nil {
3 years ago
return err
}
return nil
}
func callbackBeforeSendGroupMsg(msg *pbChat.SendMsgReq) (canSend bool, err error) {
3 years ago
if !config.Config.Callback.CallbackBeforeSendGroupMsg.Enable {
3 years ago
return true, nil
}
log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), msg)
3 years ago
commonCallbackReq := copyCallbackCommonReqStruct(msg)
commonCallbackReq.CallbackCommand = constant.CallbackBeforeSendGroupMsgCommand
3 years ago
req := cbApi.CallbackAfterSendGroupMsgReq{
3 years ago
CommonCallbackReq: commonCallbackReq,
3 years ago
GroupID: msg.MsgData.GroupID,
3 years ago
}
3 years ago
resp := &cbApi.CallbackBeforeSendGroupMsgResp{CommonCallbackResp: cbApi.CommonCallbackResp{}}
3 years ago
//utils.CopyStructFields(req, msg.MsgData)
defer log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), req, *resp)
3 years ago
if err := http.PostReturn(config.Config.Callback.CallbackUrl, req, resp, config.Config.Callback.CallbackBeforeSendGroupMsg.CallbackTimeOut); err != nil {
if !config.Config.Callback.CallbackBeforeSendGroupMsg.CallbackFailedContinue {
3 years ago
return false, err
3 years ago
} else {
return true, err
3 years ago
}
3 years ago
} else {
if resp.ActionCode == constant.ActionForbidden && resp.ErrCode == constant.CallbackHandleSuccess {
3 years ago
return false, nil
}
3 years ago
}
3 years ago
return true, err
3 years ago
}
func callbackAfterSendGroupMsg(msg *pbChat.SendMsgReq) error {
3 years ago
if !config.Config.Callback.CallbackAfterSendGroupMsg.Enable {
3 years ago
return nil
}
log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), msg)
3 years ago
commonCallbackReq := copyCallbackCommonReqStruct(msg)
3 years ago
commonCallbackReq.CallbackCommand = constant.CallbackAfterSendGroupMsgCommand
req := cbApi.CallbackAfterSendGroupMsgReq{
3 years ago
CommonCallbackReq: commonCallbackReq,
3 years ago
GroupID: msg.MsgData.GroupID,
}
3 years ago
resp := &cbApi.CallbackAfterSendGroupMsgResp{CommonCallbackResp: cbApi.CommonCallbackResp{}}
//utils.CopyStructFields(req, msg.MsgData)
defer log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), req, *resp)
3 years ago
if err := http.PostReturn(config.Config.Callback.CallbackUrl, req, resp, config.Config.Callback.CallbackAfterSendGroupMsg.CallbackTimeOut); err != nil {
return err
}
3 years ago
return nil
}
3 years ago
func callbackWordFilter(msg *pbChat.SendMsgReq) (canSend bool, err error) {
3 years ago
if !config.Config.Callback.CallbackWordFilter.Enable || msg.MsgData.ContentType != constant.Text {
return true, nil
}
log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), msg)
3 years ago
commonCallbackReq := copyCallbackCommonReqStruct(msg)
commonCallbackReq.CallbackCommand = constant.CallbackWordFilterCommand
req := cbApi.CallbackWordFilterReq{
3 years ago
CommonCallbackReq: commonCallbackReq,
}
3 years ago
resp := &cbApi.CallbackWordFilterResp{CommonCallbackResp: cbApi.CommonCallbackResp{}}
//utils.CopyStructFields(&req., msg.MsgData)
defer log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), req, *resp)
3 years ago
if err := http.PostReturn(config.Config.Callback.CallbackUrl, req, resp, config.Config.Callback.CallbackWordFilter.CallbackTimeOut); err != nil {
3 years ago
if !config.Config.Callback.CallbackWordFilter.CallbackFailedContinue {
3 years ago
log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), "callback failed and config disable, stop this operation")
3 years ago
return false, err
3 years ago
} else {
return true, err
3 years ago
}
3 years ago
} else {
if resp.ActionCode == constant.ActionForbidden && resp.ErrCode == constant.CallbackHandleSuccess {
3 years ago
return false, nil
}
3 years ago
if resp.ErrCode == constant.CallbackHandleSuccess {
msg.MsgData.Content = []byte(resp.Content)
}
log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), string(msg.MsgData.Content))
3 years ago
}
3 years ago
return true, err
3 years ago
}