Files
open-im-server/internal/rpc/msg/callback.go
T

229 lines
8.9 KiB
Go
Raw Normal View History

2022-02-28 17:57:03 +08:00
package msg
import (
2023-01-13 18:00:18 +08:00
cbApi "Open_IM/pkg/callback_struct"
2022-08-04 14:21:03 +08:00
"Open_IM/pkg/common/callback"
2022-02-28 17:57:03 +08:00
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/constant"
"Open_IM/pkg/common/http"
"Open_IM/pkg/common/log"
2022-07-20 21:11:30 +08:00
pbChat "Open_IM/pkg/proto/msg"
2022-02-28 17:57:03 +08:00
"Open_IM/pkg/utils"
2022-05-26 18:02:00 +08:00
http2 "net/http"
2022-02-28 17:57:03 +08:00
)
2022-03-03 18:49:36 +08:00
func copyCallbackCommonReqStruct(msg *pbChat.SendMsgReq) cbApi.CommonCallbackReq {
2022-08-04 14:21:03 +08:00
req := cbApi.CommonCallbackReq{
2022-03-04 17:16:21 +08:00
SendID: msg.MsgData.SendID,
ServerMsgID: msg.MsgData.ServerMsgID,
ClientMsgID: msg.MsgData.ClientMsgID,
OperationID: msg.OperationID,
2022-03-03 18:49:36 +08:00
SenderPlatformID: msg.MsgData.SenderPlatformID,
2022-03-04 17:16:21 +08:00
SenderNickname: msg.MsgData.SenderNickname,
SessionType: msg.MsgData.SessionType,
MsgFrom: msg.MsgData.MsgFrom,
ContentType: msg.MsgData.ContentType,
Status: msg.MsgData.Status,
CreateTime: msg.MsgData.CreateTime,
2022-06-30 18:50:10 +08:00
AtUserIDList: msg.MsgData.AtUserIDList,
SenderFaceURL: msg.MsgData.SenderFaceURL,
2022-08-04 14:21:03 +08:00
Content: callback.GetContent(msg.MsgData),
2022-08-08 17:42:06 +08:00
Seq: msg.MsgData.Seq,
Ex: msg.MsgData.Ex,
2022-03-03 18:49:36 +08:00
}
2022-08-04 14:21:03 +08:00
return req
2022-03-03 18:49:36 +08:00
}
2022-05-26 18:02:00 +08:00
func callbackBeforeSendSingleMsg(msg *pbChat.SendMsgReq) cbApi.CommonCallbackResp {
callbackResp := cbApi.CommonCallbackResp{OperationID: msg.OperationID}
2022-03-03 19:39:49 +08:00
if !config.Config.Callback.CallbackBeforeSendSingleMsg.Enable {
2022-05-26 18:02:00 +08:00
return callbackResp
2022-02-28 17:57:03 +08:00
}
2022-03-03 16:23:59 +08:00
log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), msg)
2022-03-03 18:49:36 +08:00
commonCallbackReq := copyCallbackCommonReqStruct(msg)
commonCallbackReq.CallbackCommand = constant.CallbackBeforeSendSingleMsgCommand
req := cbApi.CallbackBeforeSendSingleMsgReq{
CommonCallbackReq: commonCallbackReq,
2022-03-04 17:16:21 +08:00
RecvID: msg.MsgData.RecvID,
2022-03-03 18:49:36 +08:00
}
resp := &cbApi.CallbackBeforeSendSingleMsgResp{
2022-11-08 17:10:27 +08:00
CommonCallbackResp: &callbackResp,
2022-03-03 18:49:36 +08:00
}
//utils.CopyStructFields(req, msg.MsgData)
2022-03-03 16:50:06 +08:00
defer log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), req, *resp)
2023-01-11 16:23:16 +08:00
if err := http.CallBackPostReturn(config.Config.Callback.CallbackUrl, constant.CallbackBeforeSendSingleMsgCommand,
req, resp, config.Config.Callback.CallbackBeforeSendSingleMsg.CallbackTimeOut, &config.Config.Callback.CallbackBeforeSendSingleMsg.CallbackFailedContinue); err != nil {
2022-05-26 18:02:00 +08:00
callbackResp.ErrCode = http2.StatusInternalServerError
callbackResp.ErrMsg = err.Error()
2022-03-03 19:39:49 +08:00
if !config.Config.Callback.CallbackBeforeSendSingleMsg.CallbackFailedContinue {
2022-05-26 18:02:00 +08:00
callbackResp.ActionCode = constant.ActionForbidden
return callbackResp
2022-03-04 18:23:58 +08:00
} else {
2022-05-26 18:02:00 +08:00
callbackResp.ActionCode = constant.ActionAllow
return callbackResp
2022-03-03 14:42:50 +08:00
}
2022-02-28 17:57:03 +08:00
}
2022-05-26 18:02:00 +08:00
return callbackResp
2022-02-28 17:57:03 +08:00
}
2022-05-26 18:02:00 +08:00
func callbackAfterSendSingleMsg(msg *pbChat.SendMsgReq) cbApi.CommonCallbackResp {
callbackResp := cbApi.CommonCallbackResp{OperationID: msg.OperationID}
2022-03-02 19:07:17 +08:00
if !config.Config.Callback.CallbackAfterSendSingleMsg.Enable {
2022-05-26 18:02:00 +08:00
return callbackResp
2022-02-28 17:57:03 +08:00
}
2022-03-03 16:23:59 +08:00
log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), msg)
2022-03-03 18:49:36 +08:00
commonCallbackReq := copyCallbackCommonReqStruct(msg)
commonCallbackReq.CallbackCommand = constant.CallbackAfterSendSingleMsgCommand
req := cbApi.CallbackAfterSendSingleMsgReq{
CommonCallbackReq: commonCallbackReq,
2022-03-04 17:16:21 +08:00
RecvID: msg.MsgData.RecvID,
2022-03-03 18:49:36 +08:00
}
2022-11-08 17:10:27 +08:00
resp := &cbApi.CallbackAfterSendSingleMsgResp{CommonCallbackResp: &callbackResp}
2022-03-03 16:50:06 +08:00
defer log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), req, *resp)
2023-01-11 16:23:16 +08:00
if err := http.CallBackPostReturn(config.Config.Callback.CallbackUrl, constant.CallbackAfterSendSingleMsgCommand,
req, resp, config.Config.Callback.CallbackAfterSendSingleMsg.CallbackTimeOut, &config.Config.Callback.CallbackAfterSendSingleMsg.CallbackFailedContinue); err != nil {
2022-05-26 18:02:00 +08:00
callbackResp.ErrCode = http2.StatusInternalServerError
callbackResp.ErrMsg = err.Error()
return callbackResp
2022-02-28 17:57:03 +08:00
}
2022-05-26 18:02:00 +08:00
return callbackResp
2022-02-28 17:57:03 +08:00
}
2022-05-26 18:02:00 +08:00
func callbackBeforeSendGroupMsg(msg *pbChat.SendMsgReq) cbApi.CommonCallbackResp {
callbackResp := cbApi.CommonCallbackResp{OperationID: msg.OperationID}
2022-03-02 19:07:17 +08:00
if !config.Config.Callback.CallbackBeforeSendGroupMsg.Enable {
2022-05-26 18:02:00 +08:00
return callbackResp
2022-02-28 17:57:03 +08:00
}
2022-03-03 16:23:59 +08:00
log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), msg)
2022-03-03 18:49:36 +08:00
commonCallbackReq := copyCallbackCommonReqStruct(msg)
commonCallbackReq.CallbackCommand = constant.CallbackBeforeSendGroupMsgCommand
2022-03-04 15:20:59 +08:00
req := cbApi.CallbackAfterSendGroupMsgReq{
2022-03-03 18:49:36 +08:00
CommonCallbackReq: commonCallbackReq,
2022-03-04 17:16:21 +08:00
GroupID: msg.MsgData.GroupID,
2022-03-03 18:49:36 +08:00
}
2022-11-08 17:10:27 +08:00
resp := &cbApi.CallbackBeforeSendGroupMsgResp{CommonCallbackResp: &callbackResp}
2022-03-03 16:50:06 +08:00
defer log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), req, *resp)
2023-01-11 16:23:16 +08:00
if err := http.CallBackPostReturn(config.Config.Callback.CallbackUrl, constant.CallbackBeforeSendGroupMsgCommand,
req, resp, config.Config.Callback.CallbackBeforeSendGroupMsg.CallbackTimeOut, &config.Config.Callback.CallbackBeforeSendGroupMsg.CallbackFailedContinue); err != nil {
2022-05-26 18:02:00 +08:00
callbackResp.ErrCode = http2.StatusInternalServerError
callbackResp.ErrMsg = err.Error()
2022-05-27 11:34:10 +08:00
if !config.Config.Callback.CallbackBeforeSendGroupMsg.CallbackFailedContinue {
2022-05-26 18:02:00 +08:00
callbackResp.ActionCode = constant.ActionForbidden
return callbackResp
2022-03-04 18:23:58 +08:00
} else {
2022-05-26 18:02:00 +08:00
callbackResp.ActionCode = constant.ActionAllow
return callbackResp
2022-03-03 14:18:28 +08:00
}
2022-02-28 17:57:03 +08:00
}
2022-05-26 18:02:00 +08:00
return callbackResp
2022-02-28 17:57:03 +08:00
}
2022-05-26 18:02:00 +08:00
func callbackAfterSendGroupMsg(msg *pbChat.SendMsgReq) cbApi.CommonCallbackResp {
callbackResp := cbApi.CommonCallbackResp{OperationID: msg.OperationID}
2022-03-02 19:07:17 +08:00
if !config.Config.Callback.CallbackAfterSendGroupMsg.Enable {
2022-05-26 18:02:00 +08:00
return callbackResp
2022-02-28 17:57:03 +08:00
}
2022-03-03 16:23:59 +08:00
log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), msg)
2022-03-03 18:49:36 +08:00
commonCallbackReq := copyCallbackCommonReqStruct(msg)
2022-03-04 14:27:54 +08:00
commonCallbackReq.CallbackCommand = constant.CallbackAfterSendGroupMsgCommand
req := cbApi.CallbackAfterSendGroupMsgReq{
2022-03-03 18:49:36 +08:00
CommonCallbackReq: commonCallbackReq,
2022-03-04 17:16:21 +08:00
GroupID: msg.MsgData.GroupID,
2022-03-03 17:53:37 +08:00
}
2022-11-08 17:10:27 +08:00
resp := &cbApi.CallbackAfterSendGroupMsgResp{CommonCallbackResp: &callbackResp}
2022-03-03 16:50:06 +08:00
defer log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), req, *resp)
2023-01-11 16:23:16 +08:00
if err := http.CallBackPostReturn(config.Config.Callback.CallbackUrl, constant.CallbackAfterSendGroupMsgCommand, req, resp,
config.Config.Callback.CallbackAfterSendGroupMsg.CallbackTimeOut, nil); err != nil {
2022-05-26 18:02:00 +08:00
callbackResp.ErrCode = http2.StatusInternalServerError
callbackResp.ErrMsg = err.Error()
return callbackResp
2022-03-02 19:07:17 +08:00
}
2022-05-26 18:02:00 +08:00
return callbackResp
2022-02-28 17:57:03 +08:00
}
2022-10-21 15:44:55 +08:00
func callbackMsgModify(msg *pbChat.SendMsgReq) cbApi.CommonCallbackResp {
log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), msg)
2022-05-26 18:02:00 +08:00
callbackResp := cbApi.CommonCallbackResp{OperationID: msg.OperationID}
2022-10-21 15:44:55 +08:00
if !config.Config.Callback.CallbackMsgModify.Enable || msg.MsgData.ContentType != constant.Text {
2022-05-26 18:02:00 +08:00
return callbackResp
2022-02-28 17:57:03 +08:00
}
2022-03-03 18:49:36 +08:00
commonCallbackReq := copyCallbackCommonReqStruct(msg)
2022-10-21 15:44:55 +08:00
commonCallbackReq.CallbackCommand = constant.CallbackMsgModifyCommand
req := cbApi.CallbackMsgModifyCommandReq{
2022-03-03 18:49:36 +08:00
CommonCallbackReq: commonCallbackReq,
2022-03-03 17:53:37 +08:00
}
2022-11-08 17:10:27 +08:00
resp := &cbApi.CallbackMsgModifyCommandResp{CommonCallbackResp: &callbackResp}
2022-03-03 16:47:03 +08:00
defer log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), req, *resp)
2023-01-11 16:23:16 +08:00
if err := http.CallBackPostReturn(config.Config.Callback.CallbackUrl, constant.CallbackMsgModifyCommand, req, resp,
config.Config.Callback.CallbackMsgModify.CallbackTimeOut, &config.Config.Callback.CallbackMsgModify.CallbackFailedContinue); err != nil {
2022-05-26 18:02:00 +08:00
callbackResp.ErrCode = http2.StatusInternalServerError
callbackResp.ErrMsg = err.Error()
2022-10-21 15:44:55 +08:00
if !config.Config.Callback.CallbackMsgModify.CallbackFailedContinue {
2022-05-26 18:02:00 +08:00
callbackResp.ActionCode = constant.ActionForbidden
return callbackResp
2022-03-04 18:23:58 +08:00
} else {
2022-05-26 18:02:00 +08:00
callbackResp.ActionCode = constant.ActionAllow
return callbackResp
2022-03-02 19:07:17 +08:00
}
}
2022-10-21 15:44:55 +08:00
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
}
2022-05-26 18:02:00 +08:00
}
log.NewDebug(msg.OperationID, utils.GetSelfFuncName(), string(msg.MsgData.Content))
return callbackResp
2022-02-28 17:57:03 +08:00
}