Files
open-im-server/pkg/common/db/unrelation/mongo_model.go
T

752 lines
24 KiB
Go
Raw Normal View History

2023-01-28 13:19:36 +08:00
package unrelation
2021-05-26 19:17:51 +08:00
import (
"Open_IM/pkg/common/config"
2022-03-17 19:00:05 +08:00
"Open_IM/pkg/common/constant"
2023-01-16 20:14:26 +08:00
"Open_IM/pkg/common/db"
"Open_IM/pkg/common/log"
2022-07-20 21:05:12 +08:00
pbMsg "Open_IM/pkg/proto/msg"
2022-01-18 09:25:41 +08:00
open_im_sdk "Open_IM/pkg/proto/sdk_ws"
2022-01-20 11:36:43 +08:00
"Open_IM/pkg/utils"
2022-02-21 15:01:06 +08:00
"context"
2021-05-26 19:17:51 +08:00
"errors"
2022-03-17 19:00:05 +08:00
"fmt"
2022-12-27 11:01:21 +08:00
"math/rand"
"sync"
2022-05-31 19:16:29 +08:00
"github.com/go-redis/redis/v8"
2022-03-17 19:00:05 +08:00
"github.com/gogo/protobuf/sortkeys"
2022-11-02 11:55:48 +08:00
"go.mongodb.org/mongo-driver/bson/primitive"
2022-05-30 11:55:27 +08:00
"go.mongodb.org/mongo-driver/mongo"
2022-03-28 12:17:07 +08:00
"go.mongodb.org/mongo-driver/mongo/options"
2022-03-17 19:00:05 +08:00
2022-02-21 15:01:06 +08:00
//"github.com/garyburd/redigo/redis"
2021-05-26 19:17:51 +08:00
"github.com/golang/protobuf/proto"
2022-04-24 17:42:41 +08:00
"go.mongodb.org/mongo-driver/bson"
2022-02-21 15:01:06 +08:00
"strconv"
2021-05-26 19:17:51 +08:00
"time"
)
2021-12-23 17:34:32 +08:00
const cChat = "msg"
2021-07-15 11:14:07 +08:00
const cGroup = "group"
2023-01-28 16:36:38 +08:00
2022-04-18 19:24:36 +08:00
const cCommentMsg = "comment_msg"
2023-01-28 13:19:36 +08:00
const singleGocMsgNum = 5000
2021-06-28 15:32:26 +08:00
2022-05-20 11:00:11 +08:00
func GetSingleGocMsgNum() int {
return singleGocMsgNum
}
2021-06-28 15:32:26 +08:00
type MsgInfo struct {
SendTime int64
Msg []byte
}
2021-05-26 19:17:51 +08:00
type UserChat struct {
UID string
2022-08-08 11:30:10 +08:00
//ListIndex int `bson:"index"`
2021-06-28 15:32:26 +08:00
Msg []MsgInfo
2021-05-26 19:17:51 +08:00
}
2021-12-31 10:40:32 +08:00
type GroupMember_x struct {
2021-07-15 11:14:07 +08:00
GroupID string
UIDList []string
}
2022-11-01 19:49:19 +08:00
var ErrMsgListNotExist = errors.New("user not have msg in mongoDB")
2022-05-04 18:02:24 +08:00
// deleteMsgByLogic
2023-01-16 20:14:26 +08:00
func (d *db.DataBases) DelMsgBySeqList(userID string, seqList []uint32, operationID string) (totalUnexistSeqList []uint32, err error) {
2022-05-04 18:35:13 +08:00
log.Debug(operationID, utils.GetSelfFuncName(), "args ", userID, seqList)
2022-05-04 18:02:24 +08:00
sortkeys.Uint32s(seqList)
suffixUserID2SubSeqList := func(uid string, seqList []uint32) map[string][]uint32 {
t := make(map[string][]uint32)
for i := 0; i < len(seqList); i++ {
seqUid := getSeqUid(uid, seqList[i])
if value, ok := t[seqUid]; !ok {
var temp []uint32
t[seqUid] = append(temp, seqList[i])
} else {
t[seqUid] = append(value, seqList[i])
}
}
return t
}(userID, seqList)
2022-05-25 15:56:39 +08:00
lock := sync.Mutex{}
2022-05-04 18:02:24 +08:00
var wg sync.WaitGroup
wg.Add(len(suffixUserID2SubSeqList))
for k, v := range suffixUserID2SubSeqList {
go func(suffixUserID string, subSeqList []uint32, operationID string) {
2022-05-25 15:56:39 +08:00
defer wg.Done()
unexistSeqList, err := d.DelMsgBySeqListInOneDoc(suffixUserID, subSeqList, operationID)
if err != nil {
2022-05-25 16:16:23 +08:00
log.Error(operationID, "DelMsgBySeqListInOneDoc failed ", err.Error(), suffixUserID, subSeqList)
2022-05-25 15:56:39 +08:00
return
2022-05-04 18:02:24 +08:00
}
2022-05-25 15:56:39 +08:00
lock.Lock()
totalUnexistSeqList = append(totalUnexistSeqList, unexistSeqList...)
lock.Unlock()
2022-05-04 18:02:24 +08:00
}(k, v, operationID)
}
2022-05-25 15:56:39 +08:00
return totalUnexistSeqList, err
2022-05-04 18:02:24 +08:00
}
2023-01-16 20:14:26 +08:00
func (d *db.DataBases) DelMsgBySeqListInOneDoc(suffixUserID string, seqList []uint32, operationID string) ([]uint32, error) {
2022-05-04 18:35:13 +08:00
log.Debug(operationID, utils.GetSelfFuncName(), "args ", suffixUserID, seqList)
2022-05-25 15:56:39 +08:00
seqMsgList, indexList, unexistSeqList, err := d.GetMsgAndIndexBySeqListInOneMongo2(suffixUserID, seqList, operationID)
2022-05-04 18:02:24 +08:00
if err != nil {
2022-05-25 15:56:39 +08:00
return nil, utils.Wrap(err, "")
2022-05-04 18:02:24 +08:00
}
for i, v := range seqMsgList {
if err := d.ReplaceMsgByIndex(suffixUserID, v, operationID, indexList[i]); err != nil {
2022-05-25 15:56:39 +08:00
return nil, utils.Wrap(err, "")
2022-05-04 18:02:24 +08:00
}
}
2022-05-25 15:56:39 +08:00
return unexistSeqList, nil
2022-05-04 18:02:24 +08:00
}
2022-03-17 19:00:05 +08:00
// deleteMsgByLogic
2023-01-16 20:14:26 +08:00
func (d *db.DataBases) DelMsgLogic(uid string, seqList []uint32, operationID string) error {
2022-03-17 19:00:05 +08:00
sortkeys.Uint32s(seqList)
seqMsgs, err := d.GetMsgBySeqListMongo2(uid, seqList, operationID)
if err != nil {
2022-03-21 16:56:52 +08:00
return utils.Wrap(err, "")
2022-03-17 19:00:05 +08:00
}
for _, seqMsg := range seqMsgs {
2022-03-21 16:56:52 +08:00
log.NewDebug(operationID, utils.GetSelfFuncName(), *seqMsg)
2022-03-17 19:00:05 +08:00
seqMsg.Status = constant.MsgDeleted
if err = d.ReplaceMsgBySeq(uid, seqMsg, operationID); err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), "ReplaceMsgListBySeq error", err.Error())
}
}
return nil
}
2023-01-16 20:14:26 +08:00
func (d *db.DataBases) ReplaceMsgByIndex(suffixUserID string, msg *open_im_sdk.MsgData, operationID string, seqIndex int) error {
2022-05-04 18:02:24 +08:00
log.NewInfo(operationID, utils.GetSelfFuncName(), suffixUserID, *msg)
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
s := fmt.Sprintf("msg.%d.msg", seqIndex)
log.NewDebug(operationID, utils.GetSelfFuncName(), seqIndex, s)
msg.Status = constant.MsgDeleted
bytes, err := proto.Marshal(msg)
if err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), "proto marshal failed ", err.Error(), msg.String())
return utils.Wrap(err, "")
}
updateResult, err := c.UpdateOne(ctx, bson.M{"uid": suffixUserID}, bson.M{"$set": bson.M{s: bytes}})
log.NewInfo(operationID, utils.GetSelfFuncName(), updateResult)
if err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), "UpdateOne", err.Error())
return utils.Wrap(err, "")
}
return nil
}
2023-01-16 20:14:26 +08:00
func (d *db.DataBases) ReplaceMsgBySeq(uid string, msg *open_im_sdk.MsgData, operationID string) error {
2022-03-21 16:56:52 +08:00
log.NewInfo(operationID, utils.GetSelfFuncName(), uid, *msg)
2022-03-17 19:00:05 +08:00
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
uid = getSeqUid(uid, msg.Seq)
seqIndex := getMsgIndex(msg.Seq)
s := fmt.Sprintf("msg.%d.msg", seqIndex)
log.NewDebug(operationID, utils.GetSelfFuncName(), seqIndex, s)
2022-03-21 16:56:52 +08:00
bytes, err := proto.Marshal(msg)
if err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), "proto marshal", err.Error())
return utils.Wrap(err, "")
}
2022-05-04 18:02:24 +08:00
2022-03-17 19:00:05 +08:00
updateResult, err := c.UpdateOne(
ctx, bson.M{"uid": uid},
2022-03-21 16:56:52 +08:00
bson.M{"$set": bson.M{s: bytes}})
2022-03-17 19:00:05 +08:00
log.NewInfo(operationID, utils.GetSelfFuncName(), updateResult)
if err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), "UpdateOne", err.Error())
2022-03-21 16:56:52 +08:00
return utils.Wrap(err, "")
2022-03-17 19:00:05 +08:00
}
return nil
2022-12-27 11:01:21 +08:00
}
2023-01-16 20:14:26 +08:00
func (d *db.DataBases) UpdateOneMsgList(msg *UserChat) error {
2022-12-27 11:01:21 +08:00
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
_, err := c.UpdateOne(ctx, bson.M{"uid": msg.UID}, bson.M{"$set": bson.M{"msg": msg.Msg}})
return err
2022-03-17 19:00:05 +08:00
}
2023-01-16 20:14:26 +08:00
func (d *db.DataBases) GetMsgBySeqList(uid string, seqList []uint32, operationID string) (seqMsg []*open_im_sdk.MsgData, err error) {
2022-02-23 12:00:02 +08:00
log.NewInfo(operationID, utils.GetSelfFuncName(), uid, seqList)
2022-02-21 15:01:06 +08:00
var hasSeqList []uint32
singleCount := 0
2021-11-10 18:13:04 +08:00
session := d.mgoSession.Clone()
if session == nil {
2022-02-21 15:01:06 +08:00
return nil, errors.New("session == nil")
2021-11-10 18:13:04 +08:00
}
defer session.Close()
c := session.DB(config.Config.Mongo.DBDatabase).C(cChat)
2022-02-21 15:01:06 +08:00
m := func(uid string, seqList []uint32) map[string][]uint32 {
t := make(map[string][]uint32)
for i := 0; i < len(seqList); i++ {
seqUid := getSeqUid(uid, seqList[i])
if value, ok := t[seqUid]; !ok {
var temp []uint32
t[seqUid] = append(temp, seqList[i])
2021-11-10 18:13:04 +08:00
} else {
2022-02-21 15:01:06 +08:00
t[seqUid] = append(value, seqList[i])
2021-11-10 18:13:04 +08:00
}
}
2022-02-21 15:01:06 +08:00
return t
}(uid, seqList)
sChat := UserChat{}
for seqUid, value := range m {
if err = c.Find(bson.M{"uid": seqUid}).One(&sChat); err != nil {
log.NewError(operationID, "not find seqUid", seqUid, value, uid, seqList, err.Error())
continue
}
singleCount = 0
for i := 0; i < len(sChat.Msg); i++ {
msg := new(open_im_sdk.MsgData)
if err = proto.Unmarshal(sChat.Msg[i].Msg, msg); err != nil {
log.NewError(operationID, "Unmarshal err", seqUid, value, uid, seqList, err.Error())
return nil, err
}
if isContainInt32(msg.Seq, value) {
seqMsg = append(seqMsg, msg)
hasSeqList = append(hasSeqList, msg.Seq)
singleCount++
if singleCount == len(value) {
break
}
}
}
}
if len(hasSeqList) != len(seqList) {
var diff []uint32
diff = utils.Difference(hasSeqList, seqList)
exceptionMSg := genExceptionMessageBySeqList(diff)
seqMsg = append(seqMsg, exceptionMSg...)
2021-11-10 18:13:04 +08:00
}
2022-02-21 15:01:06 +08:00
return seqMsg, nil
2021-11-10 18:13:04 +08:00
}
2022-02-21 15:01:06 +08:00
2023-01-16 20:14:26 +08:00
func (d *db.DataBases) GetUserMsgListByIndex(ID string, index int64) (*UserChat, error) {
2022-08-08 11:30:10 +08:00
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
2022-11-02 12:00:23 +08:00
regex := fmt.Sprintf("^%s", ID)
2022-08-11 12:09:44 +08:00
findOpts := options.Find().SetLimit(1).SetSkip(index).SetSort(bson.M{"uid": 1})
2022-11-01 19:14:33 +08:00
var msgs []UserChat
2022-11-02 11:40:55 +08:00
//primitive.Regex{Pattern: regex}
2022-11-02 11:55:48 +08:00
cursor, err := c.Find(ctx, bson.M{"uid": primitive.Regex{Pattern: regex}}, findOpts)
2022-08-08 11:30:10 +08:00
if err != nil {
2022-11-01 16:54:23 +08:00
return nil, utils.Wrap(err, "")
2022-08-08 11:30:10 +08:00
}
2022-11-01 19:14:33 +08:00
err = cursor.All(context.Background(), &msgs)
2022-08-11 12:09:44 +08:00
if err != nil {
2022-11-01 17:05:48 +08:00
return nil, utils.Wrap(err, fmt.Sprintf("cursor is %s", cursor.Current.String()))
2022-08-10 19:31:57 +08:00
}
2022-11-01 19:14:33 +08:00
if len(msgs) > 0 {
2022-11-03 18:31:29 +08:00
return &msgs[0], nil
2022-11-01 19:14:33 +08:00
} else {
2022-11-01 19:49:19 +08:00
return nil, ErrMsgListNotExist
2022-11-01 19:14:33 +08:00
}
2022-08-08 11:30:10 +08:00
}
2023-01-16 20:14:26 +08:00
func (d *db.DataBases) DelMongoMsgs(IDList []string) error {
2022-08-08 11:30:10 +08:00
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
_, err := c.DeleteMany(ctx, bson.M{"uid": bson.M{"$in": IDList}})
return err
}
2023-01-16 20:14:26 +08:00
func (d *db.DataBases) ReplaceMsgToBlankByIndex(suffixID string, index int) (replaceMaxSeq uint32, err error) {
2022-08-08 11:30:10 +08:00
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
userChat := &UserChat{}
2022-12-01 19:34:54 +08:00
err = c.FindOne(ctx, bson.M{"uid": suffixID}).Decode(&userChat)
2022-08-08 11:30:10 +08:00
if err != nil {
2022-12-01 19:34:54 +08:00
return 0, err
2022-08-08 11:30:10 +08:00
}
for i, msg := range userChat.Msg {
if i <= index {
2022-11-04 16:18:45 +08:00
msgPb := &open_im_sdk.MsgData{}
if err = proto.Unmarshal(msg.Msg, msgPb); err != nil {
continue
}
newMsgPb := &open_im_sdk.MsgData{Seq: msgPb.Seq}
bytes, err := proto.Marshal(newMsgPb)
if err != nil {
continue
}
msg.Msg = bytes
2022-08-08 11:30:10 +08:00
msg.SendTime = 0
2022-12-01 19:34:54 +08:00
replaceMaxSeq = msgPb.Seq
2022-08-08 11:30:10 +08:00
}
}
2022-11-03 19:16:23 +08:00
_, err = c.UpdateOne(ctx, bson.M{"uid": suffixID}, bson.M{"$set": bson.M{"msg": userChat.Msg}})
2022-12-01 19:34:54 +08:00
return replaceMaxSeq, err
2022-08-08 11:30:10 +08:00
}
2023-01-16 20:14:26 +08:00
func (d *db.DataBases) GetNewestMsg(ID string) (msg *open_im_sdk.MsgData, err error) {
2022-08-10 19:31:57 +08:00
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
regex := fmt.Sprintf("^%s", ID)
2022-08-11 12:09:44 +08:00
findOpts := options.Find().SetLimit(1).SetSort(bson.M{"uid": -1})
2022-08-10 19:31:57 +08:00
var userChats []UserChat
cursor, err := c.Find(ctx, bson.M{"uid": bson.M{"$regex": regex}}, findOpts)
if err != nil {
return nil, err
}
2022-11-01 19:39:56 +08:00
err = cursor.All(ctx, &userChats)
2022-08-11 12:09:44 +08:00
if err != nil {
return nil, utils.Wrap(err, "")
}
2022-08-10 19:31:57 +08:00
if len(userChats) > 0 {
if len(userChats[0].Msg) > 0 {
2022-11-29 18:08:47 +08:00
msgPb := &open_im_sdk.MsgData{}
err = proto.Unmarshal(userChats[0].Msg[len(userChats[0].Msg)-1].Msg, msgPb)
if err != nil {
return nil, utils.Wrap(err, "")
}
return msgPb, nil
2022-08-10 19:31:57 +08:00
}
return nil, errors.New("len(userChats[0].Msg) < 0")
}
2022-11-03 19:16:23 +08:00
return nil, nil
2022-08-10 19:31:57 +08:00
}
2023-01-16 20:14:26 +08:00
func (d *db.DataBases) GetOldestMsg(ID string) (msg *open_im_sdk.MsgData, err error) {
2022-11-29 18:08:47 +08:00
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
regex := fmt.Sprintf("^%s", ID)
findOpts := options.Find().SetLimit(1).SetSort(bson.M{"uid": 1})
var userChats []UserChat
cursor, err := c.Find(ctx, bson.M{"uid": bson.M{"$regex": regex}}, findOpts)
if err != nil {
return nil, err
}
err = cursor.All(ctx, &userChats)
if err != nil {
return nil, utils.Wrap(err, "")
}
var oldestMsg []byte
if len(userChats) > 0 {
for _, v := range userChats[0].Msg {
if v.SendTime != 0 {
oldestMsg = v.Msg
break
}
}
if len(oldestMsg) == 0 {
oldestMsg = userChats[0].Msg[len(userChats[0].Msg)-1].Msg
}
msgPb := &open_im_sdk.MsgData{}
err = proto.Unmarshal(oldestMsg, msgPb)
if err != nil {
return nil, utils.Wrap(err, "")
}
return msgPb, nil
}
return nil, nil
}
2023-01-16 20:14:26 +08:00
func (d *db.DataBases) GetMsgBySeqListMongo2(uid string, seqList []uint32, operationID string) (seqMsg []*open_im_sdk.MsgData, err error) {
2022-01-20 11:36:43 +08:00
var hasSeqList []uint32
singleCount := 0
2022-02-21 15:01:06 +08:00
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
2022-01-20 11:36:43 +08:00
m := func(uid string, seqList []uint32) map[string][]uint32 {
t := make(map[string][]uint32)
for i := 0; i < len(seqList); i++ {
seqUid := getSeqUid(uid, seqList[i])
if value, ok := t[seqUid]; !ok {
2022-01-20 11:36:43 +08:00
var temp []uint32
t[seqUid] = append(temp, seqList[i])
} else {
t[seqUid] = append(value, seqList[i])
}
}
return t
}(uid, seqList)
sChat := UserChat{}
for seqUid, value := range m {
2022-02-21 15:01:06 +08:00
if err = c.FindOne(ctx, bson.M{"uid": seqUid}).Decode(&sChat); err != nil {
2022-01-20 18:36:30 +08:00
log.NewError(operationID, "not find seqUid", seqUid, value, uid, seqList, err.Error())
continue
}
singleCount = 0
for i := 0; i < len(sChat.Msg); i++ {
2021-12-23 17:34:32 +08:00
msg := new(open_im_sdk.MsgData)
if err = proto.Unmarshal(sChat.Msg[i].Msg, msg); err != nil {
2022-01-20 18:36:30 +08:00
log.NewError(operationID, "Unmarshal err", seqUid, value, uid, seqList, err.Error())
2022-01-20 11:36:43 +08:00
return nil, err
}
2022-01-20 11:36:43 +08:00
if isContainInt32(msg.Seq, value) {
seqMsg = append(seqMsg, msg)
hasSeqList = append(hasSeqList, msg.Seq)
singleCount++
if singleCount == len(value) {
break
}
}
}
}
2022-01-20 11:36:43 +08:00
if len(hasSeqList) != len(seqList) {
var diff []uint32
diff = utils.Difference(hasSeqList, seqList)
exceptionMSg := genExceptionMessageBySeqList(diff)
seqMsg = append(seqMsg, exceptionMSg...)
}
return seqMsg, nil
}
2023-01-16 20:14:26 +08:00
func (d *db.DataBases) GetSuperGroupMsgBySeqListMongo(groupID string, seqList []uint32, operationID string) (seqMsg []*open_im_sdk.MsgData, err error) {
2022-05-30 11:56:32 +08:00
var hasSeqList []uint32
singleCount := 0
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
m := func(uid string, seqList []uint32) map[string][]uint32 {
t := make(map[string][]uint32)
for i := 0; i < len(seqList); i++ {
seqUid := getSeqUid(uid, seqList[i])
if value, ok := t[seqUid]; !ok {
var temp []uint32
t[seqUid] = append(temp, seqList[i])
} else {
t[seqUid] = append(value, seqList[i])
}
}
return t
}(groupID, seqList)
sChat := UserChat{}
for seqUid, value := range m {
if err = c.FindOne(ctx, bson.M{"uid": seqUid}).Decode(&sChat); err != nil {
log.NewError(operationID, "not find seqGroupID", seqUid, value, groupID, seqList, err.Error())
continue
}
singleCount = 0
for i := 0; i < len(sChat.Msg); i++ {
msg := new(open_im_sdk.MsgData)
if err = proto.Unmarshal(sChat.Msg[i].Msg, msg); err != nil {
log.NewError(operationID, "Unmarshal err", seqUid, value, groupID, seqList, err.Error())
return nil, err
}
if isContainInt32(msg.Seq, value) {
seqMsg = append(seqMsg, msg)
hasSeqList = append(hasSeqList, msg.Seq)
singleCount++
if singleCount == len(value) {
break
}
}
}
}
if len(hasSeqList) != len(seqList) {
var diff []uint32
diff = utils.Difference(hasSeqList, seqList)
exceptionMSg := genExceptionSuperGroupMessageBySeqList(diff, groupID)
seqMsg = append(seqMsg, exceptionMSg...)
}
return seqMsg, nil
}
2022-02-21 15:01:06 +08:00
2023-01-16 20:14:26 +08:00
func (d *db.DataBases) GetMsgAndIndexBySeqListInOneMongo2(suffixUserID string, seqList []uint32, operationID string) (seqMsg []*open_im_sdk.MsgData, indexList []int, unexistSeqList []uint32, err error) {
2022-05-04 18:02:24 +08:00
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
sChat := UserChat{}
if err = c.FindOne(ctx, bson.M{"uid": suffixUserID}).Decode(&sChat); err != nil {
log.NewError(operationID, "not find seqUid", suffixUserID, err.Error())
2022-05-25 15:56:39 +08:00
return nil, nil, nil, utils.Wrap(err, "")
2022-05-04 18:02:24 +08:00
}
singleCount := 0
var hasSeqList []uint32
for i := 0; i < len(sChat.Msg); i++ {
msg := new(open_im_sdk.MsgData)
if err = proto.Unmarshal(sChat.Msg[i].Msg, msg); err != nil {
log.NewError(operationID, "Unmarshal err", msg.String(), err.Error())
2022-05-25 15:56:39 +08:00
return nil, nil, nil, err
2022-05-04 18:02:24 +08:00
}
if isContainInt32(msg.Seq, seqList) {
indexList = append(indexList, i)
seqMsg = append(seqMsg, msg)
hasSeqList = append(hasSeqList, msg.Seq)
singleCount++
if singleCount == len(seqList) {
break
}
}
}
2022-05-25 15:56:39 +08:00
for _, i := range seqList {
if isContainInt32(i, hasSeqList) {
continue
}
unexistSeqList = append(unexistSeqList, i)
}
return seqMsg, indexList, unexistSeqList, nil
2022-05-04 18:02:24 +08:00
}
2022-01-20 11:36:43 +08:00
func genExceptionMessageBySeqList(seqList []uint32) (exceptionMsg []*open_im_sdk.MsgData) {
for _, v := range seqList {
msg := new(open_im_sdk.MsgData)
msg.Seq = v
exceptionMsg = append(exceptionMsg, msg)
}
return exceptionMsg
}
2022-05-30 11:56:32 +08:00
func genExceptionSuperGroupMessageBySeqList(seqList []uint32, groupID string) (exceptionMsg []*open_im_sdk.MsgData) {
for _, v := range seqList {
msg := new(open_im_sdk.MsgData)
msg.Seq = v
msg.GroupID = groupID
msg.SessionType = constant.SuperGroupChatType
exceptionMsg = append(exceptionMsg, msg)
}
return exceptionMsg
}
2023-01-16 20:14:26 +08:00
func (d *db.DataBases) SaveUserChatMongo2(uid string, sendTime int64, m *pbMsg.MsgDataToDB) error {
2022-02-21 15:01:06 +08:00
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
newTime := getCurrentTimestampByMill()
operationID := ""
seqUid := getSeqUid(uid, m.MsgData.Seq)
filter := bson.M{"uid": seqUid}
var err error
sMsg := MsgInfo{}
sMsg.SendTime = sendTime
if sMsg.Msg, err = proto.Marshal(m.MsgData); err != nil {
2022-03-16 18:02:26 +08:00
return utils.Wrap(err, "")
2022-02-21 15:01:06 +08:00
}
err = c.FindOneAndUpdate(ctx, filter, bson.M{"$push": bson.M{"msg": sMsg}}).Err()
2022-05-20 11:00:11 +08:00
log.NewWarn(operationID, "get mgoSession cost time", getCurrentTimestampByMill()-newTime)
2022-02-21 15:01:06 +08:00
if err != nil {
sChat := UserChat{}
sChat.UID = seqUid
sChat.Msg = append(sChat.Msg, sMsg)
2022-03-16 18:02:26 +08:00
if _, err = c.InsertOne(ctx, &sChat); err != nil {
2022-02-21 15:01:06 +08:00
log.NewDebug(operationID, "InsertOne failed", filter)
return utils.Wrap(err, "")
}
2022-03-16 18:02:26 +08:00
} else {
2022-02-21 15:01:06 +08:00
log.NewDebug(operationID, "FindOneAndUpdate ok", filter)
}
log.NewDebug(operationID, "find mgo uid cost time", getCurrentTimestampByMill()-newTime)
return nil
}
2022-05-20 11:00:11 +08:00
//
//func (d *DataBases) SaveUserChatListMongo2(uid string, sendTime int64, msgList []*pbMsg.MsgDataToDB) error {
// ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
// c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
// newTime := getCurrentTimestampByMill()
// operationID := ""
// seqUid := ""
// msgListToMongo := make([]MsgInfo, 0)
//
// for _, m := range msgList {
// seqUid = getSeqUid(uid, m.MsgData.Seq)
// var err error
// sMsg := MsgInfo{}
// sMsg.SendTime = sendTime
// if sMsg.Msg, err = proto.Marshal(m.MsgData); err != nil {
// return utils.Wrap(err, "")
// }
// msgListToMongo = append(msgListToMongo, sMsg)
// }
//
// filter := bson.M{"uid": seqUid}
// log.NewDebug(operationID, "filter ", seqUid)
// err := c.FindOneAndUpdate(ctx, filter, bson.M{"$push": bson.M{"msg": bson.M{"$each": msgListToMongo}}}).Err()
// log.NewWarn(operationID, "get mgoSession cost time", getCurrentTimestampByMill()-newTime)
// if err != nil {
// sChat := UserChat{}
// sChat.UID = seqUid
// sChat.Msg = msgListToMongo
//
// if _, err = c.InsertOne(ctx, &sChat); err != nil {
// log.NewError(operationID, "InsertOne failed", filter, err.Error(), sChat)
// return utils.Wrap(err, "")
// }
// } else {
// log.NewDebug(operationID, "FindOneAndUpdate ok", filter)
// }
//
// log.NewDebug(operationID, "find mgo uid cost time", getCurrentTimestampByMill()-newTime)
// return nil
//}
2023-01-16 20:14:26 +08:00
func (d *db.DataBases) SaveUserChat(uid string, sendTime int64, m *pbMsg.MsgDataToDB) error {
var seqUid string
newTime := getCurrentTimestampByMill()
2021-06-28 15:32:26 +08:00
session := d.mgoSession.Clone()
2021-05-26 19:17:51 +08:00
if session == nil {
return errors.New("session == nil")
}
defer session.Close()
2021-12-10 10:49:49 +08:00
log.NewDebug("", "get mgoSession cost time", getCurrentTimestampByMill()-newTime)
2021-06-28 15:32:26 +08:00
c := session.DB(config.Config.Mongo.DBDatabase).C(cChat)
2021-12-23 17:34:32 +08:00
seqUid = getSeqUid(uid, m.MsgData.Seq)
n, err := c.Find(bson.M{"uid": seqUid}).Count()
2021-05-26 19:17:51 +08:00
if err != nil {
return err
}
2021-12-10 10:49:49 +08:00
log.NewDebug("", "find mgo uid cost time", getCurrentTimestampByMill()-newTime)
2021-06-28 15:32:26 +08:00
sMsg := MsgInfo{}
sMsg.SendTime = sendTime
2022-01-20 18:38:20 +08:00
if sMsg.Msg, err = proto.Marshal(m.MsgData); err != nil {
2021-06-28 15:32:26 +08:00
return err
}
2021-05-26 19:17:51 +08:00
if n == 0 {
sChat := UserChat{}
sChat.UID = seqUid
2021-06-28 15:32:26 +08:00
sChat.Msg = append(sChat.Msg, sMsg)
2021-05-26 19:17:51 +08:00
err = c.Insert(&sChat)
if err != nil {
return err
}
} else {
err = c.Update(bson.M{"uid": seqUid}, bson.M{"$push": bson.M{"msg": sMsg}})
2021-05-26 19:17:51 +08:00
if err != nil {
return err
}
}
2021-12-10 10:49:49 +08:00
log.NewDebug("", "insert mgo data cost time", getCurrentTimestampByMill()-newTime)
2021-05-26 19:17:51 +08:00
return nil
}
2023-01-16 20:14:26 +08:00
func (d *db.DataBases) DelUserChatMongo2(uid string) error {
2022-02-21 15:01:06 +08:00
ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second)
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
filter := bson.M{"uid": uid}
2021-05-26 19:17:51 +08:00
2021-06-28 15:32:26 +08:00
delTime := time.Now().Unix() - int64(config.Config.Mongo.DBRetainChatRecords)*24*3600
2022-02-21 15:01:06 +08:00
if _, err := c.UpdateOne(ctx, filter, bson.M{"$pull": bson.M{"msg": bson.M{"sendtime": bson.M{"$lte": delTime}}}}); err != nil {
return utils.Wrap(err, "")
2021-05-26 19:17:51 +08:00
}
return nil
}
2023-01-16 20:14:26 +08:00
func (d *db.DataBases) MgoSkipUID(count int) (string, error) {
2022-02-21 15:01:06 +08:00
return "", nil
//session := d.mgoSession.Clone()
//if session == nil {
// return "", errors.New("session == nil")
//}
//defer session.Close()
//
//c := session.DB(config.Config.Mongo.DBDatabase).C(cChat)
//
//sChat := UserChat{}
//c.Find(nil).Skip(count).Limit(1).One(&sChat)
//return sChat.UID, nil
2021-05-26 19:17:51 +08:00
}
2021-07-15 11:14:07 +08:00
2022-03-25 18:46:27 +08:00
func generateTagID(tagName, userID string) string {
2022-03-29 15:50:20 +08:00
return utils.Md5(tagName + userID + strconv.Itoa(rand.Int()) + time.Now().String())
2022-03-25 18:46:27 +08:00
}
2022-04-15 17:43:04 +08:00
func generateWorkMomentID(userID string) string {
return utils.Md5(userID + strconv.Itoa(rand.Int()) + time.Now().String())
}
2022-04-18 19:24:36 +08:00
func generateWorkMomentCommentID(workMomentID string) string {
return utils.Md5(workMomentID + strconv.Itoa(rand.Int()) + time.Now().String())
}
func getCurrentTimestampByMill() int64 {
return time.Now().UnixNano() / 1e6
}
2022-05-22 15:09:21 +08:00
func GetCurrentTimestampByMill() int64 {
return time.Now().UnixNano() / 1e6
}
2022-03-25 18:46:27 +08:00
2022-01-20 11:36:43 +08:00
func getSeqUid(uid string, seq uint32) string {
seqSuffix := seq / singleGocMsgNum
2021-11-10 18:13:04 +08:00
return indexGen(uid, seqSuffix)
}
2022-05-31 19:16:29 +08:00
func getSeqUserIDList(userID string, maxSeq uint32) []string {
seqMaxSuffix := maxSeq / singleGocMsgNum
var seqUserIDList []string
for i := 0; i <= int(seqMaxSuffix); i++ {
seqUserID := indexGen(userID, uint32(i))
seqUserIDList = append(seqUserIDList, seqUserID)
}
return seqUserIDList
}
2022-05-28 18:10:08 +08:00
func getSeqSuperGroupID(groupID string, seq uint32) string {
seqSuffix := seq / singleGocMsgNum
return superGroupIndexGen(groupID, seqSuffix)
}
2022-03-17 19:00:05 +08:00
2022-05-24 21:34:02 +08:00
func GetSeqUid(uid string, seq uint32) string {
return getSeqUid(uid, seq)
}
2022-03-17 19:00:05 +08:00
func getMsgIndex(seq uint32) int {
seqSuffix := seq / singleGocMsgNum
var index uint32
if seqSuffix == 0 {
2022-05-04 18:02:24 +08:00
index = (seq - seqSuffix*singleGocMsgNum) - 1
2022-03-17 19:00:05 +08:00
} else {
index = seq - seqSuffix*singleGocMsgNum
}
return int(index)
}
2022-01-20 11:36:43 +08:00
func isContainInt32(target uint32, List []uint32) bool {
for _, element := range List {
if target == element {
return true
}
}
return false
2022-05-25 15:56:39 +08:00
}
2022-05-25 15:56:39 +08:00
func isNotContainInt32(target uint32, List []uint32) bool {
for _, i := range List {
if i == target {
return false
}
}
return true
}
2022-05-25 15:56:39 +08:00
2022-01-20 11:36:43 +08:00
func indexGen(uid string, seqSuffix uint32) string {
return uid + ":" + strconv.FormatInt(int64(seqSuffix), 10)
2021-11-10 18:13:04 +08:00
}
2022-08-08 11:30:10 +08:00
2022-05-28 18:10:08 +08:00
func superGroupIndexGen(groupID string, seqSuffix uint32) string {
return "super_group_" + groupID + ":" + strconv.FormatInt(int64(seqSuffix), 10)
}
2022-05-31 19:16:29 +08:00
2023-01-16 20:14:26 +08:00
func (d *db.DataBases) CleanUpUserMsgFromMongo(userID string, operationID string) error {
2022-05-31 19:16:29 +08:00
ctx := context.Background()
c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cChat)
maxSeq, err := d.GetUserMaxSeq(userID)
if err == redis.Nil {
return nil
}
if err != nil {
return utils.Wrap(err, "")
}
seqUsers := getSeqUserIDList(userID, uint32(maxSeq))
2022-06-01 15:53:14 +08:00
log.Error(operationID, "getSeqUserIDList", seqUsers)
2022-05-31 19:16:29 +08:00
_, err = c.DeleteMany(ctx, bson.M{"uid": bson.M{"$in": seqUsers}})
if err == mongo.ErrNoDocuments {
return nil
}
return utils.Wrap(err, "")
}