@@ -64,7 +64,9 @@ func (c *Conversation) getAdvancedHistoryMessageList(ctx context.Context, req sd
64
64
}
65
65
startTime = m .SendTime
66
66
} else {
67
- c .messagePullMinSeqMap .Delete (conversationID )
67
+ // Clear both maps when the user enters the conversation
68
+ c .messagePullForwardEndSeqMap .Delete (conversationID )
69
+ c .messagePullReverseEndSeqMap .Delete (conversationID )
68
70
}
69
71
log .ZDebug (ctx , "Assembly conversation parameters" , "cost time" , time .Since (t ), "conversationID" ,
70
72
conversationID , "startTime:" , startTime , "count:" , req .Count , "startTime" , startTime )
@@ -75,41 +77,48 @@ func (c *Conversation) getAdvancedHistoryMessageList(ctx context.Context, req sd
75
77
log .ZDebug (ctx , "pull message" , "pull cost time" , time .Since (t ))
76
78
t = time .Now ()
77
79
78
- var thisMinSeq int64
79
- thisMinSeq , messageList = c .LocalChatLog2MsgStruct (ctx , list )
80
+ var thisEndSeq int64
81
+ thisEndSeq , messageList = c .LocalChatLog2MsgStruct (list , isReverse )
80
82
log .ZDebug (ctx , "message convert and unmarshal" , "unmarshal cost time" , time .Since (t ))
81
83
t = time .Now ()
82
84
if ! isReverse {
83
85
sort .Sort (messageList )
86
+ if thisEndSeq != 0 {
87
+ c .messagePullForwardEndSeqMap .Store (conversationID , thisEndSeq )
88
+ }
89
+ } else {
90
+ if thisEndSeq != 0 {
91
+ c .messagePullReverseEndSeqMap .Store (conversationID , thisEndSeq )
92
+ }
84
93
}
85
94
log .ZDebug (ctx , "sort" , "sort cost time" , time .Since (t ))
86
95
messageListCallback .MessageList = messageList
87
- if thisMinSeq != 0 {
88
- c .messagePullMinSeqMap .Store (conversationID , thisMinSeq )
89
- }
90
- return & messageListCallback , nil
91
96
97
+ return & messageListCallback , nil
92
98
}
93
99
94
100
func (c * Conversation ) fetchMessagesWithGapCheck (ctx context.Context , conversationID string ,
95
101
count int , startTime int64 , isReverse bool , messageListCallback * sdk.GetAdvancedHistoryMessageListCallback ) ([]* model_struct.LocalChatLog , error ) {
96
102
97
- var list []* model_struct.LocalChatLog
103
+ var list , validMessages []* model_struct.LocalChatLog
98
104
99
- // If all retrieved messages are either deleted or filtered out, continue fetching messages from an earlier point .
100
- shouldFetchMoreMessages := func (messages []* model_struct.LocalChatLog ) bool {
105
+ // Get the number of invalid messages in this batch to recursive fetching from earlier points .
106
+ shouldFetchMoreMessagesNum := func (messages []* model_struct.LocalChatLog ) int {
101
107
if len (messages ) == 0 {
102
- return false
108
+ return count
103
109
}
104
110
105
- allDeleted := true
111
+ // Represents the number of valid messages in the batch
112
+ validateMessageNum := 0
106
113
for _ , msg := range messages {
107
114
if msg .Status < constant .MsgStatusHasDeleted {
108
- allDeleted = false
109
- break
115
+ validateMessageNum ++
116
+ validMessages = append (validMessages , msg )
117
+ } else {
118
+ log .ZDebug (ctx , "this message has been deleted or exception message" , "msg" , msg )
110
119
}
111
120
}
112
- return allDeleted
121
+ return count - validateMessageNum
113
122
}
114
123
getNewStartTime := func (messages []* model_struct.LocalChatLog ) int64 {
115
124
if len (messages ) == 0 {
@@ -128,39 +137,49 @@ func (c *Conversation) fetchMessagesWithGapCheck(ctx context.Context, conversati
128
137
return nil , err
129
138
}
130
139
t = time .Now ()
131
- maxSeq := c .validateAndFillInternalGaps (ctx , conversationID , isReverse ,
140
+ thisStartSeq := c .validateAndFillInternalGaps (ctx , conversationID , isReverse ,
132
141
count , startTime , & list , messageListCallback )
133
142
log .ZDebug (ctx , "internal continuity check" , "cost time" , time .Since (t ))
134
143
t = time .Now ()
135
- c .validateAndFillInterBlockGaps (ctx , maxSeq , conversationID ,
144
+ c .validateAndFillInterBlockGaps (ctx , thisStartSeq , conversationID ,
136
145
isReverse , count , startTime , & list , messageListCallback )
137
146
log .ZDebug (ctx , "between continuity check" , "cost time" , time .Since (t ))
138
147
t = time .Now ()
139
148
c .validateAndFillEndBlockContinuity (ctx , conversationID , isReverse ,
140
149
count , startTime , & list , messageListCallback )
141
150
log .ZDebug (ctx , "end continuity check" , "cost time" , time .Since (t ))
142
- // If all retrieved messages are either deleted or filtered out,
143
- //continue fetching recursively until either valid messages are found or all messages have been fetched.
144
- if shouldFetchMoreMessages (list ) && ! messageListCallback .IsEnd {
145
- return c .fetchMessagesWithGapCheck (ctx , conversationID , count , getNewStartTime (list ), isReverse , messageListCallback )
151
+ // If the number of valid messages retrieved is less than the count,
152
+ // continue fetching recursively until the valid messages are sufficient or all messages have been fetched.
153
+ missingCount := shouldFetchMoreMessagesNum (list )
154
+ if missingCount > 0 && ! messageListCallback .IsEnd {
155
+ log .ZDebug (ctx , "fetch more messages" , "missingCount" , missingCount , "conversationID" , conversationID )
156
+ missingMessages , err := c .fetchMessagesWithGapCheck (ctx , conversationID , missingCount , getNewStartTime (list ), isReverse , messageListCallback )
157
+ if err != nil {
158
+ return nil , err
159
+ }
160
+ log .ZDebug (ctx , "fetch more messages" , "missingMessages" , missingMessages )
161
+ return append (validMessages , missingMessages ... ), nil
146
162
}
147
163
148
- return list , nil
164
+ return validMessages , nil
149
165
}
150
166
151
- func (c * Conversation ) LocalChatLog2MsgStruct (ctx context. Context , list []* model_struct.LocalChatLog ) (int64 , []* sdk_struct.MsgStruct ) {
167
+ func (c * Conversation ) LocalChatLog2MsgStruct (list []* model_struct.LocalChatLog , isReverse bool ) (int64 , []* sdk_struct.MsgStruct ) {
152
168
messageList := make ([]* sdk_struct.MsgStruct , 0 , len (list ))
153
- var thisMinSeq int64
169
+ var thisEndSeq int64
154
170
for _ , v := range list {
155
- if v .Seq != 0 && thisMinSeq == 0 {
156
- thisMinSeq = v .Seq
171
+ if v .Seq != 0 && thisEndSeq == 0 {
172
+ thisEndSeq = v .Seq
157
173
}
158
- if v .Seq < thisMinSeq && v .Seq != 0 {
159
- thisMinSeq = v .Seq
160
- }
161
- if v .Status >= constant .MsgStatusHasDeleted {
162
- log .ZDebug (ctx , "this message has been deleted or exception message" , "msg" , v )
163
- continue
174
+ if isReverse {
175
+ if v .Seq > thisEndSeq && thisEndSeq != 0 {
176
+ thisEndSeq = v .Seq
177
+ }
178
+
179
+ } else {
180
+ if v .Seq < thisEndSeq && v .Seq != 0 {
181
+ thisEndSeq = v .Seq
182
+ }
164
183
}
165
184
temp := LocalChatLogToMsgStruct (v )
166
185
@@ -169,7 +188,7 @@ func (c *Conversation) LocalChatLog2MsgStruct(ctx context.Context, list []*model
169
188
}
170
189
messageList = append (messageList , temp )
171
190
}
172
- return thisMinSeq , messageList
191
+ return thisEndSeq , messageList
173
192
}
174
193
175
194
func (c * Conversation ) typingStatusUpdate (ctx context.Context , recvID , msgTip string ) error {
0 commit comments