Skip to content

Commit 3eeb046

Browse files
committed
feat(skd): compute_latest_events calls the new LatestEvent::update method.
1 parent 6f84a44 commit 3eeb046

File tree

2 files changed

+91
-30
lines changed

2 files changed

+91
-30
lines changed

crates/matrix-sdk/src/latest_events/latest_event.rs

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,53 @@
1515
use eyeball::{AsyncLock, SharedObservable, Subscriber};
1616
use ruma::{EventId, OwnedEventId, OwnedRoomId, RoomId};
1717

18+
use crate::event_cache::RoomEventCache;
19+
1820
/// The latest event of a room or a thread.
1921
///
2022
/// Use [`LatestEvent::subscribe`] to get a stream of updates.
2123
#[derive(Debug)]
2224
pub(super) struct LatestEvent {
2325
/// The room owning this latest event.
24-
_room_id: OwnedRoomId,
26+
room_id: OwnedRoomId,
2527
/// The thread (if any) owning this latest event.
26-
_thread_id: Option<OwnedEventId>,
28+
thread_id: Option<OwnedEventId>,
2729
/// The latest event value.
2830
value: SharedObservable<LatestEventValue, AsyncLock>,
2931
}
3032

3133
impl LatestEvent {
32-
pub(super) fn new(room_id: &RoomId, thread_id: Option<&EventId>) -> Option<Self> {
33-
Some(Self {
34-
_room_id: room_id.to_owned(),
35-
_thread_id: thread_id.map(ToOwned::to_owned),
36-
value: SharedObservable::new_async(LatestEventValue::None),
37-
})
34+
pub(super) async fn new(
35+
room_id: &RoomId,
36+
thread_id: Option<&EventId>,
37+
room_event_cache: &RoomEventCache,
38+
) -> Self {
39+
Self {
40+
room_id: room_id.to_owned(),
41+
thread_id: thread_id.map(ToOwned::to_owned),
42+
value: SharedObservable::new_async(
43+
LatestEventValue::new(room_id, thread_id, room_event_cache).await,
44+
),
45+
}
3846
}
3947

4048
/// Return a [`Subscriber`] to new values.
4149
pub async fn subscribe(&self) -> Subscriber<LatestEventValue, AsyncLock> {
4250
self.value.subscribe().await
4351
}
52+
53+
/// Update the inner latest event value.
54+
pub async fn update(&mut self, room_event_cache: &RoomEventCache) {
55+
let new_value =
56+
LatestEventValue::new(&self.room_id, self.thread_id.as_deref(), room_event_cache).await;
57+
58+
match new_value {
59+
LatestEventValue::None => {
60+
// The new value is `None`. It means no new value has been
61+
// computed. Let's keep the old value.
62+
}
63+
}
64+
}
4465
}
4566

4667
/// A latest event value!
@@ -49,3 +70,13 @@ pub enum LatestEventValue {
4970
/// No value has been computed yet, or no candidate value was found.
5071
None,
5172
}
73+
74+
impl LatestEventValue {
75+
async fn new(
76+
_room_id: &RoomId,
77+
_thread_id: Option<&EventId>,
78+
_room_event_cache: &RoomEventCache,
79+
) -> Self {
80+
LatestEventValue::None
81+
}
82+
}

crates/matrix-sdk/src/latest_events/mod.rs

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use tokio::sync::{broadcast, mpsc, RwLock, RwLockReadGuard, RwLockWriteGuard};
6666
use tracing::error;
6767

6868
use crate::{
69-
event_cache::{EventCache, EventCacheError, RoomEventCacheGenericUpdate},
69+
event_cache::{EventCache, EventCacheError, RoomEventCache, RoomEventCacheGenericUpdate},
7070
send_queue::SendQueue,
7171
};
7272

@@ -270,12 +270,12 @@ impl RegisteredRooms {
270270
// In `RoomLatestEvents`, the `LatestEvent` for this thread doesn't exist. Let's
271271
// create and insert it.
272272
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+
);
279279
}
280280
}
281281

@@ -398,35 +398,45 @@ struct RoomLatestEvents {
398398

399399
/// The latest events for each thread.
400400
per_thread: HashMap<OwnedEventId, LatestEvent>,
401+
402+
room_event_cache: RoomEventCache,
401403
}
402404

403405
impl RoomLatestEvents {
404406
async fn new(
405407
room_id: &RoomId,
406408
event_cache: &EventCache,
407409
) -> 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 {
409411
// It's fine to drop the `EventCacheDropHandles` here as the caller
410412
// (`LatestEventState`) owns a clone of the `EventCache`.
411413
Ok((room_event_cache, _drop_handles)) => room_event_cache,
412414
Err(EventCacheError::RoomNotFound { .. }) => return Ok(None),
413415
Err(err) => return Err(LatestEventsError::EventCache(err)),
414416
};
415417

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+
}))
422424
}
423425

424-
#[allow(clippy::unused_async)]
425426
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(
426435
room_id: &RoomId,
427436
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
430440
}
431441

432442
/// Get the [`LatestEvent`] for the room.
@@ -438,6 +448,15 @@ impl RoomLatestEvents {
438448
fn for_thread(&self, thread_id: &EventId) -> Option<&LatestEvent> {
439449
self.per_thread.get(thread_id)
440450
}
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+
}
441460
}
442461

443462
/// The task responsible to listen to the [`EventCache`] and the [`SendQueue`].
@@ -543,19 +562,30 @@ async fn compute_latest_events_task(
543562
registered_rooms: Arc<RegisteredRooms>,
544563
mut latest_event_queue_receiver: mpsc::UnboundedReceiver<OwnedRoomId>,
545564
) {
546-
let mut buffer = Vec::with_capacity(16);
565+
const BUFFER_SIZE: usize = 16;
566+
567+
let mut buffer = Vec::with_capacity(BUFFER_SIZE);
547568

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 {
549570
compute_latest_events(&registered_rooms, &buffer).await;
550571
buffer.clear();
551572
}
552573

553574
error!("`compute_latest_events_task` has stopped");
554575
}
555576

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+
}
559589
}
560590

561591
#[cfg(all(test, not(target_family = "wasm")))]

0 commit comments

Comments
 (0)