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

201 lines
6.6 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 (
2023-06-05 18:01:59 +08:00
ExcludeContentType = []int{constant.HasReadReceipt}
2022-09-14 17:45:38 +08:00
)
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-06-06 20:01:50 +08:00
func (m *msgServer) messageVerification(ctx context.Context, data *msg.SendMsgReq) 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-06-06 20:01:50 +08:00
return nil
2022-08-05 15:51:34 +08:00
}
if data.MsgData.ContentType <= constant.NotificationEnd && data.MsgData.ContentType >= constant.NotificationBegin {
2023-06-06 20:01:50 +08:00
return 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-06-06 20:01:50 +08:00
return err
2022-06-16 14:26:03 +08:00
}
2023-02-13 17:48:01 +08:00
if black {
2023-06-06 20:01:50 +08:00
return 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-06-06 20:01:50 +08:00
return err
2022-06-16 14:26:03 +08:00
}
2023-02-13 17:48:01 +08:00
if !friend {
2023-06-06 20:01:50 +08:00
return errs.ErrNotPeersFriend.Wrap()
2022-05-28 18:10:08 +08:00
}
2023-06-06 20:01:50 +08:00
return nil
2022-07-25 19:19:00 +08:00
}
2023-06-06 20:01:50 +08:00
return nil
2022-07-25 19:19:00 +08:00
case constant.SuperGroupChatType:
2023-06-06 20:01:50 +08:00
groupInfo, err := m.Group.GetGroupInfoCache(ctx, data.MsgData.GroupID)
2022-07-25 19:19:00 +08:00
if err != nil {
2023-06-06 20:01:50 +08:00
return err
2022-07-25 19:19:00 +08:00
}
2023-06-04 14:37:45 +08:00
if groupInfo.Status == constant.GroupStatusDismissed && data.MsgData.ContentType != constant.GroupDismissedNotification {
2023-06-06 20:01:50 +08:00
return errs.ErrDismissedAlready.Wrap()
2023-06-04 12:25:30 +08:00
}
2022-08-01 19:13:26 +08:00
if groupInfo.GroupType == constant.SuperGroup {
2023-06-06 20:01:50 +08:00
return nil
2023-02-13 17:48:01 +08:00
}
if utils.IsContain(data.MsgData.SendID, config.Config.Manager.AppManagerUid) {
2023-06-06 20:01:50 +08:00
return nil
2023-02-13 17:48:01 +08:00
}
if data.MsgData.ContentType <= constant.NotificationEnd && data.MsgData.ContentType >= constant.NotificationBegin {
2023-06-06 20:01:50 +08:00
return nil
2022-07-25 19:19:00 +08:00
}
2023-06-13 20:52:20 +08:00
// memberIDs, err := m.GroupLocalCache.GetGroupMemberIDs(ctx, data.MsgData.GroupID)
// if err != nil {
// return err
// }
// if !utils.IsContain(data.MsgData.SendID, memberIDs) {
// return errs.ErrNotInGroupYet.Wrap()
// }
2023-06-06 20:01:50 +08:00
groupMemberInfo, err := m.Group.GetGroupMemberCache(ctx, data.MsgData.GroupID, data.MsgData.SendID)
2023-02-13 17:48:01 +08:00
if err != nil {
2023-06-06 20:01:50 +08:00
return err
2023-02-13 17:48:01 +08:00
}
2023-06-08 11:42:06 +08:00
if groupMemberInfo.RoleLevel == constant.GroupOwner {
2023-06-06 20:01:50 +08:00
return nil
} else {
if groupMemberInfo.MuteEndTime >= time.Now().Unix() {
return errs.ErrMutedInGroup.Wrap()
}
2023-06-08 11:42:06 +08:00
if groupInfo.Status == constant.GroupStatusMuted && groupMemberInfo.RoleLevel != constant.GroupAdmin {
2023-06-06 20:01:50 +08:00
return errs.ErrMutedGroup.Wrap()
}
2023-02-13 17:48:01 +08:00
}
2023-06-06 20:01:50 +08:00
return nil
2022-07-25 19:19:00 +08:00
default:
2023-06-06 20:01:50 +08:00
return 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
}