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

150 lines
5.6 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"
)
func callbackBeforeSendSingleMsg(msg *pbChat.SendMsgReq) (canSend bool, err error) {
3 years ago
if !config.Config.Callback.CallbackbeforeSendSingleMsg.Enable {
3 years ago
return true, nil
}
log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), msg)
3 years ago
req := cbApi.CallbackBeforeSendSingleMsgReq{CommonCallbackReq:cbApi.CommonCallbackReq{
CallbackCommand: constant.CallbackBeforeSendSingleMsgCommand,
3 years ago
}}
resp := &cbApi.CallbackBeforeSendSingleMsgResp{CommonCallbackResp:cbApi.CommonCallbackResp{
}}
utils.CopyStructFields(req, msg.MsgData)
3 years ago
req.Content = string(msg.MsgData.Content)
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 {
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
if !config.Config.Callback.CallbackAfterSendSingleMsg.Enable {
3 years ago
return nil
}
log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), msg)
req := cbApi.CallbackAfterSendSingleMsgReq{CommonCallbackReq: cbApi.CommonCallbackReq{CallbackCommand:constant.CallbackAfterSendSingleMsgCommand}}
3 years ago
resp := &cbApi.CallbackAfterSendSingleMsgResp{CommonCallbackResp: cbApi.CommonCallbackResp{}}
utils.CopyStructFields(req, msg.MsgData)
3 years ago
req.Content = string(msg.MsgData.Content)
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)
req := cbApi.CallbackBeforeSendSingleMsgReq{CommonCallbackReq: cbApi.CommonCallbackReq{CallbackCommand:constant.CallbackBeforeSendGroupMsgCommand}}
3 years ago
resp := &cbApi.CallbackBeforeSendGroupMsgResp{CommonCallbackResp: cbApi.CommonCallbackResp{}}
utils.CopyStructFields(req, msg.MsgData)
3 years ago
req.Content = string(msg.MsgData.Content)
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
if !config.Config.Callback.CallbackAfterSendGroupMsg.Enable {
3 years ago
return nil
}
log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), msg)
req := cbApi.CallbackAfterSendGroupMsgReq{
CommonCallbackReq: cbApi.CommonCallbackReq{
CallbackCommand:constant.CallbackAfterSendGroupMsgCommand,
ServerMsgID: msg.MsgData.ServerMsgID,
ClientMsgID: msg.MsgData.ClientMsgID,
OperationID: msg.OperationID,
},
//GroupMsg: cbApi.GroupMsg{
// Msg: cbApi.Msg{
// SendID: msg.MsgData.
// },
// GroupID: msg.MsgData.GroupID,
//}
}
3 years ago
resp := &cbApi.CallbackAfterSendGroupMsgResp{CommonCallbackResp: cbApi.CommonCallbackResp{}}
//utils.CopyStructFields(req, msg.MsgData)
3 years ago
req.Content = string(msg.MsgData.Content)
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
}
func callBackWordFilter(msg *pbChat.SendMsgReq) (canSend bool, err error) {
if !config.Config.Callback.CallbackWordFilter.Enable || msg.MsgData.ContentType != constant.Text {
return true, nil
}
log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), msg)
//req := cbApi.CallbackWordFilterReq{
// CommonCallbackReq: cbApi.CommonCallbackReq{
// CallbackCommand: constant.CallbackWordFilterCommand,
// ServerMsgID: msg.MsgData.ServerMsgID,
// ClientMsgID: msg.MsgData.ClientMsgID,
// OperationID: msg.OperationID,
// },
// Content: string(msg.MsgData.Content),
//}
req := cbApi.CallbackWordFilterReq{
CommonCallbackReq: cbApi.CommonCallbackReq{},
}
utils.CopyStructFields(req.CommonCallbackReq, msg.MsgData)
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
}