@@ -109,36 +109,19 @@ func (ms *MessageStore) GetMessages(jid types.JID) []Message {
109109 return ml
110110}
111111
112- // GetMessagesPaged returns a page of messages for a chat
113- // beforeTimestamp: only return messages before this timestamp (0 = latest)
114- // limit: max number of messages to return
115- // Returns messages in chronological order (oldest first within the page)
116- func (ms * MessageStore ) GetMessagesPaged (jid types.JID , beforeTimestamp int64 , limit int ) []Message {
117- var rows * sql.Rows
118- var err error
119-
120- if beforeTimestamp == 0 {
121- // Get latest messages using the optimized query
122- rows , err = ms .db .Query (query .SelectLatestMessagesByChat , jid .String (), limit )
123- } else {
124- // Get messages before timestamp using the optimized query
125- rows , err = ms .db .Query (query .SelectMessagesByChatBeforeTimestamp , jid .String (), beforeTimestamp , limit )
126- }
127-
128- if err != nil {
129- return []Message {}
130- }
131- defer rows .Close ()
112+ func getMessageArrayFromRows (rows * sql.Rows ) []Message {
113+ var (
114+ messages []Message
115+ chat string
116+ msgID string
117+ ts int64
118+ minf []byte
119+ raw []byte
120+ )
132121
133- var messages []Message
134122 for rows .Next () {
135- var (
136- chat string
137- msgID string
138- ts int64
139- minf []byte
140- raw []byte
141- )
123+ minf = minf [:0 ]
124+ raw = raw [:0 ]
142125
143126 if err := rows .Scan (& chat , & msgID , & ts , & minf , & raw ); err != nil {
144127 continue
@@ -150,7 +133,7 @@ func (ms *MessageStore) GetMessagesPaged(jid types.JID, beforeTimestamp int64, l
150133 }
151134
152135 var waMsg * waE2E.Message
153- waMsg , err = unmarshalMessageContent (raw )
136+ waMsg , err : = unmarshalMessageContent (raw )
154137 if err != nil {
155138 continue
156139 }
@@ -159,58 +142,34 @@ func (ms *MessageStore) GetMessagesPaged(jid types.JID, beforeTimestamp int64, l
159142 Info : messageInfo ,
160143 Content : waMsg ,
161144 })
162- ms .mCache .Set (msgID , 1 )
163145 }
164146
165147 return messages
166148}
167149
168- // loadAndCacheAllMessages loads all messages for a chat from DB and caches them
169- func (ms * MessageStore ) loadAndCacheAllMessages (jid types.JID ) []Message {
170- rows , err := ms .db .Query (query .SelectMessagesByChat , jid .String ())
171- if err != nil {
172- return []Message {}
173- }
174- defer rows .Close ()
175-
176- var messages []Message
177- for rows .Next () {
178- var (
179- chat string
180- msgID string
181- ts int64
182- minf []byte
183- raw []byte
184- )
185-
186- if err := rows .Scan (& chat , & msgID , & ts , & minf , & raw ); err != nil {
187- continue
188- }
189-
190- var messageInfo types.MessageInfo
191- if err := gobDecode (minf , & messageInfo ); err != nil {
192- continue
193- }
194-
195- var waMsg * waE2E.Message
196- waMsg , err = unmarshalMessageContent (raw )
197- if err != nil {
198- continue
199- }
150+ // GetMessagesPaged returns a page of messages for a chat
151+ // beforeTimestamp: only return messages before this timestamp (0 = latest)
152+ // limit: max number of messages to return
153+ // Returns messages in chronological order (oldest first within the page)
154+ func (ms * MessageStore ) GetMessagesPaged (jid types.JID , beforeTimestamp int64 , limit int ) []Message {
155+ var rows * sql.Rows
156+ var err error
200157
201- messages = append (messages , Message {
202- Info : messageInfo ,
203- Content : waMsg ,
204- })
205- ms .mCache .Set (msgID , 1 )
158+ if beforeTimestamp == 0 {
159+ // Get latest messages using the optimized query
160+ rows , err = ms .db .Query (query .SelectLatestMessagesByChat , jid .String (), limit )
161+ } else {
162+ // Get messages before timestamp using the optimized query
163+ rows , err = ms .db .Query (query .SelectMessagesByChatBeforeTimestamp , jid .String (), beforeTimestamp , limit )
206164 }
207165
208- // Cache the loaded messages
209- if len (messages ) > 0 {
210- ms .msgMap .Set (jid .User , messages )
166+ if err != nil {
167+ return []Message {}
211168 }
212169
213- return messages
170+ defer rows .Close ()
171+
172+ return getMessageArrayFromRows (rows )
214173}
215174
216175// loadMessagesFromDBForChat loads messages for a specific chat from DB
@@ -221,38 +180,7 @@ func (ms *MessageStore) loadMessagesFromDBForChat(jid types.JID) []Message {
221180 }
222181 defer rows .Close ()
223182
224- var messages []Message
225- for rows .Next () {
226- var (
227- chat string
228- msgID string
229- ts int64
230- minf []byte
231- raw []byte
232- )
233-
234- if err := rows .Scan (& chat , & msgID , & ts , & minf , & raw ); err != nil {
235- continue
236- }
237-
238- var messageInfo types.MessageInfo
239- if err := gobDecode (minf , & messageInfo ); err != nil {
240- continue
241- }
242-
243- var waMsg * waE2E.Message
244- waMsg , err = unmarshalMessageContent (raw )
245- if err != nil {
246- continue
247- }
248-
249- messages = append (messages , Message {
250- Info : messageInfo ,
251- Content : waMsg ,
252- })
253- ms .mCache .Set (msgID , 1 )
254- }
255- return messages
183+ return getMessageArrayFromRows (rows )
256184}
257185
258186// GetTotalMessageCount returns the total number of messages in a chat
@@ -294,14 +222,18 @@ func (ms *MessageStore) GetChatList() []ChatMessage {
294222 defer rows .Close ()
295223
296224 var chatList []ChatMessage
225+
226+ var (
227+ chat string
228+ msgID string
229+ ts int64
230+ minf []byte
231+ raw []byte
232+ )
233+
297234 for rows .Next () {
298- var (
299- chat string
300- msgID string
301- ts int64
302- minf []byte
303- raw []byte
304- )
235+ minf = minf [:0 ]
236+ raw = raw [:0 ]
305237
306238 if err := rows .Scan (& chat , & msgID , & ts , & minf , & raw ); err != nil {
307239 continue
0 commit comments