@@ -66,7 +66,7 @@ use tokio::sync::{broadcast, mpsc, RwLock, RwLockReadGuard, RwLockWriteGuard};
66
66
use tracing:: error;
67
67
68
68
use crate :: {
69
- event_cache:: { EventCache , EventCacheError , RoomEventCacheGenericUpdate } ,
69
+ event_cache:: { EventCache , EventCacheError , RoomEventCache , RoomEventCacheGenericUpdate } ,
70
70
send_queue:: SendQueue ,
71
71
} ;
72
72
@@ -270,12 +270,12 @@ impl RegisteredRooms {
270
270
// In `RoomLatestEvents`, the `LatestEvent` for this thread doesn't exist. Let's
271
271
// create and insert it.
272
272
if room_latest_event. per_thread . contains_key ( thread_id) . not ( ) {
273
- if let Some ( latest_event ) =
274
- RoomLatestEvents :: create_latest_event_for ( room_id , Some ( thread_id) )
275
- . await
276
- {
277
- room_latest_event . per_thread . insert ( thread_id . to_owned ( ) , latest_event ) ;
278
- }
273
+ room_latest_event . per_thread . insert (
274
+ thread_id. to_owned ( ) ,
275
+ room_latest_event
276
+ . create_latest_event_for ( room_id , Some ( thread_id ) )
277
+ . await ,
278
+ ) ;
279
279
}
280
280
}
281
281
@@ -398,35 +398,45 @@ struct RoomLatestEvents {
398
398
399
399
/// The latest events for each thread.
400
400
per_thread : HashMap < OwnedEventId , LatestEvent > ,
401
+
402
+ room_event_cache : RoomEventCache ,
401
403
}
402
404
403
405
impl RoomLatestEvents {
404
406
async fn new (
405
407
room_id : & RoomId ,
406
408
event_cache : & EventCache ,
407
409
) -> Result < Option < Self > , LatestEventsError > {
408
- let _room_event_cache = match event_cache. for_room ( room_id) . await {
410
+ let room_event_cache = match event_cache. for_room ( room_id) . await {
409
411
// It's fine to drop the `EventCacheDropHandles` here as the caller
410
412
// (`LatestEventState`) owns a clone of the `EventCache`.
411
413
Ok ( ( room_event_cache, _drop_handles) ) => room_event_cache,
412
414
Err ( EventCacheError :: RoomNotFound { .. } ) => return Ok ( None ) ,
413
415
Err ( err) => return Err ( LatestEventsError :: EventCache ( err) ) ,
414
416
} ;
415
417
416
- let latest_event = match Self :: create_latest_event_for ( room_id , None ) . await {
417
- Some ( latest_event ) => latest_event ,
418
- None => return Ok ( None ) ,
419
- } ;
420
-
421
- Ok ( Some ( Self { for_the_room : latest_event , per_thread : HashMap :: new ( ) } ) )
418
+ Ok ( Some ( Self {
419
+ for_the_room : Self :: create_latest_event_for_inner ( room_id , None , & room_event_cache )
420
+ . await ,
421
+ per_thread : HashMap :: new ( ) ,
422
+ room_event_cache ,
423
+ } ) )
422
424
}
423
425
424
- #[ allow( clippy:: unused_async) ]
425
426
async fn create_latest_event_for (
427
+ & self ,
428
+ room_id : & RoomId ,
429
+ thread_id : Option < & EventId > ,
430
+ ) -> LatestEvent {
431
+ Self :: create_latest_event_for_inner ( room_id, thread_id, & self . room_event_cache ) . await
432
+ }
433
+
434
+ async fn create_latest_event_for_inner (
426
435
room_id : & RoomId ,
427
436
thread_id : Option < & EventId > ,
428
- ) -> Option < LatestEvent > {
429
- LatestEvent :: new ( room_id, thread_id)
437
+ room_event_cache : & RoomEventCache ,
438
+ ) -> LatestEvent {
439
+ LatestEvent :: new ( room_id, thread_id, room_event_cache) . await
430
440
}
431
441
432
442
/// Get the [`LatestEvent`] for the room.
@@ -438,6 +448,15 @@ impl RoomLatestEvents {
438
448
fn for_thread ( & self , thread_id : & EventId ) -> Option < & LatestEvent > {
439
449
self . per_thread . get ( thread_id)
440
450
}
451
+
452
+ /// Update the latest events for the room and its threads.
453
+ async fn update ( & mut self ) {
454
+ self . for_the_room . update ( & self . room_event_cache ) . await ;
455
+
456
+ for latest_event in self . per_thread . values_mut ( ) {
457
+ latest_event. update ( & self . room_event_cache ) . await ;
458
+ }
459
+ }
441
460
}
442
461
443
462
/// The task responsible to listen to the [`EventCache`] and the [`SendQueue`].
@@ -543,19 +562,30 @@ async fn compute_latest_events_task(
543
562
registered_rooms : Arc < RegisteredRooms > ,
544
563
mut latest_event_queue_receiver : mpsc:: UnboundedReceiver < OwnedRoomId > ,
545
564
) {
546
- let mut buffer = Vec :: with_capacity ( 16 ) ;
565
+ const BUFFER_SIZE : usize = 16 ;
566
+
567
+ let mut buffer = Vec :: with_capacity ( BUFFER_SIZE ) ;
547
568
548
- while latest_event_queue_receiver. recv_many ( & mut buffer, 16 ) . await > 0 {
569
+ while latest_event_queue_receiver. recv_many ( & mut buffer, BUFFER_SIZE ) . await > 0 {
549
570
compute_latest_events ( & registered_rooms, & buffer) . await ;
550
571
buffer. clear ( ) ;
551
572
}
552
573
553
574
error ! ( "`compute_latest_events_task` has stopped" ) ;
554
575
}
555
576
556
- #[ allow( clippy:: unused_async) ]
557
- async fn compute_latest_events ( _registered_rooms : & RegisteredRooms , _for_rooms : & [ OwnedRoomId ] ) {
558
- // todo
577
+ async fn compute_latest_events ( registered_rooms : & RegisteredRooms , for_rooms : & [ OwnedRoomId ] ) {
578
+ for room_id in for_rooms {
579
+ let mut rooms = registered_rooms. rooms . write ( ) . await ;
580
+
581
+ if let Some ( room_latest_events) = rooms. get_mut ( room_id) {
582
+ room_latest_events. update ( ) . await ;
583
+ } else {
584
+ error ! ( ?room_id, "Failed to find the room" ) ;
585
+
586
+ continue ;
587
+ }
588
+ }
559
589
}
560
590
561
591
#[ cfg( all( test, not( target_family = "wasm" ) ) ) ]
0 commit comments