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

170 lines
7.0 KiB

3 years ago
package msg
import (
cbApi "Open_IM/pkg/call_back_struct"
2 years ago
"Open_IM/pkg/common/callback"
3 years ago
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/constant"
"Open_IM/pkg/common/http"
"Open_IM/pkg/common/log"
2 years ago
pbChat "Open_IM/pkg/proto/msg"
3 years ago
"Open_IM/pkg/utils"
3 years ago
http2 "net/http"
3 years ago
)
3 years ago
func copyCallbackCommonReqStruct(msg *pbChat.SendMsgReq) cbApi.CommonCallbackReq {
2 years ago
req := 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,
2 years ago
AtUserIDList: msg.MsgData.AtUserIDList,
SenderFaceURL: msg.MsgData.SenderFaceURL,
2 years ago
Content: callback.GetContent(msg.MsgData),
2 years ago
Seq: msg.MsgData.Seq,
Ex: msg.MsgData.Ex,
3 years ago
}
2 years ago
return req
3 years ago
}
3 years ago
func callbackBeforeSendSingleMsg(msg *pbChat.SendMsgReq) cbApi.CommonCallbackResp {
callbackResp := cbApi.CommonCallbackResp{OperationID: msg.OperationID}
3 years ago
if !config.Config.Callback.CallbackBeforeSendSingleMsg.Enable {
3 years ago
return callbackResp
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{
3 years ago
CommonCallbackResp: &callbackResp,
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.CallbackBeforeSendSingleMsg.CallbackTimeOut); err != nil {
3 years ago
callbackResp.ErrCode = http2.StatusInternalServerError
callbackResp.ErrMsg = err.Error()
3 years ago
if !config.Config.Callback.CallbackBeforeSendSingleMsg.CallbackFailedContinue {
3 years ago
callbackResp.ActionCode = constant.ActionForbidden
return callbackResp
3 years ago
} else {
3 years ago
callbackResp.ActionCode = constant.ActionAllow
return callbackResp
3 years ago
}
3 years ago
}
3 years ago
return callbackResp
3 years ago
}
3 years ago
func callbackAfterSendSingleMsg(msg *pbChat.SendMsgReq) cbApi.CommonCallbackResp {
callbackResp := cbApi.CommonCallbackResp{OperationID: msg.OperationID}
3 years ago
if !config.Config.Callback.CallbackAfterSendSingleMsg.Enable {
3 years ago
return callbackResp
3 years ago
}
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: &callbackResp}
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
callbackResp.ErrCode = http2.StatusInternalServerError
callbackResp.ErrMsg = err.Error()
return callbackResp
3 years ago
}
3 years ago
return callbackResp
3 years ago
}
3 years ago
func callbackBeforeSendGroupMsg(msg *pbChat.SendMsgReq) cbApi.CommonCallbackResp {
callbackResp := cbApi.CommonCallbackResp{OperationID: msg.OperationID}
3 years ago
if !config.Config.Callback.CallbackBeforeSendGroupMsg.Enable {
3 years ago
return callbackResp
3 years ago
}
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: &callbackResp}
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 {
3 years ago
callbackResp.ErrCode = http2.StatusInternalServerError
callbackResp.ErrMsg = err.Error()
3 years ago
if !config.Config.Callback.CallbackBeforeSendGroupMsg.CallbackFailedContinue {
3 years ago
callbackResp.ActionCode = constant.ActionForbidden
return callbackResp
3 years ago
} else {
3 years ago
callbackResp.ActionCode = constant.ActionAllow
return callbackResp
3 years ago
}
3 years ago
}
3 years ago
return callbackResp
3 years ago
}
3 years ago
func callbackAfterSendGroupMsg(msg *pbChat.SendMsgReq) cbApi.CommonCallbackResp {
callbackResp := cbApi.CommonCallbackResp{OperationID: msg.OperationID}
3 years ago
if !config.Config.Callback.CallbackAfterSendGroupMsg.Enable {
3 years ago
return callbackResp
3 years ago
}
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: &callbackResp}
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 {
3 years ago
callbackResp.ErrCode = http2.StatusInternalServerError
callbackResp.ErrMsg = err.Error()
return callbackResp
3 years ago
}
3 years ago
return callbackResp
3 years ago
}
3 years ago
func callbackWordFilter(msg *pbChat.SendMsgReq) cbApi.CommonCallbackResp {
log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), msg)
3 years ago
callbackResp := cbApi.CommonCallbackResp{OperationID: msg.OperationID}
3 years ago
if !config.Config.Callback.CallbackWordFilter.Enable || msg.MsgData.ContentType != constant.Text {
3 years ago
return callbackResp
3 years ago
}
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: &callbackResp}
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
callbackResp.ErrCode = http2.StatusInternalServerError
callbackResp.ErrMsg = err.Error()
3 years ago
if !config.Config.Callback.CallbackWordFilter.CallbackFailedContinue {
3 years ago
callbackResp.ActionCode = constant.ActionForbidden
return callbackResp
3 years ago
} else {
3 years ago
callbackResp.ActionCode = constant.ActionAllow
return callbackResp
3 years ago
}
}
3 years ago
if resp.ErrCode == constant.CallbackHandleSuccess && resp.ActionCode == constant.ActionAllow && resp.Content != "" {
3 years ago
msg.MsgData.Content = []byte(resp.Content)
}
log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), string(msg.MsgData.Content))
return callbackResp
3 years ago
}