1515use std:: { fmt, fs, path:: Path , sync:: Arc } ;
1616
1717use ruma:: {
18- EventId , OwnedEventId , OwnedRoomId , RoomId , events:: AnySyncMessageLikeEvent ,
19- room_version_rules:: RedactionRules ,
18+ EventId , OwnedEventId , OwnedRoomId , RoomId , events:: room:: message:: OriginalSyncRoomMessageEvent ,
2019} ;
2120use tantivy:: {
2221 Index , IndexReader , TantivyDocument ,
@@ -35,16 +34,17 @@ use crate::{
3534} ;
3635
3736/// 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 ) ,
4141 /// Remove all documents in the index where
42- /// [ `MatrixSearchIndexSchema::deletion_key()`] matches this event id.
42+ /// `MatrixSearchIndexSchema::deletion_key()` matches this event id.
4343 Remove ( OwnedEventId ) ,
4444 /// 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 ) ,
4848 /// Do nothing.
4949 Noop ,
5050}
@@ -137,37 +137,6 @@ impl RoomIndex {
137137 RoomIndex :: new_with ( index, schema, room_id)
138138 }
139139
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-
171140 /// Commit added events to [`RoomIndex`]
172141 pub fn commit ( & mut self ) -> Result < OpStamp , IndexError > {
173142 let last_commit_opstamp = self . writer . commit ( ) ?; // TODO: This is blocking. Handle it.
@@ -218,6 +187,31 @@ impl RoomIndex {
218187 Ok ( ret)
219188 }
220189
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+
221215 fn contains ( & self , event_id : & EventId ) -> bool {
222216 let search_result = self . search ( format ! ( "event_id:\" {event_id}\" " ) . as_str ( ) , 1 ) ;
223217 match search_result {
@@ -236,11 +230,52 @@ mod tests {
236230
237231 use matrix_sdk_test:: event_factory:: EventFactory ;
238232 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,
241239 } ;
242240
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+ }
244279
245280 #[ test]
246281 fn test_make_index_in_memory ( ) {
@@ -251,7 +286,7 @@ mod tests {
251286 }
252287
253288 #[ test]
254- fn test_handle_event ( ) {
289+ fn test_add_event ( ) {
255290 let room_id = room_id ! ( "!room_id:localhost" ) ;
256291 let mut index =
257292 RoomIndex :: new_in_memory ( room_id) . expect ( "failed to make index in ram: {index:?}" ) ;
@@ -263,7 +298,7 @@ mod tests {
263298 . sender ( user_id ! ( "@user_id:localhost" ) )
264299 . into_any_sync_message_like_event ( ) ;
265300
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:?}" ) ;
267302 }
268303
269304 #[ test]
@@ -278,23 +313,23 @@ mod tests {
278313 let user_id = user_id ! ( "@user_id:localhost" ) ;
279314 let f = EventFactory :: new ( ) . room ( room_id) . sender ( user_id) ;
280315
281- index. handle_event (
316+ index_message (
317+ & mut index,
282318 f. text_msg ( "This is a sentence" )
283319 . event_id ( event_id_1)
284320 . into_any_sync_message_like_event ( ) ,
285- & RedactionRules :: V11 ,
286321 ) ?;
287322
288- index. handle_event (
323+ index_message (
324+ & mut index,
289325 f. text_msg ( "All new words" ) . event_id ( event_id_2) . into_any_sync_message_like_event ( ) ,
290- & RedactionRules :: V11 ,
291326 ) ?;
292327
293- index. handle_event (
328+ index_message (
329+ & mut index,
294330 f. text_msg ( "A similar sentence" )
295331 . event_id ( event_id_3)
296332 . into_any_sync_message_like_event ( ) ,
297- & RedactionRules :: V11 ,
298333 ) ?;
299334
300335 index. commit_and_reload ( ) ?;
@@ -351,7 +386,7 @@ mod tests {
351386 . sender ( user_id ! ( "@user_id:localhost" ) )
352387 . into_any_sync_message_like_event ( ) ;
353388
354- index . handle_event ( event , & RedactionRules :: V11 ) ?;
389+ index_message ( & mut index , event ) ?;
355390
356391 index. commit_and_reload ( ) ?;
357392
@@ -361,7 +396,7 @@ mod tests {
361396 }
362397
363398 #[ test]
364- fn test_indexing_idempotency ( ) -> Result < ( ) , Box < dyn Error > > {
399+ fn test_index_add_idempotency ( ) -> Result < ( ) , Box < dyn Error > > {
365400 let room_id = room_id ! ( "!room_id:localhost" ) ;
366401 let mut index = RoomIndex :: new_in_memory ( room_id) ?;
367402
@@ -373,14 +408,14 @@ mod tests {
373408 . sender ( user_id ! ( "@user_id:localhost" ) )
374409 . into_any_sync_message_like_event ( ) ;
375410
376- index . handle_event ( event. clone ( ) , & RedactionRules :: V11 ) ?;
411+ index_message ( & mut index , event. clone ( ) ) ?;
377412
378413 index. commit_and_reload ( ) ?;
379414
380415 assert ! ( index. contains( event_id) , "Index should contain event" ) ;
381416
382417 // indexing again should do nothing
383- index . handle_event ( event , & RedactionRules :: V11 ) ?;
418+ index_message ( & mut index , event ) ?;
384419
385420 index. commit_and_reload ( ) ?;
386421
@@ -394,7 +429,7 @@ mod tests {
394429 }
395430
396431 #[ test]
397- fn test_redaction_removes_event ( ) -> Result < ( ) , Box < dyn Error > > {
432+ fn test_remove_event ( ) -> Result < ( ) , Box < dyn Error > > {
398433 let room_id = room_id ! ( "!room_id:localhost" ) ;
399434 let mut index = RoomIndex :: new_in_memory ( room_id) ?;
400435
@@ -405,17 +440,13 @@ mod tests {
405440 let event =
406441 f. text_msg ( "This is a sentence" ) . event_id ( event_id) . into_any_sync_message_like_event ( ) ;
407442
408- index . handle_event ( event , & RedactionRules :: V11 ) ?;
443+ index_message ( & mut index , event ) ?;
409444
410445 index. commit_and_reload ( ) ?;
411446
412447 assert ! ( index. contains( event_id) , "Index should contain event" ) ;
413448
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) ?;
419450
420451 index. commit_and_reload ( ) ?;
421452
@@ -438,7 +469,7 @@ mod tests {
438469 . event_id ( old_event_id)
439470 . into_any_sync_message_like_event ( ) ;
440471
441- index . handle_event ( old_event , & RedactionRules :: V11 ) ?;
472+ index_message ( & mut index , old_event ) ?;
442473
443474 index. commit_and_reload ( ) ?;
444475
@@ -452,9 +483,9 @@ mod tests {
452483 RoomMessageEventContentWithoutRelation :: text_plain ( "This is a brand new sentence!" ) ,
453484 )
454485 . event_id ( new_event_id)
455- . into_any_sync_message_like_event ( ) ;
486+ . into_original_sync_room_message_event ( ) ;
456487
457- index . handle_event ( edit , & RedactionRules :: V11 ) ?;
488+ index_edit ( & mut index , old_event_id , edit ) ?;
458489
459490 index. commit_and_reload ( ) ?;
460491
0 commit comments