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

312 lines
9.9 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 (
"context"
2023-05-04 15:06:23 +08:00
"math/rand"
"strconv"
"time"
2023-03-16 10:46:06 +08:00
"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/errs"
"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"
2021-05-26 19:40:38 +08:00
)
2022-09-14 17:45:38 +08:00
var (
ExcludeContentType = []int{constant.HasReadReceipt, constant.GroupHasReadReceipt}
)
2022-08-05 19:10:08 +08:00
2022-09-29 17:57:26 +08:00
type Validator interface {
2023-02-10 22:10:37 +08:00
validate(pb *msg.SendMsgReq) (bool, int32, string)
2022-09-29 17:57:26 +08:00
}
type MessageRevoked struct {
RevokerID string `json:"revokerID"`
RevokerRole int32 `json:"revokerRole"`
ClientMsgID string `json:"clientMsgID"`
RevokerNickname string `json:"revokerNickname"`
RevokeTime int64 `json:"revokeTime"`
SourceMessageSendTime int64 `json:"sourceMessageSendTime"`
SourceMessageSendID string `json:"sourceMessageSendID"`
SourceMessageSenderNickname string `json:"sourceMessageSenderNickname"`
SessionType int32 `json:"sessionType"`
Seq uint32 `json:"seq"`
}
2021-05-26 19:40:38 +08:00
2023-02-14 22:04:03 +08:00
func (m *msgServer) userIsMuteAndIsAdminInGroup(ctx context.Context, groupID, userID string) (isMute bool, err error) {
groupMemberInfo, err := m.Group.GetGroupMemberInfo(ctx, groupID, userID)
2022-11-11 18:06:15 +08:00
if err != nil {
2023-02-13 17:48:01 +08:00
return false, err
2022-11-11 18:06:15 +08:00
}
2023-02-13 17:48:01 +08:00
if groupMemberInfo.MuteEndTime >= time.Now().Unix() {
return true, nil
2022-11-11 18:06:15 +08:00
}
2023-02-13 17:48:01 +08:00
return false, nil
2022-11-11 18:06:15 +08:00
}
2023-02-13 17:48:01 +08:00
// 如果禁言了,再看下是否群管理员
2023-02-14 22:04:03 +08:00
func (m *msgServer) groupIsMuted(ctx context.Context, groupID string, userID string) (bool, bool, error) {
groupInfo, err := m.Group.GetGroupInfo(ctx, groupID)
2022-11-29 14:41:20 +08:00
if err != nil {
2023-02-13 17:48:01 +08:00
return false, false, err
2022-11-29 14:41:20 +08:00
}
2023-02-13 17:48:01 +08:00
2022-11-29 14:41:20 +08:00
if groupInfo.Status == constant.GroupStatusMuted {
2023-02-14 22:04:03 +08:00
groupMemberInfo, err := m.Group.GetGroupMemberInfo(ctx, groupID, userID)
2023-02-13 17:48:01 +08:00
if err != nil {
return false, false, err
}
return true, groupMemberInfo.RoleLevel > constant.GroupOrdinaryUsers, nil
2022-11-29 14:41:20 +08:00
}
2023-02-13 17:48:01 +08:00
return false, false, nil
2022-11-29 14:41:20 +08:00
}
2023-02-14 22:04:03 +08:00
func (m *msgServer) GetGroupMemberIDs(ctx context.Context, groupID string) (groupMemberIDs []string, err error) {
return m.GroupLocalCache.GetGroupMemberIDs(ctx, groupID)
2023-02-13 17:48:01 +08:00
}
2023-02-13 18:14:26 +08:00
func (m *msgServer) messageVerification(ctx context.Context, data *msg.SendMsgReq) ([]string, error) {
2022-07-25 19:19:00 +08:00
switch data.MsgData.SessionType {
case constant.SingleChatType:
2022-08-05 15:51:34 +08:00
if utils.IsContain(data.MsgData.SendID, config.Config.Manager.AppManagerUid) {
2023-02-13 17:48:01 +08:00
return nil, nil
2022-08-05 15:51:34 +08:00
}
if data.MsgData.ContentType <= constant.NotificationEnd && data.MsgData.ContentType >= constant.NotificationBegin {
2023-02-13 17:48:01 +08:00
return nil, nil
2022-08-05 15:51:34 +08:00
}
2023-02-14 22:04:03 +08:00
black, err := m.black.IsBlocked(ctx, data.MsgData.SendID, data.MsgData.RecvID)
2023-01-11 11:15:46 +08:00
if err != nil {
2023-02-13 17:48:01 +08:00
return nil, err
2022-06-16 14:26:03 +08:00
}
2023-02-13 17:48:01 +08:00
if black {
2023-03-07 12:19:30 +08:00
return nil, errs.ErrBlockedByPeer.Wrap()
2022-03-15 19:03:22 +08:00
}
2022-11-10 14:51:35 +08:00
if *config.Config.MessageVerify.FriendVerify {
2023-02-14 22:04:03 +08:00
friend, err := m.friend.IsFriend(ctx, data.MsgData.SendID, data.MsgData.RecvID)
2023-01-11 11:15:46 +08:00
if err != nil {
2023-02-13 17:48:01 +08:00
return nil, err
2022-06-16 14:26:03 +08:00
}
2023-02-13 17:48:01 +08:00
if !friend {
2023-03-07 12:19:30 +08:00
return nil, errs.ErrNotPeersFriend.Wrap()
2022-05-28 18:10:08 +08:00
}
2023-02-13 17:48:01 +08:00
return nil, nil
2022-07-25 19:19:00 +08:00
}
2023-02-13 17:48:01 +08:00
return nil, nil
2022-07-25 19:19:00 +08:00
case constant.GroupChatType:
2023-02-13 17:48:01 +08:00
if utils.IsContain(data.MsgData.SendID, config.Config.Manager.AppManagerUid) {
return nil, nil
}
2023-02-14 22:04:03 +08:00
userIDList, err := m.GetGroupMemberIDs(ctx, data.MsgData.GroupID)
2022-10-10 17:51:57 +08:00
if err != nil {
2023-02-13 17:48:01 +08:00
return nil, err
2022-10-10 17:51:57 +08:00
}
2023-02-13 17:48:01 +08:00
if utils.IsContain(data.MsgData.SendID, config.Config.Manager.AppManagerUid) {
return userIDList, nil
2022-11-21 10:20:56 +08:00
}
2022-11-22 14:18:57 +08:00
if data.MsgData.ContentType <= constant.NotificationEnd && data.MsgData.ContentType >= constant.NotificationBegin {
2023-02-13 17:48:01 +08:00
return userIDList, nil
}
if !utils.IsContain(data.MsgData.SendID, userIDList) {
2023-03-07 12:19:30 +08:00
return nil, errs.ErrNotInGroupYet.Wrap()
2022-10-10 17:51:57 +08:00
}
2023-02-14 22:04:03 +08:00
isMute, err := m.userIsMuteAndIsAdminInGroup(ctx, data.MsgData.GroupID, data.MsgData.SendID)
2022-11-29 14:41:20 +08:00
if err != nil {
2023-02-13 17:48:01 +08:00
return nil, err
2022-11-29 14:41:20 +08:00
}
if isMute {
2023-03-07 12:19:30 +08:00
return nil, errs.ErrMutedInGroup.Wrap()
2022-11-29 14:41:20 +08:00
}
2023-02-13 17:48:01 +08:00
2023-02-14 22:04:03 +08:00
isMute, isAdmin, err := m.groupIsMuted(ctx, data.MsgData.GroupID, data.MsgData.SendID)
2022-11-11 18:06:15 +08:00
if err != nil {
2023-02-13 17:48:01 +08:00
return nil, err
}
if isAdmin {
return userIDList, nil
2022-11-11 18:06:15 +08:00
}
2023-02-13 17:48:01 +08:00
2022-11-11 18:06:15 +08:00
if isMute {
2023-03-07 12:19:30 +08:00
return nil, errs.ErrMutedGroup.Wrap()
2022-11-11 18:06:15 +08:00
}
2023-02-13 17:48:01 +08:00
return userIDList, nil
2022-07-25 19:19:00 +08:00
case constant.SuperGroupChatType:
2023-02-14 22:04:03 +08:00
groupInfo, err := m.Group.GetGroupInfo(ctx, data.MsgData.GroupID)
2022-07-25 19:19:00 +08:00
if err != nil {
2023-02-13 17:48:01 +08:00
return nil, err
2022-07-25 19:19:00 +08:00
}
2022-09-29 17:57:26 +08:00
if data.MsgData.ContentType == constant.AdvancedRevoke {
2022-09-29 18:31:36 +08:00
revokeMessage := new(MessageRevoked)
err := utils.JsonStringToStruct(string(data.MsgData.Content), revokeMessage)
2022-09-29 17:57:26 +08:00
if err != nil {
2023-03-07 12:19:30 +08:00
return nil, errs.ErrArgs.Wrap()
2022-09-29 17:57:26 +08:00
}
2023-02-13 17:48:01 +08:00
2022-09-29 19:29:22 +08:00
if revokeMessage.RevokerID != revokeMessage.SourceMessageSendID {
2023-05-05 12:19:04 +08:00
resp, err := m.MsgDatabase.GetMsgBySeqs(ctx, data.MsgData.GroupID, []int64{int64(revokeMessage.Seq)})
2022-09-29 19:29:22 +08:00
if err != nil {
2023-02-13 17:48:01 +08:00
return nil, err
}
2023-02-21 14:30:08 +08:00
if resp[0].ClientMsgID == revokeMessage.ClientMsgID && resp[0].Seq == int64(revokeMessage.Seq) {
revokeMessage.SourceMessageSendTime = resp[0].SendTime
revokeMessage.SourceMessageSenderNickname = resp[0].SenderNickname
revokeMessage.SourceMessageSendID = resp[0].SendID
2023-02-13 17:48:01 +08:00
data.MsgData.Content = []byte(utils.StructToJsonString(revokeMessage))
2022-09-29 17:57:26 +08:00
} else {
2023-03-07 12:19:30 +08:00
return nil, errs.ErrData.Wrap("MsgData")
2022-09-29 17:57:26 +08:00
}
}
}
2022-08-01 19:13:26 +08:00
if groupInfo.GroupType == constant.SuperGroup {
2023-02-13 17:48:01 +08:00
return nil, nil
}
2023-02-14 22:04:03 +08:00
userIDList, err := m.GetGroupMemberIDs(ctx, data.MsgData.GroupID)
2023-02-13 17:48:01 +08:00
if err != nil {
return nil, err
}
if utils.IsContain(data.MsgData.SendID, config.Config.Manager.AppManagerUid) {
return nil, nil
}
if data.MsgData.ContentType <= constant.NotificationEnd && data.MsgData.ContentType >= constant.NotificationBegin {
return userIDList, nil
2022-08-01 19:13:26 +08:00
} else {
2023-02-13 17:48:01 +08:00
if !utils.IsContain(data.MsgData.SendID, userIDList) {
2023-03-07 12:19:30 +08:00
return nil, errs.ErrNotInGroupYet.Wrap()
2022-11-11 18:06:15 +08:00
}
2022-07-25 19:19:00 +08:00
}
2023-02-14 22:04:03 +08:00
isMute, err := m.userIsMuteAndIsAdminInGroup(ctx, data.MsgData.GroupID, data.MsgData.SendID)
2023-02-13 17:48:01 +08:00
if err != nil {
return nil, err
}
if isMute {
2023-03-07 12:19:30 +08:00
return nil, errs.ErrMutedInGroup.Wrap()
2023-02-13 17:48:01 +08:00
}
2023-02-14 22:04:03 +08:00
isMute, isAdmin, err := m.groupIsMuted(ctx, data.MsgData.GroupID, data.MsgData.SendID)
2023-02-13 17:48:01 +08:00
if err != nil {
return nil, err
}
if isAdmin {
return userIDList, nil
}
if isMute {
2023-03-07 12:19:30 +08:00
return nil, errs.ErrMutedGroup.Wrap()
2023-02-13 17:48:01 +08:00
}
return userIDList, nil
2022-07-25 19:19:00 +08:00
default:
2023-02-13 17:48:01 +08:00
return nil, nil
2022-03-15 19:03:22 +08:00
}
2022-02-21 18:18:53 +08:00
}
2023-02-13 18:14:26 +08:00
func (m *msgServer) encapsulateMsgData(msg *sdkws.MsgData) {
2021-12-23 17:19:57 +08:00
msg.ServerMsgID = GetMsgID(msg.SendID)
2022-02-22 16:01:32 +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)
2022-03-28 18:30:12 +08:00
utils.SetSwitchFromOptions(msg.Options, constant.IsSenderConversationUpdate, false)
2021-12-23 17:19:57 +08:00
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)
2022-03-28 18:30:12 +08:00
utils.SetSwitchFromOptions(msg.Options, constant.IsSenderConversationUpdate, false)
2021-12-23 17:19:57 +08:00
utils.SetSwitchFromOptions(msg.Options, constant.IsUnreadCount, false)
utils.SetSwitchFromOptions(msg.Options, constant.IsOfflinePush, false)
}
}
2021-12-10 17:30:11 +08:00
2021-05-26 19:40:38 +08:00
func GetMsgID(sendID string) string {
t := time.Now().Format("2006-01-02 15:04:05")
2022-02-25 18:35:04 +08:00
return utils.Md5(t + "-" + sendID + "-" + strconv.Itoa(rand.Int()))
2021-05-26 19:40:38 +08:00
}
2021-12-21 21:40:50 +08:00
2023-05-04 15:06:23 +08:00
func (m *msgServer) modifyMessageByUserMessageReceiveOpt(ctx context.Context, userID, conversationID string, sessionType int, pb *msg.SendMsgReq) (bool, error) {
2023-02-14 22:04:03 +08:00
opt, err := m.User.GetUserGlobalMsgRecvOpt(ctx, userID)
2022-06-16 12:53:39 +08:00
if err != nil {
2023-02-13 16:16:47 +08:00
return false, err
2022-06-16 12:53:39 +08:00
}
switch opt {
case constant.ReceiveMessage:
case constant.NotReceiveMessage:
2023-02-13 17:48:01 +08:00
return false, nil
2022-06-16 12:53:39 +08:00
case constant.ReceiveNotNotifyMessage:
if pb.MsgData.Options == nil {
pb.MsgData.Options = make(map[string]bool, 10)
}
utils.SetSwitchFromOptions(pb.MsgData.Options, constant.IsOfflinePush, false)
2023-02-13 17:48:01 +08:00
return true, nil
2022-06-16 12:53:39 +08:00
}
2023-05-04 15:06:23 +08:00
// conversationID := utils.GetConversationIDBySessionType(conversationID, sessionType)
2023-02-14 22:04:03 +08:00
singleOpt, err := m.Conversation.GetSingleConversationRecvMsgOpt(ctx, userID, conversationID)
2023-05-10 11:33:32 +08:00
if errs.ErrRecordNotFound.Is(err) {
2023-05-10 11:49:55 +08:00
return true, nil
2023-05-10 11:33:32 +08:00
} else if err != nil {
2023-05-04 15:06:23 +08:00
return false, err
}
2022-06-16 12:53:39 +08:00
switch singleOpt {
2021-12-06 20:03:59 +08:00
case constant.ReceiveMessage:
2023-02-13 17:48:01 +08:00
return true, nil
2021-12-06 20:03:59 +08:00
case constant.NotReceiveMessage:
2022-08-05 19:10:08 +08:00
if utils.IsContainInt(int(pb.MsgData.ContentType), ExcludeContentType) {
2023-02-14 22:04:03 +08:00
return true, nil
2022-08-05 19:10:08 +08:00
}
2023-02-13 17:48:01 +08:00
return false, nil
2021-12-06 20:03:59 +08:00
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)
2023-02-13 17:48:01 +08:00
return true, nil
2021-12-06 20:03:59 +08:00
}
2023-02-13 17:48:01 +08:00
return true, nil
2021-12-06 20:03:59 +08:00
}
2021-12-21 21:40:50 +08:00
2023-02-13 16:17:52 +08:00
func valueCopy(pb *msg.SendMsgReq) *msg.SendMsgReq {
2023-02-09 20:36:34 +08:00
offlinePushInfo := sdkws.OfflinePushInfo{}
2022-05-22 18:28:51 +08:00
if pb.MsgData.OfflinePushInfo != nil {
offlinePushInfo = *pb.MsgData.OfflinePushInfo
}
2023-02-09 20:36:34 +08:00
msgData := sdkws.MsgData{}
2022-05-22 18:28:51 +08:00
msgData = *pb.MsgData
msgData.OfflinePushInfo = &offlinePushInfo
options := make(map[string]bool, 10)
for key, value := range pb.MsgData.Options {
options[key] = value
}
msgData.Options = options
2023-02-13 16:17:52 +08:00
return &msg.SendMsgReq{MsgData: &msgData}
2022-05-22 18:28:51 +08:00
}