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

141 lines
3.7 KiB

2 years ago
package unrelation
2 years ago
import (
"context"
"strconv"
"strings"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws"
2 years ago
)
const (
singleGocMsgNum = 5000
Msg = "msg"
OldestList = 0
NewestList = -1
)
type MsgDocModel struct {
2 years ago
DocID string `bson:"doc_id"`
Msg []MsgInfoModel `bson:"msgs"`
2 years ago
}
type MsgInfoModel struct {
SendTime int64 `bson:"sendtime"`
Msg []byte `bson:"msg"`
}
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)
2 years ago
GetMsgBySeqIndexIn1Doc(ctx context.Context, docID string, beginSeq, endSeq int64) ([]*sdkws.MsgData, []int64, error)
2 years ago
GetNewestMsg(ctx context.Context, conversationID string) (*MsgInfoModel, error)
GetOldestMsg(ctx context.Context, conversationID string) (*MsgInfoModel, error)
2 years ago
Delete(ctx context.Context, docIDs []string) error
2 years ago
GetMsgsByIndex(ctx context.Context, conversationID string, index int64) (*MsgDocModel, error)
2 years ago
UpdateOneDoc(ctx context.Context, msg *MsgDocModel) error
}
func (MsgDocModel) TableName() string {
return Msg
}
func (MsgDocModel) GetSingleGocMsgNum() int64 {
return singleGocMsgNum
}
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(conversationID string, seq int64) string {
2 years ago
seqSuffix := seq / singleGocMsgNum
2 years ago
return m.indexGen(conversationID, seqSuffix)
2 years ago
}
func (m MsgDocModel) GetSeqDocIDList(userID string, maxSeq int64) []string {
seqMaxSuffix := maxSeq / singleGocMsgNum
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 MsgDocModel) ToNextDoc(docID string) string {
l := strings.Split(docID, ":")
index, _ := strconv.Atoi(l[len(l)-1])
index++
return strings.Split(docID, ":")[0] + ":" + strconv.Itoa(index)
}
2 years ago
2 years ago
func (m MsgDocModel) GetDocIDSeqsMap(conversationID string, seqs []int64) map[string][]int64 {
2 years ago
t := make(map[string][]int64)
for i := 0; i < len(seqs); i++ {
2 years ago
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 MsgDocModel) GetSeqsBeginEnd(seqs []int64) (int64, int64) {
if len(seqs) == 0 {
return 0, 0
}
return seqs[0], seqs[len(seqs)-1]
}
func (m MsgDocModel) GetMsgIndex(seq int64) int64 {
2 years ago
seqSuffix := seq / singleGocMsgNum
2 years ago
var index int64
2 years ago
if seqSuffix == 0 {
index = (seq - seqSuffix*singleGocMsgNum) - 1
} else {
index = seq - seqSuffix*singleGocMsgNum
}
2 years ago
return index
2 years ago
}
2 years ago
func (m MsgDocModel) indexGen(conversationID string, seqSuffix int64) string {
return conversationID + ":" + strconv.FormatInt(seqSuffix, 10)
2 years ago
}
func (MsgDocModel) GenExceptionMessageBySeqs(seqs []int64) (exceptionMsg []*sdkws.MsgData) {
for _, v := range seqs {
msg := new(sdkws.MsgData)
msg.Seq = v
exceptionMsg = append(exceptionMsg, msg)
}
return exceptionMsg
}
func (MsgDocModel) GenExceptionSuperGroupMessageBySeqs(seqs []int64, groupID string) (exceptionMsg []*sdkws.MsgData) {
for _, v := range seqs {
msg := new(sdkws.MsgData)
msg.Seq = v
msg.GroupID = groupID
msg.SessionType = constant.SuperGroupChatType
exceptionMsg = append(exceptionMsg, msg)
}
return exceptionMsg
2 years ago
}