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

165 lines
6.4 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{
SendID: msg.MsgData.SendID,
ServerMsgID: msg.MsgData.ServerMsgID,
ClientMsgID: msg.MsgData.ClientMsgID,
OperationID: msg.OperationID,
SenderPlatformID: msg.MsgData.SenderPlatformID,
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
func callbackBeforeSendSingleMsg(msg *pbChat.SendMsgReq) (canSend bool, err error) {
3 years ago
log.NewDebug(msg.OperationID, config.Config.Callback.CallbackBeforeSendSingleMsg.Enable)
if !config.Config.Callback.CallbackBeforeSendSingleMsg.Enable {
3 years ago
return true, nil
}
log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), msg)
3 years ago
commonCallbackReq := copyCallbackCommonReqStruct(msg)
commonCallbackReq.CallbackCommand = constant.CallbackBeforeSendSingleMsgCommand
req := cbApi.CallbackBeforeSendSingleMsgReq{
CommonCallbackReq: commonCallbackReq,
RecvID: msg.MsgData.RecvID,
}
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{
if !config.Config.Callback.CallbackBeforeSendSingleMsg.CallbackFailedContinue {
3 years ago
return false, err
}
3 years ago
} else {
if resp.ActionCode == constant.ActionForbidden && resp.ErrCode == constant.CallbackHandleSuccess {
3 years ago
return false, nil
}
3 years ago
}
return true, nil
}
func callbackAfterSendSingleMsg(msg *pbChat.SendMsgReq) error {
3 years ago
log.NewDebug(msg.OperationID, config.Config.Callback.CallbackAfterSendSingleMsg.Enable)
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,
RecvID: msg.MsgData.RecvID,
}
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
log.NewDebug(msg.OperationID, config.Config.Callback.CallbackBeforeSendGroupMsg.Enable)
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
req := cbApi.CallbackBeforeSendSingleMsgReq{
CommonCallbackReq: commonCallbackReq,
RecvID: msg.MsgData.RecvID,
}
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 {
return false, nil
}
3 years ago
} else {
if resp.ActionCode == constant.ActionForbidden && resp.ErrCode == constant.CallbackHandleSuccess {
3 years ago
return false, nil
}
3 years ago
}
return true, nil
}
func callbackAfterSendGroupMsg(msg *pbChat.SendMsgReq) error {
3 years ago
log.NewDebug(msg.OperationID, config.Config.Callback.CallbackAfterSendGroupMsg.Enable)
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)
commonCallbackReq.CallbackCommand = constant.CallbackBeforeSendGroupMsgCommand
req := cbApi.CallbackBeforeSendSingleMsgReq{
CommonCallbackReq: commonCallbackReq,
RecvID: msg.MsgData.RecvID,
}
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) {
log.NewDebug(msg.OperationID, config.Config.Callback.CallbackWordFilter.Enable)
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(), "config disable, stop this operation")
3 years ago
return false, err
}
3 years ago
} else {
if resp.ActionCode == constant.ActionForbidden && resp.ErrCode == constant.CallbackHandleSuccess {
3 years ago
return false, nil
}
3 years ago
msg.MsgData.Content = []byte(resp.Content)
log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), string(msg.MsgData.Content))
3 years ago
}
3 years ago
return true, nil
}