Skip to content

Commit e217870

Browse files
committed
event cache: internalize handling of the account data
(By moving handling of the fully read marker into the event cache itself.)
1 parent ab9e4f7 commit e217870

File tree

4 files changed

+64
-69
lines changed

4 files changed

+64
-69
lines changed

crates/matrix-sdk-ui/src/timeline/builder.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -185,18 +185,17 @@ impl TimelineBuilder {
185185
inner.clear().await;
186186
}
187187

188-
RoomEventCacheUpdate::Append {
189-
events,
190-
account_data,
191-
ephemeral,
192-
ambiguity_changes,
193-
} => {
188+
RoomEventCacheUpdate::UpdateReadMarker { event_id } => {
189+
trace!(target = %event_id, "Handling fully read marker.");
190+
inner.handle_fully_read_marker(event_id).await;
191+
}
192+
193+
RoomEventCacheUpdate::Append { events, ephemeral, ambiguity_changes } => {
194194
trace!("Received new events");
195195

196-
// TODO: (bnjbvr) account_data and ephemeral should be handled by the
197-
// event cache, and we should replace this with a simple
198-
// `handle_add_events`.
199-
inner.handle_sync_events(events, account_data, ephemeral).await;
196+
// TODO: (bnjbvr) ephemeral should be handled by the event cache, and
197+
// we should replace this with a simple `add_events_at`.
198+
inner.handle_sync_events(events, ephemeral).await;
200199

201200
let member_ambiguity_changes = ambiguity_changes
202201
.values()

crates/matrix-sdk-ui/src/timeline/inner/mod.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ use ruma::{
4141
message::{MessageType, Relation},
4242
redaction::RoomRedactionEventContent,
4343
},
44-
AnyMessageLikeEventContent, AnyRoomAccountDataEvent, AnySyncEphemeralRoomEvent,
45-
AnySyncMessageLikeEvent, AnySyncTimelineEvent, MessageLikeEventType,
44+
AnyMessageLikeEventContent, AnySyncEphemeralRoomEvent, AnySyncMessageLikeEvent,
45+
AnySyncTimelineEvent, MessageLikeEventType,
4646
},
4747
serde::Raw,
4848
EventId, OwnedEventId, OwnedTransactionId, RoomVersionId, TransactionId, UserId,
@@ -426,22 +426,17 @@ impl<P: RoomDataProvider> TimelineInner<P> {
426426
self.state.write().await.clear();
427427
}
428428

429+
pub(super) async fn handle_fully_read_marker(&self, fully_read_event_id: OwnedEventId) {
430+
self.state.write().await.handle_fully_read_marker(fully_read_event_id);
431+
}
432+
429433
pub(super) async fn handle_sync_events(
430434
&self,
431435
events: Vec<SyncTimelineEvent>,
432-
account_data: Vec<Raw<AnyRoomAccountDataEvent>>,
433436
ephemeral: Vec<Raw<AnySyncEphemeralRoomEvent>>,
434437
) {
435438
let mut state = self.state.write().await;
436-
state
437-
.handle_sync_events(
438-
events,
439-
account_data,
440-
ephemeral,
441-
&self.room_data_provider,
442-
&self.settings,
443-
)
444-
.await;
439+
state.handle_sync_events(events, ephemeral, &self.room_data_provider, &self.settings).await;
445440
}
446441

447442
#[cfg(test)]

crates/matrix-sdk-ui/src/timeline/inner/state.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use ruma::events::receipt::ReceiptEventContent;
2323
use ruma::{
2424
events::{
2525
relation::Annotation, room::redaction::RoomRedactionEventContent,
26-
AnyMessageLikeEventContent, AnyRoomAccountDataEvent, AnySyncEphemeralRoomEvent,
26+
AnyMessageLikeEventContent, AnySyncEphemeralRoomEvent,
2727
},
2828
push::Action,
2929
serde::Raw,
@@ -109,11 +109,18 @@ impl TimelineInnerState {
109109
handle_many_res
110110
}
111111

112+
/// Marks the given event as fully read, using the read marker received from
113+
/// sync.
114+
pub(super) fn handle_fully_read_marker(&mut self, fully_read_event_id: OwnedEventId) {
115+
let mut txn = self.transaction();
116+
txn.set_fully_read_event(fully_read_event_id);
117+
txn.commit();
118+
}
119+
112120
#[instrument(skip_all)]
113121
pub(super) async fn handle_sync_events<P: RoomDataProvider>(
114122
&mut self,
115123
events: Vec<SyncTimelineEvent>,
116-
account_data: Vec<Raw<AnyRoomAccountDataEvent>>,
117124
ephemeral: Vec<Raw<AnySyncEphemeralRoomEvent>>,
118125
room_data_provider: &P,
119126
settings: &TimelineInnerSettings,
@@ -128,20 +135,6 @@ impl TimelineInnerState {
128135
)
129136
.await;
130137

131-
trace!("Handling account data");
132-
for raw_event in account_data {
133-
match raw_event.deserialize() {
134-
Ok(AnyRoomAccountDataEvent::FullyRead(ev)) => {
135-
txn.set_fully_read_event(ev.content.event_id);
136-
}
137-
Ok(_) => {}
138-
Err(e) => {
139-
let event_type = raw_event.get_field::<String>("type").ok().flatten();
140-
warn!(event_type, "Failed to deserialize account data: {e}");
141-
}
142-
}
143-
}
144-
145138
if !ephemeral.is_empty() {
146139
trace!("Handling ephemeral room events");
147140
let own_user_id = room_data_provider.own_user_id();

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

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,7 @@ impl EventCache {
263263

264264
room_cache
265265
.inner
266-
.append_events(
267-
&**store,
268-
events,
269-
prev_batch,
270-
Default::default(),
271-
Default::default(),
272-
Default::default(),
273-
)
266+
.append_events(&**store, events, prev_batch, Default::default(), Default::default())
274267
.await?;
275268

276269
Ok(())
@@ -477,6 +470,30 @@ impl RoomEventCacheInner {
477470
}
478471
}
479472

473+
fn handle_account_data(&self, account_data: Vec<Raw<AnyRoomAccountDataEvent>>) {
474+
trace!("Handling account data");
475+
for raw_event in account_data {
476+
match raw_event.deserialize() {
477+
Ok(AnyRoomAccountDataEvent::FullyRead(ev)) => {
478+
// Propagate to observers. We ignore the fact there aren't any.
479+
let _ = self.sender.send(RoomEventCacheUpdate::UpdateReadMarker {
480+
event_id: ev.content.event_id,
481+
});
482+
}
483+
484+
Ok(_) => {
485+
// We're not interested in other room account data updates,
486+
// at this point.
487+
}
488+
489+
Err(e) => {
490+
let event_type = raw_event.get_field::<String>("type").ok().flatten();
491+
warn!(event_type, "Failed to deserialize account data: {e}");
492+
}
493+
}
494+
}
495+
}
496+
480497
async fn handle_joined_room_update(
481498
&self,
482499
store: &dyn EventCacheStore,
@@ -486,10 +503,12 @@ impl RoomEventCacheInner {
486503
store,
487504
updates.timeline,
488505
updates.ephemeral.clone(),
489-
updates.account_data,
490506
updates.ambiguity_changes,
491507
)
492508
.await?;
509+
510+
self.handle_account_data(updates.account_data);
511+
493512
Ok(())
494513
}
495514

@@ -498,7 +517,6 @@ impl RoomEventCacheInner {
498517
store: &dyn EventCacheStore,
499518
timeline: Timeline,
500519
ephemeral: Vec<Raw<AnySyncEphemeralRoomEvent>>,
501-
account_data: Vec<Raw<AnyRoomAccountDataEvent>>,
502520
ambiguity_changes: BTreeMap<OwnedEventId, AmbiguityChange>,
503521
) -> Result<()> {
504522
if timeline.limited {
@@ -510,7 +528,7 @@ impl RoomEventCacheInner {
510528
// Clear internal state (events, pagination tokens, etc.).
511529
store.clear_room(self.room.room_id()).await?;
512530

513-
// Propagate to observers.
531+
// Propagate to observers. We ignore the fact there aren't any.
514532
let _ = self.sender.send(RoomEventCacheUpdate::Clear);
515533
}
516534

@@ -520,7 +538,6 @@ impl RoomEventCacheInner {
520538
store,
521539
timeline.events,
522540
timeline.prev_batch,
523-
account_data,
524541
ephemeral,
525542
ambiguity_changes,
526543
)
@@ -534,14 +551,8 @@ impl RoomEventCacheInner {
534551
store: &dyn EventCacheStore,
535552
updates: LeftRoomUpdate,
536553
) -> Result<()> {
537-
self.handle_timeline(
538-
store,
539-
updates.timeline,
540-
Vec::new(),
541-
Vec::new(),
542-
updates.ambiguity_changes,
543-
)
544-
.await?;
554+
self.handle_timeline(store, updates.timeline, Vec::new(), updates.ambiguity_changes)
555+
.await?;
545556
Ok(())
546557
}
547558

@@ -552,14 +563,12 @@ impl RoomEventCacheInner {
552563
store: &dyn EventCacheStore,
553564
events: Vec<SyncTimelineEvent>,
554565
prev_batch: Option<String>,
555-
account_data: Vec<Raw<AnyRoomAccountDataEvent>>,
556566
ephemeral: Vec<Raw<AnySyncEphemeralRoomEvent>>,
557567
ambiguity_changes: BTreeMap<OwnedEventId, AmbiguityChange>,
558568
) -> Result<()> {
559569
if events.is_empty()
560570
&& prev_batch.is_none()
561571
&& ephemeral.is_empty()
562-
&& account_data.is_empty()
563572
&& ambiguity_changes.is_empty()
564573
{
565574
return Ok(());
@@ -585,12 +594,8 @@ impl RoomEventCacheInner {
585594
self.pagination_token_notifier.notify_one();
586595
}
587596

588-
let _ = self.sender.send(RoomEventCacheUpdate::Append {
589-
events,
590-
account_data,
591-
ephemeral,
592-
ambiguity_changes,
593-
});
597+
let _ =
598+
self.sender.send(RoomEventCacheUpdate::Append { events, ephemeral, ambiguity_changes });
594599

595600
Ok(())
596601
}
@@ -738,13 +743,16 @@ pub enum RoomEventCacheUpdate {
738743
/// The room has been cleared from events.
739744
Clear,
740745

746+
/// The fully read marker has moved to a different event.
747+
UpdateReadMarker {
748+
/// Event at which the read marker has been moved.
749+
event_id: OwnedEventId,
750+
},
751+
741752
/// The room has new events.
742753
Append {
743754
/// All the new events that have been added to the room's timeline.
744755
events: Vec<SyncTimelineEvent>,
745-
/// XXX: this is temporary, until account data lives in the event cache
746-
/// — or will it live there?
747-
account_data: Vec<Raw<AnyRoomAccountDataEvent>>,
748756
/// XXX: this is temporary, until read receipts are handled in the event
749757
/// cache
750758
ephemeral: Vec<Raw<AnySyncEphemeralRoomEvent>>,

0 commit comments

Comments
 (0)