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/msg.go

134 lines
3.6 KiB

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