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/send.go

107 lines
3.8 KiB

package msg
import (
"context"
2 years ago
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
2 years ago
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"
2 years ago
pbMsg "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/msg"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
)
2 years ago
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")
}
}
2 years ago
func (m *msgServer) sendMsgSuperGroupChat(ctx context.Context, req *pbMsg.SendMsgReq) (resp *pbMsg.SendMsgResp, err error) {
resp = &pbMsg.SendMsgResp{}
2 years ago
promePkg.Inc(promePkg.WorkSuperGroupChatMsgRecvSuccessCounter)
if _, err = m.messageVerification(ctx, req); err != nil {
2 years ago
promePkg.Inc(promePkg.WorkSuperGroupChatMsgProcessFailedCounter)
return nil, err
}
2 years ago
err = m.MsgDatabase.MsgToMQ(ctx, utils.GenConversationUniqueKeyForGroup(req.MsgData.GroupID), req.MsgData)
if err != nil {
return nil, err
}
2 years ago
if err = callbackAfterSendGroupMsg(ctx, req); err != nil {
2 years ago
log.ZError(ctx, "CallbackAfterSendGroupMsg", err)
}
2 years ago
promePkg.Inc(promePkg.WorkSuperGroupChatMsgProcessSuccessCounter)
2 years ago
resp.SendTime = req.MsgData.SendTime
resp.ServerMsgID = req.MsgData.ServerMsgID
resp.ClientMsgID = req.MsgData.ClientMsgID
return resp, nil
}
2 years ago
2 years ago
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
}
2 years ago
resp = &pbMsg.SendMsgResp{
2 years ago
ServerMsgID: req.MsgData.ServerMsgID,
ClientMsgID: req.MsgData.ClientMsgID,
SendTime: req.MsgData.SendTime,
2 years ago
}
return resp, nil
}
2 years ago
func (m *msgServer) sendMsgSingleChat(ctx context.Context, req *pbMsg.SendMsgReq) (resp *pbMsg.SendMsgResp, err error) {
2 years ago
promePkg.Inc(promePkg.SingleChatMsgRecvSuccessCounter)
_, err = m.messageVerification(ctx, req)
if err != nil {
return nil, err
}
2 years ago
var isSend bool = true
2 years ago
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)
2 years ago
if err != nil {
return nil, err
}
}
2 years ago
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
}
2 years ago
err = callbackAfterSendSingleMsg(ctx, req)
2 years ago
if err != nil && err != errs.ErrCallbackContinue {
return nil, err
}
2 years ago
resp = &msg.SendMsgResp{
ServerMsgID: req.MsgData.ServerMsgID,
ClientMsgID: req.MsgData.ClientMsgID,
SendTime: req.MsgData.SendTime,
}
2 years ago
promePkg.Inc(promePkg.SingleChatMsgProcessSuccessCounter)
return resp, nil
}
}