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

303 lines
11 KiB
Go
Raw Normal View History

2021-12-23 17:19:57 +08:00
package msg
2021-05-26 19:40:38 +08:00
import (
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/constant"
2021-12-06 20:03:59 +08:00
"Open_IM/pkg/common/db"
http2 "Open_IM/pkg/common/http"
"Open_IM/pkg/common/log"
"Open_IM/pkg/grpc-etcdv3/getcdv3"
2021-10-11 22:00:38 +08:00
pbChat "Open_IM/pkg/proto/chat"
pbGroup "Open_IM/pkg/proto/group"
2021-12-23 17:19:57 +08:00
sdk_ws "Open_IM/pkg/proto/sdk_ws"
"Open_IM/pkg/utils"
2021-05-26 19:40:38 +08:00
"context"
2021-06-28 15:35:18 +08:00
"encoding/json"
2021-12-23 17:19:57 +08:00
"github.com/garyburd/redigo/redis"
2021-05-26 19:40:38 +08:00
"math/rand"
2021-06-28 15:35:18 +08:00
"net/http"
2021-05-26 19:40:38 +08:00
"strconv"
2021-06-28 15:35:18 +08:00
"strings"
2021-05-26 19:40:38 +08:00
"time"
)
2021-06-28 15:35:18 +08:00
type MsgCallBackReq struct {
SendID string `json:"sendID"`
RecvID string `json:"recvID"`
Content string `json:"content"`
SendTime int64 `json:"sendTime"`
MsgFrom int32 `json:"msgFrom"`
ContentType int32 `json:"contentType"`
SessionType int32 `json:"sessionType"`
PlatformID int32 `json:"senderPlatformID"`
MsgID string `json:"msgID"`
IsOnlineOnly bool `json:"isOnlineOnly"`
2021-06-28 15:35:18 +08:00
}
type MsgCallBackResp struct {
ErrCode int32 `json:"errCode"`
ErrMsg string `json:"errMsg"`
ResponseErrCode int32 `json:"responseErrCode"`
ResponseResult struct {
ModifiedMsg string `json:"modifiedMsg"`
Ext string `json:"ext"`
}
}
2021-05-26 19:40:38 +08:00
2021-12-23 17:19:57 +08:00
func (rpc *rpcChat) encapsulateMsgData(msg *sdk_ws.MsgData) {
msg.ServerMsgID = GetMsgID(msg.SendID)
if msg.SendTime == 0 {
2022-01-18 11:54:48 +08:00
msg.SendTime = utils.GetCurrentTimestampByMill()
2021-12-23 17:19:57 +08:00
}
switch msg.ContentType {
case constant.Text:
fallthrough
case constant.Picture:
fallthrough
case constant.Voice:
fallthrough
case constant.Video:
fallthrough
case constant.File:
fallthrough
case constant.AtText:
fallthrough
case constant.Merger:
fallthrough
case constant.Card:
fallthrough
case constant.Location:
fallthrough
case constant.Custom:
fallthrough
case constant.Quote:
utils.SetSwitchFromOptions(msg.Options, constant.IsConversationUpdate, true)
utils.SetSwitchFromOptions(msg.Options, constant.IsUnreadCount, true)
utils.SetSwitchFromOptions(msg.Options, constant.IsSenderSync, true)
case constant.Revoke:
utils.SetSwitchFromOptions(msg.Options, constant.IsUnreadCount, false)
utils.SetSwitchFromOptions(msg.Options, constant.IsOfflinePush, false)
case constant.HasReadReceipt:
utils.SetSwitchFromOptions(msg.Options, constant.IsConversationUpdate, false)
utils.SetSwitchFromOptions(msg.Options, constant.IsUnreadCount, false)
utils.SetSwitchFromOptions(msg.Options, constant.IsOfflinePush, false)
case constant.Typing:
utils.SetSwitchFromOptions(msg.Options, constant.IsHistory, false)
utils.SetSwitchFromOptions(msg.Options, constant.IsPersistent, false)
utils.SetSwitchFromOptions(msg.Options, constant.IsSenderSync, false)
utils.SetSwitchFromOptions(msg.Options, constant.IsConversationUpdate, false)
utils.SetSwitchFromOptions(msg.Options, constant.IsUnreadCount, false)
utils.SetSwitchFromOptions(msg.Options, constant.IsOfflinePush, false)
}
}
func (rpc *rpcChat) SendMsg(_ context.Context, pb *pbChat.SendMsgReq) (*pbChat.SendMsgResp, error) {
replay := pbChat.SendMsgResp{}
log.NewDebug(pb.OperationID, "rpc sendMsg come here", pb.String())
//if !utils.VerifyToken(pb.Token, pb.SendID) {
// return returnMsg(&replay, pb, http.StatusUnauthorized, "token validate err,not authorized", "", 0)
2021-12-23 17:19:57 +08:00
rpc.encapsulateMsgData(pb.MsgData)
msgToMQ := pbChat.MsgDataToMQ{Token: pb.Token, OperationID: pb.OperationID}
//options := utils.JsonStringToMap(pbData.Options)
isHistory := utils.GetSwitchFromOptions(pb.MsgData.Options, constant.IsHistory)
mReq := MsgCallBackReq{
2021-12-23 17:19:57 +08:00
SendID: pb.MsgData.SendID,
RecvID: pb.MsgData.RecvID,
Content: string(pb.MsgData.Content),
SendTime: pb.MsgData.SendTime,
MsgFrom: pb.MsgData.MsgFrom,
ContentType: pb.MsgData.ContentType,
SessionType: pb.MsgData.SessionType,
PlatformID: pb.MsgData.SenderPlatformID,
MsgID: pb.MsgData.ClientMsgID,
}
if !isHistory {
mReq.IsOnlineOnly = true
}
mResp := MsgCallBackResp{}
2021-06-28 15:35:18 +08:00
if config.Config.MessageCallBack.CallbackSwitch {
bMsg, err := http2.Post(config.Config.MessageCallBack.CallbackUrl, mReq, config.Config.MessageCallBack.CallBackTimeOut)
2021-06-28 15:35:18 +08:00
if err != nil {
log.ErrorByKv("callback to Business server err", pb.OperationID, "args", pb.String(), "err", err.Error())
return returnMsg(&replay, pb, http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError), "", 0)
} else if err = json.Unmarshal(bMsg, &mResp); err != nil {
2021-06-28 15:35:18 +08:00
log.ErrorByKv("ws json Unmarshal err", pb.OperationID, "args", pb.String(), "err", err.Error())
return returnMsg(&replay, pb, 200, err.Error(), "", 0)
} else {
if mResp.ErrCode != 0 {
return returnMsg(&replay, pb, mResp.ResponseErrCode, mResp.ErrMsg, "", 0)
2021-06-28 15:35:18 +08:00
} else {
2021-12-23 17:19:57 +08:00
pb.MsgData.Content = []byte(mResp.ResponseResult.ModifiedMsg)
2021-06-28 15:35:18 +08:00
}
}
2021-10-21 12:10:38 +08:00
}
2021-12-23 17:19:57 +08:00
switch pb.MsgData.SessionType {
2021-10-21 12:10:38 +08:00
case constant.SingleChatType:
2021-12-23 17:19:57 +08:00
isSend := modifyMessageByUserMessageReceiveOpt(pb.MsgData.RecvID, pb.MsgData.SendID, constant.SingleChatType, pb)
2021-12-06 20:03:59 +08:00
if isSend {
2021-12-23 17:19:57 +08:00
msgToMQ.MsgData = pb.MsgData
err1 := rpc.sendMsgToKafka(&msgToMQ, msgToMQ.MsgData.RecvID)
2021-12-06 20:03:59 +08:00
if err1 != nil {
2021-12-23 17:19:57 +08:00
log.NewError(msgToMQ.OperationID, "kafka send msg err:RecvID", msgToMQ.MsgData.RecvID, msgToMQ.String())
2021-12-06 20:03:59 +08:00
return returnMsg(&replay, pb, 201, "kafka send msg err", "", 0)
}
}
2021-12-28 20:44:19 +08:00
if msgToMQ.MsgData.SendID != msgToMQ.MsgData.RecvID { //Filter messages sent to yourself
err2 := rpc.sendMsgToKafka(&msgToMQ, msgToMQ.MsgData.SendID)
if err2 != nil {
log.NewError(msgToMQ.OperationID, "kafka send msg err:SendID", msgToMQ.MsgData.SendID, msgToMQ.String())
return returnMsg(&replay, pb, 201, "kafka send msg err", "", 0)
}
2021-10-21 12:10:38 +08:00
}
2021-12-23 17:19:57 +08:00
return returnMsg(&replay, pb, 0, "", msgToMQ.MsgData.ServerMsgID, msgToMQ.MsgData.SendTime)
2021-10-21 12:10:38 +08:00
case constant.GroupChatType:
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName)
client := pbGroup.NewGroupClient(etcdConn)
req := &pbGroup.GetGroupAllMemberReq{
2021-12-23 17:19:57 +08:00
GroupID: pb.MsgData.GroupID,
OperationID: pb.OperationID,
2021-10-21 12:10:38 +08:00
}
reply, err := client.GetGroupAllMember(context.Background(), req)
if err != nil {
2021-12-23 17:19:57 +08:00
log.Error(pb.Token, pb.OperationID, "rpc send_msg getGroupInfo failed, err = %s", err.Error())
2021-10-21 12:10:38 +08:00
return returnMsg(&replay, pb, 201, err.Error(), "", 0)
}
2021-12-23 17:19:57 +08:00
if reply.ErrCode != 0 {
log.Error(pb.Token, pb.OperationID, "rpc send_msg getGroupInfo failed, err = %s", reply.ErrMsg)
return returnMsg(&replay, pb, reply.ErrCode, reply.ErrMsg, "", 0)
2021-10-21 12:10:38 +08:00
}
2021-12-23 17:19:57 +08:00
groupID := pb.MsgData.GroupID
for _, v := range reply.MemberList {
2021-12-23 19:37:41 +08:00
pb.MsgData.RecvID = v.UserID
isSend := modifyMessageByUserMessageReceiveOpt(v.UserID, groupID, constant.GroupChatType, pb)
2021-12-06 20:03:59 +08:00
if isSend {
2021-12-23 17:19:57 +08:00
msgToMQ.MsgData = pb.MsgData
2021-12-23 19:37:41 +08:00
err := rpc.sendMsgToKafka(&msgToMQ, v.UserID)
2021-12-06 20:03:59 +08:00
if err != nil {
2021-12-23 19:37:41 +08:00
log.NewError(msgToMQ.OperationID, "kafka send msg err:UserId", v.UserID, msgToMQ.String())
2021-12-06 20:03:59 +08:00
return returnMsg(&replay, pb, 201, "kafka send msg err", "", 0)
}
2021-10-21 12:10:38 +08:00
}
2021-12-06 20:03:59 +08:00
2021-10-21 12:10:38 +08:00
}
2021-12-23 17:19:57 +08:00
return returnMsg(&replay, pb, 0, "", msgToMQ.MsgData.ServerMsgID, msgToMQ.MsgData.SendTime)
2021-10-21 12:10:38 +08:00
default:
return returnMsg(&replay, pb, 203, "unkonwn sessionType", "", 0)
2021-12-20 16:29:24 +08:00
}
2021-05-26 19:40:38 +08:00
}
2021-12-10 17:30:11 +08:00
2021-12-23 17:19:57 +08:00
func (rpc *rpcChat) sendMsgToKafka(m *pbChat.MsgDataToMQ, key string) error {
2021-05-26 19:40:38 +08:00
pid, offset, err := rpc.producer.SendMessage(m, key)
if err != nil {
2021-06-28 15:35:18 +08:00
log.ErrorByKv("kafka send failed", m.OperationID, "send data", m.String(), "pid", pid, "offset", offset, "err", err.Error(), "key", key)
2021-05-26 19:40:38 +08:00
}
2021-08-06 14:56:41 +08:00
return err
2021-05-26 19:40:38 +08:00
}
func GetMsgID(sendID string) string {
t := time.Now().Format("2006-01-02 15:04:05")
return t + "-" + sendID + "-" + strconv.Itoa(rand.Int())
}
2021-12-21 21:40:50 +08:00
2021-12-23 17:19:57 +08:00
func returnMsg(replay *pbChat.SendMsgResp, pb *pbChat.SendMsgReq, errCode int32, errMsg, serverMsgID string, sendTime int64) (*pbChat.SendMsgResp, error) {
2021-06-28 15:35:18 +08:00
replay.ErrCode = errCode
replay.ErrMsg = errMsg
replay.ServerMsgID = serverMsgID
2021-12-23 17:19:57 +08:00
replay.ClientMsgID = pb.MsgData.ClientMsgID
2021-06-28 15:35:18 +08:00
replay.SendTime = sendTime
return replay, nil
}
2021-12-21 21:40:50 +08:00
2021-12-23 17:19:57 +08:00
func modifyMessageByUserMessageReceiveOpt(userID, sourceID string, sessionType int, pb *pbChat.SendMsgReq) bool {
2021-12-06 20:03:59 +08:00
conversationID := utils.GetConversationIDBySessionType(sourceID, sessionType)
2021-12-07 10:22:49 +08:00
opt, err := db.DB.GetSingleConversationMsgOpt(userID, conversationID)
2021-12-23 17:19:57 +08:00
if err != nil || err != redis.ErrNil {
2022-01-20 18:04:24 +08:00
log.NewError(pb.OperationID, "GetSingleConversationMsgOpt from redis err", conversationID, pb.String(), err.Error())
2021-12-06 20:03:59 +08:00
return true
}
switch opt {
case constant.ReceiveMessage:
return true
case constant.NotReceiveMessage:
return false
case constant.ReceiveNotNotifyMessage:
2021-12-23 17:19:57 +08:00
if pb.MsgData.Options == nil {
pb.MsgData.Options = make(map[string]bool, 10)
2021-12-08 18:30:23 +08:00
}
2021-12-23 17:19:57 +08:00
utils.SetSwitchFromOptions(pb.MsgData.Options, constant.IsOfflinePush, false)
2021-12-06 20:03:59 +08:00
return true
}
return true
}
2021-12-21 21:40:50 +08:00
type NotificationMsg struct {
SendID string
RecvID string
2022-01-15 11:14:29 +08:00
Content []byte // open_im_sdk.TipsComm
2021-12-21 21:40:50 +08:00
MsgFrom int32
ContentType int32
SessionType int32
OperationID string
}
2022-01-14 13:44:46 +08:00
func Notification(n *NotificationMsg) {
2022-01-18 14:06:42 +08:00
return
2021-12-23 17:19:57 +08:00
var req pbChat.SendMsgReq
var msg sdk_ws.MsgData
var offlineInfo sdk_ws.OfflinePushInfo
2021-12-27 11:49:01 +08:00
var title, desc, ex string
2021-12-23 17:19:57 +08:00
var pushSwitch bool
req.OperationID = n.OperationID
msg.SendID = n.SendID
msg.RecvID = n.RecvID
msg.Content = n.Content
msg.MsgFrom = n.MsgFrom
msg.ContentType = n.ContentType
msg.SessionType = n.SessionType
2022-01-18 11:54:48 +08:00
msg.CreateTime = utils.GetCurrentTimestampByMill()
2021-12-23 17:19:57 +08:00
msg.ClientMsgID = utils.GetMsgID(n.SendID)
switch n.SessionType {
case constant.GroupChatType:
msg.RecvID = ""
msg.GroupID = n.RecvID
}
2022-01-18 09:40:08 +08:00
if true {
2021-12-23 17:19:57 +08:00
msg.Options = make(map[string]bool, 10)
2021-12-27 11:49:01 +08:00
//utils.SetSwitchFromOptions(msg.Options, constant.IsOfflinePush, false)
2021-12-23 17:19:57 +08:00
utils.SetSwitchFromOptions(msg.Options, constant.IsHistory, false)
utils.SetSwitchFromOptions(msg.Options, constant.IsPersistent, false)
}
offlineInfo.IOSBadgeCount = config.Config.IOSPush.BadgeCount
offlineInfo.IOSPushSound = config.Config.IOSPush.PushSound
2022-01-18 09:40:08 +08:00
//switch msg.ContentType {
//case constant.GroupCreatedNotification:
// pushSwitch = config.Config.Notification.GroupCreated.OfflinePush.PushSwitch
// title = config.Config.Notification.GroupCreated.OfflinePush.Title
// desc = config.Config.Notification.GroupCreated.OfflinePush.Desc
// ex = config.Config.Notification.GroupCreated.OfflinePush.Ext
//case constant.GroupInfoChangedNotification:
// pushSwitch = config.Config.Notification.GroupInfoChanged.OfflinePush.PushSwitch
// title = config.Config.Notification.GroupInfoChanged.OfflinePush.Title
// desc = config.Config.Notification.GroupInfoChanged.OfflinePush.Desc
// ex = config.Config.Notification.GroupInfoChanged.OfflinePush.Ext
//case constant.JoinApplicationNotification:
// pushSwitch = config.Config.Notification.ApplyJoinGroup.OfflinePush.PushSwitch
// title = config.Config.Notification.ApplyJoinGroup.OfflinePush.Title
// desc = config.Config.Notification.ApplyJoinGroup.OfflinePush.Desc
// ex = config.Config.Notification.ApplyJoinGroup.OfflinePush.Ext
//}
2021-12-23 17:19:57 +08:00
utils.SetSwitchFromOptions(msg.Options, constant.IsOfflinePush, pushSwitch)
offlineInfo.Title = title
offlineInfo.Desc = desc
2021-12-27 11:49:01 +08:00
offlineInfo.Ex = ex
2021-12-23 17:19:57 +08:00
msg.OfflinePushInfo = &offlineInfo
req.MsgData = &msg
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImOfflineMessageName)
client := pbChat.NewChatClient(etcdConn)
reply, err := client.SendMsg(context.Background(), &req)
if err != nil {
log.NewError(req.OperationID, "SendMsg rpc failed, ", req.String(), err.Error())
} else if reply.ErrCode != 0 {
log.NewError(req.OperationID, "SendMsg rpc failed, ", req.String())
}
2021-12-21 21:40:50 +08:00
}