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/pkg/common/db/table/unrelation/notification.go

135 lines
4.0 KiB

2 years ago
package unrelation
import (
"context"
"strconv"
"strings"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws"
2 years ago
)
const (
2 years ago
singleGocNotificationNum = 5000
Notification = "notification"
//OldestList = 0
//NewestList = -1
2 years ago
)
2 years ago
type NotificationDocModel struct {
DocID string `bson:"uid"`
Msg []NotificationInfoModel `bson:"msg"`
2 years ago
}
2 years ago
type NotificationInfoModel struct {
2 years ago
SendTime int64 `bson:"sendtime"`
Msg []byte `bson:"msg"`
}
2 years ago
type NotificationDocModelInterface interface {
PushMsgsToDoc(ctx context.Context, docID string, msgsToMongo []NotificationInfoModel) error
Create(ctx context.Context, model *NotificationDocModel) error
2 years ago
UpdateMsgStatusByIndexInOneDoc(ctx context.Context, docID string, msg *sdkws.MsgData, seqIndex int, status int32) error
2 years ago
FindOneByDocID(ctx context.Context, docID string) (*NotificationDocModel, error)
2 years ago
GetNewestMsg(ctx context.Context, conversationID string) (*NotificationInfoModel, error)
GetOldestMsg(ctx context.Context, conversationID string) (*NotificationInfoModel, error)
2 years ago
Delete(ctx context.Context, docIDs []string) error
2 years ago
GetMsgsByIndex(ctx context.Context, conversationID string, index int64) (*NotificationDocModel, error)
2 years ago
UpdateOneDoc(ctx context.Context, msg *NotificationDocModel) error
2 years ago
}
2 years ago
func (NotificationDocModel) TableName() string {
return Notification
2 years ago
}
2 years ago
func (NotificationDocModel) GetsingleGocNotificationNum() int64 {
return singleGocNotificationNum
2 years ago
}
2 years ago
func (m *NotificationDocModel) IsFull() bool {
2 years ago
index, _ := strconv.Atoi(strings.Split(m.DocID, ":")[1])
if index == 0 {
2 years ago
if len(m.Msg) >= singleGocNotificationNum-1 {
2 years ago
return true
}
}
2 years ago
if len(m.Msg) >= singleGocNotificationNum {
2 years ago
return true
}
return false
}
2 years ago
func (m NotificationDocModel) GetDocID(conversationID string, seq int64) string {
2 years ago
seqSuffix := seq / singleGocNotificationNum
2 years ago
return m.indexGen(conversationID, seqSuffix)
2 years ago
}
2 years ago
func (m NotificationDocModel) GetSeqDocIDList(userID string, maxSeq int64) []string {
seqMaxSuffix := maxSeq / singleGocNotificationNum
2 years ago
var seqUserIDs []string
for i := 0; i <= int(seqMaxSuffix); i++ {
seqUserID := m.indexGen(userID, int64(i))
seqUserIDs = append(seqUserIDs, seqUserID)
}
return seqUserIDs
}
2 years ago
func (m NotificationDocModel) getSeqSuperGroupID(groupID string, seq int64) string {
seqSuffix := seq / singleGocNotificationNum
2 years ago
return m.superGroupIndexGen(groupID, seqSuffix)
}
2 years ago
func (m NotificationDocModel) superGroupIndexGen(groupID string, seqSuffix int64) string {
2 years ago
return "super_group_" + groupID + ":" + strconv.FormatInt(int64(seqSuffix), 10)
}
2 years ago
func (m NotificationDocModel) GetDocIDSeqsMap(conversationID string, seqs []int64) map[string][]int64 {
2 years ago
t := make(map[string][]int64)
for i := 0; i < len(seqs); i++ {
docID := m.GetDocID(conversationID, seqs[i])
2 years ago
if value, ok := t[docID]; !ok {
var temp []int64
t[docID] = append(temp, seqs[i])
} else {
t[docID] = append(value, seqs[i])
}
}
return t
}
2 years ago
func (m NotificationDocModel) getMsgIndex(seq uint32) int {
seqSuffix := seq / singleGocNotificationNum
2 years ago
var index uint32
if seqSuffix == 0 {
2 years ago
index = (seq - seqSuffix*singleGocNotificationNum) - 1
2 years ago
} else {
2 years ago
index = seq - seqSuffix*singleGocNotificationNum
2 years ago
}
return int(index)
}
2 years ago
func (m NotificationDocModel) indexGen(conversationID string, seqSuffix int64) string {
return conversationID + ":" + strconv.FormatInt(seqSuffix, 10)
2 years ago
}
2 years ago
func (NotificationDocModel) GenExceptionMessageBySeqs(seqs []int64) (exceptionMsg []*sdkws.MsgData) {
2 years ago
for _, v := range seqs {
msg := new(sdkws.MsgData)
msg.Seq = v
exceptionMsg = append(exceptionMsg, msg)
}
return exceptionMsg
}
2 years ago
func (NotificationDocModel) GenExceptionSuperGroupMessageBySeqs(seqs []int64, groupID string) (exceptionMsg []*sdkws.MsgData) {
2 years ago
for _, v := range seqs {
msg := new(sdkws.MsgData)
msg.Seq = v
msg.GroupID = groupID
msg.SessionType = constant.SuperGroupChatType
exceptionMsg = append(exceptionMsg, msg)
}
return exceptionMsg
}