15
15
use std:: { fmt, fs, path:: Path , sync:: Arc } ;
16
16
17
17
use ruma:: {
18
- EventId , OwnedEventId , OwnedRoomId , RoomId , events:: AnySyncMessageLikeEvent ,
19
- room_version_rules:: RedactionRules ,
18
+ EventId , OwnedEventId , OwnedRoomId , RoomId , events:: room:: message:: OriginalSyncRoomMessageEvent ,
20
19
} ;
21
20
use tantivy:: {
22
21
Index , IndexReader , TantivyDocument ,
@@ -35,16 +34,17 @@ use crate::{
35
34
} ;
36
35
37
36
/// A struct to represent the operations on a [`RoomIndex`]
38
- pub ( crate ) enum RoomIndexOperation {
39
- /// Add this document to the index.
40
- Add ( TantivyDocument ) ,
37
+ #[ derive( Debug ) ]
38
+ pub enum RoomIndexOperation {
39
+ /// Add this event to the index.
40
+ Add ( OriginalSyncRoomMessageEvent ) ,
41
41
/// Remove all documents in the index where
42
- /// [ `MatrixSearchIndexSchema::deletion_key()`] matches this event id.
42
+ /// `MatrixSearchIndexSchema::deletion_key()` matches this event id.
43
43
Remove ( OwnedEventId ) ,
44
44
/// Replace all documents in the index where
45
- /// [ `MatrixSearchIndexSchema::deletion_key()`] matches this event id with
46
- /// the new document .
47
- Edit ( OwnedEventId , TantivyDocument ) ,
45
+ /// `MatrixSearchIndexSchema::deletion_key()` matches this event id with
46
+ /// the new event .
47
+ Edit ( OwnedEventId , OriginalSyncRoomMessageEvent ) ,
48
48
/// Do nothing.
49
49
Noop ,
50
50
}
@@ -137,37 +137,6 @@ impl RoomIndex {
137
137
RoomIndex :: new_with ( index, schema, room_id)
138
138
}
139
139
140
- /// Handle [`AnySyncMessageLikeEvent`]
141
- ///
142
- /// This which will add/remove/edit an event in the index based on the
143
- /// event type.
144
- pub fn handle_event (
145
- & mut self ,
146
- event : AnySyncMessageLikeEvent ,
147
- redaction_rules : & RedactionRules ,
148
- ) -> Result < ( ) , IndexError > {
149
- let event_id = event. event_id ( ) . to_owned ( ) ;
150
-
151
- match self . schema . handle_event ( event, redaction_rules) ? {
152
- RoomIndexOperation :: Add ( document) => {
153
- if !self . contains ( & event_id) {
154
- self . writer . add ( document) ?;
155
- }
156
- }
157
- RoomIndexOperation :: Remove ( event_id) => {
158
- self . writer . remove ( & event_id) ;
159
- }
160
- RoomIndexOperation :: Edit ( remove_event_id, document) => {
161
- self . writer . remove ( & remove_event_id) ;
162
- if !self . contains ( & event_id) {
163
- self . writer . add ( document) ?;
164
- }
165
- }
166
- RoomIndexOperation :: Noop => { }
167
- }
168
- Ok ( ( ) )
169
- }
170
-
171
140
/// Commit added events to [`RoomIndex`]
172
141
pub fn commit ( & mut self ) -> Result < OpStamp , IndexError > {
173
142
let last_commit_opstamp = self . writer . commit ( ) ?; // TODO: This is blocking. Handle it.
@@ -218,6 +187,31 @@ impl RoomIndex {
218
187
Ok ( ret)
219
188
}
220
189
190
+ /// Execute [`RoomIndexOperation`]
191
+ ///
192
+ /// This which will add/remove/edit an event in the index based on the
193
+ /// operation.
194
+ pub fn execute ( & mut self , operation : RoomIndexOperation ) -> Result < ( ) , IndexError > {
195
+ match operation {
196
+ RoomIndexOperation :: Add ( event) => {
197
+ if !self . contains ( & event. event_id ) {
198
+ self . writer . add ( self . schema . make_doc ( event) ?) ?;
199
+ }
200
+ }
201
+ RoomIndexOperation :: Remove ( event_id) => {
202
+ self . writer . remove ( & event_id) ;
203
+ }
204
+ RoomIndexOperation :: Edit ( remove_event_id, event) => {
205
+ self . writer . remove ( & remove_event_id) ;
206
+ if !self . contains ( & event. event_id ) {
207
+ self . writer . add ( self . schema . make_doc ( event) ?) ?;
208
+ }
209
+ }
210
+ RoomIndexOperation :: Noop => { }
211
+ }
212
+ Ok ( ( ) )
213
+ }
214
+
221
215
fn contains ( & self , event_id : & EventId ) -> bool {
222
216
let search_result = self . search ( format ! ( "event_id:\" {event_id}\" " ) . as_str ( ) , 1 ) ;
223
217
match search_result {
@@ -236,11 +230,52 @@ mod tests {
236
230
237
231
use matrix_sdk_test:: event_factory:: EventFactory ;
238
232
use ruma:: {
239
- event_id, events:: room:: message:: RoomMessageEventContentWithoutRelation , room_id,
240
- room_version_rules:: RedactionRules , user_id,
233
+ EventId , event_id,
234
+ events:: {
235
+ AnySyncMessageLikeEvent ,
236
+ room:: message:: { OriginalSyncRoomMessageEvent , RoomMessageEventContentWithoutRelation } ,
237
+ } ,
238
+ room_id, user_id,
241
239
} ;
242
240
243
- use crate :: index:: RoomIndex ;
241
+ use crate :: {
242
+ error:: IndexError ,
243
+ index:: { RoomIndex , RoomIndexOperation } ,
244
+ } ;
245
+
246
+ /// Helper function to add a regular message to the index
247
+ ///
248
+ /// # Panic
249
+ /// Panics when event is not a [`OriginalSyncRoomMessageEvent`] with no
250
+ /// relations.
251
+ fn index_message (
252
+ index : & mut RoomIndex ,
253
+ event : AnySyncMessageLikeEvent ,
254
+ ) -> Result < ( ) , IndexError > {
255
+ if let AnySyncMessageLikeEvent :: RoomMessage ( ev) = event
256
+ && let Some ( ev) = ev. as_original ( )
257
+ && ev. content . relates_to . is_none ( )
258
+ {
259
+ return index. execute ( RoomIndexOperation :: Add ( ev. clone ( ) ) ) ;
260
+ }
261
+ panic ! ( "Event was not a relationless OriginalSyncRoomMessageEvent." )
262
+ }
263
+
264
+ /// Helper function to remove events to the index
265
+ fn index_remove ( index : & mut RoomIndex , event_id : & EventId ) -> Result < ( ) , IndexError > {
266
+ index. execute ( RoomIndexOperation :: Remove ( event_id. to_owned ( ) ) )
267
+ }
268
+
269
+ /// Helper function to edit events in index
270
+ ///
271
+ /// Edit event with `event_id` into new [`OriginalSyncRoomMessageEvent`]
272
+ fn index_edit (
273
+ index : & mut RoomIndex ,
274
+ event_id : & EventId ,
275
+ new : OriginalSyncRoomMessageEvent ,
276
+ ) -> Result < ( ) , IndexError > {
277
+ index. execute ( RoomIndexOperation :: Edit ( event_id. to_owned ( ) , new) )
278
+ }
244
279
245
280
#[ test]
246
281
fn test_make_index_in_memory ( ) {
@@ -251,7 +286,7 @@ mod tests {
251
286
}
252
287
253
288
#[ test]
254
- fn test_handle_event ( ) {
289
+ fn test_add_event ( ) {
255
290
let room_id = room_id ! ( "!room_id:localhost" ) ;
256
291
let mut index =
257
292
RoomIndex :: new_in_memory ( room_id) . expect ( "failed to make index in ram: {index:?}" ) ;
@@ -263,7 +298,7 @@ mod tests {
263
298
. sender ( user_id ! ( "@user_id:localhost" ) )
264
299
. into_any_sync_message_like_event ( ) ;
265
300
266
- index . handle_event ( event , & RedactionRules :: V11 ) . expect ( "failed to add event: {res:?}" ) ;
301
+ index_message ( & mut index , event ) . expect ( "failed to add event: {res:?}" ) ;
267
302
}
268
303
269
304
#[ test]
@@ -278,23 +313,23 @@ mod tests {
278
313
let user_id = user_id ! ( "@user_id:localhost" ) ;
279
314
let f = EventFactory :: new ( ) . room ( room_id) . sender ( user_id) ;
280
315
281
- index. handle_event (
316
+ index_message (
317
+ & mut index,
282
318
f. text_msg ( "This is a sentence" )
283
319
. event_id ( event_id_1)
284
320
. into_any_sync_message_like_event ( ) ,
285
- & RedactionRules :: V11 ,
286
321
) ?;
287
322
288
- index. handle_event (
323
+ index_message (
324
+ & mut index,
289
325
f. text_msg ( "All new words" ) . event_id ( event_id_2) . into_any_sync_message_like_event ( ) ,
290
- & RedactionRules :: V11 ,
291
326
) ?;
292
327
293
- index. handle_event (
328
+ index_message (
329
+ & mut index,
294
330
f. text_msg ( "A similar sentence" )
295
331
. event_id ( event_id_3)
296
332
. into_any_sync_message_like_event ( ) ,
297
- & RedactionRules :: V11 ,
298
333
) ?;
299
334
300
335
index. commit_and_reload ( ) ?;
@@ -351,7 +386,7 @@ mod tests {
351
386
. sender ( user_id ! ( "@user_id:localhost" ) )
352
387
. into_any_sync_message_like_event ( ) ;
353
388
354
- index . handle_event ( event , & RedactionRules :: V11 ) ?;
389
+ index_message ( & mut index , event ) ?;
355
390
356
391
index. commit_and_reload ( ) ?;
357
392
@@ -361,7 +396,7 @@ mod tests {
361
396
}
362
397
363
398
#[ test]
364
- fn test_indexing_idempotency ( ) -> Result < ( ) , Box < dyn Error > > {
399
+ fn test_index_add_idempotency ( ) -> Result < ( ) , Box < dyn Error > > {
365
400
let room_id = room_id ! ( "!room_id:localhost" ) ;
366
401
let mut index = RoomIndex :: new_in_memory ( room_id) ?;
367
402
@@ -373,14 +408,14 @@ mod tests {
373
408
. sender ( user_id ! ( "@user_id:localhost" ) )
374
409
. into_any_sync_message_like_event ( ) ;
375
410
376
- index . handle_event ( event. clone ( ) , & RedactionRules :: V11 ) ?;
411
+ index_message ( & mut index , event. clone ( ) ) ?;
377
412
378
413
index. commit_and_reload ( ) ?;
379
414
380
415
assert ! ( index. contains( event_id) , "Index should contain event" ) ;
381
416
382
417
// indexing again should do nothing
383
- index . handle_event ( event , & RedactionRules :: V11 ) ?;
418
+ index_message ( & mut index , event ) ?;
384
419
385
420
index. commit_and_reload ( ) ?;
386
421
@@ -394,7 +429,7 @@ mod tests {
394
429
}
395
430
396
431
#[ test]
397
- fn test_redaction_removes_event ( ) -> Result < ( ) , Box < dyn Error > > {
432
+ fn test_remove_event ( ) -> Result < ( ) , Box < dyn Error > > {
398
433
let room_id = room_id ! ( "!room_id:localhost" ) ;
399
434
let mut index = RoomIndex :: new_in_memory ( room_id) ?;
400
435
@@ -405,17 +440,13 @@ mod tests {
405
440
let event =
406
441
f. text_msg ( "This is a sentence" ) . event_id ( event_id) . into_any_sync_message_like_event ( ) ;
407
442
408
- index . handle_event ( event , & RedactionRules :: V11 ) ?;
443
+ index_message ( & mut index , event ) ?;
409
444
410
445
index. commit_and_reload ( ) ?;
411
446
412
447
assert ! ( index. contains( event_id) , "Index should contain event" ) ;
413
448
414
- let redaction_event_id = event_id ! ( "$redaction_event_id:localhost" ) ;
415
- let redaction =
416
- f. redaction ( event_id) . event_id ( redaction_event_id) . into_any_sync_message_like_event ( ) ;
417
-
418
- index. handle_event ( redaction, & RedactionRules :: V11 ) ?;
449
+ index_remove ( & mut index, event_id) ?;
419
450
420
451
index. commit_and_reload ( ) ?;
421
452
@@ -438,7 +469,7 @@ mod tests {
438
469
. event_id ( old_event_id)
439
470
. into_any_sync_message_like_event ( ) ;
440
471
441
- index . handle_event ( old_event , & RedactionRules :: V11 ) ?;
472
+ index_message ( & mut index , old_event ) ?;
442
473
443
474
index. commit_and_reload ( ) ?;
444
475
@@ -452,9 +483,9 @@ mod tests {
452
483
RoomMessageEventContentWithoutRelation :: text_plain ( "This is a brand new sentence!" ) ,
453
484
)
454
485
. event_id ( new_event_id)
455
- . into_any_sync_message_like_event ( ) ;
486
+ . into_original_sync_room_message_event ( ) ;
456
487
457
- index . handle_event ( edit , & RedactionRules :: V11 ) ?;
488
+ index_edit ( & mut index , old_event_id , edit ) ?;
458
489
459
490
index. commit_and_reload ( ) ?;
460
491
0 commit comments