From 4bd823f8a3ff790432ea58f56568003be3b9a1fa Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Tue, 7 Jan 2025 16:29:01 +0100 Subject: [PATCH 1/8] refactor(event cache): simplfiy `append_related_event` --- crates/matrix-sdk/src/event_cache/room/mod.rs | 110 +++++++++--------- 1 file changed, 55 insertions(+), 55 deletions(-) diff --git a/crates/matrix-sdk/src/event_cache/room/mod.rs b/crates/matrix-sdk/src/event_cache/room/mod.rs index b0efde1395..3cd36fc0f1 100644 --- a/crates/matrix-sdk/src/event_cache/room/mod.rs +++ b/crates/matrix-sdk/src/event_cache/room/mod.rs @@ -449,66 +449,66 @@ impl RoomEventCacheInner { event: &SyncTimelineEvent, ) { // Handle and cache events and relations. - if let Ok(AnySyncTimelineEvent::MessageLike(ev)) = event.raw().deserialize() { - // Handle redactions separately, as their logic is slightly different. - if let AnySyncMessageLikeEvent::RoomRedaction(SyncRoomRedactionEvent::Original(ev)) = - &ev + let Ok(AnySyncTimelineEvent::MessageLike(ev)) = event.raw().deserialize() else { + return; + }; + + // Handle redactions separately, as their logic is slightly different. + if let AnySyncMessageLikeEvent::RoomRedaction(SyncRoomRedactionEvent::Original(ev)) = &ev { + if let Some(redacted_event_id) = + ev.content.redacts.as_ref().or_else(|| ev.redacts.as_ref()) { - if let Some(redacted_event_id) = ev.content.redacts.as_ref().or(ev.redacts.as_ref()) - { - cache - .relations - .entry(redacted_event_id.to_owned()) - .or_default() - .insert(ev.event_id.to_owned(), RelationType::Replacement); - } - } else { - let relationship = match ev.original_content() { - Some(AnyMessageLikeEventContent::RoomMessage(c)) => { - if let Some(relation) = c.relates_to { - match relation { - Relation::Replacement(replacement) => { - Some((replacement.event_id, RelationType::Replacement)) - } - Relation::Reply { in_reply_to } => { - Some((in_reply_to.event_id, RelationType::Reference)) - } - Relation::Thread(thread) => { - Some((thread.event_id, RelationType::Thread)) - } - // Do nothing for custom - _ => None, - } - } else { - None + cache + .relations + .entry(redacted_event_id.to_owned()) + .or_default() + .insert(ev.event_id.to_owned(), RelationType::Replacement); + } + return; + } + + let relationship = match ev.original_content() { + Some(AnyMessageLikeEventContent::RoomMessage(c)) => { + if let Some(relation) = c.relates_to { + match relation { + Relation::Replacement(replacement) => { + Some((replacement.event_id, RelationType::Replacement)) } + Relation::Reply { in_reply_to } => { + Some((in_reply_to.event_id, RelationType::Reference)) + } + Relation::Thread(thread) => Some((thread.event_id, RelationType::Thread)), + // Do nothing for custom + _ => None, } - Some(AnyMessageLikeEventContent::PollResponse(c)) => { - Some((c.relates_to.event_id, RelationType::Reference)) - } - Some(AnyMessageLikeEventContent::PollEnd(c)) => { - Some((c.relates_to.event_id, RelationType::Reference)) - } - Some(AnyMessageLikeEventContent::UnstablePollResponse(c)) => { - Some((c.relates_to.event_id, RelationType::Reference)) - } - Some(AnyMessageLikeEventContent::UnstablePollEnd(c)) => { - Some((c.relates_to.event_id, RelationType::Reference)) - } - Some(AnyMessageLikeEventContent::Reaction(c)) => { - Some((c.relates_to.event_id, RelationType::Annotation)) - } - _ => None, - }; - - if let Some(relationship) = relationship { - cache - .relations - .entry(relationship.0) - .or_default() - .insert(ev.event_id().to_owned(), relationship.1); + } else { + None } } + Some(AnyMessageLikeEventContent::PollResponse(c)) => { + Some((c.relates_to.event_id, RelationType::Reference)) + } + Some(AnyMessageLikeEventContent::PollEnd(c)) => { + Some((c.relates_to.event_id, RelationType::Reference)) + } + Some(AnyMessageLikeEventContent::UnstablePollResponse(c)) => { + Some((c.relates_to.event_id, RelationType::Reference)) + } + Some(AnyMessageLikeEventContent::UnstablePollEnd(c)) => { + Some((c.relates_to.event_id, RelationType::Reference)) + } + Some(AnyMessageLikeEventContent::Reaction(c)) => { + Some((c.relates_to.event_id, RelationType::Annotation)) + } + _ => None, + }; + + if let Some(relationship) = relationship { + cache + .relations + .entry(relationship.0) + .or_default() + .insert(ev.event_id().to_owned(), relationship.1); } } From 1172232b3292c62ca1de56b99db3ef0256679697 Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Tue, 7 Jan 2025 16:30:20 +0100 Subject: [PATCH 2/8] refactor(event cache): simplify signature of `RoomEventCacheInner::append_related_event` --- crates/matrix-sdk/src/event_cache/room/mod.rs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/crates/matrix-sdk/src/event_cache/room/mod.rs b/crates/matrix-sdk/src/event_cache/room/mod.rs index 3cd36fc0f1..b6cc3a637b 100644 --- a/crates/matrix-sdk/src/event_cache/room/mod.rs +++ b/crates/matrix-sdk/src/event_cache/room/mod.rs @@ -34,7 +34,7 @@ use ruma::{ }; use tokio::sync::{ broadcast::{Receiver, Sender}, - Notify, RwLock, RwLockReadGuard, RwLockWriteGuard, + Notify, RwLock, RwLockReadGuard, }; use tracing::{debug, trace, warn}; @@ -192,7 +192,7 @@ impl RoomEventCache { if let Some(event_id) = event.event_id() { let mut cache = self.inner.all_events.write().await; - self.inner.append_related_event(&mut cache, &event); + RoomEventCacheInner::append_related_event(&mut cache, &event); cache.events.insert(event_id, (self.inner.room_id.clone(), event)); } else { warn!("couldn't save event without event id in the event cache"); @@ -209,7 +209,7 @@ impl RoomEventCache { let mut cache = self.inner.all_events.write().await; for event in events { if let Some(event_id) = event.event_id() { - self.inner.append_related_event(&mut cache, &event); + RoomEventCacheInner::append_related_event(&mut cache, &event); cache.events.insert(event_id, (self.inner.room_id.clone(), event)); } else { warn!("couldn't save event without event id in the event cache"); @@ -443,11 +443,7 @@ impl RoomEventCacheInner { /// If the event is related to another one, its id is added to the /// relations map. - fn append_related_event( - &self, - cache: &mut RwLockWriteGuard<'_, AllEventsCache>, - event: &SyncTimelineEvent, - ) { + fn append_related_event(cache: &mut AllEventsCache, event: &SyncTimelineEvent) { // Handle and cache events and relations. let Ok(AnySyncTimelineEvent::MessageLike(ev)) = event.raw().deserialize() else { return; @@ -563,7 +559,7 @@ impl RoomEventCacheInner { for sync_timeline_event in &sync_timeline_events { if let Some(event_id) = sync_timeline_event.event_id() { - self.append_related_event(&mut all_events, sync_timeline_event); + Self::append_related_event(&mut all_events, sync_timeline_event); all_events.events.insert( event_id.to_owned(), (self.room_id.clone(), sync_timeline_event.clone()), From 3be17fb45396f4f94652991cc1adeaba1e6002bd Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Tue, 7 Jan 2025 16:31:40 +0100 Subject: [PATCH 3/8] refactor(event cache): move `append_related_event` to a method of `AllEventsCache` No code changes. --- crates/matrix-sdk/src/event_cache/mod.rs | 72 ++++++++++++++++- crates/matrix-sdk/src/event_cache/room/mod.rs | 80 +------------------ 2 files changed, 75 insertions(+), 77 deletions(-) diff --git a/crates/matrix-sdk/src/event_cache/mod.rs b/crates/matrix-sdk/src/event_cache/mod.rs index 6f28858399..a4c51974da 100644 --- a/crates/matrix-sdk/src/event_cache/mod.rs +++ b/crates/matrix-sdk/src/event_cache/mod.rs @@ -45,7 +45,12 @@ use matrix_sdk_common::executor::{spawn, JoinHandle}; use once_cell::sync::OnceCell; use room::RoomEventCacheState; use ruma::{ - events::{relation::RelationType, AnySyncEphemeralRoomEvent}, + events::{ + relation::RelationType, + room::{message::Relation, redaction::SyncRoomRedactionEvent}, + AnyMessageLikeEventContent, AnySyncEphemeralRoomEvent, AnySyncMessageLikeEvent, + AnySyncTimelineEvent, + }, serde::Raw, EventId, OwnedEventId, OwnedRoomId, RoomId, }; @@ -363,6 +368,71 @@ impl AllEventsCache { self.events.clear(); self.relations.clear(); } + + /// If the event is related to another one, its id is added to the relations + /// map. + fn append_related_event(&mut self, event: &SyncTimelineEvent) { + // Handle and cache events and relations. + let Ok(AnySyncTimelineEvent::MessageLike(ev)) = event.raw().deserialize() else { + return; + }; + + // Handle redactions separately, as their logic is slightly different. + if let AnySyncMessageLikeEvent::RoomRedaction(SyncRoomRedactionEvent::Original(ev)) = &ev { + if let Some(redacted_event_id) = + ev.content.redacts.as_ref().or_else(|| ev.redacts.as_ref()) + { + self.relations + .entry(redacted_event_id.to_owned()) + .or_default() + .insert(ev.event_id.to_owned(), RelationType::Replacement); + } + return; + } + + let relationship = match ev.original_content() { + Some(AnyMessageLikeEventContent::RoomMessage(c)) => { + if let Some(relation) = c.relates_to { + match relation { + Relation::Replacement(replacement) => { + Some((replacement.event_id, RelationType::Replacement)) + } + Relation::Reply { in_reply_to } => { + Some((in_reply_to.event_id, RelationType::Reference)) + } + Relation::Thread(thread) => Some((thread.event_id, RelationType::Thread)), + // Do nothing for custom + _ => None, + } + } else { + None + } + } + Some(AnyMessageLikeEventContent::PollResponse(c)) => { + Some((c.relates_to.event_id, RelationType::Reference)) + } + Some(AnyMessageLikeEventContent::PollEnd(c)) => { + Some((c.relates_to.event_id, RelationType::Reference)) + } + Some(AnyMessageLikeEventContent::UnstablePollResponse(c)) => { + Some((c.relates_to.event_id, RelationType::Reference)) + } + Some(AnyMessageLikeEventContent::UnstablePollEnd(c)) => { + Some((c.relates_to.event_id, RelationType::Reference)) + } + Some(AnyMessageLikeEventContent::Reaction(c)) => { + Some((c.relates_to.event_id, RelationType::Annotation)) + } + _ => None, + }; + + if let Some(relationship) = relationship { + self.relations + .entry(relationship.0) + .or_default() + .insert(ev.event_id().to_owned(), relationship.1); + } + } } struct EventCacheInner { diff --git a/crates/matrix-sdk/src/event_cache/room/mod.rs b/crates/matrix-sdk/src/event_cache/room/mod.rs index b6cc3a637b..a4c762a502 100644 --- a/crates/matrix-sdk/src/event_cache/room/mod.rs +++ b/crates/matrix-sdk/src/event_cache/room/mod.rs @@ -23,12 +23,7 @@ use matrix_sdk_base::{ sync::{JoinedRoomUpdate, LeftRoomUpdate, Timeline}, }; use ruma::{ - events::{ - relation::RelationType, - room::{message::Relation, redaction::SyncRoomRedactionEvent}, - AnyMessageLikeEventContent, AnyRoomAccountDataEvent, AnySyncEphemeralRoomEvent, - AnySyncMessageLikeEvent, AnySyncTimelineEvent, - }, + events::{relation::RelationType, AnyRoomAccountDataEvent, AnySyncEphemeralRoomEvent}, serde::Raw, EventId, OwnedEventId, OwnedRoomId, }; @@ -192,7 +187,7 @@ impl RoomEventCache { if let Some(event_id) = event.event_id() { let mut cache = self.inner.all_events.write().await; - RoomEventCacheInner::append_related_event(&mut cache, &event); + cache.append_related_event(&event); cache.events.insert(event_id, (self.inner.room_id.clone(), event)); } else { warn!("couldn't save event without event id in the event cache"); @@ -209,7 +204,7 @@ impl RoomEventCache { let mut cache = self.inner.all_events.write().await; for event in events { if let Some(event_id) = event.event_id() { - RoomEventCacheInner::append_related_event(&mut cache, &event); + cache.append_related_event(&event); cache.events.insert(event_id, (self.inner.room_id.clone(), event)); } else { warn!("couldn't save event without event id in the event cache"); @@ -441,73 +436,6 @@ impl RoomEventCacheInner { .await } - /// If the event is related to another one, its id is added to the - /// relations map. - fn append_related_event(cache: &mut AllEventsCache, event: &SyncTimelineEvent) { - // Handle and cache events and relations. - let Ok(AnySyncTimelineEvent::MessageLike(ev)) = event.raw().deserialize() else { - return; - }; - - // Handle redactions separately, as their logic is slightly different. - if let AnySyncMessageLikeEvent::RoomRedaction(SyncRoomRedactionEvent::Original(ev)) = &ev { - if let Some(redacted_event_id) = - ev.content.redacts.as_ref().or_else(|| ev.redacts.as_ref()) - { - cache - .relations - .entry(redacted_event_id.to_owned()) - .or_default() - .insert(ev.event_id.to_owned(), RelationType::Replacement); - } - return; - } - - let relationship = match ev.original_content() { - Some(AnyMessageLikeEventContent::RoomMessage(c)) => { - if let Some(relation) = c.relates_to { - match relation { - Relation::Replacement(replacement) => { - Some((replacement.event_id, RelationType::Replacement)) - } - Relation::Reply { in_reply_to } => { - Some((in_reply_to.event_id, RelationType::Reference)) - } - Relation::Thread(thread) => Some((thread.event_id, RelationType::Thread)), - // Do nothing for custom - _ => None, - } - } else { - None - } - } - Some(AnyMessageLikeEventContent::PollResponse(c)) => { - Some((c.relates_to.event_id, RelationType::Reference)) - } - Some(AnyMessageLikeEventContent::PollEnd(c)) => { - Some((c.relates_to.event_id, RelationType::Reference)) - } - Some(AnyMessageLikeEventContent::UnstablePollResponse(c)) => { - Some((c.relates_to.event_id, RelationType::Reference)) - } - Some(AnyMessageLikeEventContent::UnstablePollEnd(c)) => { - Some((c.relates_to.event_id, RelationType::Reference)) - } - Some(AnyMessageLikeEventContent::Reaction(c)) => { - Some((c.relates_to.event_id, RelationType::Annotation)) - } - _ => None, - }; - - if let Some(relationship) = relationship { - cache - .relations - .entry(relationship.0) - .or_default() - .insert(ev.event_id().to_owned(), relationship.1); - } - } - /// Append a set of events and associated room data. /// /// This is a private implementation. It must not be exposed publicly. @@ -559,7 +487,7 @@ impl RoomEventCacheInner { for sync_timeline_event in &sync_timeline_events { if let Some(event_id) = sync_timeline_event.event_id() { - Self::append_related_event(&mut all_events, sync_timeline_event); + all_events.append_related_event(sync_timeline_event); all_events.events.insert( event_id.to_owned(), (self.room_id.clone(), sync_timeline_event.clone()), From 2645c576867300e66f7daac116c57472f8700a02 Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Tue, 7 Jan 2025 16:38:34 +0100 Subject: [PATCH 4/8] refactor(event cache): simplify `collect_related_events` too Reduce indent, simplify parameter type, etc. --- crates/matrix-sdk/src/event_cache/room/mod.rs | 45 ++++++++++--------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/crates/matrix-sdk/src/event_cache/room/mod.rs b/crates/matrix-sdk/src/event_cache/room/mod.rs index a4c762a502..d5390d8f6e 100644 --- a/crates/matrix-sdk/src/event_cache/room/mod.rs +++ b/crates/matrix-sdk/src/event_cache/room/mod.rs @@ -29,7 +29,7 @@ use ruma::{ }; use tokio::sync::{ broadcast::{Receiver, Sender}, - Notify, RwLock, RwLockReadGuard, + Notify, RwLock, }; use tracing::{debug, trace, warn}; @@ -117,7 +117,7 @@ impl RoomEventCache { let cache = self.inner.all_events.read().await; if let Some((_, event)) = cache.events.get(event_id) { - Self::collect_related_events(&cache, event_id, &filter, &mut relation_events); + Self::collect_related_events(&cache, event_id, filter.as_deref(), &mut relation_events); Some((event.clone(), relation_events)) } else { None @@ -149,31 +149,34 @@ impl RoomEventCache { /// the `results` parameter. Then it'll recursively get the related /// event ids for those too. fn collect_related_events( - cache: &RwLockReadGuard<'_, AllEventsCache>, + cache: &AllEventsCache, event_id: &EventId, - filter: &Option>, + filter: Option<&[RelationType]>, results: &mut Vec, ) { - if let Some(related_event_ids) = cache.relations.get(event_id) { - for (related_event_id, relation_type) in related_event_ids { - if let Some(filter) = filter { - if !filter.contains(relation_type) { - continue; - } - } + let Some(related_event_ids) = cache.relations.get(event_id) else { + return; + }; - // If the event was already added to the related ones, skip it. - if results.iter().any(|e| { - e.event_id().is_some_and(|added_related_event_id| { - added_related_event_id == *related_event_id - }) - }) { + for (related_event_id, relation_type) in related_event_ids { + if let Some(filter) = filter { + if !filter.contains(relation_type) { continue; } - if let Some((_, ev)) = cache.events.get(related_event_id) { - results.push(ev.clone()); - Self::collect_related_events(cache, related_event_id, filter, results); - } + } + + // If the event was already added to the related ones, skip it. + if results.iter().any(|event| { + event.event_id().is_some_and(|added_related_event_id| { + added_related_event_id == *related_event_id + }) + }) { + continue; + } + + if let Some((_, ev)) = cache.events.get(related_event_id) { + results.push(ev.clone()); + Self::collect_related_events(cache, related_event_id, filter, results); } } } From 42f9250d1ba8053bbd9e002aa38fd072a89155db Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Tue, 7 Jan 2025 16:39:58 +0100 Subject: [PATCH 5/8] refactor(event cache): move `collect_related_events` to `AllEventsCache` --- crates/matrix-sdk/src/event_cache/mod.rs | 36 ++++++++++++++++++ crates/matrix-sdk/src/event_cache/room/mod.rs | 38 +------------------ 2 files changed, 37 insertions(+), 37 deletions(-) diff --git a/crates/matrix-sdk/src/event_cache/mod.rs b/crates/matrix-sdk/src/event_cache/mod.rs index a4c51974da..89554d4335 100644 --- a/crates/matrix-sdk/src/event_cache/mod.rs +++ b/crates/matrix-sdk/src/event_cache/mod.rs @@ -433,6 +433,42 @@ impl AllEventsCache { .insert(ev.event_id().to_owned(), relationship.1); } } + + /// Looks for related event ids for the passed event id, and appends them to + /// the `results` parameter. Then it'll recursively get the related + /// event ids for those too. + fn collect_related_events( + &self, + event_id: &EventId, + filter: Option<&[RelationType]>, + results: &mut Vec, + ) { + let Some(related_event_ids) = self.relations.get(event_id) else { + return; + }; + + for (related_event_id, relation_type) in related_event_ids { + if let Some(filter) = filter { + if !filter.contains(relation_type) { + continue; + } + } + + // If the event was already added to the related ones, skip it. + if results.iter().any(|event| { + event.event_id().is_some_and(|added_related_event_id| { + added_related_event_id == *related_event_id + }) + }) { + continue; + } + + if let Some((_, ev)) = self.events.get(related_event_id) { + results.push(ev.clone()); + self.collect_related_events(related_event_id, filter, results); + } + } + } } struct EventCacheInner { diff --git a/crates/matrix-sdk/src/event_cache/room/mod.rs b/crates/matrix-sdk/src/event_cache/room/mod.rs index d5390d8f6e..b447eb34db 100644 --- a/crates/matrix-sdk/src/event_cache/room/mod.rs +++ b/crates/matrix-sdk/src/event_cache/room/mod.rs @@ -117,7 +117,7 @@ impl RoomEventCache { let cache = self.inner.all_events.read().await; if let Some((_, event)) = cache.events.get(event_id) { - Self::collect_related_events(&cache, event_id, filter.as_deref(), &mut relation_events); + cache.collect_related_events(event_id, filter.as_deref(), &mut relation_events); Some((event.clone(), relation_events)) } else { None @@ -145,42 +145,6 @@ impl RoomEventCache { Ok(()) } - /// Looks for related event ids for the passed event id, and appends them to - /// the `results` parameter. Then it'll recursively get the related - /// event ids for those too. - fn collect_related_events( - cache: &AllEventsCache, - event_id: &EventId, - filter: Option<&[RelationType]>, - results: &mut Vec, - ) { - let Some(related_event_ids) = cache.relations.get(event_id) else { - return; - }; - - for (related_event_id, relation_type) in related_event_ids { - if let Some(filter) = filter { - if !filter.contains(relation_type) { - continue; - } - } - - // If the event was already added to the related ones, skip it. - if results.iter().any(|event| { - event.event_id().is_some_and(|added_related_event_id| { - added_related_event_id == *related_event_id - }) - }) { - continue; - } - - if let Some((_, ev)) = cache.events.get(related_event_id) { - results.push(ev.clone()); - Self::collect_related_events(cache, related_event_id, filter, results); - } - } - } - /// Save a single event in the event cache, for further retrieval with /// [`Self::event`]. // TODO: This doesn't insert the event into the linked chunk. In the future From f1858f0db68e2c5e21b59fcc5edf40f0d33b99fe Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Tue, 7 Jan 2025 16:42:36 +0100 Subject: [PATCH 6/8] refactor(event cache): make `collect_related_events` signature less awkward The last parameter was passed to handle recursivity: introduce a small `_rec` helper function, and call that instead, so external callers don't have to interact with the weird API. --- crates/matrix-sdk/src/event_cache/mod.rs | 12 +++++++++++- crates/matrix-sdk/src/event_cache/room/mod.rs | 6 ++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/crates/matrix-sdk/src/event_cache/mod.rs b/crates/matrix-sdk/src/event_cache/mod.rs index 89554d4335..6a2013c5cc 100644 --- a/crates/matrix-sdk/src/event_cache/mod.rs +++ b/crates/matrix-sdk/src/event_cache/mod.rs @@ -441,6 +441,16 @@ impl AllEventsCache { &self, event_id: &EventId, filter: Option<&[RelationType]>, + ) -> Vec { + let mut results = Vec::new(); + self.collect_related_events_rec(event_id, filter, &mut results); + results + } + + fn collect_related_events_rec( + &self, + event_id: &EventId, + filter: Option<&[RelationType]>, results: &mut Vec, ) { let Some(related_event_ids) = self.relations.get(event_id) else { @@ -465,7 +475,7 @@ impl AllEventsCache { if let Some((_, ev)) = self.events.get(related_event_id) { results.push(ev.clone()); - self.collect_related_events(related_event_id, filter, results); + self.collect_related_events_rec(related_event_id, filter, results); } } } diff --git a/crates/matrix-sdk/src/event_cache/room/mod.rs b/crates/matrix-sdk/src/event_cache/room/mod.rs index b447eb34db..0e0c44932a 100644 --- a/crates/matrix-sdk/src/event_cache/room/mod.rs +++ b/crates/matrix-sdk/src/event_cache/room/mod.rs @@ -113,12 +113,10 @@ impl RoomEventCache { event_id: &EventId, filter: Option>, ) -> Option<(SyncTimelineEvent, Vec)> { - let mut relation_events = Vec::new(); - let cache = self.inner.all_events.read().await; if let Some((_, event)) = cache.events.get(event_id) { - cache.collect_related_events(event_id, filter.as_deref(), &mut relation_events); - Some((event.clone(), relation_events)) + let related_events = cache.collect_related_events(event_id, filter.as_deref()); + Some((event.clone(), related_events)) } else { None } From 780bc14cc7361a22366f529130cc1f4a364d67b7 Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Tue, 7 Jan 2025 16:45:35 +0100 Subject: [PATCH 7/8] doc(event cache): fix links in doc comment --- crates/matrix-sdk/src/event_cache/room/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/matrix-sdk/src/event_cache/room/mod.rs b/crates/matrix-sdk/src/event_cache/room/mod.rs index 0e0c44932a..0ef69e3609 100644 --- a/crates/matrix-sdk/src/event_cache/room/mod.rs +++ b/crates/matrix-sdk/src/event_cache/room/mod.rs @@ -195,9 +195,9 @@ pub(super) struct RoomEventCacheInner { /// State for this room's event cache. pub state: RwLock, - /// See comment of [`EventCacheInner::all_events`]. + /// See comment of [`super::EventCacheInner::all_events`]. /// - /// This is shared between the [`EventCacheInner`] singleton and all + /// This is shared between the [`super::EventCacheInner`] singleton and all /// [`RoomEventCacheInner`] instances. all_events: Arc>, From 6de91dedd9a9259954fe18848e63f28981cb41f7 Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Tue, 7 Jan 2025 16:54:50 +0100 Subject: [PATCH 8/8] fixup! refactor(event cache): move `append_related_event` to a method of `AllEventsCache` --- crates/matrix-sdk/src/event_cache/mod.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/crates/matrix-sdk/src/event_cache/mod.rs b/crates/matrix-sdk/src/event_cache/mod.rs index 6a2013c5cc..f85b85bc33 100644 --- a/crates/matrix-sdk/src/event_cache/mod.rs +++ b/crates/matrix-sdk/src/event_cache/mod.rs @@ -379,9 +379,7 @@ impl AllEventsCache { // Handle redactions separately, as their logic is slightly different. if let AnySyncMessageLikeEvent::RoomRedaction(SyncRoomRedactionEvent::Original(ev)) = &ev { - if let Some(redacted_event_id) = - ev.content.redacts.as_ref().or_else(|| ev.redacts.as_ref()) - { + if let Some(redacted_event_id) = ev.content.redacts.as_ref().or(ev.redacts.as_ref()) { self.relations .entry(redacted_event_id.to_owned()) .or_default()