Skip to content

Commit b43aac1

Browse files
committed
chore: address review comments
1 parent c019009 commit b43aac1

File tree

5 files changed

+53
-29
lines changed

5 files changed

+53
-29
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/matrix-sdk/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ futures-util.workspace = true
9595
http.workspace = true
9696
imbl = { workspace = true, features = ["serde"] }
9797
indexmap.workspace = true
98+
itertools.workspace = true
9899
js_int = "0.2.2"
99100
language-tags = { version = "0.3.2" }
100101
matrix-sdk-base.workspace = true

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

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,12 @@ impl EventCache {
433433
self.inner.generic_update_sender.subscribe()
434434
}
435435

436+
/// React to a given linked chunk update by subscribing the user to a
437+
/// thread, if needs be (when the user got mentioned in a thread reply, for
438+
/// a thread they were not subscribed to).
439+
///
440+
/// Returns a boolean indicating whether the task should keep on running or
441+
/// not.
436442
#[instrument(skip(client, thread_subscriber_sender))]
437443
async fn handle_thread_subscriber_linked_chunk_update(
438444
client: &WeakClient,
@@ -445,7 +451,7 @@ impl EventCache {
445451
return false;
446452
};
447453

448-
let OwnedLinkedChunkId::Thread(room_id, thread_root) = &up.linked_chunk else {
454+
let OwnedLinkedChunkId::Thread(room_id, thread_root) = &up.linked_chunk_id else {
449455
trace!("received an update for a non-thread linked chunk, ignoring");
450456
return true;
451457
};
@@ -457,8 +463,9 @@ impl EventCache {
457463

458464
let thread_root = thread_root.clone();
459465

460-
let new_events = up.events();
461-
if new_events.is_empty() {
466+
let mut new_events = up.events().peekable();
467+
468+
if new_events.peek().is_none() {
462469
// No new events, nothing to do.
463470
return true;
464471
}
@@ -489,8 +496,9 @@ impl EventCache {
489496
let mut subscribe_up_to = None;
490497

491498
// Find if there's an event that would trigger a mention for the current
492-
// user, iterating from the end of the new events towards the oldest,
493-
for ev in new_events.into_iter().rev() {
499+
// user, iterating from the end of the new events towards the oldest, so we can
500+
// find the most recent event to subscribe to.
501+
for ev in new_events.rev() {
494502
if push_context
495503
.for_event(ev.raw())
496504
.await
@@ -520,6 +528,12 @@ impl EventCache {
520528
true
521529
}
522530

531+
/// React to a given send queue update by subscribing the user to a
532+
/// thread, if needs be (when the user sent an event in a thread they were
533+
/// not subscribed to).
534+
///
535+
/// Returns a boolean indicating whether the task should keep on running or
536+
/// not.
523537
#[instrument(skip(client, thread_subscriber_sender))]
524538
async fn handle_thread_subscriber_send_queue_update(
525539
client: &WeakClient,
@@ -710,7 +724,8 @@ struct EventCacheInner {
710724
/// A sender for a persisted linked chunk update.
711725
///
712726
/// This is used to notify that some linked chunk has persisted some updates
713-
/// to a store, and can be used by observers to look for new events.
727+
/// to a store, during sync or a back-pagination of *any* linked chunk.
728+
/// This can be used by observers to look for new events.
714729
///
715730
/// See doc comment of [`RoomEventCacheLinkedChunkUpdate`].
716731
linked_chunk_update_sender: Sender<RoomEventCacheLinkedChunkUpdate>,
@@ -964,34 +979,37 @@ pub struct RoomEventCacheGenericUpdate {
964979
#[derive(Clone, Debug)]
965980
struct RoomEventCacheLinkedChunkUpdate {
966981
/// The linked chunk affected by the update.
967-
linked_chunk: OwnedLinkedChunkId,
982+
linked_chunk_id: OwnedLinkedChunkId,
968983

969-
/// A vector of all the updates that happened during this update.
984+
/// A vector of all the linked chunk updates that happened during this event
985+
/// cache update.
970986
updates: Vec<linked_chunk::Update<TimelineEvent, Gap>>,
971987
}
972988

973989
impl RoomEventCacheLinkedChunkUpdate {
974990
/// Return all the new events propagated by this update, in topological
975991
/// order.
976-
pub fn events(self) -> Vec<TimelineEvent> {
977-
self.updates
978-
.into_iter()
979-
.flat_map(|update| match update {
980-
linked_chunk::Update::PushItems { items, .. } => items,
981-
linked_chunk::Update::ReplaceItem { item, .. } => vec![item],
982-
linked_chunk::Update::RemoveItem { .. }
983-
| linked_chunk::Update::DetachLastItems { .. }
984-
| linked_chunk::Update::StartReattachItems
985-
| linked_chunk::Update::EndReattachItems
986-
| linked_chunk::Update::NewItemsChunk { .. }
987-
| linked_chunk::Update::NewGapChunk { .. }
988-
| linked_chunk::Update::RemoveChunk(..)
989-
| linked_chunk::Update::Clear => {
990-
// All these updates don't contain any new event.
991-
vec![]
992-
}
993-
})
994-
.collect()
992+
pub fn events(self) -> impl DoubleEndedIterator<Item = TimelineEvent> {
993+
use itertools::Either;
994+
self.updates.into_iter().flat_map(|update| match update {
995+
linked_chunk::Update::PushItems { items, .. } => {
996+
Either::Left(Either::Left(items.into_iter()))
997+
}
998+
linked_chunk::Update::ReplaceItem { item, .. } => {
999+
Either::Left(Either::Right(std::iter::once(item)))
1000+
}
1001+
linked_chunk::Update::RemoveItem { .. }
1002+
| linked_chunk::Update::DetachLastItems { .. }
1003+
| linked_chunk::Update::StartReattachItems
1004+
| linked_chunk::Update::EndReattachItems
1005+
| linked_chunk::Update::NewItemsChunk { .. }
1006+
| linked_chunk::Update::NewGapChunk { .. }
1007+
| linked_chunk::Update::RemoveChunk(..)
1008+
| linked_chunk::Update::Clear => {
1009+
// All these updates don't contain any new event.
1010+
Either::Right(std::iter::empty())
1011+
}
1012+
})
9951013
}
9961014
}
9971015

crates/matrix-sdk/src/event_cache/room/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1280,7 +1280,7 @@ mod private {
12801280

12811281
// Forward that the store got updated to observers.
12821282
let _ = self.linked_chunk_update_sender.send(RoomEventCacheLinkedChunkUpdate {
1283-
linked_chunk: OwnedLinkedChunkId::Room(self.room.clone()),
1283+
linked_chunk_id: OwnedLinkedChunkId::Room(self.room.clone()),
12841284
updates,
12851285
});
12861286

crates/matrix-sdk/src/event_cache/room/threads.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ pub(crate) struct ThreadEventCache {
5555
/// A sender for live events updates in this thread.
5656
sender: Sender<ThreadEventCacheUpdate>,
5757

58+
/// A sender for the globally observable linked chunk updates that happened
59+
/// during a sync or a back-pagination.
60+
///
61+
/// See also [`super::super::EventCacheInner::linked_chunk_update_sender`].
5862
linked_chunk_update_sender: Sender<RoomEventCacheLinkedChunkUpdate>,
5963
}
6064

@@ -102,7 +106,7 @@ impl ThreadEventCache {
102106

103107
let _ = self.linked_chunk_update_sender.send(RoomEventCacheLinkedChunkUpdate {
104108
updates,
105-
linked_chunk: OwnedLinkedChunkId::Thread(
109+
linked_chunk_id: OwnedLinkedChunkId::Thread(
106110
self.room_id.clone(),
107111
self.thread_root.clone(),
108112
),

0 commit comments

Comments
 (0)