@@ -87,11 +87,13 @@ module.exports = {
87
87
*/
88
88
saveConvo : async ( req , { conversationId, newConversationId, ...convo } , metadata ) => {
89
89
try {
90
- if ( metadata && metadata ?. context ) {
90
+ if ( metadata ?. context ) {
91
91
logger . debug ( `[saveConvo] ${ metadata . context } ` ) ;
92
92
}
93
+
93
94
const messages = await getMessages ( { conversationId } , '_id' ) ;
94
95
const update = { ...convo , messages, user : req . user . id } ;
96
+
95
97
if ( newConversationId ) {
96
98
update . conversationId = newConversationId ;
97
99
}
@@ -143,26 +145,44 @@ module.exports = {
143
145
} ,
144
146
getConvosByCursor : async (
145
147
user ,
146
- { cursor, limit = 25 , isArchived = false , tags, order = 'desc' } = { } ,
148
+ { cursor, limit = 25 , isArchived = false , tags, search , order = 'desc' } = { } ,
147
149
) => {
148
- const query = { user } ;
150
+ const filters = [ { user } ] ;
149
151
150
152
if ( isArchived ) {
151
- query . isArchived = true ;
153
+ filters . push ( { isArchived : true } ) ;
152
154
} else {
153
- query . $or = [ { isArchived : false } , { isArchived : { $exists : false } } ] ;
155
+ filters . push ( { $or : [ { isArchived : false } , { isArchived : { $exists : false } } ] } ) ;
154
156
}
155
157
156
158
if ( Array . isArray ( tags ) && tags . length > 0 ) {
157
- query . tags = { $in : tags } ;
159
+ filters . push ( { tags : { $in : tags } } ) ;
158
160
}
159
161
160
- query . $and = [ { $or : [ { expiredAt : null } , { expiredAt : { $exists : false } } ] } ] ;
162
+ filters . push ( { $or : [ { expiredAt : null } , { expiredAt : { $exists : false } } ] } ) ;
163
+
164
+ if ( search ) {
165
+ try {
166
+ const meiliResults = await Conversation . meiliSearch ( search ) ;
167
+ const matchingIds = Array . isArray ( meiliResults . hits )
168
+ ? meiliResults . hits . map ( ( result ) => result . conversationId )
169
+ : [ ] ;
170
+ if ( ! matchingIds . length ) {
171
+ return { conversations : [ ] , nextCursor : null } ;
172
+ }
173
+ filters . push ( { conversationId : { $in : matchingIds } } ) ;
174
+ } catch ( error ) {
175
+ logger . error ( '[getConvosByCursor] Error during meiliSearch' , error ) ;
176
+ return { message : 'Error during meiliSearch' } ;
177
+ }
178
+ }
161
179
162
180
if ( cursor ) {
163
- query . updatedAt = { $lt : new Date ( cursor ) } ;
181
+ filters . push ( { updatedAt : { $lt : new Date ( cursor ) } } ) ;
164
182
}
165
183
184
+ const query = filters . length === 1 ? filters [ 0 ] : { $and : filters } ;
185
+
166
186
try {
167
187
const convos = await Conversation . find ( query )
168
188
. select ( 'conversationId endpoint title createdAt updatedAt user' )
@@ -184,45 +204,34 @@ module.exports = {
184
204
} ,
185
205
getConvosQueried : async ( user , convoIds , cursor = null , limit = 25 ) => {
186
206
try {
187
- if ( ! convoIds || convoIds . length === 0 ) {
207
+ if ( ! convoIds ? .length ) {
188
208
return { conversations : [ ] , nextCursor : null , convoMap : { } } ;
189
209
}
190
210
191
- const convoMap = { } ;
192
- const promises = [ ] ;
193
-
194
- convoIds . forEach ( ( convo ) =>
195
- promises . push (
196
- Conversation . findOne ( {
197
- user,
198
- conversationId : convo . conversationId ,
199
- $or : [ { expiredAt : { $exists : false } } , { expiredAt : null } ] ,
200
- } ) . lean ( ) ,
201
- ) ,
202
- ) ;
211
+ const conversationIds = convoIds . map ( ( convo ) => convo . conversationId ) ;
203
212
204
- // Fetch all matching conversations and filter out any falsy results
205
- const results = ( await Promise . all ( promises ) ) . filter ( Boolean ) ;
213
+ const results = await Conversation . find ( {
214
+ user,
215
+ conversationId : { $in : conversationIds } ,
216
+ $or : [ { expiredAt : { $exists : false } } , { expiredAt : null } ] ,
217
+ } ) . lean ( ) ;
206
218
207
- // Sort conversations by updatedAt descending (most recent first)
208
219
results . sort ( ( a , b ) => new Date ( b . updatedAt ) - new Date ( a . updatedAt ) ) ;
209
220
210
- // If a cursor is provided and not "start", filter out recrods newer or equal to the cursor date
211
221
let filtered = results ;
212
222
if ( cursor && cursor !== 'start' ) {
213
223
const cursorDate = new Date ( cursor ) ;
214
224
filtered = results . filter ( ( convo ) => new Date ( convo . updatedAt ) < cursorDate ) ;
215
225
}
216
226
217
- // Retrieve limit + 1 results to determine if there's a next page.
218
227
const limited = filtered . slice ( 0 , limit + 1 ) ;
219
228
let nextCursor = null ;
220
229
if ( limited . length > limit ) {
221
230
const lastConvo = limited . pop ( ) ;
222
231
nextCursor = lastConvo . updatedAt . toISOString ( ) ;
223
232
}
224
233
225
- // Build convoMap for ease of access if required by caller
234
+ const convoMap = { } ;
226
235
limited . forEach ( ( convo ) => {
227
236
convoMap [ convo . conversationId ] = convo ;
228
237
} ) ;
@@ -268,10 +277,22 @@ module.exports = {
268
277
* logger.error(result); // { n: 5, ok: 1, deletedCount: 5, messages: { n: 10, ok: 1, deletedCount: 10 } }
269
278
*/
270
279
deleteConvos : async ( user , filter ) => {
271
- let toRemove = await Conversation . find ( { ...filter , user } ) . select ( 'conversationId' ) ;
272
- const ids = toRemove . map ( ( instance ) => instance . conversationId ) ;
273
- let deleteCount = await Conversation . deleteMany ( { ...filter , user } ) ;
274
- deleteCount . messages = await deleteMessages ( { conversationId : { $in : ids } } ) ;
275
- return deleteCount ;
280
+ try {
281
+ const userFilter = { ...filter , user } ;
282
+
283
+ const conversations = await Conversation . find ( userFilter ) . select ( 'conversationId' ) ;
284
+ const conversationIds = conversations . map ( ( c ) => c . conversationId ) ;
285
+
286
+ const deleteConvoResult = await Conversation . deleteMany ( userFilter ) ;
287
+
288
+ const deleteMessagesResult = await deleteMessages ( {
289
+ conversationId : { $in : conversationIds } ,
290
+ } ) ;
291
+
292
+ return { ...deleteConvoResult , messages : deleteMessagesResult } ;
293
+ } catch ( error ) {
294
+ logger . error ( '[deleteConvos] Error deleting conversations and messages' , error ) ;
295
+ throw error ;
296
+ }
276
297
} ,
277
298
} ;
0 commit comments