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

121 lines
4.9 KiB

3 years ago
package msg
import (
2 years ago
"context"
2 years ago
2 years ago
cbapi "github.com/OpenIMSDK/Open-IM-Server/pkg/callbackstruct"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/http"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext"
2 years ago
pbChat "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/msg"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
3 years ago
)
2 years ago
func cbURL() string {
return config.Config.Callback.CallbackUrl
}
func toCommonCallback(ctx context.Context, msg *pbChat.SendMsgReq, command string) cbapi.CommonCallbackReq {
return cbapi.CommonCallbackReq{
3 years ago
SendID: msg.MsgData.SendID,
ServerMsgID: msg.MsgData.ServerMsgID,
2 years ago
CallbackCommand: command,
3 years ago
ClientMsgID: msg.MsgData.ClientMsgID,
2 years ago
OperationID: mcontext.GetOperationID(ctx),
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,
Content: utils.GetContent(msg.MsgData),
Seq: uint32(msg.MsgData.Seq),
Ex: msg.MsgData.Ex,
3 years ago
}
}
2 years ago
func CallbackBeforeSendSingleMsg(ctx context.Context, msg *pbChat.SendMsgReq) error {
3 years ago
if !config.Config.Callback.CallbackBeforeSendSingleMsg.Enable {
2 years ago
return nil
3 years ago
}
2 years ago
req := &cbapi.CallbackBeforeSendSingleMsgReq{
CommonCallbackReq: toCommonCallback(ctx, msg, constant.CallbackBeforeSendSingleMsgCommand),
3 years ago
RecvID: msg.MsgData.RecvID,
3 years ago
}
2 years ago
resp := &cbapi.CallbackBeforeSendSingleMsgResp{}
2 years ago
return http.CallBackPostReturn(cbURL(), req, resp, config.Config.Callback.CallbackBeforeSendSingleMsg)
3 years ago
}
2 years ago
func CallbackAfterSendSingleMsg(ctx context.Context, msg *pbChat.SendMsgReq) error {
3 years ago
if !config.Config.Callback.CallbackAfterSendSingleMsg.Enable {
2 years ago
return nil
3 years ago
}
2 years ago
req := &cbapi.CallbackAfterSendSingleMsgReq{
CommonCallbackReq: toCommonCallback(ctx, msg, constant.CallbackBeforeSendSingleMsgCommand),
3 years ago
RecvID: msg.MsgData.RecvID,
3 years ago
}
2 years ago
resp := &cbapi.CallbackAfterSendSingleMsgResp{}
2 years ago
return http.CallBackPostReturn(cbURL(), req, resp, config.Config.Callback.CallbackAfterSendSingleMsg)
3 years ago
}
2 years ago
func CallbackBeforeSendGroupMsg(ctx context.Context, msg *pbChat.SendMsgReq) error {
if !config.Config.Callback.CallbackAfterSendSingleMsg.Enable {
return nil
3 years ago
}
2 years ago
req := &cbapi.CallbackAfterSendGroupMsgReq{
CommonCallbackReq: toCommonCallback(ctx, msg, constant.CallbackBeforeSendSingleMsgCommand),
3 years ago
GroupID: msg.MsgData.GroupID,
3 years ago
}
2 years ago
resp := &cbapi.CallbackBeforeSendGroupMsgResp{}
2 years ago
return http.CallBackPostReturn(cbURL(), req, resp, config.Config.Callback.CallbackAfterSendSingleMsg)
3 years ago
}
2 years ago
func CallbackAfterSendGroupMsg(ctx context.Context, msg *pbChat.SendMsgReq) error {
3 years ago
if !config.Config.Callback.CallbackAfterSendGroupMsg.Enable {
2 years ago
return nil
3 years ago
}
2 years ago
req := &cbapi.CallbackAfterSendGroupMsgReq{
CommonCallbackReq: toCommonCallback(ctx, msg, constant.CallbackAfterSendGroupMsgCommand),
3 years ago
GroupID: msg.MsgData.GroupID,
}
2 years ago
resp := &cbapi.CallbackAfterSendGroupMsgResp{}
2 years ago
return http.CallBackPostReturn(cbURL(), req, resp, config.Config.Callback.CallbackAfterSendGroupMsg)
3 years ago
}
2 years ago
func CallbackMsgModify(ctx context.Context, msg *pbChat.SendMsgReq) error {
2 years ago
if !config.Config.Callback.CallbackMsgModify.Enable || msg.MsgData.ContentType != constant.Text {
2 years ago
return nil
}
req := &cbapi.CallbackMsgModifyCommandReq{
CommonCallbackReq: toCommonCallback(ctx, msg, constant.CallbackMsgModifyCommand),
}
resp := &cbapi.CallbackMsgModifyCommandResp{}
2 years ago
if err := http.CallBackPostReturn(cbURL(), req, resp, config.Config.Callback.CallbackAfterSendGroupMsg); err != nil {
2 years ago
return err
}
if resp.Content != nil {
msg.MsgData.Content = []byte(*resp.Content)
}
utils.NotNilReplace(msg.MsgData.OfflinePushInfo, resp.OfflinePushInfo)
utils.NotNilReplace(&msg.MsgData.RecvID, resp.RecvID)
utils.NotNilReplace(&msg.MsgData.GroupID, resp.GroupID)
utils.NotNilReplace(&msg.MsgData.ClientMsgID, resp.ClientMsgID)
utils.NotNilReplace(&msg.MsgData.ServerMsgID, resp.ServerMsgID)
utils.NotNilReplace(&msg.MsgData.SenderPlatformID, resp.SenderPlatformID)
utils.NotNilReplace(&msg.MsgData.SenderNickname, resp.SenderNickname)
utils.NotNilReplace(&msg.MsgData.SenderFaceURL, resp.SenderFaceURL)
utils.NotNilReplace(&msg.MsgData.SessionType, resp.SessionType)
utils.NotNilReplace(&msg.MsgData.MsgFrom, resp.MsgFrom)
utils.NotNilReplace(&msg.MsgData.ContentType, resp.ContentType)
utils.NotNilReplace(&msg.MsgData.Status, resp.Status)
utils.NotNilReplace(&msg.MsgData.Options, resp.Options)
utils.NotNilReplace(&msg.MsgData.AtUserIDList, resp.AtUserIDList)
utils.NotNilReplace(&msg.MsgData.AttachedInfo, resp.AttachedInfo)
utils.NotNilReplace(&msg.MsgData.Ex, resp.Ex)
return nil
3 years ago
}