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.
150 lines
5.3 KiB
150 lines
5.3 KiB
package msg
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
"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/log"
|
|
promePkg "github.com/OpenIMSDK/Open-IM-Server/pkg/common/prome"
|
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
|
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/msg"
|
|
pbMsg "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/msg"
|
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws"
|
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
func (m *msgServer) SendMsg(ctx context.Context, req *msg.SendMsgReq) (resp *msg.SendMsgResp, error error) {
|
|
resp = &msg.SendMsgResp{}
|
|
flag := isMessageHasReadEnabled(req.MsgData)
|
|
if !flag {
|
|
return nil, errs.ErrMessageHasReadDisable.Wrap()
|
|
}
|
|
m.encapsulateMsgData(req.MsgData)
|
|
if err := callbackMsgModify(ctx, req); err != nil && err != errs.ErrCallbackContinue {
|
|
return nil, err
|
|
}
|
|
switch req.MsgData.SessionType {
|
|
case constant.SingleChatType:
|
|
return m.sendMsgSingleChat(ctx, req)
|
|
case constant.NotificationChatType:
|
|
return m.sendMsgNotification(ctx, req)
|
|
case constant.SuperGroupChatType:
|
|
return m.sendMsgSuperGroupChat(ctx, req)
|
|
default:
|
|
return nil, errs.ErrArgs.Wrap("unknown sessionType")
|
|
}
|
|
}
|
|
|
|
func (m *msgServer) sendMsgSuperGroupChat(ctx context.Context, req *pbMsg.SendMsgReq) (resp *pbMsg.SendMsgResp, err error) {
|
|
resp = &pbMsg.SendMsgResp{}
|
|
promePkg.Inc(promePkg.WorkSuperGroupChatMsgRecvSuccessCounter)
|
|
if _, err = m.messageVerification(ctx, req); err != nil {
|
|
promePkg.Inc(promePkg.WorkSuperGroupChatMsgProcessFailedCounter)
|
|
return nil, err
|
|
}
|
|
err = m.MsgDatabase.MsgToMQ(ctx, utils.GenConversationUniqueKeyForGroup(req.MsgData.GroupID), req.MsgData)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err = callbackAfterSendGroupMsg(ctx, req); err != nil {
|
|
log.ZError(ctx, "CallbackAfterSendGroupMsg", err)
|
|
}
|
|
promePkg.Inc(promePkg.WorkSuperGroupChatMsgProcessSuccessCounter)
|
|
resp.SendTime = req.MsgData.SendTime
|
|
resp.ServerMsgID = req.MsgData.ServerMsgID
|
|
resp.ClientMsgID = req.MsgData.ClientMsgID
|
|
return resp, nil
|
|
}
|
|
|
|
func (m *msgServer) sendMsgNotification(ctx context.Context, req *pbMsg.SendMsgReq) (resp *pbMsg.SendMsgResp, err error) {
|
|
promePkg.Inc(promePkg.SingleChatMsgRecvSuccessCounter)
|
|
if err := m.MsgDatabase.MsgToMQ(ctx, utils.GenConversationUniqueKeyForSingle(req.MsgData.SendID, req.MsgData.RecvID), req.MsgData); err != nil {
|
|
promePkg.Inc(promePkg.SingleChatMsgProcessFailedCounter)
|
|
return nil, err
|
|
}
|
|
resp = &pbMsg.SendMsgResp{
|
|
ServerMsgID: req.MsgData.ServerMsgID,
|
|
ClientMsgID: req.MsgData.ClientMsgID,
|
|
SendTime: req.MsgData.SendTime,
|
|
}
|
|
return resp, nil
|
|
}
|
|
|
|
func (m *msgServer) sendMsgSingleChat(ctx context.Context, req *pbMsg.SendMsgReq) (resp *pbMsg.SendMsgResp, err error) {
|
|
promePkg.Inc(promePkg.SingleChatMsgRecvSuccessCounter)
|
|
_, err = m.messageVerification(ctx, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var isSend bool = true
|
|
isNotification := utils.IsNotificationByMsg(req.MsgData)
|
|
if !isNotification {
|
|
isSend, err = m.modifyMessageByUserMessageReceiveOpt(ctx, req.MsgData.RecvID, utils.GenConversationIDForSingle(req.MsgData.SendID, req.MsgData.RecvID), constant.SingleChatType, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if !isSend {
|
|
promePkg.Inc(promePkg.SingleChatMsgProcessFailedCounter)
|
|
return nil, errs.ErrUserNotRecvMsg
|
|
} else {
|
|
if err := m.MsgDatabase.MsgToMQ(ctx, utils.GenConversationUniqueKeyForSingle(req.MsgData.SendID, req.MsgData.RecvID), req.MsgData); err != nil {
|
|
promePkg.Inc(promePkg.SingleChatMsgProcessFailedCounter)
|
|
return nil, err
|
|
}
|
|
err = callbackAfterSendSingleMsg(ctx, req)
|
|
if err != nil && err != errs.ErrCallbackContinue {
|
|
return nil, err
|
|
}
|
|
resp = &msg.SendMsgResp{
|
|
ServerMsgID: req.MsgData.ServerMsgID,
|
|
ClientMsgID: req.MsgData.ClientMsgID,
|
|
SendTime: req.MsgData.SendTime,
|
|
}
|
|
promePkg.Inc(promePkg.SingleChatMsgProcessSuccessCounter)
|
|
return resp, nil
|
|
}
|
|
}
|
|
|
|
func (m *msgServer) notification(ctx context.Context, sendID, recvID string, contentType, sessionType int32, msgPb proto.Message, cfg config.NotificationConf, opts ...utils.OptionsOpt) error {
|
|
n := sdkws.NotificationElem{Detail: utils.StructToJsonString(m)}
|
|
content, err := json.Marshal(&n)
|
|
if err != nil {
|
|
log.ZError(ctx, "MsgClient Notification json.Marshal failed", err, "sendID", sendID, "recvID", recvID, "contentType", contentType, "msg", m)
|
|
return err
|
|
}
|
|
var req msg.SendMsgReq
|
|
var msg sdkws.MsgData
|
|
var offlineInfo sdkws.OfflinePushInfo
|
|
var title, desc, ex string
|
|
msg.SendID = sendID
|
|
msg.RecvID = recvID
|
|
msg.Content = content
|
|
msg.MsgFrom = constant.SysMsgType
|
|
msg.ContentType = contentType
|
|
msg.SessionType = sessionType
|
|
if msg.SessionType == constant.SuperGroupChatType {
|
|
msg.GroupID = recvID
|
|
}
|
|
msg.CreateTime = utils.GetCurrentTimestampByMill()
|
|
msg.ClientMsgID = utils.GetMsgID(sendID)
|
|
options := config.GetOptionsByNotification(cfg)
|
|
options = utils.WithOptions(options, opts...)
|
|
msg.Options = options
|
|
offlineInfo.Title = title
|
|
offlineInfo.Desc = desc
|
|
offlineInfo.Ex = ex
|
|
msg.OfflinePushInfo = &offlineInfo
|
|
req.MsgData = &msg
|
|
_, err = m.SendMsg(ctx, &req)
|
|
if err == nil {
|
|
log.ZDebug(ctx, "MsgClient Notification SendMsg success", "req", &req)
|
|
} else {
|
|
log.ZError(ctx, "MsgClient Notification SendMsg failed", err, "req", &req)
|
|
}
|
|
return err
|
|
}
|