|
|
|
@ -1,68 +1,56 @@
|
|
|
|
|
// Copyright © 2023 OpenIM. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
|
|
package unrelation
|
|
|
|
|
package mgo
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
table "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/unrelation"
|
|
|
|
|
"github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation"
|
|
|
|
|
"github.com/openimsdk/protocol/constant"
|
|
|
|
|
"github.com/openimsdk/protocol/msg"
|
|
|
|
|
"github.com/openimsdk/protocol/sdkws"
|
|
|
|
|
"github.com/openimsdk/tools/errs"
|
|
|
|
|
"github.com/openimsdk/tools/log"
|
|
|
|
|
"github.com/openimsdk/tools/mgoutil"
|
|
|
|
|
"github.com/openimsdk/tools/utils"
|
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var ErrMsgListNotExist = errors.New("user not have msg in mongoDB")
|
|
|
|
|
|
|
|
|
|
type MsgMongoDriver struct {
|
|
|
|
|
MsgCollection *mongo.Collection
|
|
|
|
|
model table.MsgDocModel
|
|
|
|
|
func NewMsgMongo(db *mongo.Database) (relation.MsgDocModelInterface, error) {
|
|
|
|
|
coll := db.Collection(new(relation.MsgDocModel).TableName())
|
|
|
|
|
_, err := coll.Indexes().CreateOne(context.Background(), mongo.IndexModel{
|
|
|
|
|
Keys: bson.D{
|
|
|
|
|
{Key: "doc_id", Value: 1},
|
|
|
|
|
},
|
|
|
|
|
Options: options.Index().SetUnique(true),
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errs.Wrap(err)
|
|
|
|
|
}
|
|
|
|
|
return &MsgMgo{coll: coll}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewMsgMongoDriver(database *mongo.Database) table.MsgDocModelInterface {
|
|
|
|
|
collection := database.Collection(table.MsgDocModel{}.TableName())
|
|
|
|
|
return &MsgMongoDriver{MsgCollection: collection}
|
|
|
|
|
type MsgMgo struct {
|
|
|
|
|
coll *mongo.Collection
|
|
|
|
|
model relation.MsgDocModel
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgMongoDriver) PushMsgsToDoc(ctx context.Context, docID string, msgsToMongo []table.MsgInfoModel) error {
|
|
|
|
|
return m.MsgCollection.FindOneAndUpdate(ctx, bson.M{"doc_id": docID}, bson.M{"$push": bson.M{"msgs": bson.M{"$each": msgsToMongo}}}).
|
|
|
|
|
Err()
|
|
|
|
|
func (m *MsgMgo) PushMsgsToDoc(ctx context.Context, docID string, msgsToMongo []relation.MsgInfoModel) error {
|
|
|
|
|
filter := bson.M{"doc_id": docID}
|
|
|
|
|
update := bson.M{"$push": bson.M{"msgs": bson.M{"$each": msgsToMongo}}}
|
|
|
|
|
return mgoutil.UpdateOne(ctx, m.coll, filter, update, false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgMongoDriver) Create(ctx context.Context, model *table.MsgDocModel) error {
|
|
|
|
|
_, err := m.MsgCollection.InsertOne(ctx, model)
|
|
|
|
|
return err
|
|
|
|
|
func (m *MsgMgo) Create(ctx context.Context, model *relation.MsgDocModel) error {
|
|
|
|
|
return mgoutil.InsertMany(ctx, m.coll, []*relation.MsgDocModel{model})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgMongoDriver) UpdateMsg(
|
|
|
|
|
ctx context.Context,
|
|
|
|
|
docID string,
|
|
|
|
|
index int64,
|
|
|
|
|
key string,
|
|
|
|
|
value any,
|
|
|
|
|
) (*mongo.UpdateResult, error) {
|
|
|
|
|
func (m *MsgMgo) UpdateMsg(ctx context.Context, docID string, index int64, key string, value any) (*mongo.UpdateResult, error) {
|
|
|
|
|
var field string
|
|
|
|
|
if key == "" {
|
|
|
|
|
field = fmt.Sprintf("msgs.%d", index)
|
|
|
|
@ -71,21 +59,10 @@ func (m *MsgMongoDriver) UpdateMsg(
|
|
|
|
|
}
|
|
|
|
|
filter := bson.M{"doc_id": docID}
|
|
|
|
|
update := bson.M{"$set": bson.M{field: value}}
|
|
|
|
|
res, err := m.MsgCollection.UpdateOne(ctx, filter, update)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errs.Wrap(err)
|
|
|
|
|
}
|
|
|
|
|
return res, nil
|
|
|
|
|
return mgoutil.UpdateOneResult(ctx, m.coll, filter, update)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PushUnique value must slice.
|
|
|
|
|
func (m *MsgMongoDriver) PushUnique(
|
|
|
|
|
ctx context.Context,
|
|
|
|
|
docID string,
|
|
|
|
|
index int64,
|
|
|
|
|
key string,
|
|
|
|
|
value any,
|
|
|
|
|
) (*mongo.UpdateResult, error) {
|
|
|
|
|
func (m *MsgMgo) PushUnique(ctx context.Context, docID string, index int64, key string, value any) (*mongo.UpdateResult, error) {
|
|
|
|
|
var field string
|
|
|
|
|
if key == "" {
|
|
|
|
|
field = fmt.Sprintf("msgs.%d", index)
|
|
|
|
@ -98,138 +75,24 @@ func (m *MsgMongoDriver) PushUnique(
|
|
|
|
|
field: bson.M{"$each": value},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
res, err := m.MsgCollection.UpdateOne(ctx, filter, update)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errs.Wrap(err)
|
|
|
|
|
}
|
|
|
|
|
return res, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgMongoDriver) UpdateMsgContent(ctx context.Context, docID string, index int64, msg []byte) error {
|
|
|
|
|
_, err := m.MsgCollection.UpdateOne(
|
|
|
|
|
ctx,
|
|
|
|
|
bson.M{"doc_id": docID},
|
|
|
|
|
bson.M{"$set": bson.M{fmt.Sprintf("msgs.%d.msg", index): msg}},
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errs.Wrap(err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgMongoDriver) UpdateMsgStatusByIndexInOneDoc(ctx context.Context, docID string, msg *sdkws.MsgData, seqIndex int, status int32) error {
|
|
|
|
|
msg.Status = status
|
|
|
|
|
bytes, err := proto.Marshal(msg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errs.Wrap(err)
|
|
|
|
|
}
|
|
|
|
|
_, err = m.MsgCollection.UpdateOne(
|
|
|
|
|
ctx,
|
|
|
|
|
bson.M{"doc_id": docID},
|
|
|
|
|
bson.M{"$set": bson.M{fmt.Sprintf("msgs.%d.msg", seqIndex): bytes}},
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errs.WrapMsg(err, fmt.Sprintf("docID is %s, seqIndex is %d", docID, seqIndex))
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgMongoDriver) FindOneByDocID(ctx context.Context, docID string) (*table.MsgDocModel, error) {
|
|
|
|
|
doc := &table.MsgDocModel{}
|
|
|
|
|
err := m.MsgCollection.FindOne(ctx, bson.M{"doc_id": docID}).Decode(doc)
|
|
|
|
|
return doc, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgMongoDriver) GetMsgDocModelByIndex(
|
|
|
|
|
ctx context.Context,
|
|
|
|
|
conversationID string,
|
|
|
|
|
index, sort int64,
|
|
|
|
|
) (*table.MsgDocModel, error) {
|
|
|
|
|
if sort != 1 && sort != -1 {
|
|
|
|
|
return nil, errs.ErrArgs.WrapMsg("mongo sort must be 1 or -1")
|
|
|
|
|
}
|
|
|
|
|
findOpts := options.Find().SetLimit(1).SetSkip(index).SetSort(bson.M{"doc_id": sort})
|
|
|
|
|
cursor, err := m.MsgCollection.Find(
|
|
|
|
|
ctx,
|
|
|
|
|
bson.M{"doc_id": primitive.Regex{Pattern: fmt.Sprintf("^%s:", conversationID)}},
|
|
|
|
|
findOpts,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errs.WrapMsg(err, fmt.Sprintf("conversationID is %s", conversationID))
|
|
|
|
|
}
|
|
|
|
|
var msgs []table.MsgDocModel
|
|
|
|
|
err = cursor.All(ctx, &msgs)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errs.WrapMsg(err, fmt.Sprintf("cursor is %s", cursor.Current.String()))
|
|
|
|
|
}
|
|
|
|
|
if len(msgs) > 0 {
|
|
|
|
|
return &msgs[0], nil
|
|
|
|
|
}
|
|
|
|
|
return nil, ErrMsgListNotExist
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgMongoDriver) GetNewestMsg(ctx context.Context, conversationID string) (*table.MsgInfoModel, error) {
|
|
|
|
|
var skip int64 = 0
|
|
|
|
|
for {
|
|
|
|
|
msgDocModel, err := m.GetMsgDocModelByIndex(ctx, conversationID, skip, -1)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
for i := len(msgDocModel.Msg) - 1; i >= 0; i-- {
|
|
|
|
|
if msgDocModel.Msg[i].Msg != nil {
|
|
|
|
|
return msgDocModel.Msg[i], nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
skip++
|
|
|
|
|
}
|
|
|
|
|
return mgoutil.UpdateOneResult(ctx, m.coll, filter, update)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgMongoDriver) GetOldestMsg(ctx context.Context, conversationID string) (*table.MsgInfoModel, error) {
|
|
|
|
|
var skip int64 = 0
|
|
|
|
|
for {
|
|
|
|
|
msgDocModel, err := m.GetMsgDocModelByIndex(ctx, conversationID, skip, 1)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
for i, v := range msgDocModel.Msg {
|
|
|
|
|
if v.Msg != nil {
|
|
|
|
|
return msgDocModel.Msg[i], nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
skip++
|
|
|
|
|
}
|
|
|
|
|
func (m *MsgMgo) UpdateMsgContent(ctx context.Context, docID string, index int64, msg []byte) error {
|
|
|
|
|
filter := bson.M{"doc_id": docID}
|
|
|
|
|
update := bson.M{"$set": bson.M{fmt.Sprintf("msgs.%d.msg", index): msg}}
|
|
|
|
|
return mgoutil.UpdateOne(ctx, m.coll, filter, update, false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgMongoDriver) DeleteMsgsInOneDocByIndex(ctx context.Context, docID string, indexes []int) error {
|
|
|
|
|
updates := bson.M{
|
|
|
|
|
"$set": bson.M{},
|
|
|
|
|
}
|
|
|
|
|
for _, index := range indexes {
|
|
|
|
|
updates["$set"].(bson.M)[fmt.Sprintf("msgs.%d", index)] = bson.M{
|
|
|
|
|
"msg": nil,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_, err := m.MsgCollection.UpdateMany(ctx, bson.M{"doc_id": docID}, updates)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errs.WrapMsg(err, fmt.Sprintf("docID is %s, indexes is %v", docID, indexes))
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
func (m *MsgMgo) IsExistDocID(ctx context.Context, docID string) (bool, error) {
|
|
|
|
|
return mgoutil.Exist(ctx, m.coll, bson.M{"doc_id": docID})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgMongoDriver) DeleteDocs(ctx context.Context, docIDs []string) error {
|
|
|
|
|
if docIDs == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
_, err := m.MsgCollection.DeleteMany(ctx, bson.M{"doc_id": bson.M{"$in": docIDs}})
|
|
|
|
|
return err
|
|
|
|
|
func (m *MsgMgo) FindOneByDocID(ctx context.Context, docID string) (*relation.MsgDocModel, error) {
|
|
|
|
|
return mgoutil.FindOne[*relation.MsgDocModel](ctx, m.coll, bson.M{"doc_id": docID})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgMongoDriver) GetMsgBySeqIndexIn1Doc(
|
|
|
|
|
ctx context.Context,
|
|
|
|
|
userID string,
|
|
|
|
|
docID string,
|
|
|
|
|
seqs []int64,
|
|
|
|
|
) (msgs []*table.MsgInfoModel, err error) {
|
|
|
|
|
func (m *MsgMgo) GetMsgBySeqIndexIn1Doc(ctx context.Context, userID, docID string, seqs []int64) ([]*relation.MsgInfoModel, error) {
|
|
|
|
|
indexs := make([]int64, 0, len(seqs))
|
|
|
|
|
for _, seq := range seqs {
|
|
|
|
|
indexs = append(indexs, m.model.GetMsgIndex(seq))
|
|
|
|
@ -270,20 +133,14 @@ func (m *MsgMongoDriver) GetMsgBySeqIndexIn1Doc(
|
|
|
|
|
{Key: "msgs.del_list", Value: 0},
|
|
|
|
|
}}},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cur, err := m.MsgCollection.Aggregate(ctx, pipeline)
|
|
|
|
|
msgDocModel, err := mgoutil.Aggregate[*relation.MsgDocModel](ctx, m.coll, pipeline)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errs.Wrap(err)
|
|
|
|
|
}
|
|
|
|
|
defer cur.Close(ctx)
|
|
|
|
|
var msgDocModel []table.MsgDocModel
|
|
|
|
|
if err := cur.All(ctx, &msgDocModel); err != nil {
|
|
|
|
|
return nil, errs.WrapMsg(err, fmt.Sprintf("docID is %s, seqs is %v", docID, seqs))
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if len(msgDocModel) == 0 {
|
|
|
|
|
return nil, errs.Wrap(mongo.ErrNoDocuments)
|
|
|
|
|
}
|
|
|
|
|
msgs = make([]*table.MsgInfoModel, 0, len(msgDocModel[0].Msg))
|
|
|
|
|
msgs := make([]*relation.MsgInfoModel, 0, len(msgDocModel[0].Msg))
|
|
|
|
|
for i := range msgDocModel[0].Msg {
|
|
|
|
|
msg := msgDocModel[0].Msg[i]
|
|
|
|
|
if msg == nil || msg.Msg == nil {
|
|
|
|
@ -303,14 +160,14 @@ func (m *MsgMongoDriver) GetMsgBySeqIndexIn1Doc(
|
|
|
|
|
Seq: msg.Msg.Seq,
|
|
|
|
|
Ex: msg.Msg.Ex,
|
|
|
|
|
}
|
|
|
|
|
data, err := json.Marshal(&revokeContent)
|
|
|
|
|
data, err := utils.JsonMarshal(&revokeContent)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errs.WrapMsg(err, fmt.Sprintf("docID is %s, seqs is %v", docID, seqs))
|
|
|
|
|
}
|
|
|
|
|
elem := sdkws.NotificationElem{
|
|
|
|
|
Detail: string(data),
|
|
|
|
|
}
|
|
|
|
|
content, err := json.Marshal(&elem)
|
|
|
|
|
content, err := utils.JsonMarshal(&elem)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errs.WrapMsg(err, fmt.Sprintf("docID is %s, seqs is %v", docID, seqs))
|
|
|
|
|
}
|
|
|
|
@ -322,16 +179,72 @@ func (m *MsgMongoDriver) GetMsgBySeqIndexIn1Doc(
|
|
|
|
|
return msgs, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgMongoDriver) IsExistDocID(ctx context.Context, docID string) (bool, error) {
|
|
|
|
|
count, err := m.MsgCollection.CountDocuments(ctx, bson.M{"doc_id": docID})
|
|
|
|
|
func (m *MsgMgo) GetNewestMsg(ctx context.Context, conversationID string) (*relation.MsgInfoModel, error) {
|
|
|
|
|
for skip := int64(0); ; skip++ {
|
|
|
|
|
msgDocModel, err := m.GetMsgDocModelByIndex(ctx, conversationID, skip, -1)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
for i := len(msgDocModel.Msg) - 1; i >= 0; i-- {
|
|
|
|
|
if msgDocModel.Msg[i].Msg != nil {
|
|
|
|
|
return msgDocModel.Msg[i], nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgMgo) GetOldestMsg(ctx context.Context, conversationID string) (*relation.MsgInfoModel, error) {
|
|
|
|
|
for skip := int64(0); ; skip++ {
|
|
|
|
|
msgDocModel, err := m.GetMsgDocModelByIndex(ctx, conversationID, skip, 1)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
for i, v := range msgDocModel.Msg {
|
|
|
|
|
if v.Msg != nil {
|
|
|
|
|
return msgDocModel.Msg[i], nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgMgo) DeleteDocs(ctx context.Context, docIDs []string) error {
|
|
|
|
|
if len(docIDs) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return mgoutil.DeleteMany(ctx, m.coll, bson.M{"doc_id": bson.M{"$in": docIDs}})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgMgo) GetMsgDocModelByIndex(ctx context.Context, conversationID string, index, sort int64) (*relation.MsgDocModel, error) {
|
|
|
|
|
if sort != 1 && sort != -1 {
|
|
|
|
|
return nil, errs.ErrArgs.WrapMsg("mongo sort must be 1 or -1")
|
|
|
|
|
}
|
|
|
|
|
opt := options.Find().SetLimit(1).SetSkip(index).SetSort(bson.M{"doc_id": sort}).SetLimit(1)
|
|
|
|
|
filter := bson.M{"doc_id": primitive.Regex{Pattern: fmt.Sprintf("^%s:", conversationID)}}
|
|
|
|
|
msgs, err := mgoutil.Find[*relation.MsgDocModel](ctx, m.coll, filter, opt)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, errs.WrapMsg(err, fmt.Sprintf("docID is %s", docID))
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if len(msgs) > 0 {
|
|
|
|
|
return msgs[0], nil
|
|
|
|
|
}
|
|
|
|
|
return nil, errs.Wrap(ErrMsgListNotExist)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgMgo) DeleteMsgsInOneDocByIndex(ctx context.Context, docID string, indexes []int) error {
|
|
|
|
|
update := bson.M{
|
|
|
|
|
"$set": bson.M{},
|
|
|
|
|
}
|
|
|
|
|
for _, index := range indexes {
|
|
|
|
|
update["$set"].(bson.M)[fmt.Sprintf("msgs.%d", index)] = bson.M{
|
|
|
|
|
"msg": nil,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return count > 0, nil
|
|
|
|
|
_, err := mgoutil.UpdateMany(ctx, m.coll, bson.M{"doc_id": docID}, update)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgMongoDriver) MarkSingleChatMsgsAsRead(ctx context.Context, userID string, docID string, indexes []int64) error {
|
|
|
|
|
updates := []mongo.WriteModel{}
|
|
|
|
|
func (m *MsgMgo) MarkSingleChatMsgsAsRead(ctx context.Context, userID string, docID string, indexes []int64) error {
|
|
|
|
|
var updates []mongo.WriteModel
|
|
|
|
|
for _, index := range indexes {
|
|
|
|
|
filter := bson.M{
|
|
|
|
|
"doc_id": docID,
|
|
|
|
@ -349,204 +262,110 @@ func (m *MsgMongoDriver) MarkSingleChatMsgsAsRead(ctx context.Context, userID st
|
|
|
|
|
SetUpdate(update)
|
|
|
|
|
updates = append(updates, updateModel)
|
|
|
|
|
}
|
|
|
|
|
_, err := m.MsgCollection.BulkWrite(ctx, updates)
|
|
|
|
|
return errs.WrapMsg(err, fmt.Sprintf("docID is %s, indexes is %v", docID, indexes))
|
|
|
|
|
if _, err := m.coll.BulkWrite(ctx, updates); err != nil {
|
|
|
|
|
return errs.WrapMsg(err, fmt.Sprintf("docID is %s, indexes is %v", docID, indexes))
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgMgo) SearchMessage(ctx context.Context, req *msg.SearchMessageReq) (int32, []*relation.MsgInfoModel, error) {
|
|
|
|
|
var pipe mongo.Pipeline
|
|
|
|
|
condition := bson.A{}
|
|
|
|
|
if req.SendTime != "" {
|
|
|
|
|
// Changed to keyed fields for bson.M to avoid govet errors
|
|
|
|
|
condition = append(condition, bson.M{"$eq": bson.A{bson.M{"$dateToString": bson.M{"format": "%Y-%m-%d", "date": bson.M{"$toDate": "$$item.msg.send_time"}}}, req.SendTime}})
|
|
|
|
|
}
|
|
|
|
|
if req.ContentType != 0 {
|
|
|
|
|
condition = append(condition, bson.M{"$eq": bson.A{"$$item.msg.content_type", req.ContentType}})
|
|
|
|
|
}
|
|
|
|
|
if req.SessionType != 0 {
|
|
|
|
|
condition = append(condition, bson.M{"$eq": bson.A{"$$item.msg.session_type", req.SessionType}})
|
|
|
|
|
}
|
|
|
|
|
if req.RecvID != "" {
|
|
|
|
|
condition = append(condition, bson.M{"$regexFind": bson.M{"input": "$$item.msg.recv_id", "regex": req.RecvID}})
|
|
|
|
|
}
|
|
|
|
|
if req.SendID != "" {
|
|
|
|
|
condition = append(condition, bson.M{"$regexFind": bson.M{"input": "$$item.msg.send_id", "regex": req.SendID}})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
or := bson.A{
|
|
|
|
|
bson.M{"doc_id": bson.M{"$regex": "^si_", "$options": "i"}},
|
|
|
|
|
bson.M{"doc_id": bson.M{"$regex": "^g_", "$options": "i"}},
|
|
|
|
|
bson.M{"doc_id": bson.M{"$regex": "^sg_", "$options": "i"}},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Use bson.D with keyed fields to specify the order explicitly
|
|
|
|
|
pipe = mongo.Pipeline{
|
|
|
|
|
{{"$match", bson.D{{Key: "$or", Value: or}}}},
|
|
|
|
|
{{"$project", bson.D{
|
|
|
|
|
{Key: "msgs", Value: bson.D{
|
|
|
|
|
{Key: "$filter", Value: bson.D{
|
|
|
|
|
{Key: "input", Value: "$msgs"},
|
|
|
|
|
{Key: "as", Value: "item"},
|
|
|
|
|
{Key: "cond", Value: bson.D{{Key: "$and", Value: condition}}},
|
|
|
|
|
}},
|
|
|
|
|
}},
|
|
|
|
|
{Key: "doc_id", Value: 1},
|
|
|
|
|
}}},
|
|
|
|
|
{{"$unwind", bson.M{"path": "$msgs"}}},
|
|
|
|
|
{{"$sort", bson.M{"msgs.msg.send_time": -1}}},
|
|
|
|
|
}
|
|
|
|
|
type docModel struct {
|
|
|
|
|
DocID string `bson:"doc_id"`
|
|
|
|
|
Msg *relation.MsgInfoModel `bson:"msgs"`
|
|
|
|
|
}
|
|
|
|
|
msgsDocs, err := mgoutil.Aggregate[*docModel](ctx, m.coll, pipe)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, nil, err
|
|
|
|
|
}
|
|
|
|
|
msgs := make([]*relation.MsgInfoModel, 0)
|
|
|
|
|
for _, doc := range msgsDocs {
|
|
|
|
|
msgInfo := doc.Msg
|
|
|
|
|
if msgInfo == nil || msgInfo.Msg == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if msgInfo.Revoke != nil {
|
|
|
|
|
revokeContent := sdkws.MessageRevokedContent{
|
|
|
|
|
RevokerID: msgInfo.Revoke.UserID,
|
|
|
|
|
RevokerRole: msgInfo.Revoke.Role,
|
|
|
|
|
ClientMsgID: msgInfo.Msg.ClientMsgID,
|
|
|
|
|
RevokerNickname: msgInfo.Revoke.Nickname,
|
|
|
|
|
RevokeTime: msgInfo.Revoke.Time,
|
|
|
|
|
SourceMessageSendTime: msgInfo.Msg.SendTime,
|
|
|
|
|
SourceMessageSendID: msgInfo.Msg.SendID,
|
|
|
|
|
SourceMessageSenderNickname: msgInfo.Msg.SenderNickname,
|
|
|
|
|
SessionType: msgInfo.Msg.SessionType,
|
|
|
|
|
Seq: msgInfo.Msg.Seq,
|
|
|
|
|
Ex: msgInfo.Msg.Ex,
|
|
|
|
|
}
|
|
|
|
|
data, err := utils.JsonMarshal(&revokeContent)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, nil, errs.WrapMsg(err, "json.Marshal revokeContent")
|
|
|
|
|
}
|
|
|
|
|
elem := sdkws.NotificationElem{Detail: string(data)}
|
|
|
|
|
content, err := utils.JsonMarshal(&elem)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, nil, errs.WrapMsg(err, "json.Marshal elem")
|
|
|
|
|
}
|
|
|
|
|
msgInfo.Msg.ContentType = constant.MsgRevokeNotification
|
|
|
|
|
msgInfo.Msg.Content = string(content)
|
|
|
|
|
}
|
|
|
|
|
msgs = append(msgs, msgInfo)
|
|
|
|
|
}
|
|
|
|
|
start := (req.Pagination.PageNumber - 1) * req.Pagination.ShowNumber
|
|
|
|
|
n := int32(len(msgs))
|
|
|
|
|
if start >= n {
|
|
|
|
|
return n, []*relation.MsgInfoModel{}, nil
|
|
|
|
|
}
|
|
|
|
|
if start+req.Pagination.ShowNumber < n {
|
|
|
|
|
msgs = msgs[start : start+req.Pagination.ShowNumber]
|
|
|
|
|
} else {
|
|
|
|
|
msgs = msgs[start:]
|
|
|
|
|
}
|
|
|
|
|
return n, msgs, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RangeUserSendCount
|
|
|
|
|
// db.msg.aggregate([
|
|
|
|
|
//
|
|
|
|
|
// {
|
|
|
|
|
// $match: {
|
|
|
|
|
// "msgs.msg.send_time": {
|
|
|
|
|
// "$gte": 0,
|
|
|
|
|
// "$lt": 1788122092317
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// },
|
|
|
|
|
// {
|
|
|
|
|
// "$addFields": {
|
|
|
|
|
// "msgs": {
|
|
|
|
|
// "$filter": {
|
|
|
|
|
// "input": "$msgs",
|
|
|
|
|
// "as": "item",
|
|
|
|
|
// "cond": {
|
|
|
|
|
// "$and": [
|
|
|
|
|
// {
|
|
|
|
|
// $gte: ["$$item.msg.send_time", 0]
|
|
|
|
|
// },
|
|
|
|
|
// {
|
|
|
|
|
// $lt: ["$$item.msg.send_time", 1788122092317]
|
|
|
|
|
// }
|
|
|
|
|
// ]
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// },
|
|
|
|
|
// {
|
|
|
|
|
// "$project": {
|
|
|
|
|
// "_id": 0,
|
|
|
|
|
//
|
|
|
|
|
// },
|
|
|
|
|
//
|
|
|
|
|
// },
|
|
|
|
|
// {
|
|
|
|
|
// "$project": {
|
|
|
|
|
// "result": {
|
|
|
|
|
// "$map": {
|
|
|
|
|
// "input": "$msgs",
|
|
|
|
|
// "as": "item",
|
|
|
|
|
// "in": {
|
|
|
|
|
// user_id: "$$item.msg.send_id",
|
|
|
|
|
// send_date: {
|
|
|
|
|
// $dateToString: {
|
|
|
|
|
// format: "%Y-%m-%d",
|
|
|
|
|
// date: {
|
|
|
|
|
// $toDate: "$$item.msg.send_time"
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// },
|
|
|
|
|
//
|
|
|
|
|
// },
|
|
|
|
|
// {
|
|
|
|
|
// "$unwind": "$result"
|
|
|
|
|
// },
|
|
|
|
|
// {
|
|
|
|
|
// "$group": {
|
|
|
|
|
// _id: "$result.send_date",
|
|
|
|
|
// count: {
|
|
|
|
|
// $sum: 1
|
|
|
|
|
// },
|
|
|
|
|
// original: {
|
|
|
|
|
// $push: "$$ROOT"
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// },
|
|
|
|
|
// {
|
|
|
|
|
// "$addFields": {
|
|
|
|
|
// "dates": "$$ROOT"
|
|
|
|
|
// }
|
|
|
|
|
// },
|
|
|
|
|
// {
|
|
|
|
|
// "$project": {
|
|
|
|
|
// "_id": 0,
|
|
|
|
|
// "count": 0,
|
|
|
|
|
// "dates.original": 0,
|
|
|
|
|
//
|
|
|
|
|
// },
|
|
|
|
|
//
|
|
|
|
|
// },
|
|
|
|
|
// {
|
|
|
|
|
// "$group": {
|
|
|
|
|
// _id: null,
|
|
|
|
|
// count: {
|
|
|
|
|
// $sum: 1
|
|
|
|
|
// },
|
|
|
|
|
// dates: {
|
|
|
|
|
// $push: "$dates"
|
|
|
|
|
// },
|
|
|
|
|
// original: {
|
|
|
|
|
// $push: "$original"
|
|
|
|
|
// },
|
|
|
|
|
//
|
|
|
|
|
// }
|
|
|
|
|
// },
|
|
|
|
|
// {
|
|
|
|
|
// "$unwind": "$original"
|
|
|
|
|
// },
|
|
|
|
|
// {
|
|
|
|
|
// "$unwind": "$original"
|
|
|
|
|
// },
|
|
|
|
|
// {
|
|
|
|
|
// "$group": {
|
|
|
|
|
// _id: "$original.result.user_id",
|
|
|
|
|
// count: {
|
|
|
|
|
// $sum: 1
|
|
|
|
|
// },
|
|
|
|
|
// original: {
|
|
|
|
|
// $push: "$dates"
|
|
|
|
|
// },
|
|
|
|
|
//
|
|
|
|
|
// }
|
|
|
|
|
// },
|
|
|
|
|
// {
|
|
|
|
|
// "$addFields": {
|
|
|
|
|
// "dates": {
|
|
|
|
|
// $arrayElemAt: ["$original", 0]
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// },
|
|
|
|
|
// {
|
|
|
|
|
// "$project": {
|
|
|
|
|
// original: 0
|
|
|
|
|
// }
|
|
|
|
|
// },
|
|
|
|
|
// {
|
|
|
|
|
// $sort: {
|
|
|
|
|
// count: - 1
|
|
|
|
|
// }
|
|
|
|
|
// },
|
|
|
|
|
// {
|
|
|
|
|
// "$group": {
|
|
|
|
|
// _id: null,
|
|
|
|
|
// user_count: {
|
|
|
|
|
// $sum: 1
|
|
|
|
|
// },
|
|
|
|
|
// users: {
|
|
|
|
|
// $push: "$$ROOT"
|
|
|
|
|
// },
|
|
|
|
|
//
|
|
|
|
|
// }
|
|
|
|
|
// },
|
|
|
|
|
// {
|
|
|
|
|
// "$addFields": {
|
|
|
|
|
// "dates": {
|
|
|
|
|
// $arrayElemAt: ["$users", 0]
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// },
|
|
|
|
|
// {
|
|
|
|
|
// "$addFields": {
|
|
|
|
|
// "dates": "$dates.dates"
|
|
|
|
|
// }
|
|
|
|
|
// },
|
|
|
|
|
// {
|
|
|
|
|
// "$project": {
|
|
|
|
|
// _id: 0,
|
|
|
|
|
// "users.dates": 0,
|
|
|
|
|
//
|
|
|
|
|
// }
|
|
|
|
|
// },
|
|
|
|
|
// {
|
|
|
|
|
// "$addFields": {
|
|
|
|
|
// "msg_count": {
|
|
|
|
|
// $sum: "$users.count"
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// },
|
|
|
|
|
// {
|
|
|
|
|
// "$addFields": {
|
|
|
|
|
// users: {
|
|
|
|
|
// $slice: ["$users", 0, 10]
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// ]).
|
|
|
|
|
func (m *MsgMongoDriver) RangeUserSendCount(
|
|
|
|
|
ctx context.Context,
|
|
|
|
|
start time.Time,
|
|
|
|
|
end time.Time,
|
|
|
|
|
group bool,
|
|
|
|
|
ase bool,
|
|
|
|
|
pageNumber int32,
|
|
|
|
|
showNumber int32,
|
|
|
|
|
) (msgCount int64, userCount int64, users []*table.UserCount, dateCount map[string]int64, err error) {
|
|
|
|
|
func (m *MsgMgo) RangeUserSendCount(ctx context.Context, start time.Time, end time.Time, group bool, ase bool, pageNumber int32, showNumber int32) (msgCount int64, userCount int64, users []*relation.UserCount, dateCount map[string]int64, err error) {
|
|
|
|
|
var sort int
|
|
|
|
|
if ase {
|
|
|
|
|
sort = 1
|
|
|
|
@ -773,21 +592,16 @@ func (m *MsgMongoDriver) RangeUserSendCount(
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
cur, err := m.MsgCollection.Aggregate(ctx, pipeline, options.Aggregate().SetAllowDiskUse(true))
|
|
|
|
|
result, err := mgoutil.Aggregate[*Result](ctx, m.coll, pipeline, options.Aggregate().SetAllowDiskUse(true))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, 0, nil, nil, errs.Wrap(err)
|
|
|
|
|
}
|
|
|
|
|
defer cur.Close(ctx)
|
|
|
|
|
var result []Result
|
|
|
|
|
if err = cur.All(ctx, &result); err != nil {
|
|
|
|
|
return 0, 0, nil, nil, errs.Wrap(err)
|
|
|
|
|
return 0, 0, nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
if len(result) == 0 {
|
|
|
|
|
return 0, 0, nil, nil, errs.Wrap(err)
|
|
|
|
|
}
|
|
|
|
|
users = make([]*table.UserCount, len(result[0].Users))
|
|
|
|
|
users = make([]*relation.UserCount, len(result[0].Users))
|
|
|
|
|
for i, r := range result[0].Users {
|
|
|
|
|
users[i] = &table.UserCount{
|
|
|
|
|
users[i] = &relation.UserCount{
|
|
|
|
|
UserID: r.UserID,
|
|
|
|
|
Count: r.Count,
|
|
|
|
|
}
|
|
|
|
@ -799,14 +613,7 @@ func (m *MsgMongoDriver) RangeUserSendCount(
|
|
|
|
|
return result[0].MsgCount, result[0].UserCount, users, dateCount, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgMongoDriver) RangeGroupSendCount(
|
|
|
|
|
ctx context.Context,
|
|
|
|
|
start time.Time,
|
|
|
|
|
end time.Time,
|
|
|
|
|
ase bool,
|
|
|
|
|
pageNumber int32,
|
|
|
|
|
showNumber int32,
|
|
|
|
|
) (msgCount int64, userCount int64, groups []*table.GroupCount, dateCount map[string]int64, err error) {
|
|
|
|
|
func (m *MsgMgo) RangeGroupSendCount(ctx context.Context, start time.Time, end time.Time, ase bool, pageNumber int32, showNumber int32) (msgCount int64, userCount int64, groups []*relation.GroupCount, dateCount map[string]int64, err error) {
|
|
|
|
|
var sort int
|
|
|
|
|
if ase {
|
|
|
|
|
sort = 1
|
|
|
|
@ -1022,21 +829,16 @@ func (m *MsgMongoDriver) RangeGroupSendCount(
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
cur, err := m.MsgCollection.Aggregate(ctx, pipeline, options.Aggregate().SetAllowDiskUse(true))
|
|
|
|
|
result, err := mgoutil.Aggregate[*Result](ctx, m.coll, pipeline, options.Aggregate().SetAllowDiskUse(true))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, 0, nil, nil, errs.Wrap(err)
|
|
|
|
|
}
|
|
|
|
|
defer cur.Close(ctx)
|
|
|
|
|
var result []Result
|
|
|
|
|
if err = cur.All(ctx, &result); err != nil {
|
|
|
|
|
return 0, 0, nil, nil, errs.Wrap(err)
|
|
|
|
|
return 0, 0, nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
if len(result) == 0 {
|
|
|
|
|
return 0, 0, nil, nil, errs.Wrap(err)
|
|
|
|
|
}
|
|
|
|
|
groups = make([]*table.GroupCount, len(result[0].Groups))
|
|
|
|
|
groups = make([]*relation.GroupCount, len(result[0].Groups))
|
|
|
|
|
for i, r := range result[0].Groups {
|
|
|
|
|
groups[i] = &table.GroupCount{
|
|
|
|
|
groups[i] = &relation.GroupCount{
|
|
|
|
|
GroupID: r.GroupID,
|
|
|
|
|
Count: r.Count,
|
|
|
|
|
}
|
|
|
|
@ -1048,113 +850,51 @@ func (m *MsgMongoDriver) RangeGroupSendCount(
|
|
|
|
|
return result[0].MsgCount, result[0].UserCount, groups, dateCount, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgMongoDriver) SearchMessage(ctx context.Context, req *msg.SearchMessageReq) (int32, []*table.MsgInfoModel, error) {
|
|
|
|
|
total, msgs, err := m.searchMessage(ctx, req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, nil, err
|
|
|
|
|
}
|
|
|
|
|
return total, msgs, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *MsgMongoDriver) searchMessage(ctx context.Context, req *msg.SearchMessageReq) (int32, []*table.MsgInfoModel, error) {
|
|
|
|
|
var pipe mongo.Pipeline
|
|
|
|
|
condition := bson.A{}
|
|
|
|
|
if req.SendTime != "" {
|
|
|
|
|
// Changed to keyed fields for bson.M to avoid govet errors
|
|
|
|
|
condition = append(condition, bson.M{"$eq": bson.A{bson.M{"$dateToString": bson.M{"format": "%Y-%m-%d", "date": bson.M{"$toDate": "$$item.msg.send_time"}}}, req.SendTime}})
|
|
|
|
|
}
|
|
|
|
|
if req.ContentType != 0 {
|
|
|
|
|
condition = append(condition, bson.M{"$eq": bson.A{"$$item.msg.content_type", req.ContentType}})
|
|
|
|
|
}
|
|
|
|
|
if req.SessionType != 0 {
|
|
|
|
|
condition = append(condition, bson.M{"$eq": bson.A{"$$item.msg.session_type", req.SessionType}})
|
|
|
|
|
}
|
|
|
|
|
if req.RecvID != "" {
|
|
|
|
|
condition = append(condition, bson.M{"$regexFind": bson.M{"input": "$$item.msg.recv_id", "regex": req.RecvID}})
|
|
|
|
|
}
|
|
|
|
|
if req.SendID != "" {
|
|
|
|
|
condition = append(condition, bson.M{"$regexFind": bson.M{"input": "$$item.msg.send_id", "regex": req.SendID}})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
or := bson.A{
|
|
|
|
|
bson.M{"doc_id": bson.M{"$regex": "^si_", "$options": "i"}},
|
|
|
|
|
bson.M{"doc_id": bson.M{"$regex": "^g_", "$options": "i"}},
|
|
|
|
|
bson.M{"doc_id": bson.M{"$regex": "^sg_", "$options": "i"}},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Use bson.D with keyed fields to specify the order explicitly
|
|
|
|
|
pipe = mongo.Pipeline{
|
|
|
|
|
{{"$match", bson.D{{Key: "$or", Value: or}}}},
|
|
|
|
|
{{"$project", bson.D{
|
|
|
|
|
{Key: "msgs", Value: bson.D{
|
|
|
|
|
{Key: "$filter", Value: bson.D{
|
|
|
|
|
{Key: "input", Value: "$msgs"},
|
|
|
|
|
{Key: "as", Value: "item"},
|
|
|
|
|
{Key: "cond", Value: bson.D{{Key: "$and", Value: condition}}},
|
|
|
|
|
}},
|
|
|
|
|
}},
|
|
|
|
|
{Key: "doc_id", Value: 1},
|
|
|
|
|
}}},
|
|
|
|
|
{{"$unwind", bson.M{"path": "$msgs"}}},
|
|
|
|
|
{{"$sort", bson.M{"msgs.msg.send_time": -1}}},
|
|
|
|
|
}
|
|
|
|
|
cursor, err := m.MsgCollection.Aggregate(ctx, pipe)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, nil, err
|
|
|
|
|
}
|
|
|
|
|
type docModel struct {
|
|
|
|
|
DocID string `bson:"doc_id"`
|
|
|
|
|
Msg *table.MsgInfoModel `bson:"msgs"`
|
|
|
|
|
}
|
|
|
|
|
var msgsDocs []docModel
|
|
|
|
|
err = cursor.All(ctx, &msgsDocs)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, nil, errs.WrapMsg(err, "cursor.All msgsDocs")
|
|
|
|
|
}
|
|
|
|
|
log.ZDebug(ctx, "query mongoDB", "result", msgsDocs)
|
|
|
|
|
msgs := make([]*table.MsgInfoModel, 0)
|
|
|
|
|
for _, doc := range msgsDocs {
|
|
|
|
|
msgInfo := doc.Msg
|
|
|
|
|
if msgInfo == nil || msgInfo.Msg == nil {
|
|
|
|
|
func (m *MsgMgo) ConvertMsgsDocLen(ctx context.Context, conversationIDs []string) {
|
|
|
|
|
for _, conversationID := range conversationIDs {
|
|
|
|
|
regex := primitive.Regex{Pattern: fmt.Sprintf("^%s:", conversationID)}
|
|
|
|
|
msgDocs, err := mgoutil.Find[*relation.MsgDocModel](ctx, m.coll, bson.M{"doc_id": regex})
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.ZError(ctx, "convertAll find msg doc failed", err, "conversationID", conversationID)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if msgInfo.Revoke != nil {
|
|
|
|
|
revokeContent := sdkws.MessageRevokedContent{
|
|
|
|
|
RevokerID: msgInfo.Revoke.UserID,
|
|
|
|
|
RevokerRole: msgInfo.Revoke.Role,
|
|
|
|
|
ClientMsgID: msgInfo.Msg.ClientMsgID,
|
|
|
|
|
RevokerNickname: msgInfo.Revoke.Nickname,
|
|
|
|
|
RevokeTime: msgInfo.Revoke.Time,
|
|
|
|
|
SourceMessageSendTime: msgInfo.Msg.SendTime,
|
|
|
|
|
SourceMessageSendID: msgInfo.Msg.SendID,
|
|
|
|
|
SourceMessageSenderNickname: msgInfo.Msg.SenderNickname,
|
|
|
|
|
SessionType: msgInfo.Msg.SessionType,
|
|
|
|
|
Seq: msgInfo.Msg.Seq,
|
|
|
|
|
Ex: msgInfo.Msg.Ex,
|
|
|
|
|
if len(msgDocs) < 1 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
log.ZInfo(ctx, "msg doc convert", "conversationID", conversationID, "len(msgDocs)", len(msgDocs))
|
|
|
|
|
if len(msgDocs[0].Msg) == int(m.model.GetSingleGocMsgNum5000()) {
|
|
|
|
|
if err := mgoutil.DeleteMany(ctx, m.coll, bson.M{"doc_id": regex}); err != nil {
|
|
|
|
|
log.ZError(ctx, "convertAll delete many failed", err, "conversationID", conversationID)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
data, err := json.Marshal(&revokeContent)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, nil, errs.WrapMsg(err, "json.Marshal revokeContent")
|
|
|
|
|
var newMsgDocs []any
|
|
|
|
|
for _, msgDoc := range msgDocs {
|
|
|
|
|
if int64(len(msgDoc.Msg)) == m.model.GetSingleGocMsgNum() {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
var index int64
|
|
|
|
|
for index < int64(len(msgDoc.Msg)) {
|
|
|
|
|
msg := msgDoc.Msg[index]
|
|
|
|
|
if msg != nil && msg.Msg != nil {
|
|
|
|
|
msgDocModel := relation.MsgDocModel{DocID: m.model.GetDocID(conversationID, msg.Msg.Seq)}
|
|
|
|
|
end := index + m.model.GetSingleGocMsgNum()
|
|
|
|
|
if int(end) >= len(msgDoc.Msg) {
|
|
|
|
|
msgDocModel.Msg = msgDoc.Msg[index:]
|
|
|
|
|
} else {
|
|
|
|
|
msgDocModel.Msg = msgDoc.Msg[index:end]
|
|
|
|
|
}
|
|
|
|
|
newMsgDocs = append(newMsgDocs, msgDocModel)
|
|
|
|
|
index = end
|
|
|
|
|
} else {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
elem := sdkws.NotificationElem{Detail: string(data)}
|
|
|
|
|
content, err := json.Marshal(&elem)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, nil, errs.WrapMsg(err, "json.Marshal elem")
|
|
|
|
|
if err = mgoutil.InsertMany(ctx, m.coll, newMsgDocs); err != nil {
|
|
|
|
|
log.ZError(ctx, "convertAll insert many failed", err, "conversationID", conversationID, "len(newMsgDocs)", len(newMsgDocs))
|
|
|
|
|
} else {
|
|
|
|
|
log.ZInfo(ctx, "msg doc convert", "conversationID", conversationID, "len(newMsgDocs)", len(newMsgDocs))
|
|
|
|
|
}
|
|
|
|
|
msgInfo.Msg.ContentType = constant.MsgRevokeNotification
|
|
|
|
|
msgInfo.Msg.Content = string(content)
|
|
|
|
|
}
|
|
|
|
|
msgs = append(msgs, msgInfo)
|
|
|
|
|
}
|
|
|
|
|
start := (req.Pagination.PageNumber - 1) * req.Pagination.ShowNumber
|
|
|
|
|
n := int32(len(msgs))
|
|
|
|
|
if start >= n {
|
|
|
|
|
return n, []*table.MsgInfoModel{}, nil
|
|
|
|
|
}
|
|
|
|
|
if start+req.Pagination.ShowNumber < n {
|
|
|
|
|
msgs = msgs[start : start+req.Pagination.ShowNumber]
|
|
|
|
|
} else {
|
|
|
|
|
msgs = msgs[start:]
|
|
|
|
|
}
|
|
|
|
|
return n, msgs, nil
|
|
|
|
|
}
|