@@ -86,12 +86,12 @@ func (c *Conversation) checkEndBlock(ctx context.Context, conversationID string,
86
86
// Perform an end-of-block check if the retrieved message count is less than requested
87
87
if len (* list ) < count {
88
88
if isReverse {
89
- currentMaxSeq := c .maxSeqRecorder . Get ( conversationID )
89
+ currentMaxSeq := c .getConversationMaxSeq ( ctx , conversationID )
90
90
maxSeq , _ , _ := c .getMaxAndMinHaveSeqList (* list )
91
91
log .ZDebug (ctx , "validateAndFillEndBlockContinuity" , "maxSeq" , maxSeq , "conversationID" , conversationID , "currentMaxSeq" , currentMaxSeq )
92
92
// Use >= to prevent the currentMaxSeq from being updated too slowly,
93
93
// which could lead to misjudgments and cause repeated message fetching."
94
- if maxSeq >= currentMaxSeq { // todo Replace `1` with the minimum sequence value as defined by the user or system
94
+ if maxSeq >= currentMaxSeq {
95
95
messageListCallback .IsEnd = true
96
96
} else {
97
97
lastEndSeq , _ := c .messagePullReverseEndSeqMap .Load (conversationID )
@@ -104,7 +104,7 @@ func (c *Conversation) checkEndBlock(ctx context.Context, conversationID string,
104
104
// The batch includes sequences but has not reached the maximum value,
105
105
// This condition indicates local-only messages, with `maxSeq < maxSeqRecorderMaxSeq` as the only case,
106
106
// since `lastEndSeq < maxSeqRecorderMaxSeq` is handled in inter-block continuity.
107
- lostSeqList := getLostSeqListWithLimitLength (maxSeq + 1 , c . maxSeqRecorder . Get ( conversationID ) , []int64 {})
107
+ lostSeqList := getLostSeqListWithLimitLength (maxSeq + 1 , currentMaxSeq , []int64 {})
108
108
if len (lostSeqList ) > 0 {
109
109
isShouldFetchMessage = true
110
110
seqList = lostSeqList
@@ -115,22 +115,24 @@ func (c *Conversation) checkEndBlock(ctx context.Context, conversationID string,
115
115
}
116
116
return isShouldFetchMessage , seqList
117
117
} else {
118
+ userCanPullMinSeq := c .getConversationMinSeq (ctx , conversationID )
118
119
_ , minSeq , _ := c .getMaxAndMinHaveSeqList (* list )
119
- log .ZDebug (ctx , "validateAndFillEndBlockContinuity" , "minSeq" , minSeq , "conversationID" , conversationID )
120
- if minSeq == 1 { // todo Replace `1` with the minimum sequence value as defined by the user or system
120
+ log .ZDebug (ctx , "validateAndFillEndBlockContinuity" , "minSeq" , minSeq ,
121
+ "conversationID" , conversationID , "userCanPullMinSeq" , userCanPullMinSeq )
122
+ if minSeq == userCanPullMinSeq {
121
123
messageListCallback .IsEnd = true
122
124
} else {
123
125
lastMinSeq , _ := c .messagePullForwardEndSeqMap .Load (conversationID )
124
126
log .ZDebug (ctx , "validateAndFillEndBlockContinuity" , "lastMinSeq" , lastMinSeq , "conversationID" , conversationID )
125
127
// If `minSeq` is zero and `lastMinSeq` is at the minimum server sequence, this batch is fully local
126
- if minSeq == 0 && lastMinSeq == 1 { // All messages in this batch are local messages,
128
+ if minSeq == 0 && lastMinSeq == userCanPullMinSeq { // All messages in this batch are local messages,
127
129
// and the minimum seq of the last batch of valid messages has already reached the minimum pullable seq from the server.
128
130
messageListCallback .IsEnd = true
129
131
} else {
130
132
// The batch includes sequences but has not reached the minimum value,
131
133
// This condition indicates local-only messages, with `minSeq > 1` as the only case,
132
134
// since `lastMinSeq > 1` is handled in inter-block continuity.
133
- lostSeqList := getLostSeqListWithLimitLength (1 , minSeq - 1 , []int64 {})
135
+ lostSeqList := getLostSeqListWithLimitLength (userCanPullMinSeq , minSeq - 1 , []int64 {})
134
136
if len (lostSeqList ) > 0 {
135
137
isShouldFetchMessage = true
136
138
seqList = lostSeqList
@@ -200,6 +202,11 @@ func (c *Conversation) fetchAndMergeMissingMessages(ctx context.Context, convers
200
202
conversationSeqs .ConversationID = conversationID
201
203
conversationSeqs .Seqs = seqList
202
204
getSeqMessageReq .Conversations = append (getSeqMessageReq .Conversations , & conversationSeqs )
205
+ if isReverse {
206
+ getSeqMessageReq .Order = sdkws .PullOrder_PullOrderAsc
207
+ } else {
208
+ getSeqMessageReq .Order = sdkws .PullOrder_PullOrderDesc
209
+ }
203
210
log .ZDebug (ctx , "conversation pull message, " , "req" , getSeqMessageReq )
204
211
if startTime == 0 && ! c .LongConnMgr .IsConnected () {
205
212
return
@@ -219,6 +226,9 @@ func (c *Conversation) fetchAndMergeMissingMessages(ctx context.Context, convers
219
226
c .pullMessageIntoTable (ctx , getSeqMessageResp .Msgs )
220
227
log .ZDebug (ctx , "syncMsgFromServerSplit pull msg success" ,
221
228
"conversationID" , conversationID , "count" , count , "len" , len (* list ), "msgLen" , len (v .Msgs ))
229
+ if v .IsEnd {
230
+ c .setConversationMinSeq (ctx , isReverse , conversationID , v .EndSeq )
231
+ }
222
232
localMessage := datautil .Batch (MsgDataToLocalChatLog , v .Msgs )
223
233
if ! isReverse {
224
234
reverse (localMessage )
@@ -228,6 +238,56 @@ func (c *Conversation) fetchAndMergeMissingMessages(ctx context.Context, convers
228
238
229
239
}
230
240
}
241
+
242
+ func (c * Conversation ) getConversationMaxSeq (ctx context.Context , conversationID string ) int64 {
243
+ conversation , err := c .db .GetConversation (ctx , conversationID )
244
+ if err != nil {
245
+ log .ZWarn (ctx , "Failed to get conversation" , err )
246
+ return c .maxSeqRecorder .Get (conversationID )
247
+ }
248
+ if conversation .MaxSeq == 0 {
249
+ return c .maxSeqRecorder .Get (conversationID )
250
+
251
+ }
252
+ return conversation .MaxSeq
253
+ }
254
+ func (c * Conversation ) getConversationMinSeq (ctx context.Context , conversationID string ) int64 {
255
+ conversation , err := c .db .GetConversation (ctx , conversationID )
256
+ if err != nil {
257
+ log .ZWarn (ctx , "Failed to get conversation" , err )
258
+ return 1
259
+ }
260
+ if conversation .MinSeq == 0 {
261
+ return 1
262
+
263
+ }
264
+ return conversation .MinSeq
265
+ }
266
+ func (c * Conversation ) setConversationMinSeq (ctx context.Context , isReverse bool , conversationID string , endSeq int64 ) {
267
+ conversation , err := c .db .GetConversation (ctx , conversationID )
268
+ if err != nil {
269
+ log .ZWarn (ctx , "Failed to get conversation" , err )
270
+ return
271
+ }
272
+ if ! isReverse {
273
+ if conversation .MinSeq == 0 || endSeq > conversation .MinSeq {
274
+ conversation .MinSeq = endSeq
275
+ }
276
+ } else {
277
+ if conversation .MaxSeq == 0 || endSeq < conversation .MaxSeq {
278
+ conversation .MaxSeq = endSeq
279
+ err = c .db .UpdateConversation (ctx , conversation )
280
+ if err != nil {
281
+ log .ZWarn (ctx , "Failed to update conversation" , err )
282
+ }
283
+ }
284
+
285
+ }
286
+ err = c .db .UpdateConversation (ctx , conversation )
287
+ if err != nil {
288
+ log .ZWarn (ctx , "Failed to update conversation" , err )
289
+ }
290
+ }
231
291
func errHandle (seqList []int64 , list * []* model_struct.LocalChatLog , err error , messageListCallback * sdk.GetAdvancedHistoryMessageListCallback ) {
232
292
messageListCallback .ErrCode = 100
233
293
messageListCallback .ErrMsg = err .Error ()
0 commit comments