Skip to content

Commit cc974dd

Browse files
committed
refactor(event cache): use Event instead of TimelineEvent more evenly
1 parent 8b2a8e7 commit cc974dd

File tree

1 file changed

+26
-33
lines changed
  • crates/matrix-sdk/src/event_cache/room

1 file changed

+26
-33
lines changed

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

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ use events::sort_positions_descending;
2828
use eyeball::SharedObservable;
2929
use eyeball_im::VectorDiff;
3030
use matrix_sdk_base::{
31-
deserialized_responses::{AmbiguityChange, TimelineEvent},
31+
deserialized_responses::AmbiguityChange,
32+
event_cache::Event,
3233
linked_chunk::Position,
3334
sync::{JoinedRoomUpdate, LeftRoomUpdate, Timeline},
3435
};
@@ -162,7 +163,7 @@ impl RoomEventCache {
162163
///
163164
/// Use [`RoomEventCache::subscribe`] to get all current events, plus a
164165
/// listener/subscriber.
165-
pub async fn events(&self) -> Vec<TimelineEvent> {
166+
pub async fn events(&self) -> Vec<Event> {
166167
let state = self.inner.state.read().await;
167168

168169
state.events().events().map(|(_position, item)| item.clone()).collect()
@@ -174,7 +175,7 @@ impl RoomEventCache {
174175
/// Use [`RoomEventCache::events`] to get all current events without the
175176
/// listener/subscriber. Creating, and especially dropping, a
176177
/// [`RoomEventCacheListener`] isn't free.
177-
pub async fn subscribe(&self) -> (Vec<TimelineEvent>, RoomEventCacheListener) {
178+
pub async fn subscribe(&self) -> (Vec<Event>, RoomEventCacheListener) {
178179
let state = self.inner.state.read().await;
179180
let events = state.events().events().map(|(_position, item)| item.clone()).collect();
180181

@@ -199,7 +200,7 @@ impl RoomEventCache {
199200
}
200201

201202
/// Try to find an event by id in this room.
202-
pub async fn event(&self, event_id: &EventId) -> Option<TimelineEvent> {
203+
pub async fn event(&self, event_id: &EventId) -> Option<Event> {
203204
self.inner
204205
.state
205206
.read()
@@ -219,7 +220,7 @@ impl RoomEventCache {
219220
&self,
220221
event_id: &EventId,
221222
filter: Option<Vec<RelationType>>,
222-
) -> Option<(TimelineEvent, Vec<TimelineEvent>)> {
223+
) -> Option<(Event, Vec<Event>)> {
223224
// Search in all loaded or stored events.
224225
self.inner
225226
.state
@@ -250,7 +251,7 @@ impl RoomEventCache {
250251

251252
/// Save some events in the event cache, for further retrieval with
252253
/// [`Self::event`].
253-
pub(crate) async fn save_events(&self, events: impl IntoIterator<Item = TimelineEvent>) {
254+
pub(crate) async fn save_events(&self, events: impl IntoIterator<Item = Event>) {
254255
if let Err(err) = self.inner.state.write().await.save_event(events).await {
255256
warn!("couldn't save event in the event cache: {err}");
256257
}
@@ -434,11 +435,7 @@ pub(super) enum LoadMoreEventsBackwardsOutcome {
434435
StartOfTimeline,
435436

436437
/// Events have been inserted.
437-
Events {
438-
events: Vec<TimelineEvent>,
439-
timeline_event_diffs: Vec<VectorDiff<TimelineEvent>>,
440-
reached_start: bool,
441-
},
438+
Events { events: Vec<Event>, timeline_event_diffs: Vec<VectorDiff<Event>>, reached_start: bool },
442439

443440
/// The caller must wait for the initial previous-batch token, and retry.
444441
WaitForInitialPrevToken,
@@ -455,9 +452,7 @@ mod private {
455452
use eyeball_im::VectorDiff;
456453
use matrix_sdk_base::{
457454
apply_redaction,
458-
deserialized_responses::{
459-
ThreadSummary, ThreadSummaryStatus, TimelineEvent, TimelineEventKind,
460-
},
455+
deserialized_responses::{ThreadSummary, ThreadSummaryStatus, TimelineEventKind},
461456
event_cache::{store::EventCacheStoreLock, Event, Gap},
462457
linked_chunk::{
463458
lazy_loader, ChunkContent, ChunkIdentifier, ChunkIdentifierGenerator, LinkedChunkId,
@@ -782,7 +777,7 @@ mod private {
782777
#[must_use = "Propagate `VectorDiff` updates via `RoomEventCacheUpdate`"]
783778
pub(crate) async fn auto_shrink_if_no_listeners(
784779
&mut self,
785-
) -> Result<Option<Vec<VectorDiff<TimelineEvent>>>, EventCacheError> {
780+
) -> Result<Option<Vec<VectorDiff<Event>>>, EventCacheError> {
786781
let listener_count = self.listener_count.load(std::sync::atomic::Ordering::SeqCst);
787782

788783
trace!(listener_count, "received request to auto-shrink");
@@ -800,7 +795,7 @@ mod private {
800795
#[cfg(test)]
801796
pub(crate) async fn force_shrink_to_last_chunk(
802797
&mut self,
803-
) -> Result<Vec<VectorDiff<TimelineEvent>>, EventCacheError> {
798+
) -> Result<Vec<VectorDiff<Event>>, EventCacheError> {
804799
self.shrink_to_last_chunk().await?;
805800
Ok(self.events.updates_as_vector_diffs())
806801
}
@@ -824,7 +819,7 @@ mod private {
824819
let _ = closure();
825820
}
826821

827-
fn strip_relations_from_event(ev: &mut TimelineEvent) {
822+
fn strip_relations_from_event(ev: &mut Event) {
828823
match &mut ev.kind {
829824
TimelineEventKind::Decrypted(decrypted) => {
830825
// Remove all information about encryption info for
@@ -843,7 +838,7 @@ mod private {
843838
}
844839

845840
/// Strips the bundled relations from a collection of events.
846-
fn strip_relations_from_events(items: &mut [TimelineEvent]) {
841+
fn strip_relations_from_events(items: &mut [Event]) {
847842
for ev in items.iter_mut() {
848843
Self::strip_relations_from_event(ev);
849844
}
@@ -902,7 +897,7 @@ mod private {
902897

903898
async fn send_updates_to_store(
904899
&mut self,
905-
mut updates: Vec<Update<TimelineEvent, Gap>>,
900+
mut updates: Vec<Update<Event, Gap>>,
906901
) -> Result<(), EventCacheError> {
907902
if updates.is_empty() {
908903
return Ok(());
@@ -956,7 +951,7 @@ mod private {
956951
/// result, the caller may override any pending diff updates
957952
/// with the result of this function.
958953
#[must_use = "Propagate `VectorDiff` updates via `RoomEventCacheUpdate`"]
959-
pub async fn reset(&mut self) -> Result<Vec<VectorDiff<TimelineEvent>>, EventCacheError> {
954+
pub async fn reset(&mut self) -> Result<Vec<VectorDiff<Event>>, EventCacheError> {
960955
self.reset_internal().await?;
961956

962957
let diff_updates = self.events.updates_as_vector_diffs();
@@ -995,7 +990,7 @@ mod private {
995990
pub async fn find_event(
996991
&self,
997992
event_id: &EventId,
998-
) -> Result<Option<(EventLocation, TimelineEvent)>, EventCacheError> {
993+
) -> Result<Option<(EventLocation, Event)>, EventCacheError> {
999994
// There are supposedly fewer events loaded in memory than in the store. Let's
1000995
// start by looking up in the `RoomEvents`.
1001996
for (position, event) in self.events.revents() {
@@ -1021,7 +1016,7 @@ mod private {
10211016
&self,
10221017
event_id: &EventId,
10231018
filters: Option<Vec<RelationType>>,
1024-
) -> Result<Option<(TimelineEvent, Vec<TimelineEvent>)>, EventCacheError> {
1019+
) -> Result<Option<(Event, Vec<Event>)>, EventCacheError> {
10251020
let store = self.store.lock().await?;
10261021

10271022
// First, hit storage to get the target event and its related events.
@@ -1070,7 +1065,7 @@ mod private {
10701065
/// linked chunk.
10711066
async fn post_process_new_events(
10721067
&mut self,
1073-
events: Vec<TimelineEvent>,
1068+
events: Vec<Event>,
10741069
is_live_sync: bool,
10751070
) -> Result<(), EventCacheError> {
10761071
// Update the store before doing the post-processing.
@@ -1170,7 +1165,7 @@ mod private {
11701165
async fn replace_event_at(
11711166
&mut self,
11721167
location: EventLocation,
1173-
event: TimelineEvent,
1168+
event: Event,
11741169
) -> Result<(), EventCacheError> {
11751170
match location {
11761171
EventLocation::Memory(position) => {
@@ -1268,7 +1263,7 @@ mod private {
12681263
/// the event. Instead, an update to the linked chunk must be used.
12691264
pub async fn save_event(
12701265
&self,
1271-
events: impl IntoIterator<Item = TimelineEvent>,
1266+
events: impl IntoIterator<Item = Event>,
12721267
) -> Result<(), EventCacheError> {
12731268
let store = self.store.clone();
12741269
let room_id = self.room.clone();
@@ -1299,7 +1294,7 @@ mod private {
12991294
pub async fn handle_sync(
13001295
&mut self,
13011296
mut timeline: Timeline,
1302-
) -> Result<(bool, Vec<VectorDiff<TimelineEvent>>), EventCacheError> {
1297+
) -> Result<(bool, Vec<VectorDiff<Event>>), EventCacheError> {
13031298
let mut prev_batch = timeline.prev_batch.take();
13041299

13051300
let (
@@ -1386,11 +1381,10 @@ mod private {
13861381
#[must_use = "Propagate `VectorDiff` updates via `RoomEventCacheUpdate`"]
13871382
pub async fn handle_backpagination(
13881383
&mut self,
1389-
events: Vec<TimelineEvent>,
1384+
events: Vec<Event>,
13901385
mut new_gap: Option<Gap>,
13911386
prev_gap_id: Option<ChunkIdentifier>,
1392-
) -> Result<(BackPaginationOutcome, Vec<VectorDiff<TimelineEvent>>), EventCacheError>
1393-
{
1387+
) -> Result<(BackPaginationOutcome, Vec<VectorDiff<Event>>), EventCacheError> {
13941388
// If there's no new gap (previous batch token), then we've reached the start of
13951389
// the timeline.
13961390
let network_reached_start = new_gap.is_none();
@@ -1535,8 +1529,7 @@ pub(super) use private::RoomEventCacheState;
15351529

15361530
#[cfg(test)]
15371531
mod tests {
1538-
1539-
use matrix_sdk_common::deserialized_responses::TimelineEvent;
1532+
use matrix_sdk_base::event_cache::Event;
15401533
use matrix_sdk_test::{async_test, event_factory::EventFactory};
15411534
use ruma::{
15421535
event_id,
@@ -1749,8 +1742,8 @@ mod tests {
17491742

17501743
async fn assert_relations(
17511744
room_id: &RoomId,
1752-
original_event: TimelineEvent,
1753-
related_event: TimelineEvent,
1745+
original_event: Event,
1746+
related_event: Event,
17541747
event_factory: EventFactory,
17551748
) {
17561749
let client = logged_in_client(None).await;

0 commit comments

Comments
 (0)