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.
235 lines
7.5 KiB
235 lines
7.5 KiB
3 years ago
|
package msg
|
||
4 years ago
|
|
||
|
import (
|
||
|
"context"
|
||
2 years ago
|
"math/rand"
|
||
|
"strconv"
|
||
|
"time"
|
||
|
|
||
2 years ago
|
"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"
|
||
4 years ago
|
)
|
||
|
|
||
2 years ago
|
var (
|
||
|
ExcludeContentType = []int{constant.HasReadReceipt, constant.GroupHasReadReceipt}
|
||
|
)
|
||
2 years ago
|
|
||
2 years ago
|
type Validator interface {
|
||
2 years ago
|
validate(pb *msg.SendMsgReq) (bool, int32, string)
|
||
2 years ago
|
}
|
||
|
|
||
|
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"`
|
||
|
}
|
||
4 years ago
|
|
||
2 years ago
|
func (m *msgServer) userIsMuteAndIsAdminInGroup(ctx context.Context, groupID, userID string) (isMute bool, err error) {
|
||
|
groupMemberInfo, err := m.Group.GetGroupMemberInfo(ctx, groupID, userID)
|
||
2 years ago
|
if err != nil {
|
||
2 years ago
|
return false, err
|
||
2 years ago
|
}
|
||
2 years ago
|
if groupMemberInfo.MuteEndTime >= time.Now().Unix() {
|
||
|
return true, nil
|
||
2 years ago
|
}
|
||
2 years ago
|
return false, nil
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
// 如果禁言了,再看下是否群管理员
|
||
2 years ago
|
func (m *msgServer) groupIsMuted(ctx context.Context, groupID string, userID string) (bool, bool, error) {
|
||
|
groupInfo, err := m.Group.GetGroupInfo(ctx, groupID)
|
||
2 years ago
|
if err != nil {
|
||
2 years ago
|
return false, false, err
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
if groupInfo.Status == constant.GroupStatusMuted {
|
||
2 years ago
|
groupMemberInfo, err := m.Group.GetGroupMemberInfo(ctx, groupID, userID)
|
||
2 years ago
|
if err != nil {
|
||
|
return false, false, err
|
||
|
}
|
||
|
return true, groupMemberInfo.RoleLevel > constant.GroupOrdinaryUsers, nil
|
||
2 years ago
|
}
|
||
2 years ago
|
return false, false, nil
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (m *msgServer) GetGroupMemberIDs(ctx context.Context, groupID string) (groupMemberIDs []string, err error) {
|
||
|
return m.GroupLocalCache.GetGroupMemberIDs(ctx, groupID)
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func (m *msgServer) messageVerification(ctx context.Context, data *msg.SendMsgReq) ([]string, error) {
|
||
2 years ago
|
switch data.MsgData.SessionType {
|
||
|
case constant.SingleChatType:
|
||
2 years ago
|
if utils.IsContain(data.MsgData.SendID, config.Config.Manager.AppManagerUid) {
|
||
2 years ago
|
return nil, nil
|
||
2 years ago
|
}
|
||
|
if data.MsgData.ContentType <= constant.NotificationEnd && data.MsgData.ContentType >= constant.NotificationBegin {
|
||
2 years ago
|
return nil, nil
|
||
2 years ago
|
}
|
||
2 years ago
|
black, err := m.black.IsBlocked(ctx, data.MsgData.SendID, data.MsgData.RecvID)
|
||
2 years ago
|
if err != nil {
|
||
2 years ago
|
return nil, err
|
||
2 years ago
|
}
|
||
2 years ago
|
if black {
|
||
2 years ago
|
return nil, errs.ErrBlockedByPeer.Wrap()
|
||
3 years ago
|
}
|
||
2 years ago
|
if *config.Config.MessageVerify.FriendVerify {
|
||
2 years ago
|
friend, err := m.friend.IsFriend(ctx, data.MsgData.SendID, data.MsgData.RecvID)
|
||
2 years ago
|
if err != nil {
|
||
2 years ago
|
return nil, err
|
||
2 years ago
|
}
|
||
2 years ago
|
if !friend {
|
||
2 years ago
|
return nil, errs.ErrNotPeersFriend.Wrap()
|
||
3 years ago
|
}
|
||
2 years ago
|
return nil, nil
|
||
2 years ago
|
}
|
||
2 years ago
|
return nil, nil
|
||
2 years ago
|
case constant.SuperGroupChatType:
|
||
2 years ago
|
groupInfo, err := m.Group.GetGroupInfo(ctx, data.MsgData.GroupID)
|
||
2 years ago
|
if err != nil {
|
||
2 years ago
|
return nil, err
|
||
2 years ago
|
}
|
||
2 years ago
|
if groupInfo.GroupType == constant.SuperGroup {
|
||
2 years ago
|
return nil, nil
|
||
|
}
|
||
2 years ago
|
userIDList, err := m.GetGroupMemberIDs(ctx, data.MsgData.GroupID)
|
||
2 years ago
|
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
|
||
2 years ago
|
} else {
|
||
2 years ago
|
if !utils.IsContain(data.MsgData.SendID, userIDList) {
|
||
2 years ago
|
return nil, errs.ErrNotInGroupYet.Wrap()
|
||
2 years ago
|
}
|
||
2 years ago
|
}
|
||
2 years ago
|
isMute, err := m.userIsMuteAndIsAdminInGroup(ctx, data.MsgData.GroupID, data.MsgData.SendID)
|
||
2 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
if isMute {
|
||
2 years ago
|
return nil, errs.ErrMutedInGroup.Wrap()
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
isMute, isAdmin, err := m.groupIsMuted(ctx, data.MsgData.GroupID, data.MsgData.SendID)
|
||
2 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
if isAdmin {
|
||
|
return userIDList, nil
|
||
|
}
|
||
|
if isMute {
|
||
2 years ago
|
return nil, errs.ErrMutedGroup.Wrap()
|
||
2 years ago
|
}
|
||
|
return userIDList, nil
|
||
|
|
||
2 years ago
|
default:
|
||
2 years ago
|
return nil, nil
|
||
3 years ago
|
}
|
||
3 years ago
|
}
|
||
2 years ago
|
func (m *msgServer) encapsulateMsgData(msg *sdkws.MsgData) {
|
||
3 years ago
|
msg.ServerMsgID = GetMsgID(msg.SendID)
|
||
3 years ago
|
msg.SendTime = utils.GetCurrentTimestampByMill()
|
||
3 years ago
|
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)
|
||
3 years ago
|
utils.SetSwitchFromOptions(msg.Options, constant.IsSenderConversationUpdate, false)
|
||
3 years ago
|
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)
|
||
3 years ago
|
utils.SetSwitchFromOptions(msg.Options, constant.IsSenderConversationUpdate, false)
|
||
3 years ago
|
utils.SetSwitchFromOptions(msg.Options, constant.IsUnreadCount, false)
|
||
|
utils.SetSwitchFromOptions(msg.Options, constant.IsOfflinePush, false)
|
||
|
}
|
||
|
}
|
||
3 years ago
|
|
||
4 years ago
|
func GetMsgID(sendID string) string {
|
||
|
t := time.Now().Format("2006-01-02 15:04:05")
|
||
3 years ago
|
return utils.Md5(t + "-" + sendID + "-" + strconv.Itoa(rand.Int()))
|
||
4 years ago
|
}
|
||
3 years ago
|
|
||
2 years ago
|
func (m *msgServer) modifyMessageByUserMessageReceiveOpt(ctx context.Context, userID, conversationID string, sessionType int, pb *msg.SendMsgReq) (bool, error) {
|
||
2 years ago
|
opt, err := m.User.GetUserGlobalMsgRecvOpt(ctx, userID)
|
||
2 years ago
|
if err != nil {
|
||
2 years ago
|
return false, err
|
||
2 years ago
|
}
|
||
|
switch opt {
|
||
|
case constant.ReceiveMessage:
|
||
|
case constant.NotReceiveMessage:
|
||
2 years ago
|
return false, nil
|
||
2 years ago
|
case constant.ReceiveNotNotifyMessage:
|
||
|
if pb.MsgData.Options == nil {
|
||
|
pb.MsgData.Options = make(map[string]bool, 10)
|
||
|
}
|
||
|
utils.SetSwitchFromOptions(pb.MsgData.Options, constant.IsOfflinePush, false)
|
||
2 years ago
|
return true, nil
|
||
2 years ago
|
}
|
||
2 years ago
|
// conversationID := utils.GetConversationIDBySessionType(conversationID, sessionType)
|
||
2 years ago
|
singleOpt, err := m.Conversation.GetSingleConversationRecvMsgOpt(ctx, userID, conversationID)
|
||
2 years ago
|
if errs.ErrRecordNotFound.Is(err) {
|
||
2 years ago
|
return true, nil
|
||
2 years ago
|
} else if err != nil {
|
||
2 years ago
|
return false, err
|
||
|
}
|
||
2 years ago
|
switch singleOpt {
|
||
3 years ago
|
case constant.ReceiveMessage:
|
||
2 years ago
|
return true, nil
|
||
3 years ago
|
case constant.NotReceiveMessage:
|
||
2 years ago
|
if utils.IsContainInt(int(pb.MsgData.ContentType), ExcludeContentType) {
|
||
2 years ago
|
return true, nil
|
||
2 years ago
|
}
|
||
2 years ago
|
return false, nil
|
||
3 years ago
|
case constant.ReceiveNotNotifyMessage:
|
||
3 years ago
|
if pb.MsgData.Options == nil {
|
||
|
pb.MsgData.Options = make(map[string]bool, 10)
|
||
3 years ago
|
}
|
||
3 years ago
|
utils.SetSwitchFromOptions(pb.MsgData.Options, constant.IsOfflinePush, false)
|
||
2 years ago
|
return true, nil
|
||
3 years ago
|
}
|
||
2 years ago
|
return true, nil
|
||
3 years ago
|
}
|