From 123abe98037f17750fe17372f3d3ca91e894bf08 Mon Sep 17 00:00:00 2001 From: OpenIM-Gordon <1432970085@qq.com> Date: Thu, 14 Aug 2025 14:16:50 +0800 Subject: [PATCH] fix: fill in the most recent sendTime for a gap message to prevent the client from repeatedly retrieving the same message due to sendTime being 0. (#3522) --- pkg/common/storage/database/mgo/msg.go | 13 +++++++------ pkg/common/storage/model/msg.go | 4 ++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/pkg/common/storage/database/mgo/msg.go b/pkg/common/storage/database/mgo/msg.go index 975e7a0ef..315f4530b 100644 --- a/pkg/common/storage/database/mgo/msg.go +++ b/pkg/common/storage/database/mgo/msg.go @@ -5,6 +5,11 @@ import ( "fmt" "time" + "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" + "github.com/openimsdk/open-im-server/v3/pkg/common/storage/database" "github.com/openimsdk/open-im-server/v3/pkg/common/storage/model" "github.com/openimsdk/protocol/constant" @@ -14,10 +19,6 @@ import ( "github.com/openimsdk/tools/errs" "github.com/openimsdk/tools/utils/datautil" "github.com/openimsdk/tools/utils/jsonutil" - "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" ) func NewMsgMongo(db *mongo.Database) (database.Msg, error) { @@ -1154,7 +1155,7 @@ func (m *MsgMgo) findBeforeDocSendTime(ctx context.Context, docID string, limit if err != nil { return 0, 0, err } - for i := len(res) - 1; i > 0; i-- { + for i := len(res) - 1; i >= 0; i-- { v := res[i] if v.Msgs != nil && v.Msgs.Msg != nil && v.Msgs.Msg.SendTime > 0 { return v.Msgs.Msg.Seq, v.Msgs.Msg.SendTime, nil @@ -1169,7 +1170,7 @@ func (m *MsgMgo) findBeforeSendTime(ctx context.Context, conversationID string, limit := int64(-1) if first { first = false - limit = m.model.GetMsgIndex(seq) + limit = m.model.GetLimitForSingleDoc(seq) } docID := m.model.BuildDocIDByIndex(conversationID, i) msgSeq, msgSendTime, err := m.findBeforeDocSendTime(ctx, docID, limit) diff --git a/pkg/common/storage/model/msg.go b/pkg/common/storage/model/msg.go index 9d5b56b42..2bb4882f7 100644 --- a/pkg/common/storage/model/msg.go +++ b/pkg/common/storage/model/msg.go @@ -132,6 +132,10 @@ func (*MsgDocModel) GetMsgIndex(seq int64) int64 { return (seq - 1) % singleGocMsgNum } +func (*MsgDocModel) GetLimitForSingleDoc(seq int64) int64 { + return seq % singleGocMsgNum +} + func (*MsgDocModel) indexGen(conversationID string, seqSuffix int64) string { return conversationID + ":" + strconv.FormatInt(seqSuffix, 10) }