mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-04-28 14:29:19 +08:00
feat: support GetLastMessage (#3029)
* pb * fix: Modifying other fields while setting IsPrivateChat does not take effect * fix: quote message error revoke * refactoring scheduled tasks * refactoring scheduled tasks * refactoring scheduled tasks * refactoring scheduled tasks * refactoring scheduled tasks * refactoring scheduled tasks * upgrading pkg tools * fix * fix * optimize log output * feat: support GetLastMessage * feat: support GetLastMessage * feat: s3 switch * feat: s3 switch
This commit is contained in:
@@ -97,6 +97,8 @@ type CommonMsgDatabase interface {
|
||||
DeleteDoc(ctx context.Context, docID string) error
|
||||
|
||||
GetLastMessageSeqByTime(ctx context.Context, conversationID string, time int64) (int64, error)
|
||||
|
||||
GetLastMessage(ctx context.Context, conversationIDS []string, userID string) (map[string]*sdkws.MsgData, error)
|
||||
}
|
||||
|
||||
func NewCommonMsgDatabase(msgDocModel database.Msg, msg cache.MsgCache, seqUser cache.SeqUser, seqConversation cache.SeqConversationCache, kafkaConf *config.Kafka) (CommonMsgDatabase, error) {
|
||||
@@ -811,8 +813,29 @@ func (db *commonMsgDatabase) GetMessageBySeqs(ctx context.Context, conversationI
|
||||
if v, ok := seqMsgs[seq]; ok {
|
||||
res = append(res, convert.MsgDB2Pb(v.Msg))
|
||||
} else {
|
||||
res = append(res, &sdkws.MsgData{Seq: seq})
|
||||
res = append(res, &sdkws.MsgData{Seq: seq, Status: constant.MsgStatusHasDeleted})
|
||||
}
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (db *commonMsgDatabase) GetLastMessage(ctx context.Context, conversationIDs []string, userID string) (map[string]*sdkws.MsgData, error) {
|
||||
res := make(map[string]*sdkws.MsgData)
|
||||
for _, conversationID := range conversationIDs {
|
||||
if _, ok := res[conversationID]; ok {
|
||||
continue
|
||||
}
|
||||
msg, err := db.msgDocDatabase.GetLastMessage(ctx, conversationID)
|
||||
if err != nil {
|
||||
if errs.Unwrap(err) == mongo.ErrNoDocuments {
|
||||
continue
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
tmp := []*model.MsgInfoModel{msg}
|
||||
db.handlerDeleteAndRevoked(ctx, userID, tmp)
|
||||
db.handlerQuote(ctx, userID, conversationID, tmp)
|
||||
res[conversationID] = convert.MsgDB2Pb(msg.Msg)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
@@ -997,6 +997,68 @@ func (m *MsgMgo) GetLastMessageSeqByTime(ctx context.Context, conversationID str
|
||||
return seq, nil
|
||||
}
|
||||
|
||||
func (m *MsgMgo) GetLastMessage(ctx context.Context, conversationID string) (*model.MsgInfoModel, error) {
|
||||
pipeline := []bson.M{
|
||||
{
|
||||
"$match": bson.M{
|
||||
"doc_id": bson.M{
|
||||
"$regex": fmt.Sprintf("^%s", conversationID),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"$match": bson.M{
|
||||
"msgs.msg.status": bson.M{
|
||||
"$lt": constant.MsgStatusHasDeleted,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"$sort": bson.M{
|
||||
"_id": -1,
|
||||
},
|
||||
},
|
||||
{
|
||||
"$limit": 1,
|
||||
},
|
||||
{
|
||||
"$project": bson.M{
|
||||
"_id": 0,
|
||||
"doc_id": 0,
|
||||
},
|
||||
},
|
||||
{
|
||||
"$unwind": "$msgs",
|
||||
},
|
||||
{
|
||||
"$match": bson.M{
|
||||
"msgs.msg.status": bson.M{
|
||||
"$lt": constant.MsgStatusHasDeleted,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"$sort": bson.M{
|
||||
"msgs.msg.seq": -1,
|
||||
},
|
||||
},
|
||||
{
|
||||
"$limit": 1,
|
||||
},
|
||||
}
|
||||
type Result struct {
|
||||
Msgs *model.MsgInfoModel `bson:"msgs"`
|
||||
}
|
||||
res, err := mongoutil.Aggregate[*Result](ctx, m.coll, pipeline)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(res) == 0 {
|
||||
return nil, errs.Wrap(mongo.ErrNoDocuments)
|
||||
}
|
||||
return res[0].Msgs, nil
|
||||
}
|
||||
|
||||
func (m *MsgMgo) onlyFindDocIndex(ctx context.Context, docID string, indexes []int64) ([]*model.MsgInfoModel, error) {
|
||||
if len(indexes) == 0 {
|
||||
return nil, nil
|
||||
|
||||
@@ -112,3 +112,15 @@ func (o *S3Mongo) FindExpirationObject(ctx context.Context, engine string, expir
|
||||
func (o *S3Mongo) GetKeyCount(ctx context.Context, engine string, key string) (int64, error) {
|
||||
return mongoutil.Count(ctx, o.coll, bson.M{"engine": engine, "key": key})
|
||||
}
|
||||
|
||||
func (o *S3Mongo) GetEngineCount(ctx context.Context, engine string) (int64, error) {
|
||||
return mongoutil.Count(ctx, o.coll, bson.M{"engine": engine})
|
||||
}
|
||||
|
||||
func (o *S3Mongo) GetEngineInfo(ctx context.Context, engine string, limit int, skip int) ([]*model.Object, error) {
|
||||
return mongoutil.Find[*model.Object](ctx, o.coll, bson.M{"engine": engine}, options.Find().SetLimit(int64(limit)).SetSkip(int64(skip)))
|
||||
}
|
||||
|
||||
func (o *S3Mongo) UpdateEngine(ctx context.Context, oldEngine, oldName string, newEngine string) error {
|
||||
return mongoutil.UpdateOne(ctx, o.coll, bson.M{"engine": oldEngine, "name": oldName}, bson.M{"$set": bson.M{"engine": newEngine}}, false)
|
||||
}
|
||||
|
||||
@@ -39,5 +39,6 @@ type Msg interface {
|
||||
DeleteDoc(ctx context.Context, docID string) error
|
||||
GetRandBeforeMsg(ctx context.Context, ts int64, limit int) ([]*model.MsgDocModel, error)
|
||||
GetLastMessageSeqByTime(ctx context.Context, conversationID string, time int64) (int64, error)
|
||||
GetLastMessage(ctx context.Context, conversationID string) (*model.MsgInfoModel, error)
|
||||
FindSeqs(ctx context.Context, conversationID string, seqs []int64) ([]*model.MsgInfoModel, error)
|
||||
}
|
||||
|
||||
@@ -27,4 +27,8 @@ type ObjectInfo interface {
|
||||
Delete(ctx context.Context, engine string, name []string) error
|
||||
FindExpirationObject(ctx context.Context, engine string, expiration time.Time, needDelType []string, count int64) ([]*model.Object, error)
|
||||
GetKeyCount(ctx context.Context, engine string, key string) (int64, error)
|
||||
|
||||
GetEngineCount(ctx context.Context, engine string) (int64, error)
|
||||
GetEngineInfo(ctx context.Context, engine string, limit int, skip int) ([]*model.Object, error)
|
||||
UpdateEngine(ctx context.Context, oldEngine, oldName string, newEngine string) error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user