matrix_sdk/event_cache/room/
mod.rs

1// Copyright 2024 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! All event cache types for a single room.
16
17use std::{
18    collections::BTreeMap,
19    fmt,
20    ops::{Deref, DerefMut},
21    sync::{
22        Arc,
23        atomic::{AtomicUsize, Ordering},
24    },
25};
26
27use events::sort_positions_descending;
28use eyeball::SharedObservable;
29use eyeball_im::VectorDiff;
30use matrix_sdk_base::{
31    deserialized_responses::AmbiguityChange,
32    event_cache::Event,
33    linked_chunk::Position,
34    sync::{JoinedRoomUpdate, LeftRoomUpdate, Timeline},
35};
36use ruma::{
37    EventId, OwnedEventId, OwnedRoomId,
38    api::Direction,
39    events::{AnyRoomAccountDataEvent, AnySyncEphemeralRoomEvent, relation::RelationType},
40    serde::Raw,
41};
42use tokio::sync::{
43    Notify,
44    broadcast::{Receiver, Sender},
45    mpsc,
46};
47use tracing::{instrument, trace, warn};
48
49use super::{
50    AutoShrinkChannelPayload, EventsOrigin, Result, RoomEventCacheGenericUpdate,
51    RoomEventCacheUpdate, RoomPagination, RoomPaginationStatus,
52};
53use crate::{
54    client::WeakClient,
55    event_cache::EventCacheError,
56    room::{IncludeRelations, RelationsOptions, WeakRoom},
57};
58
59pub(super) mod events;
60mod threads;
61
62pub use threads::ThreadEventCacheUpdate;
63
64/// A subset of an event cache, for a room.
65///
66/// Cloning is shallow, and thus is cheap to do.
67#[derive(Clone)]
68pub struct RoomEventCache {
69    pub(super) inner: Arc<RoomEventCacheInner>,
70}
71
72impl fmt::Debug for RoomEventCache {
73    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74        f.debug_struct("RoomEventCache").finish_non_exhaustive()
75    }
76}
77
78/// Thin wrapper for a room event cache subscriber, so as to trigger
79/// side-effects when all subscribers are gone.
80///
81/// The current side-effect is: auto-shrinking the [`RoomEventCache`] when no
82/// more subscribers are active. This is an optimisation to reduce the number of
83/// data held in memory by a [`RoomEventCache`]: when no more subscribers are
84/// active, all data are reduced to the minimum.
85///
86/// The side-effect takes effect on `Drop`.
87#[allow(missing_debug_implementations)]
88pub struct RoomEventCacheSubscriber {
89    /// Underlying receiver of the room event cache's updates.
90    recv: Receiver<RoomEventCacheUpdate>,
91
92    /// To which room are we listening?
93    room_id: OwnedRoomId,
94
95    /// Sender to the auto-shrink channel.
96    auto_shrink_sender: mpsc::Sender<AutoShrinkChannelPayload>,
97
98    /// Shared instance of the auto-shrinker.
99    subscriber_count: Arc<AtomicUsize>,
100}
101
102impl Drop for RoomEventCacheSubscriber {
103    fn drop(&mut self) {
104        let previous_subscriber_count = self.subscriber_count.fetch_sub(1, Ordering::SeqCst);
105
106        trace!(
107            "dropping a room event cache subscriber; previous count: {previous_subscriber_count}"
108        );
109
110        if previous_subscriber_count == 1 {
111            // We were the last instance of the subscriber; let the auto-shrinker know by
112            // notifying it of our room id.
113
114            let mut room_id = self.room_id.clone();
115
116            // Try to send without waiting for channel capacity, and restart in a spin-loop
117            // if it failed (until a maximum number of attempts is reached, or
118            // the send was successful). The channel shouldn't be super busy in
119            // general, so this should resolve quickly enough.
120
121            let mut num_attempts = 0;
122
123            while let Err(err) = self.auto_shrink_sender.try_send(room_id) {
124                num_attempts += 1;
125
126                if num_attempts > 1024 {
127                    // If we've tried too many times, just give up with a warning; after all, this
128                    // is only an optimization.
129                    warn!(
130                        "couldn't send notification to the auto-shrink channel \
131                         after 1024 attempts; giving up"
132                    );
133                    return;
134                }
135
136                match err {
137                    mpsc::error::TrySendError::Full(stolen_room_id) => {
138                        room_id = stolen_room_id;
139                    }
140                    mpsc::error::TrySendError::Closed(_) => return,
141                }
142            }
143
144            trace!("sent notification to the parent channel that we were the last subscriber");
145        }
146    }
147}
148
149impl Deref for RoomEventCacheSubscriber {
150    type Target = Receiver<RoomEventCacheUpdate>;
151
152    fn deref(&self) -> &Self::Target {
153        &self.recv
154    }
155}
156
157impl DerefMut for RoomEventCacheSubscriber {
158    fn deref_mut(&mut self) -> &mut Self::Target {
159        &mut self.recv
160    }
161}
162
163impl RoomEventCache {
164    /// Create a new [`RoomEventCache`] using the given room and store.
165    pub(super) fn new(
166        client: WeakClient,
167        state: RoomEventCacheStateLock,
168        pagination_status: SharedObservable<RoomPaginationStatus>,
169        room_id: OwnedRoomId,
170        auto_shrink_sender: mpsc::Sender<AutoShrinkChannelPayload>,
171        update_sender: Sender<RoomEventCacheUpdate>,
172        generic_update_sender: Sender<RoomEventCacheGenericUpdate>,
173    ) -> Self {
174        Self {
175            inner: Arc::new(RoomEventCacheInner::new(
176                client,
177                state,
178                pagination_status,
179                room_id,
180                auto_shrink_sender,
181                update_sender,
182                generic_update_sender,
183            )),
184        }
185    }
186
187    /// Read all current events.
188    ///
189    /// Use [`RoomEventCache::subscribe`] to get all current events, plus a
190    /// subscriber.
191    pub async fn events(&self) -> Result<Vec<Event>> {
192        let state = self.inner.state.read().await?;
193
194        Ok(state.room_linked_chunk().events().map(|(_position, item)| item.clone()).collect())
195    }
196
197    /// Subscribe to this room updates, after getting the initial list of
198    /// events.
199    ///
200    /// Use [`RoomEventCache::events`] to get all current events without the
201    /// subscriber. Creating, and especially dropping, a
202    /// [`RoomEventCacheSubscriber`] isn't free, as it triggers side-effects.
203    pub async fn subscribe(&self) -> Result<(Vec<Event>, RoomEventCacheSubscriber)> {
204        let state = self.inner.state.read().await?;
205        let events =
206            state.room_linked_chunk().events().map(|(_position, item)| item.clone()).collect();
207
208        let subscriber_count = state.subscriber_count();
209        let previous_subscriber_count = subscriber_count.fetch_add(1, Ordering::SeqCst);
210        trace!("added a room event cache subscriber; new count: {}", previous_subscriber_count + 1);
211
212        let recv = self.inner.update_sender.subscribe();
213        let subscriber = RoomEventCacheSubscriber {
214            recv,
215            room_id: self.inner.room_id.clone(),
216            auto_shrink_sender: self.inner.auto_shrink_sender.clone(),
217            subscriber_count: subscriber_count.clone(),
218        };
219
220        Ok((events, subscriber))
221    }
222
223    /// Subscribe to thread for a given root event, and get a (maybe empty)
224    /// initially known list of events for that thread.
225    pub async fn subscribe_to_thread(
226        &self,
227        thread_root: OwnedEventId,
228    ) -> Result<(Vec<Event>, Receiver<ThreadEventCacheUpdate>)> {
229        let mut state = self.inner.state.write().await?;
230        Ok(state.subscribe_to_thread(thread_root))
231    }
232
233    /// Paginate backwards in a thread, given its root event ID.
234    ///
235    /// Returns whether we've hit the start of the thread, in which case the
236    /// root event will be prepended to the thread.
237    #[instrument(skip(self), fields(room_id = %self.inner.room_id))]
238    pub async fn paginate_thread_backwards(
239        &self,
240        thread_root: OwnedEventId,
241        num_events: u16,
242    ) -> Result<bool> {
243        let room = self.inner.weak_room.get().ok_or(EventCacheError::ClientDropped)?;
244
245        // Take the lock only for a short time here.
246        let mut outcome =
247            self.inner.state.write().await?.load_more_thread_events_backwards(thread_root.clone());
248
249        loop {
250            match outcome {
251                LoadMoreEventsBackwardsOutcome::Gap { prev_token } => {
252                    // Start a threaded pagination from this gap.
253                    let options = RelationsOptions {
254                        from: prev_token.clone(),
255                        dir: Direction::Backward,
256                        limit: Some(num_events.into()),
257                        include_relations: IncludeRelations::AllRelations,
258                        recurse: true,
259                    };
260
261                    let mut result = room
262                        .relations(thread_root.clone(), options)
263                        .await
264                        .map_err(|err| EventCacheError::BackpaginationError(Box::new(err)))?;
265
266                    let reached_start = result.next_batch_token.is_none();
267                    trace!(num_events = result.chunk.len(), %reached_start, "received a /relations response");
268
269                    // Because the state lock is taken again in `load_or_fetch_event`, we need
270                    // to do this *before* we take the state lock again.
271                    let root_event =
272                        if reached_start {
273                            // Prepend the thread root event to the results.
274                            Some(room.load_or_fetch_event(&thread_root, None).await.map_err(
275                                |err| EventCacheError::BackpaginationError(Box::new(err)),
276                            )?)
277                        } else {
278                            None
279                        };
280
281                    let mut state = self.inner.state.write().await?;
282
283                    // Save all the events (but the thread root) in the store.
284                    state.save_events(result.chunk.iter().cloned()).await?;
285
286                    // Note: the events are still in the reversed order at this point, so
287                    // pushing will eventually make it so that the root event is the first.
288                    result.chunk.extend(root_event);
289
290                    if let Some(outcome) = state.finish_thread_network_pagination(
291                        thread_root.clone(),
292                        prev_token,
293                        result.next_batch_token,
294                        result.chunk,
295                    ) {
296                        return Ok(outcome.reached_start);
297                    }
298
299                    // fallthrough: restart the pagination.
300                    outcome = state.load_more_thread_events_backwards(thread_root.clone());
301                }
302
303                LoadMoreEventsBackwardsOutcome::StartOfTimeline => {
304                    // We're done!
305                    return Ok(true);
306                }
307
308                LoadMoreEventsBackwardsOutcome::Events { .. } => {
309                    // TODO: implement :)
310                    unimplemented!("loading from disk for threads is not implemented yet");
311                }
312            }
313        }
314    }
315
316    /// Return a [`RoomPagination`] API object useful for running
317    /// back-pagination queries in the current room.
318    pub fn pagination(&self) -> RoomPagination {
319        RoomPagination { inner: self.inner.clone() }
320    }
321
322    /// Try to find a single event in this room, starting from the most recent
323    /// event.
324    ///
325    /// The `predicate` receives two arguments: the current event, and the
326    /// ID of the _previous_ (older) event.
327    ///
328    /// **Warning**! It looks into the loaded events from the in-memory linked
329    /// chunk **only**. It doesn't look inside the storage.
330    pub async fn rfind_map_event_in_memory_by<O, P>(&self, predicate: P) -> Result<Option<O>>
331    where
332        P: FnMut(&Event, Option<OwnedEventId>) -> Option<O>,
333    {
334        Ok(self.inner.state.read().await?.rfind_map_event_in_memory_by(predicate))
335    }
336
337    /// Try to find an event by ID in this room.
338    ///
339    /// It starts by looking into loaded events before looking inside the
340    /// storage.
341    pub async fn find_event(&self, event_id: &EventId) -> Result<Option<Event>> {
342        Ok(self
343            .inner
344            .state
345            .read()
346            .await?
347            .find_event(event_id)
348            .await
349            .ok()
350            .flatten()
351            .map(|(_loc, event)| event))
352    }
353
354    /// Try to find an event by ID in this room, along with its related events.
355    ///
356    /// You can filter which types of related events to retrieve using
357    /// `filter`. `None` will retrieve related events of any type.
358    ///
359    /// The related events are sorted like this:
360    ///
361    /// - events saved out-of-band (with `RoomEventCache::save_events`) will be
362    ///   located at the beginning of the array.
363    /// - events present in the linked chunk (be it in memory or in the storage)
364    ///   will be sorted according to their ordering in the linked chunk.
365    pub async fn find_event_with_relations(
366        &self,
367        event_id: &EventId,
368        filter: Option<Vec<RelationType>>,
369    ) -> Result<Option<(Event, Vec<Event>)>> {
370        // Search in all loaded or stored events.
371        Ok(self
372            .inner
373            .state
374            .read()
375            .await?
376            .find_event_with_relations(event_id, filter.clone())
377            .await
378            .ok()
379            .flatten())
380    }
381
382    /// Clear all the storage for this [`RoomEventCache`].
383    ///
384    /// This will get rid of all the events from the linked chunk and persisted
385    /// storage.
386    pub async fn clear(&self) -> Result<()> {
387        // Clear the linked chunk and persisted storage.
388        let updates_as_vector_diffs = self.inner.state.write().await?.reset().await?;
389
390        // Notify observers about the update.
391        let _ = self.inner.update_sender.send(RoomEventCacheUpdate::UpdateTimelineEvents {
392            diffs: updates_as_vector_diffs,
393            origin: EventsOrigin::Cache,
394        });
395
396        // Notify observers about the generic update.
397        let _ = self
398            .inner
399            .generic_update_sender
400            .send(RoomEventCacheGenericUpdate { room_id: self.inner.room_id.clone() });
401
402        Ok(())
403    }
404
405    /// Handle a single event from the `SendQueue`.
406    pub(crate) async fn insert_sent_event_from_send_queue(&self, event: Event) -> Result<()> {
407        self.inner
408            .handle_timeline(
409                Timeline { limited: false, prev_batch: None, events: vec![event] },
410                Vec::new(),
411                BTreeMap::new(),
412            )
413            .await
414    }
415
416    /// Save some events in the event cache, for further retrieval with
417    /// [`Self::event`].
418    pub(crate) async fn save_events(&self, events: impl IntoIterator<Item = Event>) {
419        match self.inner.state.write().await {
420            Ok(mut state_guard) => {
421                if let Err(err) = state_guard.save_events(events).await {
422                    warn!("couldn't save event in the event cache: {err}");
423                }
424            }
425
426            Err(err) => {
427                warn!("couldn't save event in the event cache: {err}");
428            }
429        }
430    }
431
432    /// Return a nice debug string (a vector of lines) for the linked chunk of
433    /// events for this room.
434    pub async fn debug_string(&self) -> Vec<String> {
435        match self.inner.state.read().await {
436            Ok(read_guard) => read_guard.room_linked_chunk().debug_string(),
437            Err(err) => {
438                warn!(?err, "Failed to obtain the read guard for the `RoomEventCache`");
439
440                vec![]
441            }
442        }
443    }
444}
445
446/// The (non-cloneable) details of the `RoomEventCache`.
447pub(super) struct RoomEventCacheInner {
448    /// The room id for this room.
449    pub(super) room_id: OwnedRoomId,
450
451    pub weak_room: WeakRoom,
452
453    /// State for this room's event cache.
454    pub state: RoomEventCacheStateLock,
455
456    /// A notifier that we received a new pagination token.
457    pub pagination_batch_token_notifier: Notify,
458
459    pub pagination_status: SharedObservable<RoomPaginationStatus>,
460
461    /// Sender to the auto-shrink channel.
462    ///
463    /// See doc comment around [`EventCache::auto_shrink_linked_chunk_task`] for
464    /// more details.
465    auto_shrink_sender: mpsc::Sender<AutoShrinkChannelPayload>,
466
467    /// Sender part for update subscribers to this room.
468    pub update_sender: Sender<RoomEventCacheUpdate>,
469
470    /// A clone of [`EventCacheInner::generic_update_sender`].
471    ///
472    /// Whilst `EventCacheInner` handles the generic updates from the sync, or
473    /// the storage, it doesn't handle the update from pagination. Having a
474    /// clone here allows to access it from [`RoomPagination`].
475    pub(super) generic_update_sender: Sender<RoomEventCacheGenericUpdate>,
476}
477
478impl RoomEventCacheInner {
479    /// Creates a new cache for a room, and subscribes to room updates, so as
480    /// to handle new timeline events.
481    fn new(
482        client: WeakClient,
483        state: RoomEventCacheStateLock,
484        pagination_status: SharedObservable<RoomPaginationStatus>,
485        room_id: OwnedRoomId,
486        auto_shrink_sender: mpsc::Sender<AutoShrinkChannelPayload>,
487        update_sender: Sender<RoomEventCacheUpdate>,
488        generic_update_sender: Sender<RoomEventCacheGenericUpdate>,
489    ) -> Self {
490        let weak_room = WeakRoom::new(client, room_id);
491
492        Self {
493            room_id: weak_room.room_id().to_owned(),
494            weak_room,
495            state,
496            update_sender,
497            pagination_batch_token_notifier: Default::default(),
498            auto_shrink_sender,
499            pagination_status,
500            generic_update_sender,
501        }
502    }
503
504    fn handle_account_data(&self, account_data: Vec<Raw<AnyRoomAccountDataEvent>>) {
505        if account_data.is_empty() {
506            return;
507        }
508
509        let mut handled_read_marker = false;
510
511        trace!("Handling account data");
512
513        for raw_event in account_data {
514            match raw_event.deserialize() {
515                Ok(AnyRoomAccountDataEvent::FullyRead(ev)) => {
516                    // If duplicated, do not forward read marker multiple times
517                    // to avoid clutter the update channel.
518                    if handled_read_marker {
519                        continue;
520                    }
521
522                    handled_read_marker = true;
523
524                    // Propagate to observers. (We ignore the error if there aren't any.)
525                    let _ = self.update_sender.send(RoomEventCacheUpdate::MoveReadMarkerTo {
526                        event_id: ev.content.event_id,
527                    });
528                }
529
530                Ok(_) => {
531                    // We're not interested in other room account data updates,
532                    // at this point.
533                }
534
535                Err(e) => {
536                    let event_type = raw_event.get_field::<String>("type").ok().flatten();
537                    warn!(event_type, "Failed to deserialize account data: {e}");
538                }
539            }
540        }
541    }
542
543    #[instrument(skip_all, fields(room_id = %self.room_id))]
544    pub(super) async fn handle_joined_room_update(&self, updates: JoinedRoomUpdate) -> Result<()> {
545        self.handle_timeline(
546            updates.timeline,
547            updates.ephemeral.clone(),
548            updates.ambiguity_changes,
549        )
550        .await?;
551        self.handle_account_data(updates.account_data);
552
553        Ok(())
554    }
555
556    #[instrument(skip_all, fields(room_id = %self.room_id))]
557    pub(super) async fn handle_left_room_update(&self, updates: LeftRoomUpdate) -> Result<()> {
558        self.handle_timeline(updates.timeline, Vec::new(), updates.ambiguity_changes).await?;
559
560        Ok(())
561    }
562
563    /// Handle a [`Timeline`], i.e. new events received by a sync for this
564    /// room.
565    async fn handle_timeline(
566        &self,
567        timeline: Timeline,
568        ephemeral_events: Vec<Raw<AnySyncEphemeralRoomEvent>>,
569        ambiguity_changes: BTreeMap<OwnedEventId, AmbiguityChange>,
570    ) -> Result<()> {
571        if timeline.events.is_empty()
572            && timeline.prev_batch.is_none()
573            && ephemeral_events.is_empty()
574            && ambiguity_changes.is_empty()
575        {
576            return Ok(());
577        }
578
579        // Add all the events to the backend.
580        trace!("adding new events");
581
582        let (stored_prev_batch_token, timeline_event_diffs) =
583            self.state.write().await?.handle_sync(timeline).await?;
584
585        // Now that all events have been added, we can trigger the
586        // `pagination_token_notifier`.
587        if stored_prev_batch_token {
588            self.pagination_batch_token_notifier.notify_one();
589        }
590
591        // The order matters here: first send the timeline event diffs, then only the
592        // related events (read receipts, etc.).
593        if !timeline_event_diffs.is_empty() {
594            let _ = self.update_sender.send(RoomEventCacheUpdate::UpdateTimelineEvents {
595                diffs: timeline_event_diffs,
596                origin: EventsOrigin::Sync,
597            });
598
599            let _ = self
600                .generic_update_sender
601                .send(RoomEventCacheGenericUpdate { room_id: self.room_id.clone() });
602        }
603
604        if !ephemeral_events.is_empty() {
605            let _ = self
606                .update_sender
607                .send(RoomEventCacheUpdate::AddEphemeralEvents { events: ephemeral_events });
608        }
609
610        if !ambiguity_changes.is_empty() {
611            let _ =
612                self.update_sender.send(RoomEventCacheUpdate::UpdateMembers { ambiguity_changes });
613        }
614
615        Ok(())
616    }
617}
618
619/// Internal type to represent the output of
620/// [`RoomEventCacheState::load_more_events_backwards`].
621#[derive(Debug)]
622pub(super) enum LoadMoreEventsBackwardsOutcome {
623    /// A gap has been inserted.
624    Gap {
625        /// The previous batch token to be used as the "end" parameter in the
626        /// back-pagination request.
627        prev_token: Option<String>,
628    },
629
630    /// The start of the timeline has been reached.
631    StartOfTimeline,
632
633    /// Events have been inserted.
634    Events { events: Vec<Event>, timeline_event_diffs: Vec<VectorDiff<Event>>, reached_start: bool },
635}
636
637// Use a private module to hide `events` to this parent module.
638mod private {
639    use std::{
640        collections::{BTreeMap, HashMap, HashSet},
641        sync::{
642            Arc,
643            atomic::{AtomicBool, AtomicUsize, Ordering},
644        },
645    };
646
647    use eyeball::SharedObservable;
648    use eyeball_im::VectorDiff;
649    use itertools::Itertools;
650    use matrix_sdk_base::{
651        apply_redaction,
652        deserialized_responses::{ThreadSummary, ThreadSummaryStatus, TimelineEventKind},
653        event_cache::{
654            Event, Gap,
655            store::{EventCacheStoreLock, EventCacheStoreLockGuard, EventCacheStoreLockState},
656        },
657        linked_chunk::{
658            ChunkContent, ChunkIdentifierGenerator, ChunkMetadata, LinkedChunkId,
659            OwnedLinkedChunkId, Position, Update, lazy_loader,
660        },
661        serde_helpers::{extract_edit_target, extract_thread_root},
662        sync::Timeline,
663    };
664    use matrix_sdk_common::executor::spawn;
665    use ruma::{
666        EventId, OwnedEventId, OwnedRoomId, RoomId,
667        events::{
668            AnySyncMessageLikeEvent, AnySyncTimelineEvent, MessageLikeEventType,
669            relation::RelationType, room::redaction::SyncRoomRedactionEvent,
670        },
671        room_version_rules::RoomVersionRules,
672        serde::Raw,
673    };
674    use tokio::sync::{
675        Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard,
676        broadcast::{Receiver, Sender},
677    };
678    use tracing::{debug, error, instrument, trace, warn};
679
680    use super::{
681        super::{
682            BackPaginationOutcome, EventCacheError, RoomEventCacheLinkedChunkUpdate,
683            RoomPaginationStatus, ThreadEventCacheUpdate,
684            deduplicator::{DeduplicationOutcome, filter_duplicate_events},
685            room::threads::ThreadEventCache,
686        },
687        EventLocation, EventsOrigin, LoadMoreEventsBackwardsOutcome, RoomEventCacheGenericUpdate,
688        RoomEventCacheUpdate,
689        events::EventLinkedChunk,
690        sort_positions_descending,
691    };
692
693    /// State for a single room's event cache.
694    ///
695    /// This contains all the inner mutable states that ought to be updated at
696    /// the same time.
697    pub struct RoomEventCacheStateLock {
698        /// The per-thread lock around the real state.
699        locked_state: RwLock<RoomEventCacheStateLockInner>,
700
701        /// Please see inline comment of [`Self::read`] to understand why it
702        /// exists.
703        read_lock_acquisition: Mutex<()>,
704    }
705
706    struct RoomEventCacheStateLockInner {
707        /// Whether thread support has been enabled for the event cache.
708        enabled_thread_support: bool,
709
710        /// The room this state relates to.
711        room_id: OwnedRoomId,
712
713        /// Reference to the underlying backing store.
714        store: EventCacheStoreLock,
715
716        /// The loaded events for the current room, that is, the in-memory
717        /// linked chunk for this room.
718        room_linked_chunk: EventLinkedChunk,
719
720        /// Threads present in this room.
721        ///
722        /// Keyed by the thread root event ID.
723        threads: HashMap<OwnedEventId, ThreadEventCache>,
724
725        pagination_status: SharedObservable<RoomPaginationStatus>,
726
727        /// A clone of [`super::RoomEventCacheInner::update_sender`].
728        ///
729        /// This is used only by the [`RoomEventCacheStateLock::read`] and
730        /// [`RoomEventCacheStateLock::write`] when the state must be reset.
731        update_sender: Sender<RoomEventCacheUpdate>,
732
733        /// A clone of [`super::super::EventCacheInner::generic_update_sender`].
734        ///
735        /// This is used only by the [`RoomEventCacheStateLock::read`] and
736        /// [`RoomEventCacheStateLock::write`] when the state must be reset.
737        generic_update_sender: Sender<RoomEventCacheGenericUpdate>,
738
739        /// A clone of
740        /// [`super::super::EventCacheInner::linked_chunk_update_sender`].
741        linked_chunk_update_sender: Sender<RoomEventCacheLinkedChunkUpdate>,
742
743        /// The rules for the version of this room.
744        room_version_rules: RoomVersionRules,
745
746        /// Have we ever waited for a previous-batch-token to come from sync, in
747        /// the context of pagination? We do this at most once per room,
748        /// the first time we try to run backward pagination. We reset
749        /// that upon clearing the timeline events.
750        waited_for_initial_prev_token: Arc<AtomicBool>,
751
752        /// An atomic count of the current number of subscriber of the
753        /// [`super::RoomEventCache`].
754        subscriber_count: Arc<AtomicUsize>,
755    }
756
757    impl RoomEventCacheStateLock {
758        /// Create a new state, or reload it from storage if it's been enabled.
759        ///
760        /// Not all events are going to be loaded. Only a portion of them. The
761        /// [`EventLinkedChunk`] relies on a [`LinkedChunk`] to store all
762        /// events. Only the last chunk will be loaded. It means the
763        /// events are loaded from the most recent to the oldest. To
764        /// load more events, see [`RoomPagination`].
765        ///
766        /// [`LinkedChunk`]: matrix_sdk_common::linked_chunk::LinkedChunk
767        /// [`RoomPagination`]: super::RoomPagination
768        #[allow(clippy::too_many_arguments)]
769        pub async fn new(
770            room_id: OwnedRoomId,
771            room_version_rules: RoomVersionRules,
772            enabled_thread_support: bool,
773            update_sender: Sender<RoomEventCacheUpdate>,
774            generic_update_sender: Sender<RoomEventCacheGenericUpdate>,
775            linked_chunk_update_sender: Sender<RoomEventCacheLinkedChunkUpdate>,
776            store: EventCacheStoreLock,
777            pagination_status: SharedObservable<RoomPaginationStatus>,
778        ) -> Result<Self, EventCacheError> {
779            let store_guard = match store.lock().await? {
780                // Lock is clean: all good!
781                EventCacheStoreLockState::Clean(guard) => guard,
782
783                // Lock is dirty, not a problem, it's the first time we are creating this state, no
784                // need to refresh.
785                EventCacheStoreLockState::Dirty(guard) => {
786                    EventCacheStoreLockGuard::clear_dirty(&guard);
787
788                    guard
789                }
790            };
791
792            let linked_chunk_id = LinkedChunkId::Room(&room_id);
793
794            // Load the full linked chunk's metadata, so as to feed the order tracker.
795            //
796            // If loading the full linked chunk failed, we'll clear the event cache, as it
797            // indicates that at some point, there's some malformed data.
798            let full_linked_chunk_metadata =
799                match load_linked_chunk_metadata(&store_guard, linked_chunk_id).await {
800                    Ok(metas) => metas,
801                    Err(err) => {
802                        error!(
803                            "error when loading a linked chunk's metadata from the store: {err}"
804                        );
805
806                        // Try to clear storage for this room.
807                        store_guard
808                            .handle_linked_chunk_updates(linked_chunk_id, vec![Update::Clear])
809                            .await?;
810
811                        // Restart with an empty linked chunk.
812                        None
813                    }
814                };
815
816            let linked_chunk = match store_guard
817                .load_last_chunk(linked_chunk_id)
818                .await
819                .map_err(EventCacheError::from)
820                .and_then(|(last_chunk, chunk_identifier_generator)| {
821                    lazy_loader::from_last_chunk(last_chunk, chunk_identifier_generator)
822                        .map_err(EventCacheError::from)
823                }) {
824                Ok(linked_chunk) => linked_chunk,
825                Err(err) => {
826                    error!(
827                        "error when loading a linked chunk's latest chunk from the store: {err}"
828                    );
829
830                    // Try to clear storage for this room.
831                    store_guard
832                        .handle_linked_chunk_updates(linked_chunk_id, vec![Update::Clear])
833                        .await?;
834
835                    None
836                }
837            };
838
839            let waited_for_initial_prev_token = Arc::new(AtomicBool::new(false));
840
841            Ok(Self {
842                locked_state: RwLock::new(RoomEventCacheStateLockInner {
843                    enabled_thread_support,
844                    room_id,
845                    store,
846                    room_linked_chunk: EventLinkedChunk::with_initial_linked_chunk(
847                        linked_chunk,
848                        full_linked_chunk_metadata,
849                    ),
850                    // The threads mapping is intentionally empty at start, since we're going to
851                    // reload threads lazily, as soon as we need to (based on external
852                    // subscribers) or when we get new information about those (from
853                    // sync).
854                    threads: HashMap::new(),
855                    pagination_status,
856                    update_sender,
857                    generic_update_sender,
858                    linked_chunk_update_sender,
859                    room_version_rules,
860                    waited_for_initial_prev_token,
861                    subscriber_count: Default::default(),
862                }),
863                read_lock_acquisition: Mutex::new(()),
864            })
865        }
866
867        /// Lock this [`RoomEventCacheStateLock`] with per-thread shared access.
868        ///
869        /// This method locks the per-thread lock over the state, and then locks
870        /// the cross-process lock over the store. It returns an RAII guard
871        /// which will drop the read access to the state and to the store when
872        /// dropped.
873        ///
874        /// If the cross-process lock over the store is dirty (see
875        /// [`EventCacheStoreLockState`]), the state is reset to the last chunk.
876        pub async fn read(&self) -> Result<RoomEventCacheStateLockReadGuard<'_>, EventCacheError> {
877            // Only one call at a time to `read` is allowed.
878            //
879            // Why? Because in case the cross-process lock over the store is dirty, we need
880            // to upgrade the read lock over the state to a write lock.
881            //
882            // ## Upgradable read lock
883            //
884            // One may argue that this upgrades can be done with an _upgradable read lock_
885            // [^1] [^2]. We don't want to use this solution: an upgradable read lock is
886            // basically a mutex because we are losing the shared access property, i.e.
887            // having multiple read locks at the same time. This is an important property to
888            // hold for performance concerns.
889            //
890            // ## Downgradable write lock
891            //
892            // One may also argue we could first obtain a write lock over the state from the
893            // beginning, thus removing the need to upgrade the read lock to a write lock.
894            // The write lock is then downgraded to a read lock once the dirty is cleaned
895            // up. It can potentially create a deadlock in the following situation:
896            //
897            // - `read` is called once, it takes a write lock, then downgrades it to a read
898            //   lock: the guard is kept alive somewhere,
899            // - `read` is called again, and waits to obtain the write lock, which is
900            //   impossible as long as the guard from the previous call is not dropped.
901            //
902            // ## “Atomic” read and write
903            //
904            // One may finally argue to first obtain a read lock over the state, then drop
905            // it if the cross-process lock over the store is dirty, and immediately obtain
906            // a write lock (which can later be downgraded to a read lock). The problem is
907            // that this write lock is async: anything can happen between the drop and the
908            // new lock acquisition, and it's not possible to pause the runtime in the
909            // meantime.
910            //
911            // ## Semaphore with 1 permit, aka a Mutex
912            //
913            // The chosen idea is to allow only one execution at a time of this method: it
914            // becomes a critical section. That way we are free to “upgrade” the read lock
915            // by dropping it and obtaining a new write lock. All callers to this method are
916            // waiting, so nothing can happen in the meantime.
917            //
918            // Note that it doesn't conflict with the `write` method because this later
919            // immediately obtains a write lock, which avoids any conflict with this method.
920            //
921            // [^1]: https://docs.rs/lock_api/0.4.14/lock_api/struct.RwLock.html#method.upgradable_read
922            // [^2]: https://docs.rs/async-lock/3.4.1/async_lock/struct.RwLock.html#method.upgradable_read
923            let _one_reader_guard = self.read_lock_acquisition.lock().await;
924
925            // Obtain a read lock.
926            let state_guard = self.locked_state.read().await;
927
928            match state_guard.store.lock().await? {
929                EventCacheStoreLockState::Clean(store_guard) => {
930                    Ok(RoomEventCacheStateLockReadGuard { state: state_guard, store: store_guard })
931                }
932                EventCacheStoreLockState::Dirty(store_guard) => {
933                    // Drop the read lock, and take a write lock to modify the state.
934                    // This is safe because only one reader at a time (see
935                    // `Self::read_lock_acquisition`) is allowed.
936                    drop(state_guard);
937                    let state_guard = self.locked_state.write().await;
938
939                    let mut guard = RoomEventCacheStateLockWriteGuard {
940                        state: state_guard,
941                        store: store_guard,
942                    };
943
944                    // Force to reload by shrinking to the last chunk.
945                    let updates_as_vector_diffs = guard.force_shrink_to_last_chunk().await?;
946
947                    // All good now, mark the cross-process lock as non-dirty.
948                    EventCacheStoreLockGuard::clear_dirty(&guard.store);
949
950                    // Downgrade the guard as soon as possible.
951                    let guard = guard.downgrade();
952
953                    // Now let the world know about the reload.
954                    if !updates_as_vector_diffs.is_empty() {
955                        // Notify observers about the update.
956                        let _ = guard.state.update_sender.send(
957                            RoomEventCacheUpdate::UpdateTimelineEvents {
958                                diffs: updates_as_vector_diffs,
959                                origin: EventsOrigin::Cache,
960                            },
961                        );
962
963                        // Notify observers about the generic update.
964                        let _ =
965                            guard.state.generic_update_sender.send(RoomEventCacheGenericUpdate {
966                                room_id: guard.state.room_id.clone(),
967                            });
968                    }
969
970                    Ok(guard)
971                }
972            }
973        }
974
975        /// Lock this [`RoomEventCacheStateLock`] with exclusive per-thread
976        /// write access.
977        ///
978        /// This method locks the per-thread lock over the state, and then locks
979        /// the cross-process lock over the store. It returns an RAII guard
980        /// which will drop the write access to the state and to the store when
981        /// dropped.
982        ///
983        /// If the cross-process lock over the store is dirty (see
984        /// [`EventCacheStoreLockState`]), the state is reset to the last chunk.
985        pub async fn write(
986            &self,
987        ) -> Result<RoomEventCacheStateLockWriteGuard<'_>, EventCacheError> {
988            let state_guard = self.locked_state.write().await;
989
990            match state_guard.store.lock().await? {
991                EventCacheStoreLockState::Clean(store_guard) => {
992                    Ok(RoomEventCacheStateLockWriteGuard { state: state_guard, store: store_guard })
993                }
994                EventCacheStoreLockState::Dirty(store_guard) => {
995                    let mut guard = RoomEventCacheStateLockWriteGuard {
996                        state: state_guard,
997                        store: store_guard,
998                    };
999
1000                    // Force to reload by shrinking to the last chunk.
1001                    let updates_as_vector_diffs = guard.force_shrink_to_last_chunk().await?;
1002
1003                    // All good now, mark the cross-process lock as non-dirty.
1004                    EventCacheStoreLockGuard::clear_dirty(&guard.store);
1005
1006                    // Now let the world know about the reload.
1007                    if !updates_as_vector_diffs.is_empty() {
1008                        // Notify observers about the update.
1009                        let _ = guard.state.update_sender.send(
1010                            RoomEventCacheUpdate::UpdateTimelineEvents {
1011                                diffs: updates_as_vector_diffs,
1012                                origin: EventsOrigin::Cache,
1013                            },
1014                        );
1015
1016                        // Notify observers about the generic update.
1017                        let _ =
1018                            guard.state.generic_update_sender.send(RoomEventCacheGenericUpdate {
1019                                room_id: guard.state.room_id.clone(),
1020                            });
1021                    }
1022
1023                    Ok(guard)
1024                }
1025            }
1026        }
1027    }
1028
1029    /// The read lock guard returned by [`RoomEventCacheStateLock::read`].
1030    pub struct RoomEventCacheStateLockReadGuard<'a> {
1031        /// The per-thread read lock guard over the
1032        /// [`RoomEventCacheStateLockInner`].
1033        state: RwLockReadGuard<'a, RoomEventCacheStateLockInner>,
1034
1035        /// The cross-process lock guard over the store.
1036        store: EventCacheStoreLockGuard,
1037    }
1038
1039    /// The write lock guard return by [`RoomEventCacheStateLock::write`].
1040    pub struct RoomEventCacheStateLockWriteGuard<'a> {
1041        /// The per-thread write lock guard over the
1042        /// [`RoomEventCacheStateLockInner`].
1043        state: RwLockWriteGuard<'a, RoomEventCacheStateLockInner>,
1044
1045        /// The cross-process lock guard over the store.
1046        store: EventCacheStoreLockGuard,
1047    }
1048
1049    impl<'a> RoomEventCacheStateLockWriteGuard<'a> {
1050        /// Synchronously downgrades a write lock into a read lock.
1051        ///
1052        /// The per-thread/state lock is downgraded atomically, without allowing
1053        /// any writers to take exclusive access of the lock in the meantime.
1054        ///
1055        /// It returns an RAII guard which will drop the write access to the
1056        /// state and to the store when dropped.
1057        fn downgrade(self) -> RoomEventCacheStateLockReadGuard<'a> {
1058            RoomEventCacheStateLockReadGuard { state: self.state.downgrade(), store: self.store }
1059        }
1060    }
1061
1062    impl<'a> RoomEventCacheStateLockReadGuard<'a> {
1063        /// Returns a read-only reference to the underlying room linked chunk.
1064        pub fn room_linked_chunk(&self) -> &EventLinkedChunk {
1065            &self.state.room_linked_chunk
1066        }
1067
1068        pub fn subscriber_count(&self) -> &Arc<AtomicUsize> {
1069            &self.state.subscriber_count
1070        }
1071
1072        /// Find a single event in this room.
1073        ///
1074        /// It starts by looking into loaded events in `EventLinkedChunk` before
1075        /// looking inside the storage.
1076        pub async fn find_event(
1077            &self,
1078            event_id: &EventId,
1079        ) -> Result<Option<(EventLocation, Event)>, EventCacheError> {
1080            find_event(event_id, &self.state.room_id, &self.state.room_linked_chunk, &self.store)
1081                .await
1082        }
1083
1084        /// Find an event and all its relations in the persisted storage.
1085        ///
1086        /// This goes straight to the database, as a simplification; we don't
1087        /// expect to need to have to look up in memory events, or that
1088        /// all the related events are actually loaded.
1089        ///
1090        /// The related events are sorted like this:
1091        /// - events saved out-of-band with
1092        ///   [`super::RoomEventCache::save_events`] will be located at the
1093        ///   beginning of the array.
1094        /// - events present in the linked chunk (be it in memory or in the
1095        ///   database) will be sorted according to their ordering in the linked
1096        ///   chunk.
1097        pub async fn find_event_with_relations(
1098            &self,
1099            event_id: &EventId,
1100            filters: Option<Vec<RelationType>>,
1101        ) -> Result<Option<(Event, Vec<Event>)>, EventCacheError> {
1102            find_event_with_relations(
1103                event_id,
1104                &self.state.room_id,
1105                filters,
1106                &self.state.room_linked_chunk,
1107                &self.store,
1108            )
1109            .await
1110        }
1111
1112        //// Find a single event in this room, starting from the most recent event.
1113        ///
1114        /// The `predicate` receives two arguments: the current event, and the
1115        /// ID of the _previous_ (older) event.
1116        ///
1117        /// **Warning**! It looks into the loaded events from the in-memory
1118        /// linked chunk **only**. It doesn't look inside the storage,
1119        /// contrary to [`Self::find_event`].
1120        pub fn rfind_map_event_in_memory_by<O, P>(&self, mut predicate: P) -> Option<O>
1121        where
1122            P: FnMut(&Event, Option<OwnedEventId>) -> Option<O>,
1123        {
1124            self.state
1125                .room_linked_chunk
1126                .revents()
1127                .peekable()
1128                .batching(|iter| {
1129                    iter.next().map(|(_position, event)| {
1130                        (
1131                            event,
1132                            iter.peek()
1133                                .and_then(|(_next_position, next_event)| next_event.event_id()),
1134                        )
1135                    })
1136                })
1137                .find_map(|(event, next_event_id)| predicate(event, next_event_id))
1138        }
1139
1140        #[cfg(test)]
1141        pub fn is_dirty(&self) -> bool {
1142            EventCacheStoreLockGuard::is_dirty(&self.store)
1143        }
1144    }
1145
1146    impl<'a> RoomEventCacheStateLockWriteGuard<'a> {
1147        /// Returns a write reference to the underlying room linked chunk.
1148        #[cfg(any(feature = "e2e-encryption", test))]
1149        pub fn room_linked_chunk(&mut self) -> &mut EventLinkedChunk {
1150            &mut self.state.room_linked_chunk
1151        }
1152
1153        /// Get a reference to the `waited_for_initial_prev_token` atomic bool.
1154        pub fn waited_for_initial_prev_token(&self) -> &Arc<AtomicBool> {
1155            &self.state.waited_for_initial_prev_token
1156        }
1157
1158        /// Find a single event in this room.
1159        ///
1160        /// It starts by looking into loaded events in `EventLinkedChunk` before
1161        /// looking inside the storage.
1162        pub async fn find_event(
1163            &self,
1164            event_id: &EventId,
1165        ) -> Result<Option<(EventLocation, Event)>, EventCacheError> {
1166            find_event(event_id, &self.state.room_id, &self.state.room_linked_chunk, &self.store)
1167                .await
1168        }
1169
1170        /// Find an event and all its relations in the persisted storage.
1171        ///
1172        /// This goes straight to the database, as a simplification; we don't
1173        /// expect to need to have to look up in memory events, or that
1174        /// all the related events are actually loaded.
1175        ///
1176        /// The related events are sorted like this:
1177        /// - events saved out-of-band with
1178        ///   [`super::RoomEventCache::save_events`] will be located at the
1179        ///   beginning of the array.
1180        /// - events present in the linked chunk (be it in memory or in the
1181        ///   database) will be sorted according to their ordering in the linked
1182        ///   chunk.
1183        pub async fn find_event_with_relations(
1184            &self,
1185            event_id: &EventId,
1186            filters: Option<Vec<RelationType>>,
1187        ) -> Result<Option<(Event, Vec<Event>)>, EventCacheError> {
1188            find_event_with_relations(
1189                event_id,
1190                &self.state.room_id,
1191                filters,
1192                &self.state.room_linked_chunk,
1193                &self.store,
1194            )
1195            .await
1196        }
1197
1198        /// Load more events backwards if the last chunk is **not** a gap.
1199        pub async fn load_more_events_backwards(
1200            &mut self,
1201        ) -> Result<LoadMoreEventsBackwardsOutcome, EventCacheError> {
1202            // If any in-memory chunk is a gap, don't load more events, and let the caller
1203            // resolve the gap.
1204            if let Some(prev_token) = self.state.room_linked_chunk.rgap().map(|gap| gap.prev_token)
1205            {
1206                return Ok(LoadMoreEventsBackwardsOutcome::Gap { prev_token: Some(prev_token) });
1207            }
1208
1209            let prev_first_chunk = self
1210                .state
1211                .room_linked_chunk
1212                .chunks()
1213                .next()
1214                .expect("a linked chunk is never empty");
1215
1216            // The first chunk is not a gap, we can load its previous chunk.
1217            let linked_chunk_id = LinkedChunkId::Room(&self.state.room_id);
1218            let new_first_chunk = match self
1219                .store
1220                .load_previous_chunk(linked_chunk_id, prev_first_chunk.identifier())
1221                .await
1222            {
1223                Ok(Some(new_first_chunk)) => {
1224                    // All good, let's continue with this chunk.
1225                    new_first_chunk
1226                }
1227
1228                Ok(None) => {
1229                    // If we never received events for this room, this means we've never received a
1230                    // sync for that room, because every room must have *at least* a room creation
1231                    // event. Otherwise, we have reached the start of the timeline.
1232
1233                    if self.state.room_linked_chunk.events().next().is_some() {
1234                        // If there's at least one event, this means we've reached the start of the
1235                        // timeline, since the chunk is fully loaded.
1236                        trace!("chunk is fully loaded and non-empty: reached_start=true");
1237                        return Ok(LoadMoreEventsBackwardsOutcome::StartOfTimeline);
1238                    }
1239
1240                    // Otherwise, start back-pagination from the end of the room.
1241                    return Ok(LoadMoreEventsBackwardsOutcome::Gap { prev_token: None });
1242                }
1243
1244                Err(err) => {
1245                    error!("error when loading the previous chunk of a linked chunk: {err}");
1246
1247                    // Clear storage for this room.
1248                    self.store
1249                        .handle_linked_chunk_updates(linked_chunk_id, vec![Update::Clear])
1250                        .await?;
1251
1252                    // Return the error.
1253                    return Err(err.into());
1254                }
1255            };
1256
1257            let chunk_content = new_first_chunk.content.clone();
1258
1259            // We've reached the start on disk, if and only if, there was no chunk prior to
1260            // the one we just loaded.
1261            //
1262            // This value is correct, if and only if, it is used for a chunk content of kind
1263            // `Items`.
1264            let reached_start = new_first_chunk.previous.is_none();
1265
1266            if let Err(err) =
1267                self.state.room_linked_chunk.insert_new_chunk_as_first(new_first_chunk)
1268            {
1269                error!("error when inserting the previous chunk into its linked chunk: {err}");
1270
1271                // Clear storage for this room.
1272                self.store
1273                    .handle_linked_chunk_updates(
1274                        LinkedChunkId::Room(&self.state.room_id),
1275                        vec![Update::Clear],
1276                    )
1277                    .await?;
1278
1279                // Return the error.
1280                return Err(err.into());
1281            }
1282
1283            // ⚠️ Let's not propagate the updates to the store! We already have these data
1284            // in the store! Let's drain them.
1285            let _ = self.state.room_linked_chunk.store_updates().take();
1286
1287            // However, we want to get updates as `VectorDiff`s.
1288            let timeline_event_diffs = self.state.room_linked_chunk.updates_as_vector_diffs();
1289
1290            Ok(match chunk_content {
1291                ChunkContent::Gap(gap) => {
1292                    trace!("reloaded chunk from disk (gap)");
1293                    LoadMoreEventsBackwardsOutcome::Gap { prev_token: Some(gap.prev_token) }
1294                }
1295
1296                ChunkContent::Items(events) => {
1297                    trace!(?reached_start, "reloaded chunk from disk ({} items)", events.len());
1298                    LoadMoreEventsBackwardsOutcome::Events {
1299                        events,
1300                        timeline_event_diffs,
1301                        reached_start,
1302                    }
1303                }
1304            })
1305        }
1306
1307        /// If storage is enabled, unload all the chunks, then reloads only the
1308        /// last one.
1309        ///
1310        /// If storage's enabled, return a diff update that starts with a clear
1311        /// of all events; as a result, the caller may override any
1312        /// pending diff updates with the result of this function.
1313        ///
1314        /// Otherwise, returns `None`.
1315        pub async fn shrink_to_last_chunk(&mut self) -> Result<(), EventCacheError> {
1316            // Attempt to load the last chunk.
1317            let linked_chunk_id = LinkedChunkId::Room(&self.state.room_id);
1318            let (last_chunk, chunk_identifier_generator) =
1319                match self.store.load_last_chunk(linked_chunk_id).await {
1320                    Ok(pair) => pair,
1321
1322                    Err(err) => {
1323                        // If loading the last chunk failed, clear the entire linked chunk.
1324                        error!("error when reloading a linked chunk from memory: {err}");
1325
1326                        // Clear storage for this room.
1327                        self.store
1328                            .handle_linked_chunk_updates(linked_chunk_id, vec![Update::Clear])
1329                            .await?;
1330
1331                        // Restart with an empty linked chunk.
1332                        (None, ChunkIdentifierGenerator::new_from_scratch())
1333                    }
1334                };
1335
1336            debug!("unloading the linked chunk, and resetting it to its last chunk");
1337
1338            // Remove all the chunks from the linked chunks, except for the last one, and
1339            // updates the chunk identifier generator.
1340            if let Err(err) =
1341                self.state.room_linked_chunk.replace_with(last_chunk, chunk_identifier_generator)
1342            {
1343                error!("error when replacing the linked chunk: {err}");
1344                return self.reset_internal().await;
1345            }
1346
1347            // Let pagination observers know that we may have not reached the start of the
1348            // timeline.
1349            // TODO: likely need to cancel any ongoing pagination.
1350            self.state
1351                .pagination_status
1352                .set(RoomPaginationStatus::Idle { hit_timeline_start: false });
1353
1354            // Don't propagate those updates to the store; this is only for the in-memory
1355            // representation that we're doing this. Let's drain those store updates.
1356            let _ = self.state.room_linked_chunk.store_updates().take();
1357
1358            Ok(())
1359        }
1360
1361        /// Automatically shrink the room if there are no more subscribers, as
1362        /// indicated by the atomic number of active subscribers.
1363        #[must_use = "Propagate `VectorDiff` updates via `RoomEventCacheUpdate`"]
1364        pub async fn auto_shrink_if_no_subscribers(
1365            &mut self,
1366        ) -> Result<Option<Vec<VectorDiff<Event>>>, EventCacheError> {
1367            let subscriber_count = self.state.subscriber_count.load(Ordering::SeqCst);
1368
1369            trace!(subscriber_count, "received request to auto-shrink");
1370
1371            if subscriber_count == 0 {
1372                // If we are the last strong reference to the auto-shrinker, we can shrink the
1373                // events data structure to its last chunk.
1374                self.shrink_to_last_chunk().await?;
1375
1376                Ok(Some(self.state.room_linked_chunk.updates_as_vector_diffs()))
1377            } else {
1378                Ok(None)
1379            }
1380        }
1381
1382        /// Force to shrink the room, whenever there is subscribers or not.
1383        #[must_use = "Propagate `VectorDiff` updates via `RoomEventCacheUpdate`"]
1384        pub async fn force_shrink_to_last_chunk(
1385            &mut self,
1386        ) -> Result<Vec<VectorDiff<Event>>, EventCacheError> {
1387            self.shrink_to_last_chunk().await?;
1388
1389            Ok(self.state.room_linked_chunk.updates_as_vector_diffs())
1390        }
1391
1392        /// Remove events by their position, in `EventLinkedChunk` and in
1393        /// `EventCacheStore`.
1394        ///
1395        /// This method is purposely isolated because it must ensure that
1396        /// positions are sorted appropriately or it can be disastrous.
1397        #[instrument(skip_all)]
1398        pub async fn remove_events(
1399            &mut self,
1400            in_memory_events: Vec<(OwnedEventId, Position)>,
1401            in_store_events: Vec<(OwnedEventId, Position)>,
1402        ) -> Result<(), EventCacheError> {
1403            // In-store events.
1404            if !in_store_events.is_empty() {
1405                let mut positions = in_store_events
1406                    .into_iter()
1407                    .map(|(_event_id, position)| position)
1408                    .collect::<Vec<_>>();
1409
1410                sort_positions_descending(&mut positions);
1411
1412                let updates = positions
1413                    .into_iter()
1414                    .map(|pos| Update::RemoveItem { at: pos })
1415                    .collect::<Vec<_>>();
1416
1417                self.apply_store_only_updates(updates).await?;
1418            }
1419
1420            // In-memory events.
1421            if in_memory_events.is_empty() {
1422                // Nothing else to do, return early.
1423                return Ok(());
1424            }
1425
1426            // `remove_events_by_position` is responsible of sorting positions.
1427            self.state
1428                .room_linked_chunk
1429                .remove_events_by_position(
1430                    in_memory_events.into_iter().map(|(_event_id, position)| position).collect(),
1431                )
1432                .expect("failed to remove an event");
1433
1434            self.propagate_changes().await
1435        }
1436
1437        async fn propagate_changes(&mut self) -> Result<(), EventCacheError> {
1438            let updates = self.state.room_linked_chunk.store_updates().take();
1439            self.send_updates_to_store(updates).await
1440        }
1441
1442        /// Apply some updates that are effective only on the store itself.
1443        ///
1444        /// This method should be used only for updates that happen *outside*
1445        /// the in-memory linked chunk. Such updates must be applied
1446        /// onto the ordering tracker as well as to the persistent
1447        /// storage.
1448        async fn apply_store_only_updates(
1449            &mut self,
1450            updates: Vec<Update<Event, Gap>>,
1451        ) -> Result<(), EventCacheError> {
1452            self.state.room_linked_chunk.order_tracker.map_updates(&updates);
1453            self.send_updates_to_store(updates).await
1454        }
1455
1456        async fn send_updates_to_store(
1457            &mut self,
1458            mut updates: Vec<Update<Event, Gap>>,
1459        ) -> Result<(), EventCacheError> {
1460            if updates.is_empty() {
1461                return Ok(());
1462            }
1463
1464            // Strip relations from updates which insert or replace items.
1465            for update in updates.iter_mut() {
1466                match update {
1467                    Update::PushItems { items, .. } => strip_relations_from_events(items),
1468                    Update::ReplaceItem { item, .. } => strip_relations_from_event(item),
1469                    // Other update kinds don't involve adding new events.
1470                    Update::NewItemsChunk { .. }
1471                    | Update::NewGapChunk { .. }
1472                    | Update::RemoveChunk(_)
1473                    | Update::RemoveItem { .. }
1474                    | Update::DetachLastItems { .. }
1475                    | Update::StartReattachItems
1476                    | Update::EndReattachItems
1477                    | Update::Clear => {}
1478                }
1479            }
1480
1481            // Spawn a task to make sure that all the changes are effectively forwarded to
1482            // the store, even if the call to this method gets aborted.
1483            //
1484            // The store cross-process locking involves an actual mutex, which ensures that
1485            // storing updates happens in the expected order.
1486
1487            let store = self.store.clone();
1488            let room_id = self.state.room_id.clone();
1489            let cloned_updates = updates.clone();
1490
1491            spawn(async move {
1492                trace!(updates = ?cloned_updates, "sending linked chunk updates to the store");
1493                let linked_chunk_id = LinkedChunkId::Room(&room_id);
1494                store.handle_linked_chunk_updates(linked_chunk_id, cloned_updates).await?;
1495                trace!("linked chunk updates applied");
1496
1497                super::Result::Ok(())
1498            })
1499            .await
1500            .expect("joining failed")?;
1501
1502            // Forward that the store got updated to observers.
1503            let _ = self.state.linked_chunk_update_sender.send(RoomEventCacheLinkedChunkUpdate {
1504                linked_chunk_id: OwnedLinkedChunkId::Room(self.state.room_id.clone()),
1505                updates,
1506            });
1507
1508            Ok(())
1509        }
1510
1511        /// Reset this data structure as if it were brand new.
1512        ///
1513        /// Return a single diff update that is a clear of all events; as a
1514        /// result, the caller may override any pending diff updates
1515        /// with the result of this function.
1516        pub async fn reset(&mut self) -> Result<Vec<VectorDiff<Event>>, EventCacheError> {
1517            self.reset_internal().await?;
1518
1519            let diff_updates = self.state.room_linked_chunk.updates_as_vector_diffs();
1520
1521            // Ensure the contract defined in the doc comment is true:
1522            debug_assert_eq!(diff_updates.len(), 1);
1523            debug_assert!(matches!(diff_updates[0], VectorDiff::Clear));
1524
1525            Ok(diff_updates)
1526        }
1527
1528        async fn reset_internal(&mut self) -> Result<(), EventCacheError> {
1529            self.state.room_linked_chunk.reset();
1530
1531            // No need to update the thread summaries: the room events are
1532            // gone because of the reset of `room_linked_chunk`.
1533            //
1534            // Clear the threads.
1535            for thread in self.state.threads.values_mut() {
1536                thread.clear();
1537            }
1538
1539            self.propagate_changes().await?;
1540
1541            // Reset the pagination state too: pretend we never waited for the initial
1542            // prev-batch token, and indicate that we're not at the start of the
1543            // timeline, since we don't know about that anymore.
1544            self.state.waited_for_initial_prev_token.store(false, Ordering::SeqCst);
1545            // TODO: likely must cancel any ongoing back-paginations too
1546            self.state
1547                .pagination_status
1548                .set(RoomPaginationStatus::Idle { hit_timeline_start: false });
1549
1550            Ok(())
1551        }
1552
1553        /// Handle the result of a sync.
1554        ///
1555        /// It may send room event cache updates to the given sender, if it
1556        /// generated any of those.
1557        ///
1558        /// Returns `true` for the first part of the tuple if a new gap
1559        /// (previous-batch token) has been inserted, `false` otherwise.
1560        #[must_use = "Propagate `VectorDiff` updates via `RoomEventCacheUpdate`"]
1561        pub async fn handle_sync(
1562            &mut self,
1563            mut timeline: Timeline,
1564        ) -> Result<(bool, Vec<VectorDiff<Event>>), EventCacheError> {
1565            let mut prev_batch = timeline.prev_batch.take();
1566
1567            let DeduplicationOutcome {
1568                all_events: events,
1569                in_memory_duplicated_event_ids,
1570                in_store_duplicated_event_ids,
1571                non_empty_all_duplicates: all_duplicates,
1572            } = filter_duplicate_events(
1573                &self.store,
1574                LinkedChunkId::Room(&self.state.room_id),
1575                &self.state.room_linked_chunk,
1576                timeline.events,
1577            )
1578            .await?;
1579
1580            // If the timeline isn't limited, and we already knew about some past events,
1581            // then this definitely knows what the timeline head is (either we know
1582            // about all the events persisted in storage, or we have a gap
1583            // somewhere). In this case, we can ditch the previous-batch
1584            // token, which is an optimization to avoid unnecessary future back-pagination
1585            // requests.
1586            //
1587            // We can also ditch it if we knew about all the events that came from sync,
1588            // namely, they were all deduplicated. In this case, using the
1589            // previous-batch token would only result in fetching other events we
1590            // knew about. This is slightly incorrect in the presence of
1591            // network splits, but this has shown to be Good Enough™.
1592            if !timeline.limited && self.state.room_linked_chunk.events().next().is_some()
1593                || all_duplicates
1594            {
1595                prev_batch = None;
1596            }
1597
1598            if prev_batch.is_some() {
1599                // Sad time: there's a gap, somewhere, in the timeline, and there's at least one
1600                // non-duplicated event. We don't know which threads might have gappy, so we
1601                // must invalidate them all :(
1602                // TODO(bnjbvr): figure out a better catchup mechanism for threads.
1603                let mut summaries_to_update = Vec::new();
1604
1605                for (thread_root, thread) in self.state.threads.iter_mut() {
1606                    // Empty the thread's linked chunk.
1607                    thread.clear();
1608
1609                    summaries_to_update.push(thread_root.clone());
1610                }
1611
1612                // Now, update the summaries to indicate that we're not sure what the latest
1613                // thread event is. The thread count can remain as is, as it might still be
1614                // valid, and there's no good value to reset it to, anyways.
1615                for thread_root in summaries_to_update {
1616                    let Some((location, mut target_event)) = self.find_event(&thread_root).await?
1617                    else {
1618                        trace!(%thread_root, "thread root event is unknown, when updating thread summary after a gappy sync");
1619                        continue;
1620                    };
1621
1622                    if let Some(mut prev_summary) = target_event.thread_summary.summary().cloned() {
1623                        prev_summary.latest_reply = None;
1624
1625                        target_event.thread_summary = ThreadSummaryStatus::Some(prev_summary);
1626
1627                        self.replace_event_at(location, target_event).await?;
1628                    }
1629                }
1630            }
1631
1632            if all_duplicates {
1633                // No new events and no gap (per the previous check), thus no need to change the
1634                // room state. We're done!
1635                return Ok((false, Vec::new()));
1636            }
1637
1638            let has_new_gap = prev_batch.is_some();
1639
1640            // If we've never waited for an initial previous-batch token, and we've now
1641            // inserted a gap, no need to wait for a previous-batch token later.
1642            if !self.state.waited_for_initial_prev_token.load(Ordering::SeqCst) && has_new_gap {
1643                self.state.waited_for_initial_prev_token.store(true, Ordering::SeqCst);
1644            }
1645
1646            // Remove the old duplicated events.
1647            //
1648            // We don't have to worry the removals can change the position of the existing
1649            // events, because we are pushing all _new_ `events` at the back.
1650            self.remove_events(in_memory_duplicated_event_ids, in_store_duplicated_event_ids)
1651                .await?;
1652
1653            self.state
1654                .room_linked_chunk
1655                .push_live_events(prev_batch.map(|prev_token| Gap { prev_token }), &events);
1656
1657            self.post_process_new_events(events, true).await?;
1658
1659            if timeline.limited && has_new_gap {
1660                // If there was a previous batch token for a limited timeline, unload the chunks
1661                // so it only contains the last one; otherwise, there might be a
1662                // valid gap in between, and observers may not render it (yet).
1663                //
1664                // We must do this *after* persisting these events to storage (in
1665                // `post_process_new_events`).
1666                self.shrink_to_last_chunk().await?;
1667            }
1668
1669            let timeline_event_diffs = self.state.room_linked_chunk.updates_as_vector_diffs();
1670
1671            Ok((has_new_gap, timeline_event_diffs))
1672        }
1673
1674        /// Handle the result of a single back-pagination request.
1675        ///
1676        /// If the `prev_token` is set, then this function will check that the
1677        /// corresponding gap is present in the in-memory linked chunk.
1678        /// If it's not the case, `Ok(None)` will be returned, and the
1679        /// caller may decide to do something based on that (e.g. restart a
1680        /// pagination).
1681        #[must_use = "Propagate `VectorDiff` updates via `RoomEventCacheUpdate`"]
1682        pub async fn handle_backpagination(
1683            &mut self,
1684            events: Vec<Event>,
1685            mut new_token: Option<String>,
1686            prev_token: Option<String>,
1687        ) -> Result<Option<(BackPaginationOutcome, Vec<VectorDiff<Event>>)>, EventCacheError>
1688        {
1689            // Check that the previous token still exists; otherwise it's a sign that the
1690            // room's timeline has been cleared.
1691            let prev_gap_id = if let Some(token) = prev_token {
1692                // Find the corresponding gap in the in-memory linked chunk.
1693                let gap_chunk_id = self.state.room_linked_chunk.chunk_identifier(|chunk| {
1694                    matches!(chunk.content(), ChunkContent::Gap(Gap { prev_token }) if *prev_token == token)
1695                });
1696
1697                if gap_chunk_id.is_none() {
1698                    // We got a previous-batch token from the linked chunk *before* running the
1699                    // request, but it is missing *after* completing the request.
1700                    //
1701                    // It may be a sign the linked chunk has been reset, but it's fine, per this
1702                    // function's contract.
1703                    return Ok(None);
1704                }
1705
1706                gap_chunk_id
1707            } else {
1708                None
1709            };
1710
1711            let DeduplicationOutcome {
1712                all_events: mut events,
1713                in_memory_duplicated_event_ids,
1714                in_store_duplicated_event_ids,
1715                non_empty_all_duplicates: all_duplicates,
1716            } = filter_duplicate_events(
1717                &self.store,
1718                LinkedChunkId::Room(&self.state.room_id),
1719                &self.state.room_linked_chunk,
1720                events,
1721            )
1722            .await?;
1723
1724            // If not all the events have been back-paginated, we need to remove the
1725            // previous ones, otherwise we can end up with misordered events.
1726            //
1727            // Consider the following scenario:
1728            // - sync returns [D, E, F]
1729            // - then sync returns [] with a previous batch token PB1, so the internal
1730            //   linked chunk state is [D, E, F, PB1].
1731            // - back-paginating with PB1 may return [A, B, C, D, E, F].
1732            //
1733            // Only inserting the new events when replacing PB1 would result in a timeline
1734            // ordering of [D, E, F, A, B, C], which is incorrect. So we do have to remove
1735            // all the events, in case this happens (see also #4746).
1736
1737            if !all_duplicates {
1738                // Let's forget all the previous events.
1739                self.remove_events(in_memory_duplicated_event_ids, in_store_duplicated_event_ids)
1740                    .await?;
1741            } else {
1742                // All new events are duplicated, they can all be ignored.
1743                events.clear();
1744                // The gap can be ditched too, as it won't be useful to backpaginate any
1745                // further.
1746                new_token = None;
1747            }
1748
1749            // `/messages` has been called with `dir=b` (backwards), so the events are in
1750            // the inverted order; reorder them.
1751            let topo_ordered_events = events.iter().rev().cloned().collect::<Vec<_>>();
1752
1753            let new_gap = new_token.map(|prev_token| Gap { prev_token });
1754            let reached_start = self.state.room_linked_chunk.finish_back_pagination(
1755                prev_gap_id,
1756                new_gap,
1757                &topo_ordered_events,
1758            );
1759
1760            // Note: this flushes updates to the store.
1761            self.post_process_new_events(topo_ordered_events, false).await?;
1762
1763            let event_diffs = self.state.room_linked_chunk.updates_as_vector_diffs();
1764
1765            Ok(Some((BackPaginationOutcome { events, reached_start }, event_diffs)))
1766        }
1767
1768        /// Subscribe to thread for a given root event, and get a (maybe empty)
1769        /// initially known list of events for that thread.
1770        pub fn subscribe_to_thread(
1771            &mut self,
1772            root: OwnedEventId,
1773        ) -> (Vec<Event>, Receiver<ThreadEventCacheUpdate>) {
1774            self.get_or_reload_thread(root).subscribe()
1775        }
1776
1777        /// Back paginate in the given thread.
1778        ///
1779        /// Will always start from the end, unless we previously paginated.
1780        pub fn finish_thread_network_pagination(
1781            &mut self,
1782            root: OwnedEventId,
1783            prev_token: Option<String>,
1784            new_token: Option<String>,
1785            events: Vec<Event>,
1786        ) -> Option<BackPaginationOutcome> {
1787            self.get_or_reload_thread(root).finish_network_pagination(prev_token, new_token, events)
1788        }
1789
1790        pub fn load_more_thread_events_backwards(
1791            &mut self,
1792            root: OwnedEventId,
1793        ) -> LoadMoreEventsBackwardsOutcome {
1794            self.get_or_reload_thread(root).load_more_events_backwards()
1795        }
1796
1797        // --------------------------------------------
1798        // utility methods
1799        // --------------------------------------------
1800
1801        /// Post-process new events, after they have been added to the in-memory
1802        /// linked chunk.
1803        ///
1804        /// Flushes updates to disk first.
1805        pub(in super::super) async fn post_process_new_events(
1806            &mut self,
1807            events: Vec<Event>,
1808            is_sync: bool,
1809        ) -> Result<(), EventCacheError> {
1810            // Update the store before doing the post-processing.
1811            self.propagate_changes().await?;
1812
1813            let mut new_events_by_thread: BTreeMap<_, Vec<_>> = BTreeMap::new();
1814
1815            for event in events {
1816                self.maybe_apply_new_redaction(&event).await?;
1817
1818                if self.state.enabled_thread_support {
1819                    // Only add the event to a thread if:
1820                    // - thread support is enabled,
1821                    // - and if this is a sync (we can't know where to insert backpaginated events
1822                    //   in threads).
1823                    if is_sync {
1824                        if let Some(thread_root) = extract_thread_root(event.raw()) {
1825                            new_events_by_thread
1826                                .entry(thread_root)
1827                                .or_default()
1828                                .push(event.clone());
1829                        } else if let Some(event_id) = event.event_id() {
1830                            // If we spot the root of a thread, add it to its linked chunk.
1831                            if self.state.threads.contains_key(&event_id) {
1832                                new_events_by_thread
1833                                    .entry(event_id)
1834                                    .or_default()
1835                                    .push(event.clone());
1836                            }
1837                        }
1838                    }
1839
1840                    // Look for edits that may apply to a thread; we'll process them later.
1841                    if let Some(edit_target) = extract_edit_target(event.raw()) {
1842                        // If the edited event is known, and part of a thread,
1843                        if let Some((_location, edit_target_event)) =
1844                            self.find_event(&edit_target).await?
1845                            && let Some(thread_root) = extract_thread_root(edit_target_event.raw())
1846                        {
1847                            // Mark the thread for processing, unless it was already marked as
1848                            // such.
1849                            new_events_by_thread.entry(thread_root).or_default();
1850                        }
1851                    }
1852                }
1853
1854                // Save a bundled thread event, if there was one.
1855                if let Some(bundled_thread) = event.bundled_latest_thread_event {
1856                    self.save_events([*bundled_thread]).await?;
1857                }
1858            }
1859
1860            if self.state.enabled_thread_support {
1861                self.update_threads(new_events_by_thread).await?;
1862            }
1863
1864            Ok(())
1865        }
1866
1867        fn get_or_reload_thread(&mut self, root_event_id: OwnedEventId) -> &mut ThreadEventCache {
1868            // TODO: when there's persistent storage, try to lazily reload from disk, if
1869            // missing from memory.
1870            let room_id = self.state.room_id.clone();
1871            let linked_chunk_update_sender = self.state.linked_chunk_update_sender.clone();
1872
1873            self.state.threads.entry(root_event_id.clone()).or_insert_with(|| {
1874                ThreadEventCache::new(room_id, root_event_id, linked_chunk_update_sender)
1875            })
1876        }
1877
1878        #[instrument(skip_all)]
1879        async fn update_threads(
1880            &mut self,
1881            new_events_by_thread: BTreeMap<OwnedEventId, Vec<Event>>,
1882        ) -> Result<(), EventCacheError> {
1883            for (thread_root, new_events) in new_events_by_thread {
1884                let thread_cache = self.get_or_reload_thread(thread_root.clone());
1885
1886                thread_cache.add_live_events(new_events);
1887
1888                let mut latest_event_id = thread_cache.latest_event_id();
1889
1890                // If there's an edit to the latest event in the thread, use the latest edit
1891                // event id as the latest event id for the thread summary.
1892                if let Some(event_id) = latest_event_id.as_ref()
1893                    && let Some((_, edits)) = self
1894                        .find_event_with_relations(event_id, Some(vec![RelationType::Replacement]))
1895                        .await?
1896                    && let Some(latest_edit) = edits.last()
1897                {
1898                    latest_event_id = latest_edit.event_id();
1899                }
1900
1901                self.maybe_update_thread_summary(thread_root, latest_event_id).await?;
1902            }
1903
1904            Ok(())
1905        }
1906
1907        /// Update a thread summary on the given thread root, if needs be.
1908        async fn maybe_update_thread_summary(
1909            &mut self,
1910            thread_root: OwnedEventId,
1911            latest_event_id: Option<OwnedEventId>,
1912        ) -> Result<(), EventCacheError> {
1913            // Add a thread summary to the (room) event which has the thread root, if we
1914            // knew about it.
1915
1916            let Some((location, mut target_event)) = self.find_event(&thread_root).await? else {
1917                trace!(%thread_root, "thread root event is missing from the room linked chunk");
1918                return Ok(());
1919            };
1920
1921            let prev_summary = target_event.thread_summary.summary();
1922
1923            // Recompute the thread summary, if needs be.
1924
1925            // Read the latest number of thread replies from the store.
1926            //
1927            // Implementation note: since this is based on the `m.relates_to` field, and
1928            // that field can only be present on room messages, we don't have to
1929            // worry about filtering out aggregation events (like
1930            // reactions/edits/etc.). Pretty neat, huh?
1931            let num_replies = {
1932                let thread_replies = self
1933                    .store
1934                    .find_event_relations(
1935                        &self.state.room_id,
1936                        &thread_root,
1937                        Some(&[RelationType::Thread]),
1938                    )
1939                    .await?;
1940                thread_replies.len().try_into().unwrap_or(u32::MAX)
1941            };
1942
1943            let new_summary = if num_replies > 0 {
1944                Some(ThreadSummary { num_replies, latest_reply: latest_event_id })
1945            } else {
1946                None
1947            };
1948
1949            if prev_summary == new_summary.as_ref() {
1950                trace!(%thread_root, "thread summary is already up-to-date");
1951                return Ok(());
1952            }
1953
1954            // Trigger an update to observers.
1955            trace!(%thread_root, "updating thread summary: {new_summary:?}");
1956            target_event.thread_summary = ThreadSummaryStatus::from_opt(new_summary);
1957            self.replace_event_at(location, target_event).await
1958        }
1959
1960        /// Replaces a single event, be it saved in memory or in the store.
1961        ///
1962        /// If it was saved in memory, this will emit a notification to
1963        /// observers that a single item has been replaced. Otherwise,
1964        /// such a notification is not emitted, because observers are
1965        /// unlikely to observe the store updates directly.
1966        pub(crate) async fn replace_event_at(
1967            &mut self,
1968            location: EventLocation,
1969            event: Event,
1970        ) -> Result<(), EventCacheError> {
1971            match location {
1972                EventLocation::Memory(position) => {
1973                    self.state
1974                        .room_linked_chunk
1975                        .replace_event_at(position, event)
1976                        .expect("should have been a valid position of an item");
1977                    // We just changed the in-memory representation; synchronize this with
1978                    // the store.
1979                    self.propagate_changes().await?;
1980                }
1981                EventLocation::Store => {
1982                    self.save_events([event]).await?;
1983                }
1984            }
1985
1986            Ok(())
1987        }
1988
1989        /// If the given event is a redaction, try to retrieve the
1990        /// to-be-redacted event in the chunk, and replace it by the
1991        /// redacted form.
1992        #[instrument(skip_all)]
1993        async fn maybe_apply_new_redaction(
1994            &mut self,
1995            event: &Event,
1996        ) -> Result<(), EventCacheError> {
1997            let raw_event = event.raw();
1998
1999            // Do not deserialise the entire event if we aren't certain it's a
2000            // `m.room.redaction`. It saves a non-negligible amount of computations.
2001            let Ok(Some(MessageLikeEventType::RoomRedaction)) =
2002                raw_event.get_field::<MessageLikeEventType>("type")
2003            else {
2004                return Ok(());
2005            };
2006
2007            // It is a `m.room.redaction`! We can deserialize it entirely.
2008
2009            let Ok(AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::RoomRedaction(
2010                redaction,
2011            ))) = raw_event.deserialize()
2012            else {
2013                return Ok(());
2014            };
2015
2016            let Some(event_id) = redaction.redacts(&self.state.room_version_rules.redaction) else {
2017                warn!("missing target event id from the redaction event");
2018                return Ok(());
2019            };
2020
2021            // Replace the redacted event by a redacted form, if we knew about it.
2022            let Some((location, mut target_event)) = self.find_event(event_id).await? else {
2023                trace!("redacted event is missing from the linked chunk");
2024                return Ok(());
2025            };
2026
2027            // Don't redact already redacted events.
2028            let thread_root = if let Ok(deserialized) = target_event.raw().deserialize() {
2029                // TODO: replace with `deserialized.is_redacted()` when
2030                // https://github.com/ruma/ruma/pull/2254 has been merged.
2031                match deserialized {
2032                    AnySyncTimelineEvent::MessageLike(ev) => {
2033                        if ev.is_redacted() {
2034                            return Ok(());
2035                        }
2036                    }
2037                    AnySyncTimelineEvent::State(ev) => {
2038                        if ev.is_redacted() {
2039                            return Ok(());
2040                        }
2041                    }
2042                }
2043
2044                // If the event is part of a thread, update the thread linked chunk and the
2045                // summary.
2046                extract_thread_root(target_event.raw())
2047            } else {
2048                warn!("failed to deserialize the event to redact");
2049                None
2050            };
2051
2052            if let Some(redacted_event) = apply_redaction(
2053                target_event.raw(),
2054                event.raw().cast_ref_unchecked::<SyncRoomRedactionEvent>(),
2055                &self.state.room_version_rules.redaction,
2056            ) {
2057                // It's safe to cast `redacted_event` here:
2058                // - either the event was an `AnyTimelineEvent` cast to `AnySyncTimelineEvent`
2059                //   when calling .raw(), so it's still one under the hood.
2060                // - or it wasn't, and it's a plain `AnySyncTimelineEvent` in this case.
2061                target_event.replace_raw(redacted_event.cast_unchecked());
2062
2063                self.replace_event_at(location, target_event).await?;
2064
2065                // If the redacted event was part of a thread, remove it in the thread linked
2066                // chunk too, and make sure to update the thread root's summary
2067                // as well.
2068                //
2069                // Note: there is an ordering issue here: the above `replace_event_at` must
2070                // happen BEFORE we recompute the summary, otherwise the set of
2071                // replies may include the to-be-redacted event.
2072                if let Some(thread_root) = thread_root
2073                    && let Some(thread_cache) = self.state.threads.get_mut(&thread_root)
2074                {
2075                    thread_cache.remove_if_present(event_id);
2076
2077                    // The number of replies may have changed, so update the thread summary if
2078                    // needs be.
2079                    let latest_event_id = thread_cache.latest_event_id();
2080                    self.maybe_update_thread_summary(thread_root, latest_event_id).await?;
2081                }
2082            }
2083
2084            Ok(())
2085        }
2086
2087        /// Save events into the database, without notifying observers.
2088        pub async fn save_events(
2089            &mut self,
2090            events: impl IntoIterator<Item = Event>,
2091        ) -> Result<(), EventCacheError> {
2092            let store = self.store.clone();
2093            let room_id = self.state.room_id.clone();
2094            let events = events.into_iter().collect::<Vec<_>>();
2095
2096            // Spawn a task so the save is uninterrupted by task cancellation.
2097            spawn(async move {
2098                for event in events {
2099                    store.save_event(&room_id, event).await?;
2100                }
2101                super::Result::Ok(())
2102            })
2103            .await
2104            .expect("joining failed")?;
2105
2106            Ok(())
2107        }
2108
2109        #[cfg(test)]
2110        pub fn is_dirty(&self) -> bool {
2111            EventCacheStoreLockGuard::is_dirty(&self.store)
2112        }
2113    }
2114
2115    /// Load a linked chunk's full metadata, making sure the chunks are
2116    /// according to their their links.
2117    ///
2118    /// Returns `None` if there's no such linked chunk in the store, or an
2119    /// error if the linked chunk is malformed.
2120    async fn load_linked_chunk_metadata(
2121        store_guard: &EventCacheStoreLockGuard,
2122        linked_chunk_id: LinkedChunkId<'_>,
2123    ) -> Result<Option<Vec<ChunkMetadata>>, EventCacheError> {
2124        let mut all_chunks = store_guard
2125            .load_all_chunks_metadata(linked_chunk_id)
2126            .await
2127            .map_err(EventCacheError::from)?;
2128
2129        if all_chunks.is_empty() {
2130            // There are no chunks, so there's nothing to do.
2131            return Ok(None);
2132        }
2133
2134        // Transform the vector into a hashmap, for quick lookup of the predecessors.
2135        let chunk_map: HashMap<_, _> =
2136            all_chunks.iter().map(|meta| (meta.identifier, meta)).collect();
2137
2138        // Find a last chunk.
2139        let mut iter = all_chunks.iter().filter(|meta| meta.next.is_none());
2140        let Some(last) = iter.next() else {
2141            return Err(EventCacheError::InvalidLinkedChunkMetadata {
2142                details: "no last chunk found".to_owned(),
2143            });
2144        };
2145
2146        // There must at most one last chunk.
2147        if let Some(other_last) = iter.next() {
2148            return Err(EventCacheError::InvalidLinkedChunkMetadata {
2149                details: format!(
2150                    "chunks {} and {} both claim to be last chunks",
2151                    last.identifier.index(),
2152                    other_last.identifier.index()
2153                ),
2154            });
2155        }
2156
2157        // Rewind the chain back to the first chunk, and do some checks at the same
2158        // time.
2159        let mut seen = HashSet::new();
2160        let mut current = last;
2161        loop {
2162            // If we've already seen this chunk, there's a cycle somewhere.
2163            if !seen.insert(current.identifier) {
2164                return Err(EventCacheError::InvalidLinkedChunkMetadata {
2165                    details: format!(
2166                        "cycle detected in linked chunk at {}",
2167                        current.identifier.index()
2168                    ),
2169                });
2170            }
2171
2172            let Some(prev_id) = current.previous else {
2173                // If there's no previous chunk, we're done.
2174                if seen.len() != all_chunks.len() {
2175                    return Err(EventCacheError::InvalidLinkedChunkMetadata {
2176                        details: format!(
2177                            "linked chunk likely has multiple components: {} chunks seen through the chain of predecessors, but {} expected",
2178                            seen.len(),
2179                            all_chunks.len()
2180                        ),
2181                    });
2182                }
2183                break;
2184            };
2185
2186            // If the previous chunk is not in the map, then it's unknown
2187            // and missing.
2188            let Some(pred_meta) = chunk_map.get(&prev_id) else {
2189                return Err(EventCacheError::InvalidLinkedChunkMetadata {
2190                    details: format!(
2191                        "missing predecessor {} chunk for {}",
2192                        prev_id.index(),
2193                        current.identifier.index()
2194                    ),
2195                });
2196            };
2197
2198            // If the previous chunk isn't connected to the next, then the link is invalid.
2199            if pred_meta.next != Some(current.identifier) {
2200                return Err(EventCacheError::InvalidLinkedChunkMetadata {
2201                    details: format!(
2202                        "chunk {}'s next ({:?}) doesn't match the current chunk ({})",
2203                        pred_meta.identifier.index(),
2204                        pred_meta.next.map(|chunk_id| chunk_id.index()),
2205                        current.identifier.index()
2206                    ),
2207                });
2208            }
2209
2210            current = *pred_meta;
2211        }
2212
2213        // At this point, `current` is the identifier of the first chunk.
2214        //
2215        // Reorder the resulting vector, by going through the chain of `next` links, and
2216        // swapping items into their final position.
2217        //
2218        // Invariant in this loop: all items in [0..i[ are in their final, correct
2219        // position.
2220        let mut current = current.identifier;
2221        for i in 0..all_chunks.len() {
2222            // Find the target metadata.
2223            let j = all_chunks
2224                .iter()
2225                .rev()
2226                .position(|meta| meta.identifier == current)
2227                .map(|j| all_chunks.len() - 1 - j)
2228                .expect("the target chunk must be present in the metadata");
2229            if i != j {
2230                all_chunks.swap(i, j);
2231            }
2232            if let Some(next) = all_chunks[i].next {
2233                current = next;
2234            }
2235        }
2236
2237        Ok(Some(all_chunks))
2238    }
2239
2240    /// Removes the bundled relations from an event, if they were present.
2241    ///
2242    /// Only replaces the present if it contained bundled relations.
2243    fn strip_relations_if_present<T>(event: &mut Raw<T>) {
2244        // We're going to get rid of the `unsigned`/`m.relations` field, if it's
2245        // present.
2246        // Use a closure that returns an option so we can quickly short-circuit.
2247        let mut closure = || -> Option<()> {
2248            let mut val: serde_json::Value = event.deserialize_as().ok()?;
2249            let unsigned = val.get_mut("unsigned")?;
2250            let unsigned_obj = unsigned.as_object_mut()?;
2251            if unsigned_obj.remove("m.relations").is_some() {
2252                *event = Raw::new(&val).ok()?.cast_unchecked();
2253            }
2254            None
2255        };
2256        let _ = closure();
2257    }
2258
2259    fn strip_relations_from_event(ev: &mut Event) {
2260        match &mut ev.kind {
2261            TimelineEventKind::Decrypted(decrypted) => {
2262                // Remove all information about encryption info for
2263                // the bundled events.
2264                decrypted.unsigned_encryption_info = None;
2265
2266                // Remove the `unsigned`/`m.relations` field, if needs be.
2267                strip_relations_if_present(&mut decrypted.event);
2268            }
2269
2270            TimelineEventKind::UnableToDecrypt { event, .. }
2271            | TimelineEventKind::PlainText { event } => {
2272                strip_relations_if_present(event);
2273            }
2274        }
2275    }
2276
2277    /// Strips the bundled relations from a collection of events.
2278    fn strip_relations_from_events(items: &mut [Event]) {
2279        for ev in items.iter_mut() {
2280            strip_relations_from_event(ev);
2281        }
2282    }
2283
2284    /// Implementation of [`RoomEventCacheStateLockReadGuard::find_event`] and
2285    /// [`RoomEventCacheStateLockWriteGuard::find_event`].
2286    async fn find_event(
2287        event_id: &EventId,
2288        room_id: &RoomId,
2289        room_linked_chunk: &EventLinkedChunk,
2290        store: &EventCacheStoreLockGuard,
2291    ) -> Result<Option<(EventLocation, Event)>, EventCacheError> {
2292        // There are supposedly fewer events loaded in memory than in the store. Let's
2293        // start by looking up in the `EventLinkedChunk`.
2294        for (position, event) in room_linked_chunk.revents() {
2295            if event.event_id().as_deref() == Some(event_id) {
2296                return Ok(Some((EventLocation::Memory(position), event.clone())));
2297            }
2298        }
2299
2300        Ok(store.find_event(room_id, event_id).await?.map(|event| (EventLocation::Store, event)))
2301    }
2302
2303    /// Implementation of
2304    /// [`RoomEventCacheStateLockReadGuard::find_event_with_relations`] and
2305    /// [`RoomEventCacheStateLockWriteGuard::find_event_with_relations`].
2306    async fn find_event_with_relations(
2307        event_id: &EventId,
2308        room_id: &RoomId,
2309        filters: Option<Vec<RelationType>>,
2310        room_linked_chunk: &EventLinkedChunk,
2311        store: &EventCacheStoreLockGuard,
2312    ) -> Result<Option<(Event, Vec<Event>)>, EventCacheError> {
2313        // First, hit storage to get the target event and its related events.
2314        let found = store.find_event(room_id, event_id).await?;
2315
2316        let Some(target) = found else {
2317            // We haven't found the event: return early.
2318            return Ok(None);
2319        };
2320
2321        // Then, initialize the stack with all the related events, to find the
2322        // transitive closure of all the related events.
2323        let mut related = store.find_event_relations(room_id, event_id, filters.as_deref()).await?;
2324        let mut stack =
2325            related.iter().filter_map(|(event, _pos)| event.event_id()).collect::<Vec<_>>();
2326
2327        // Also keep track of already seen events, in case there's a loop in the
2328        // relation graph.
2329        let mut already_seen = HashSet::new();
2330        already_seen.insert(event_id.to_owned());
2331
2332        let mut num_iters = 1;
2333
2334        // Find the related event for each previously-related event.
2335        while let Some(event_id) = stack.pop() {
2336            if !already_seen.insert(event_id.clone()) {
2337                // Skip events we've already seen.
2338                continue;
2339            }
2340
2341            let other_related =
2342                store.find_event_relations(room_id, &event_id, filters.as_deref()).await?;
2343
2344            stack.extend(other_related.iter().filter_map(|(event, _pos)| event.event_id()));
2345            related.extend(other_related);
2346
2347            num_iters += 1;
2348        }
2349
2350        trace!(num_related = %related.len(), num_iters, "computed transitive closure of related events");
2351
2352        // Sort the results by their positions in the linked chunk, if available.
2353        //
2354        // If an event doesn't have a known position, it goes to the start of the array.
2355        related.sort_by(|(_, lhs), (_, rhs)| {
2356            use std::cmp::Ordering;
2357
2358            match (lhs, rhs) {
2359                (None, None) => Ordering::Equal,
2360                (None, Some(_)) => Ordering::Less,
2361                (Some(_), None) => Ordering::Greater,
2362                (Some(lhs), Some(rhs)) => {
2363                    let lhs = room_linked_chunk.event_order(*lhs);
2364                    let rhs = room_linked_chunk.event_order(*rhs);
2365
2366                    // The events should have a definite position, but in the case they don't,
2367                    // still consider that not having a position means you'll end at the start
2368                    // of the array.
2369                    match (lhs, rhs) {
2370                        (None, None) => Ordering::Equal,
2371                        (None, Some(_)) => Ordering::Less,
2372                        (Some(_), None) => Ordering::Greater,
2373                        (Some(lhs), Some(rhs)) => lhs.cmp(&rhs),
2374                    }
2375                }
2376            }
2377        });
2378
2379        // Keep only the events, not their positions.
2380        let related = related.into_iter().map(|(event, _pos)| event).collect();
2381
2382        Ok(Some((target, related)))
2383    }
2384}
2385
2386/// An enum representing where an event has been found.
2387pub(super) enum EventLocation {
2388    /// Event lives in memory (and likely in the store!).
2389    Memory(Position),
2390
2391    /// Event lives in the store only, it has not been loaded in memory yet.
2392    Store,
2393}
2394
2395pub(super) use private::RoomEventCacheStateLock;
2396
2397#[cfg(test)]
2398mod tests {
2399    use matrix_sdk_base::event_cache::Event;
2400    use matrix_sdk_test::{async_test, event_factory::EventFactory};
2401    use ruma::{
2402        RoomId, event_id,
2403        events::{relation::RelationType, room::message::RoomMessageEventContentWithoutRelation},
2404        room_id, user_id,
2405    };
2406
2407    use crate::test_utils::logged_in_client;
2408
2409    #[async_test]
2410    async fn test_find_event_by_id_with_edit_relation() {
2411        let original_id = event_id!("$original");
2412        let related_id = event_id!("$related");
2413        let room_id = room_id!("!galette:saucisse.bzh");
2414        let f = EventFactory::new().room(room_id).sender(user_id!("@ben:saucisse.bzh"));
2415
2416        assert_relations(
2417            room_id,
2418            f.text_msg("Original event").event_id(original_id).into(),
2419            f.text_msg("* An edited event")
2420                .edit(
2421                    original_id,
2422                    RoomMessageEventContentWithoutRelation::text_plain("And edited event"),
2423                )
2424                .event_id(related_id)
2425                .into(),
2426            f,
2427        )
2428        .await;
2429    }
2430
2431    #[async_test]
2432    async fn test_find_event_by_id_with_thread_reply_relation() {
2433        let original_id = event_id!("$original");
2434        let related_id = event_id!("$related");
2435        let room_id = room_id!("!galette:saucisse.bzh");
2436        let f = EventFactory::new().room(room_id).sender(user_id!("@ben:saucisse.bzh"));
2437
2438        assert_relations(
2439            room_id,
2440            f.text_msg("Original event").event_id(original_id).into(),
2441            f.text_msg("A reply").in_thread(original_id, related_id).event_id(related_id).into(),
2442            f,
2443        )
2444        .await;
2445    }
2446
2447    #[async_test]
2448    async fn test_find_event_by_id_with_reaction_relation() {
2449        let original_id = event_id!("$original");
2450        let related_id = event_id!("$related");
2451        let room_id = room_id!("!galette:saucisse.bzh");
2452        let f = EventFactory::new().room(room_id).sender(user_id!("@ben:saucisse.bzh"));
2453
2454        assert_relations(
2455            room_id,
2456            f.text_msg("Original event").event_id(original_id).into(),
2457            f.reaction(original_id, ":D").event_id(related_id).into(),
2458            f,
2459        )
2460        .await;
2461    }
2462
2463    #[async_test]
2464    async fn test_find_event_by_id_with_poll_response_relation() {
2465        let original_id = event_id!("$original");
2466        let related_id = event_id!("$related");
2467        let room_id = room_id!("!galette:saucisse.bzh");
2468        let f = EventFactory::new().room(room_id).sender(user_id!("@ben:saucisse.bzh"));
2469
2470        assert_relations(
2471            room_id,
2472            f.poll_start("Poll start event", "A poll question", vec!["An answer"])
2473                .event_id(original_id)
2474                .into(),
2475            f.poll_response(vec!["1"], original_id).event_id(related_id).into(),
2476            f,
2477        )
2478        .await;
2479    }
2480
2481    #[async_test]
2482    async fn test_find_event_by_id_with_poll_end_relation() {
2483        let original_id = event_id!("$original");
2484        let related_id = event_id!("$related");
2485        let room_id = room_id!("!galette:saucisse.bzh");
2486        let f = EventFactory::new().room(room_id).sender(user_id!("@ben:saucisse.bzh"));
2487
2488        assert_relations(
2489            room_id,
2490            f.poll_start("Poll start event", "A poll question", vec!["An answer"])
2491                .event_id(original_id)
2492                .into(),
2493            f.poll_end("Poll ended", original_id).event_id(related_id).into(),
2494            f,
2495        )
2496        .await;
2497    }
2498
2499    #[async_test]
2500    async fn test_find_event_by_id_with_filtered_relationships() {
2501        let original_id = event_id!("$original");
2502        let related_id = event_id!("$related");
2503        let associated_related_id = event_id!("$recursive_related");
2504        let room_id = room_id!("!galette:saucisse.bzh");
2505        let event_factory = EventFactory::new().room(room_id).sender(user_id!("@ben:saucisse.bzh"));
2506
2507        let original_event = event_factory.text_msg("Original event").event_id(original_id).into();
2508        let related_event = event_factory
2509            .text_msg("* Edited event")
2510            .edit(original_id, RoomMessageEventContentWithoutRelation::text_plain("Edited event"))
2511            .event_id(related_id)
2512            .into();
2513        let associated_related_event =
2514            event_factory.reaction(related_id, "🤡").event_id(associated_related_id).into();
2515
2516        let client = logged_in_client(None).await;
2517
2518        let event_cache = client.event_cache();
2519        event_cache.subscribe().unwrap();
2520
2521        client.base_client().get_or_create_room(room_id, matrix_sdk_base::RoomState::Joined);
2522        let room = client.get_room(room_id).unwrap();
2523
2524        let (room_event_cache, _drop_handles) = room.event_cache().await.unwrap();
2525
2526        // Save the original event.
2527        room_event_cache.save_events([original_event]).await;
2528
2529        // Save the related event.
2530        room_event_cache.save_events([related_event]).await;
2531
2532        // Save the associated related event, which redacts the related event.
2533        room_event_cache.save_events([associated_related_event]).await;
2534
2535        let filter = Some(vec![RelationType::Replacement]);
2536        let (event, related_events) = room_event_cache
2537            .find_event_with_relations(original_id, filter)
2538            .await
2539            .expect("Failed to find the event with relations")
2540            .expect("Event has no relation");
2541        // Fetched event is the right one.
2542        let cached_event_id = event.event_id().unwrap();
2543        assert_eq!(cached_event_id, original_id);
2544
2545        // There's only the edit event (an edit event can't have its own edit event).
2546        assert_eq!(related_events.len(), 1);
2547
2548        let related_event_id = related_events[0].event_id().unwrap();
2549        assert_eq!(related_event_id, related_id);
2550
2551        // Now we'll filter threads instead, there should be no related events
2552        let filter = Some(vec![RelationType::Thread]);
2553        let (event, related_events) = room_event_cache
2554            .find_event_with_relations(original_id, filter)
2555            .await
2556            .expect("Failed to find the event with relations")
2557            .expect("Event has no relation");
2558
2559        // Fetched event is the right one.
2560        let cached_event_id = event.event_id().unwrap();
2561        assert_eq!(cached_event_id, original_id);
2562        // No Thread related events found
2563        assert!(related_events.is_empty());
2564    }
2565
2566    #[async_test]
2567    async fn test_find_event_by_id_with_recursive_relation() {
2568        let original_id = event_id!("$original");
2569        let related_id = event_id!("$related");
2570        let associated_related_id = event_id!("$recursive_related");
2571        let room_id = room_id!("!galette:saucisse.bzh");
2572        let event_factory = EventFactory::new().room(room_id).sender(user_id!("@ben:saucisse.bzh"));
2573
2574        let original_event = event_factory.text_msg("Original event").event_id(original_id).into();
2575        let related_event = event_factory
2576            .text_msg("* Edited event")
2577            .edit(original_id, RoomMessageEventContentWithoutRelation::text_plain("Edited event"))
2578            .event_id(related_id)
2579            .into();
2580        let associated_related_event =
2581            event_factory.reaction(related_id, "👍").event_id(associated_related_id).into();
2582
2583        let client = logged_in_client(None).await;
2584
2585        let event_cache = client.event_cache();
2586        event_cache.subscribe().unwrap();
2587
2588        client.base_client().get_or_create_room(room_id, matrix_sdk_base::RoomState::Joined);
2589        let room = client.get_room(room_id).unwrap();
2590
2591        let (room_event_cache, _drop_handles) = room.event_cache().await.unwrap();
2592
2593        // Save the original event.
2594        room_event_cache.save_events([original_event]).await;
2595
2596        // Save the related event.
2597        room_event_cache.save_events([related_event]).await;
2598
2599        // Save the associated related event, which redacts the related event.
2600        room_event_cache.save_events([associated_related_event]).await;
2601
2602        let (event, related_events) = room_event_cache
2603            .find_event_with_relations(original_id, None)
2604            .await
2605            .expect("Failed to find the event with relations")
2606            .expect("Event has no relation");
2607        // Fetched event is the right one.
2608        let cached_event_id = event.event_id().unwrap();
2609        assert_eq!(cached_event_id, original_id);
2610
2611        // There are both the related id and the associatively related id
2612        assert_eq!(related_events.len(), 2);
2613
2614        let related_event_id = related_events[0].event_id().unwrap();
2615        assert_eq!(related_event_id, related_id);
2616        let related_event_id = related_events[1].event_id().unwrap();
2617        assert_eq!(related_event_id, associated_related_id);
2618    }
2619
2620    async fn assert_relations(
2621        room_id: &RoomId,
2622        original_event: Event,
2623        related_event: Event,
2624        event_factory: EventFactory,
2625    ) {
2626        let client = logged_in_client(None).await;
2627
2628        let event_cache = client.event_cache();
2629        event_cache.subscribe().unwrap();
2630
2631        client.base_client().get_or_create_room(room_id, matrix_sdk_base::RoomState::Joined);
2632        let room = client.get_room(room_id).unwrap();
2633
2634        let (room_event_cache, _drop_handles) = room.event_cache().await.unwrap();
2635
2636        // Save the original event.
2637        let original_event_id = original_event.event_id().unwrap();
2638        room_event_cache.save_events([original_event]).await;
2639
2640        // Save an unrelated event to check it's not in the related events list.
2641        let unrelated_id = event_id!("$2");
2642        room_event_cache
2643            .save_events([event_factory
2644                .text_msg("An unrelated event")
2645                .event_id(unrelated_id)
2646                .into()])
2647            .await;
2648
2649        // Save the related event.
2650        let related_id = related_event.event_id().unwrap();
2651        room_event_cache.save_events([related_event]).await;
2652
2653        let (event, related_events) = room_event_cache
2654            .find_event_with_relations(&original_event_id, None)
2655            .await
2656            .expect("Failed to find the event with relations")
2657            .expect("Event has no relation");
2658        // Fetched event is the right one.
2659        let cached_event_id = event.event_id().unwrap();
2660        assert_eq!(cached_event_id, original_event_id);
2661
2662        // There is only the actually related event in the related ones
2663        let related_event_id = related_events[0].event_id().unwrap();
2664        assert_eq!(related_event_id, related_id);
2665    }
2666}
2667
2668#[cfg(all(test, not(target_family = "wasm")))] // This uses the cross-process lock, so needs time support.
2669mod timed_tests {
2670    use std::{ops::Not, sync::Arc};
2671
2672    use assert_matches::assert_matches;
2673    use assert_matches2::assert_let;
2674    use eyeball_im::VectorDiff;
2675    use futures_util::FutureExt;
2676    use matrix_sdk_base::{
2677        event_cache::{
2678            Gap,
2679            store::{EventCacheStore as _, MemoryStore},
2680        },
2681        linked_chunk::{
2682            ChunkContent, ChunkIdentifier, LinkedChunkId, Position, Update,
2683            lazy_loader::from_all_chunks,
2684        },
2685        store::StoreConfig,
2686        sync::{JoinedRoomUpdate, Timeline},
2687    };
2688    use matrix_sdk_test::{ALICE, BOB, async_test, event_factory::EventFactory};
2689    use ruma::{
2690        EventId, OwnedUserId, event_id,
2691        events::{AnySyncMessageLikeEvent, AnySyncTimelineEvent},
2692        room_id, user_id,
2693    };
2694    use tokio::task::yield_now;
2695
2696    use super::RoomEventCacheGenericUpdate;
2697    use crate::{
2698        assert_let_timeout,
2699        event_cache::{RoomEventCache, RoomEventCacheUpdate, room::LoadMoreEventsBackwardsOutcome},
2700        test_utils::client::MockClientBuilder,
2701    };
2702
2703    #[async_test]
2704    async fn test_write_to_storage() {
2705        let room_id = room_id!("!galette:saucisse.bzh");
2706        let f = EventFactory::new().room(room_id).sender(user_id!("@ben:saucisse.bzh"));
2707
2708        let event_cache_store = Arc::new(MemoryStore::new());
2709
2710        let client = MockClientBuilder::new(None)
2711            .on_builder(|builder| {
2712                builder.store_config(
2713                    StoreConfig::new("hodlor".to_owned())
2714                        .event_cache_store(event_cache_store.clone()),
2715                )
2716            })
2717            .build()
2718            .await;
2719
2720        let event_cache = client.event_cache();
2721
2722        // Don't forget to subscribe and like.
2723        event_cache.subscribe().unwrap();
2724
2725        client.base_client().get_or_create_room(room_id, matrix_sdk_base::RoomState::Joined);
2726        let room = client.get_room(room_id).unwrap();
2727
2728        let mut generic_stream = event_cache.subscribe_to_room_generic_updates();
2729        let (room_event_cache, _drop_handles) = room.event_cache().await.unwrap();
2730
2731        // Propagate an update for a message and a prev-batch token.
2732        let timeline = Timeline {
2733            limited: true,
2734            prev_batch: Some("raclette".to_owned()),
2735            events: vec![f.text_msg("hey yo").sender(*ALICE).into_event()],
2736        };
2737
2738        room_event_cache
2739            .inner
2740            .handle_joined_room_update(JoinedRoomUpdate { timeline, ..Default::default() })
2741            .await
2742            .unwrap();
2743
2744        // Just checking the generic update is correct.
2745        assert_matches!(
2746            generic_stream.recv().await,
2747            Ok(RoomEventCacheGenericUpdate { room_id: expected_room_id }) => {
2748                assert_eq!(expected_room_id, room_id);
2749            }
2750        );
2751
2752        // Check the storage.
2753        let linked_chunk = from_all_chunks::<3, _, _>(
2754            event_cache_store.load_all_chunks(LinkedChunkId::Room(room_id)).await.unwrap(),
2755        )
2756        .unwrap()
2757        .unwrap();
2758
2759        assert_eq!(linked_chunk.chunks().count(), 2);
2760
2761        let mut chunks = linked_chunk.chunks();
2762
2763        // We start with the gap.
2764        assert_matches!(chunks.next().unwrap().content(), ChunkContent::Gap(gap) => {
2765            assert_eq!(gap.prev_token, "raclette");
2766        });
2767
2768        // Then we have the stored event.
2769        assert_matches!(chunks.next().unwrap().content(), ChunkContent::Items(events) => {
2770            assert_eq!(events.len(), 1);
2771            let deserialized = events[0].raw().deserialize().unwrap();
2772            assert_let!(AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::RoomMessage(msg)) = deserialized);
2773            assert_eq!(msg.as_original().unwrap().content.body(), "hey yo");
2774        });
2775
2776        // That's all, folks!
2777        assert!(chunks.next().is_none());
2778    }
2779
2780    #[async_test]
2781    async fn test_write_to_storage_strips_bundled_relations() {
2782        let room_id = room_id!("!galette:saucisse.bzh");
2783        let f = EventFactory::new().room(room_id).sender(user_id!("@ben:saucisse.bzh"));
2784
2785        let event_cache_store = Arc::new(MemoryStore::new());
2786
2787        let client = MockClientBuilder::new(None)
2788            .on_builder(|builder| {
2789                builder.store_config(
2790                    StoreConfig::new("hodlor".to_owned())
2791                        .event_cache_store(event_cache_store.clone()),
2792                )
2793            })
2794            .build()
2795            .await;
2796
2797        let event_cache = client.event_cache();
2798
2799        // Don't forget to subscribe and like.
2800        event_cache.subscribe().unwrap();
2801
2802        client.base_client().get_or_create_room(room_id, matrix_sdk_base::RoomState::Joined);
2803        let room = client.get_room(room_id).unwrap();
2804
2805        let mut generic_stream = event_cache.subscribe_to_room_generic_updates();
2806        let (room_event_cache, _drop_handles) = room.event_cache().await.unwrap();
2807
2808        // Propagate an update for a message with bundled relations.
2809        let ev = f
2810            .text_msg("hey yo")
2811            .sender(*ALICE)
2812            .with_bundled_edit(f.text_msg("Hello, Kind Sir").sender(*ALICE))
2813            .into_event();
2814
2815        let timeline = Timeline { limited: false, prev_batch: None, events: vec![ev] };
2816
2817        room_event_cache
2818            .inner
2819            .handle_joined_room_update(JoinedRoomUpdate { timeline, ..Default::default() })
2820            .await
2821            .unwrap();
2822
2823        // Just checking the generic update is correct.
2824        assert_matches!(
2825            generic_stream.recv().await,
2826            Ok(RoomEventCacheGenericUpdate { room_id: expected_room_id }) => {
2827                assert_eq!(expected_room_id, room_id);
2828            }
2829        );
2830
2831        // The in-memory linked chunk keeps the bundled relation.
2832        {
2833            let events = room_event_cache.events().await.unwrap();
2834
2835            assert_eq!(events.len(), 1);
2836
2837            let ev = events[0].raw().deserialize().unwrap();
2838            assert_let!(
2839                AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::RoomMessage(msg)) = ev
2840            );
2841
2842            let original = msg.as_original().unwrap();
2843            assert_eq!(original.content.body(), "hey yo");
2844            assert!(original.unsigned.relations.replace.is_some());
2845        }
2846
2847        // The one in storage does not.
2848        let linked_chunk = from_all_chunks::<3, _, _>(
2849            event_cache_store.load_all_chunks(LinkedChunkId::Room(room_id)).await.unwrap(),
2850        )
2851        .unwrap()
2852        .unwrap();
2853
2854        assert_eq!(linked_chunk.chunks().count(), 1);
2855
2856        let mut chunks = linked_chunk.chunks();
2857        assert_matches!(chunks.next().unwrap().content(), ChunkContent::Items(events) => {
2858            assert_eq!(events.len(), 1);
2859
2860            let ev = events[0].raw().deserialize().unwrap();
2861            assert_let!(AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::RoomMessage(msg)) = ev);
2862
2863            let original = msg.as_original().unwrap();
2864            assert_eq!(original.content.body(), "hey yo");
2865            assert!(original.unsigned.relations.replace.is_none());
2866        });
2867
2868        // That's all, folks!
2869        assert!(chunks.next().is_none());
2870    }
2871
2872    #[async_test]
2873    async fn test_clear() {
2874        let room_id = room_id!("!galette:saucisse.bzh");
2875        let f = EventFactory::new().room(room_id).sender(user_id!("@ben:saucisse.bzh"));
2876
2877        let event_cache_store = Arc::new(MemoryStore::new());
2878
2879        let event_id1 = event_id!("$1");
2880        let event_id2 = event_id!("$2");
2881
2882        let ev1 = f.text_msg("hello world").sender(*ALICE).event_id(event_id1).into_event();
2883        let ev2 = f.text_msg("how's it going").sender(*BOB).event_id(event_id2).into_event();
2884
2885        // Prefill the store with some data.
2886        event_cache_store
2887            .handle_linked_chunk_updates(
2888                LinkedChunkId::Room(room_id),
2889                vec![
2890                    // An empty items chunk.
2891                    Update::NewItemsChunk {
2892                        previous: None,
2893                        new: ChunkIdentifier::new(0),
2894                        next: None,
2895                    },
2896                    // A gap chunk.
2897                    Update::NewGapChunk {
2898                        previous: Some(ChunkIdentifier::new(0)),
2899                        // Chunk IDs aren't supposed to be ordered, so use a random value here.
2900                        new: ChunkIdentifier::new(42),
2901                        next: None,
2902                        gap: Gap { prev_token: "comté".to_owned() },
2903                    },
2904                    // Another items chunk, non-empty this time.
2905                    Update::NewItemsChunk {
2906                        previous: Some(ChunkIdentifier::new(42)),
2907                        new: ChunkIdentifier::new(1),
2908                        next: None,
2909                    },
2910                    Update::PushItems {
2911                        at: Position::new(ChunkIdentifier::new(1), 0),
2912                        items: vec![ev1.clone()],
2913                    },
2914                    // And another items chunk, non-empty again.
2915                    Update::NewItemsChunk {
2916                        previous: Some(ChunkIdentifier::new(1)),
2917                        new: ChunkIdentifier::new(2),
2918                        next: None,
2919                    },
2920                    Update::PushItems {
2921                        at: Position::new(ChunkIdentifier::new(2), 0),
2922                        items: vec![ev2.clone()],
2923                    },
2924                ],
2925            )
2926            .await
2927            .unwrap();
2928
2929        let client = MockClientBuilder::new(None)
2930            .on_builder(|builder| {
2931                builder.store_config(
2932                    StoreConfig::new("hodlor".to_owned())
2933                        .event_cache_store(event_cache_store.clone()),
2934                )
2935            })
2936            .build()
2937            .await;
2938
2939        let event_cache = client.event_cache();
2940
2941        // Don't forget to subscribe and like.
2942        event_cache.subscribe().unwrap();
2943
2944        client.base_client().get_or_create_room(room_id, matrix_sdk_base::RoomState::Joined);
2945        let room = client.get_room(room_id).unwrap();
2946
2947        let (room_event_cache, _drop_handles) = room.event_cache().await.unwrap();
2948
2949        let (items, mut stream) = room_event_cache.subscribe().await.unwrap();
2950        let mut generic_stream = event_cache.subscribe_to_room_generic_updates();
2951
2952        // The rooms knows about all cached events.
2953        {
2954            assert!(room_event_cache.find_event(event_id1).await.unwrap().is_some());
2955            assert!(room_event_cache.find_event(event_id2).await.unwrap().is_some());
2956        }
2957
2958        // But only part of events are loaded from the store
2959        {
2960            // The room must contain only one event because only one chunk has been loaded.
2961            assert_eq!(items.len(), 1);
2962            assert_eq!(items[0].event_id().unwrap(), event_id2);
2963
2964            assert!(stream.is_empty());
2965        }
2966
2967        // Let's load more chunks to load all events.
2968        {
2969            room_event_cache.pagination().run_backwards_once(20).await.unwrap();
2970
2971            assert_let_timeout!(
2972                Ok(RoomEventCacheUpdate::UpdateTimelineEvents { diffs, .. }) = stream.recv()
2973            );
2974            assert_eq!(diffs.len(), 1);
2975            assert_matches!(&diffs[0], VectorDiff::Insert { index: 0, value: event } => {
2976                // Here you are `event_id1`!
2977                assert_eq!(event.event_id().unwrap(), event_id1);
2978            });
2979
2980            assert!(stream.is_empty());
2981        }
2982
2983        // After clearing,…
2984        room_event_cache.clear().await.unwrap();
2985
2986        //… we get an update that the content has been cleared.
2987        assert_let_timeout!(
2988            Ok(RoomEventCacheUpdate::UpdateTimelineEvents { diffs, .. }) = stream.recv()
2989        );
2990        assert_eq!(diffs.len(), 1);
2991        assert_let!(VectorDiff::Clear = &diffs[0]);
2992
2993        // … same with a generic update.
2994        assert_let_timeout!(
2995            Ok(RoomEventCacheGenericUpdate { room_id: received_room_id }) = generic_stream.recv()
2996        );
2997        assert_eq!(received_room_id, room_id);
2998
2999        // Events individually are not forgotten by the event cache, after clearing a
3000        // room.
3001        assert!(room_event_cache.find_event(event_id1).await.unwrap().is_some());
3002
3003        // But their presence in a linked chunk is forgotten.
3004        let items = room_event_cache.events().await.unwrap();
3005        assert!(items.is_empty());
3006
3007        // The event cache store too.
3008        let linked_chunk = from_all_chunks::<3, _, _>(
3009            event_cache_store.load_all_chunks(LinkedChunkId::Room(room_id)).await.unwrap(),
3010        )
3011        .unwrap()
3012        .unwrap();
3013
3014        // Note: while the event cache store could return `None` here, clearing it will
3015        // reset it to its initial form, maintaining the invariant that it
3016        // contains a single items chunk that's empty.
3017        assert_eq!(linked_chunk.num_items(), 0);
3018    }
3019
3020    #[async_test]
3021    async fn test_load_from_storage() {
3022        let room_id = room_id!("!galette:saucisse.bzh");
3023        let f = EventFactory::new().room(room_id).sender(user_id!("@ben:saucisse.bzh"));
3024
3025        let event_cache_store = Arc::new(MemoryStore::new());
3026
3027        let event_id1 = event_id!("$1");
3028        let event_id2 = event_id!("$2");
3029
3030        let ev1 = f.text_msg("hello world").sender(*ALICE).event_id(event_id1).into_event();
3031        let ev2 = f.text_msg("how's it going").sender(*BOB).event_id(event_id2).into_event();
3032
3033        // Prefill the store with some data.
3034        event_cache_store
3035            .handle_linked_chunk_updates(
3036                LinkedChunkId::Room(room_id),
3037                vec![
3038                    // An empty items chunk.
3039                    Update::NewItemsChunk {
3040                        previous: None,
3041                        new: ChunkIdentifier::new(0),
3042                        next: None,
3043                    },
3044                    // A gap chunk.
3045                    Update::NewGapChunk {
3046                        previous: Some(ChunkIdentifier::new(0)),
3047                        // Chunk IDs aren't supposed to be ordered, so use a random value here.
3048                        new: ChunkIdentifier::new(42),
3049                        next: None,
3050                        gap: Gap { prev_token: "cheddar".to_owned() },
3051                    },
3052                    // Another items chunk, non-empty this time.
3053                    Update::NewItemsChunk {
3054                        previous: Some(ChunkIdentifier::new(42)),
3055                        new: ChunkIdentifier::new(1),
3056                        next: None,
3057                    },
3058                    Update::PushItems {
3059                        at: Position::new(ChunkIdentifier::new(1), 0),
3060                        items: vec![ev1.clone()],
3061                    },
3062                    // And another items chunk, non-empty again.
3063                    Update::NewItemsChunk {
3064                        previous: Some(ChunkIdentifier::new(1)),
3065                        new: ChunkIdentifier::new(2),
3066                        next: None,
3067                    },
3068                    Update::PushItems {
3069                        at: Position::new(ChunkIdentifier::new(2), 0),
3070                        items: vec![ev2.clone()],
3071                    },
3072                ],
3073            )
3074            .await
3075            .unwrap();
3076
3077        let client = MockClientBuilder::new(None)
3078            .on_builder(|builder| {
3079                builder.store_config(
3080                    StoreConfig::new("hodlor".to_owned())
3081                        .event_cache_store(event_cache_store.clone()),
3082                )
3083            })
3084            .build()
3085            .await;
3086
3087        let event_cache = client.event_cache();
3088
3089        // Don't forget to subscribe and like.
3090        event_cache.subscribe().unwrap();
3091
3092        // Let's check whether the generic updates are received for the initialisation.
3093        let mut generic_stream = event_cache.subscribe_to_room_generic_updates();
3094
3095        client.base_client().get_or_create_room(room_id, matrix_sdk_base::RoomState::Joined);
3096        let room = client.get_room(room_id).unwrap();
3097
3098        let (room_event_cache, _drop_handles) = room.event_cache().await.unwrap();
3099
3100        // The room event cache has been loaded. A generic update must have been
3101        // triggered.
3102        assert_matches!(
3103            generic_stream.recv().await,
3104            Ok(RoomEventCacheGenericUpdate { room_id: expected_room_id }) => {
3105                assert_eq!(room_id, expected_room_id);
3106            }
3107        );
3108
3109        let (items, mut stream) = room_event_cache.subscribe().await.unwrap();
3110
3111        // The initial items contain one event because only the last chunk is loaded by
3112        // default.
3113        assert_eq!(items.len(), 1);
3114        assert_eq!(items[0].event_id().unwrap(), event_id2);
3115        assert!(stream.is_empty());
3116
3117        // The event cache knows only all events though, even if they aren't loaded.
3118        assert!(room_event_cache.find_event(event_id1).await.unwrap().is_some());
3119        assert!(room_event_cache.find_event(event_id2).await.unwrap().is_some());
3120
3121        // Let's paginate to load more events.
3122        room_event_cache.pagination().run_backwards_once(20).await.unwrap();
3123
3124        assert_let_timeout!(
3125            Ok(RoomEventCacheUpdate::UpdateTimelineEvents { diffs, .. }) = stream.recv()
3126        );
3127        assert_eq!(diffs.len(), 1);
3128        assert_matches!(&diffs[0], VectorDiff::Insert { index: 0, value: event } => {
3129            assert_eq!(event.event_id().unwrap(), event_id1);
3130        });
3131
3132        assert!(stream.is_empty());
3133
3134        // A generic update is triggered too.
3135        assert_matches!(
3136            generic_stream.recv().await,
3137            Ok(RoomEventCacheGenericUpdate { room_id: expected_room_id }) => {
3138                assert_eq!(expected_room_id, room_id);
3139            }
3140        );
3141
3142        // A new update with one of these events leads to deduplication.
3143        let timeline = Timeline { limited: false, prev_batch: None, events: vec![ev2] };
3144
3145        room_event_cache
3146            .inner
3147            .handle_joined_room_update(JoinedRoomUpdate { timeline, ..Default::default() })
3148            .await
3149            .unwrap();
3150
3151        // Just checking the generic update is correct. There is a duplicate event, so
3152        // no generic changes whatsoever!
3153        assert!(generic_stream.recv().now_or_never().is_none());
3154
3155        // The stream doesn't report these changes *yet*. Use the items vector given
3156        // when subscribing, to check that the items correspond to their new
3157        // positions. The duplicated item is removed (so it's not the first
3158        // element anymore), and it's added to the back of the list.
3159        let items = room_event_cache.events().await.unwrap();
3160        assert_eq!(items.len(), 2);
3161        assert_eq!(items[0].event_id().unwrap(), event_id1);
3162        assert_eq!(items[1].event_id().unwrap(), event_id2);
3163    }
3164
3165    #[async_test]
3166    async fn test_load_from_storage_resilient_to_failure() {
3167        let room_id = room_id!("!fondue:patate.ch");
3168        let event_cache_store = Arc::new(MemoryStore::new());
3169
3170        let event = EventFactory::new()
3171            .room(room_id)
3172            .sender(user_id!("@ben:saucisse.bzh"))
3173            .text_msg("foo")
3174            .event_id(event_id!("$42"))
3175            .into_event();
3176
3177        // Prefill the store with invalid data: two chunks that form a cycle.
3178        event_cache_store
3179            .handle_linked_chunk_updates(
3180                LinkedChunkId::Room(room_id),
3181                vec![
3182                    Update::NewItemsChunk {
3183                        previous: None,
3184                        new: ChunkIdentifier::new(0),
3185                        next: None,
3186                    },
3187                    Update::PushItems {
3188                        at: Position::new(ChunkIdentifier::new(0), 0),
3189                        items: vec![event],
3190                    },
3191                    Update::NewItemsChunk {
3192                        previous: Some(ChunkIdentifier::new(0)),
3193                        new: ChunkIdentifier::new(1),
3194                        next: Some(ChunkIdentifier::new(0)),
3195                    },
3196                ],
3197            )
3198            .await
3199            .unwrap();
3200
3201        let client = MockClientBuilder::new(None)
3202            .on_builder(|builder| {
3203                builder.store_config(
3204                    StoreConfig::new("holder".to_owned())
3205                        .event_cache_store(event_cache_store.clone()),
3206                )
3207            })
3208            .build()
3209            .await;
3210
3211        let event_cache = client.event_cache();
3212
3213        // Don't forget to subscribe and like.
3214        event_cache.subscribe().unwrap();
3215
3216        client.base_client().get_or_create_room(room_id, matrix_sdk_base::RoomState::Joined);
3217        let room = client.get_room(room_id).unwrap();
3218
3219        let (room_event_cache, _drop_handles) = room.event_cache().await.unwrap();
3220
3221        let items = room_event_cache.events().await.unwrap();
3222
3223        // Because the persisted content was invalid, the room store is reset: there are
3224        // no events in the cache.
3225        assert!(items.is_empty());
3226
3227        // Storage doesn't contain anything. It would also be valid that it contains a
3228        // single initial empty items chunk.
3229        let raw_chunks =
3230            event_cache_store.load_all_chunks(LinkedChunkId::Room(room_id)).await.unwrap();
3231        assert!(raw_chunks.is_empty());
3232    }
3233
3234    #[async_test]
3235    async fn test_no_useless_gaps() {
3236        let room_id = room_id!("!galette:saucisse.bzh");
3237
3238        let client = MockClientBuilder::new(None).build().await;
3239
3240        let event_cache = client.event_cache();
3241        event_cache.subscribe().unwrap();
3242
3243        client.base_client().get_or_create_room(room_id, matrix_sdk_base::RoomState::Joined);
3244        let room = client.get_room(room_id).unwrap();
3245        let (room_event_cache, _drop_handles) = room.event_cache().await.unwrap();
3246        let mut generic_stream = event_cache.subscribe_to_room_generic_updates();
3247
3248        let f = EventFactory::new().room(room_id).sender(*ALICE);
3249
3250        // Propagate an update including a limited timeline with one message and a
3251        // prev-batch token.
3252        room_event_cache
3253            .inner
3254            .handle_joined_room_update(JoinedRoomUpdate {
3255                timeline: Timeline {
3256                    limited: true,
3257                    prev_batch: Some("raclette".to_owned()),
3258                    events: vec![f.text_msg("hey yo").into_event()],
3259                },
3260                ..Default::default()
3261            })
3262            .await
3263            .unwrap();
3264
3265        // Just checking the generic update is correct.
3266        assert_matches!(
3267            generic_stream.recv().await,
3268            Ok(RoomEventCacheGenericUpdate { room_id: expected_room_id }) => {
3269                assert_eq!(expected_room_id, room_id);
3270            }
3271        );
3272
3273        {
3274            let mut state = room_event_cache.inner.state.write().await.unwrap();
3275
3276            let mut num_gaps = 0;
3277            let mut num_events = 0;
3278
3279            for c in state.room_linked_chunk().chunks() {
3280                match c.content() {
3281                    ChunkContent::Items(items) => num_events += items.len(),
3282                    ChunkContent::Gap(_) => num_gaps += 1,
3283                }
3284            }
3285
3286            // The limited sync unloads the chunk, so it will appear as if there are only
3287            // the events.
3288            assert_eq!(num_gaps, 0);
3289            assert_eq!(num_events, 1);
3290
3291            // But if I manually reload more of the chunk, the gap will be present.
3292            assert_matches!(
3293                state.load_more_events_backwards().await.unwrap(),
3294                LoadMoreEventsBackwardsOutcome::Gap { .. }
3295            );
3296
3297            num_gaps = 0;
3298            num_events = 0;
3299            for c in state.room_linked_chunk().chunks() {
3300                match c.content() {
3301                    ChunkContent::Items(items) => num_events += items.len(),
3302                    ChunkContent::Gap(_) => num_gaps += 1,
3303                }
3304            }
3305
3306            // The gap must have been stored.
3307            assert_eq!(num_gaps, 1);
3308            assert_eq!(num_events, 1);
3309        }
3310
3311        // Now, propagate an update for another message, but the timeline isn't limited
3312        // this time.
3313        room_event_cache
3314            .inner
3315            .handle_joined_room_update(JoinedRoomUpdate {
3316                timeline: Timeline {
3317                    limited: false,
3318                    prev_batch: Some("fondue".to_owned()),
3319                    events: vec![f.text_msg("sup").into_event()],
3320                },
3321                ..Default::default()
3322            })
3323            .await
3324            .unwrap();
3325
3326        // Just checking the generic update is correct.
3327        assert_matches!(
3328            generic_stream.recv().await,
3329            Ok(RoomEventCacheGenericUpdate { room_id: expected_room_id }) => {
3330                assert_eq!(expected_room_id, room_id);
3331            }
3332        );
3333
3334        {
3335            let state = room_event_cache.inner.state.read().await.unwrap();
3336
3337            let mut num_gaps = 0;
3338            let mut num_events = 0;
3339
3340            for c in state.room_linked_chunk().chunks() {
3341                match c.content() {
3342                    ChunkContent::Items(items) => num_events += items.len(),
3343                    ChunkContent::Gap(gap) => {
3344                        assert_eq!(gap.prev_token, "raclette");
3345                        num_gaps += 1;
3346                    }
3347                }
3348            }
3349
3350            // There's only the previous gap, no new ones.
3351            assert_eq!(num_gaps, 1);
3352            assert_eq!(num_events, 2);
3353        }
3354    }
3355
3356    #[async_test]
3357    async fn test_shrink_to_last_chunk() {
3358        let room_id = room_id!("!galette:saucisse.bzh");
3359
3360        let client = MockClientBuilder::new(None).build().await;
3361
3362        let f = EventFactory::new().room(room_id);
3363
3364        let evid1 = event_id!("$1");
3365        let evid2 = event_id!("$2");
3366
3367        let ev1 = f.text_msg("hello world").sender(*ALICE).event_id(evid1).into_event();
3368        let ev2 = f.text_msg("howdy").sender(*BOB).event_id(evid2).into_event();
3369
3370        // Fill the event cache store with an initial linked chunk with 2 events chunks.
3371        {
3372            client
3373                .event_cache_store()
3374                .lock()
3375                .await
3376                .expect("Could not acquire the event cache lock")
3377                .as_clean()
3378                .expect("Could not acquire a clean event cache lock")
3379                .handle_linked_chunk_updates(
3380                    LinkedChunkId::Room(room_id),
3381                    vec![
3382                        Update::NewItemsChunk {
3383                            previous: None,
3384                            new: ChunkIdentifier::new(0),
3385                            next: None,
3386                        },
3387                        Update::PushItems {
3388                            at: Position::new(ChunkIdentifier::new(0), 0),
3389                            items: vec![ev1],
3390                        },
3391                        Update::NewItemsChunk {
3392                            previous: Some(ChunkIdentifier::new(0)),
3393                            new: ChunkIdentifier::new(1),
3394                            next: None,
3395                        },
3396                        Update::PushItems {
3397                            at: Position::new(ChunkIdentifier::new(1), 0),
3398                            items: vec![ev2],
3399                        },
3400                    ],
3401                )
3402                .await
3403                .unwrap();
3404        }
3405
3406        let event_cache = client.event_cache();
3407        event_cache.subscribe().unwrap();
3408
3409        client.base_client().get_or_create_room(room_id, matrix_sdk_base::RoomState::Joined);
3410        let room = client.get_room(room_id).unwrap();
3411        let (room_event_cache, _drop_handles) = room.event_cache().await.unwrap();
3412
3413        // Sanity check: lazily loaded, so only includes one item at start.
3414        let (events, mut stream) = room_event_cache.subscribe().await.unwrap();
3415        assert_eq!(events.len(), 1);
3416        assert_eq!(events[0].event_id().as_deref(), Some(evid2));
3417        assert!(stream.is_empty());
3418
3419        let mut generic_stream = event_cache.subscribe_to_room_generic_updates();
3420
3421        // Force loading the full linked chunk by back-paginating.
3422        let outcome = room_event_cache.pagination().run_backwards_once(20).await.unwrap();
3423        assert_eq!(outcome.events.len(), 1);
3424        assert_eq!(outcome.events[0].event_id().as_deref(), Some(evid1));
3425        assert!(outcome.reached_start);
3426
3427        // We also get an update about the loading from the store.
3428        assert_let_timeout!(
3429            Ok(RoomEventCacheUpdate::UpdateTimelineEvents { diffs, .. }) = stream.recv()
3430        );
3431        assert_eq!(diffs.len(), 1);
3432        assert_matches!(&diffs[0], VectorDiff::Insert { index: 0, value } => {
3433            assert_eq!(value.event_id().as_deref(), Some(evid1));
3434        });
3435
3436        assert!(stream.is_empty());
3437
3438        // Same for the generic update.
3439        assert_let_timeout!(
3440            Ok(RoomEventCacheGenericUpdate { room_id: received_room_id }) = generic_stream.recv()
3441        );
3442        assert_eq!(received_room_id, room_id);
3443
3444        // Shrink the linked chunk to the last chunk.
3445        let diffs = room_event_cache
3446            .inner
3447            .state
3448            .write()
3449            .await
3450            .unwrap()
3451            .force_shrink_to_last_chunk()
3452            .await
3453            .expect("shrinking should succeed");
3454
3455        // We receive updates about the changes to the linked chunk.
3456        assert_eq!(diffs.len(), 2);
3457        assert_matches!(&diffs[0], VectorDiff::Clear);
3458        assert_matches!(&diffs[1], VectorDiff::Append { values} => {
3459            assert_eq!(values.len(), 1);
3460            assert_eq!(values[0].event_id().as_deref(), Some(evid2));
3461        });
3462
3463        assert!(stream.is_empty());
3464
3465        // No generic update is sent in this case.
3466        assert!(generic_stream.is_empty());
3467
3468        // When reading the events, we do get only the last one.
3469        let events = room_event_cache.events().await.unwrap();
3470        assert_eq!(events.len(), 1);
3471        assert_eq!(events[0].event_id().as_deref(), Some(evid2));
3472
3473        // But if we back-paginate, we don't need access to network to find out about
3474        // the previous event.
3475        let outcome = room_event_cache.pagination().run_backwards_once(20).await.unwrap();
3476        assert_eq!(outcome.events.len(), 1);
3477        assert_eq!(outcome.events[0].event_id().as_deref(), Some(evid1));
3478        assert!(outcome.reached_start);
3479    }
3480
3481    #[async_test]
3482    async fn test_room_ordering() {
3483        let room_id = room_id!("!galette:saucisse.bzh");
3484
3485        let client = MockClientBuilder::new(None).build().await;
3486
3487        let f = EventFactory::new().room(room_id).sender(*ALICE);
3488
3489        let evid1 = event_id!("$1");
3490        let evid2 = event_id!("$2");
3491        let evid3 = event_id!("$3");
3492
3493        let ev1 = f.text_msg("hello world").event_id(evid1).into_event();
3494        let ev2 = f.text_msg("howdy").sender(*BOB).event_id(evid2).into_event();
3495        let ev3 = f.text_msg("yo").event_id(evid3).into_event();
3496
3497        // Fill the event cache store with an initial linked chunk with 2 events chunks.
3498        {
3499            client
3500                .event_cache_store()
3501                .lock()
3502                .await
3503                .expect("Could not acquire the event cache lock")
3504                .as_clean()
3505                .expect("Could not acquire a clean event cache lock")
3506                .handle_linked_chunk_updates(
3507                    LinkedChunkId::Room(room_id),
3508                    vec![
3509                        Update::NewItemsChunk {
3510                            previous: None,
3511                            new: ChunkIdentifier::new(0),
3512                            next: None,
3513                        },
3514                        Update::PushItems {
3515                            at: Position::new(ChunkIdentifier::new(0), 0),
3516                            items: vec![ev1, ev2],
3517                        },
3518                        Update::NewItemsChunk {
3519                            previous: Some(ChunkIdentifier::new(0)),
3520                            new: ChunkIdentifier::new(1),
3521                            next: None,
3522                        },
3523                        Update::PushItems {
3524                            at: Position::new(ChunkIdentifier::new(1), 0),
3525                            items: vec![ev3.clone()],
3526                        },
3527                    ],
3528                )
3529                .await
3530                .unwrap();
3531        }
3532
3533        let event_cache = client.event_cache();
3534        event_cache.subscribe().unwrap();
3535
3536        client.base_client().get_or_create_room(room_id, matrix_sdk_base::RoomState::Joined);
3537        let room = client.get_room(room_id).unwrap();
3538        let (room_event_cache, _drop_handles) = room.event_cache().await.unwrap();
3539
3540        // Initially, the linked chunk only contains the last chunk, so only ev3 is
3541        // loaded.
3542        {
3543            let state = room_event_cache.inner.state.read().await.unwrap();
3544            let room_linked_chunk = state.room_linked_chunk();
3545
3546            // But we can get the order of ev1.
3547            assert_eq!(
3548                room_linked_chunk.event_order(Position::new(ChunkIdentifier::new(0), 0)),
3549                Some(0)
3550            );
3551
3552            // And that of ev2 as well.
3553            assert_eq!(
3554                room_linked_chunk.event_order(Position::new(ChunkIdentifier::new(0), 1)),
3555                Some(1)
3556            );
3557
3558            // ev3, which is loaded, also has a known ordering.
3559            let mut events = room_linked_chunk.events();
3560            let (pos, ev) = events.next().unwrap();
3561            assert_eq!(pos, Position::new(ChunkIdentifier::new(1), 0));
3562            assert_eq!(ev.event_id().as_deref(), Some(evid3));
3563            assert_eq!(room_linked_chunk.event_order(pos), Some(2));
3564
3565            // No other loaded events.
3566            assert!(events.next().is_none());
3567        }
3568
3569        // Force loading the full linked chunk by back-paginating.
3570        let outcome = room_event_cache.pagination().run_backwards_once(20).await.unwrap();
3571        assert!(outcome.reached_start);
3572
3573        // All events are now loaded, so their order is precisely their enumerated index
3574        // in a linear iteration.
3575        {
3576            let state = room_event_cache.inner.state.read().await.unwrap();
3577            let room_linked_chunk = state.room_linked_chunk();
3578
3579            for (i, (pos, _)) in room_linked_chunk.events().enumerate() {
3580                assert_eq!(room_linked_chunk.event_order(pos), Some(i));
3581            }
3582        }
3583
3584        // Handle a gappy sync with two events (including one duplicate, so
3585        // deduplication kicks in), so that the linked chunk is shrunk to the
3586        // last chunk, and that the linked chunk only contains the last two
3587        // events.
3588        let evid4 = event_id!("$4");
3589        room_event_cache
3590            .inner
3591            .handle_joined_room_update(JoinedRoomUpdate {
3592                timeline: Timeline {
3593                    limited: true,
3594                    prev_batch: Some("fondue".to_owned()),
3595                    events: vec![ev3, f.text_msg("sup").event_id(evid4).into_event()],
3596                },
3597                ..Default::default()
3598            })
3599            .await
3600            .unwrap();
3601
3602        {
3603            let state = room_event_cache.inner.state.read().await.unwrap();
3604            let room_linked_chunk = state.room_linked_chunk();
3605
3606            // After the shrink, only evid3 and evid4 are loaded.
3607            let mut events = room_linked_chunk.events();
3608
3609            let (pos, ev) = events.next().unwrap();
3610            assert_eq!(ev.event_id().as_deref(), Some(evid3));
3611            assert_eq!(room_linked_chunk.event_order(pos), Some(2));
3612
3613            let (pos, ev) = events.next().unwrap();
3614            assert_eq!(ev.event_id().as_deref(), Some(evid4));
3615            assert_eq!(room_linked_chunk.event_order(pos), Some(3));
3616
3617            // No other loaded events.
3618            assert!(events.next().is_none());
3619
3620            // But we can still get the order of previous events.
3621            assert_eq!(
3622                room_linked_chunk.event_order(Position::new(ChunkIdentifier::new(0), 0)),
3623                Some(0)
3624            );
3625            assert_eq!(
3626                room_linked_chunk.event_order(Position::new(ChunkIdentifier::new(0), 1)),
3627                Some(1)
3628            );
3629
3630            // ev3 doesn't have an order with its previous position, since it's been
3631            // deduplicated.
3632            assert_eq!(
3633                room_linked_chunk.event_order(Position::new(ChunkIdentifier::new(1), 0)),
3634                None
3635            );
3636        }
3637    }
3638
3639    #[async_test]
3640    async fn test_auto_shrink_after_all_subscribers_are_gone() {
3641        let room_id = room_id!("!galette:saucisse.bzh");
3642
3643        let client = MockClientBuilder::new(None).build().await;
3644
3645        let f = EventFactory::new().room(room_id);
3646
3647        let evid1 = event_id!("$1");
3648        let evid2 = event_id!("$2");
3649
3650        let ev1 = f.text_msg("hello world").sender(*ALICE).event_id(evid1).into_event();
3651        let ev2 = f.text_msg("howdy").sender(*BOB).event_id(evid2).into_event();
3652
3653        // Fill the event cache store with an initial linked chunk with 2 events chunks.
3654        {
3655            client
3656                .event_cache_store()
3657                .lock()
3658                .await
3659                .expect("Could not acquire the event cache lock")
3660                .as_clean()
3661                .expect("Could not acquire a clean event cache lock")
3662                .handle_linked_chunk_updates(
3663                    LinkedChunkId::Room(room_id),
3664                    vec![
3665                        Update::NewItemsChunk {
3666                            previous: None,
3667                            new: ChunkIdentifier::new(0),
3668                            next: None,
3669                        },
3670                        Update::PushItems {
3671                            at: Position::new(ChunkIdentifier::new(0), 0),
3672                            items: vec![ev1],
3673                        },
3674                        Update::NewItemsChunk {
3675                            previous: Some(ChunkIdentifier::new(0)),
3676                            new: ChunkIdentifier::new(1),
3677                            next: None,
3678                        },
3679                        Update::PushItems {
3680                            at: Position::new(ChunkIdentifier::new(1), 0),
3681                            items: vec![ev2],
3682                        },
3683                    ],
3684                )
3685                .await
3686                .unwrap();
3687        }
3688
3689        let event_cache = client.event_cache();
3690        event_cache.subscribe().unwrap();
3691
3692        client.base_client().get_or_create_room(room_id, matrix_sdk_base::RoomState::Joined);
3693        let room = client.get_room(room_id).unwrap();
3694        let (room_event_cache, _drop_handles) = room.event_cache().await.unwrap();
3695
3696        // Sanity check: lazily loaded, so only includes one item at start.
3697        let (events1, mut stream1) = room_event_cache.subscribe().await.unwrap();
3698        assert_eq!(events1.len(), 1);
3699        assert_eq!(events1[0].event_id().as_deref(), Some(evid2));
3700        assert!(stream1.is_empty());
3701
3702        // Force loading the full linked chunk by back-paginating.
3703        let outcome = room_event_cache.pagination().run_backwards_once(20).await.unwrap();
3704        assert_eq!(outcome.events.len(), 1);
3705        assert_eq!(outcome.events[0].event_id().as_deref(), Some(evid1));
3706        assert!(outcome.reached_start);
3707
3708        // We also get an update about the loading from the store. Ignore it, for this
3709        // test's sake.
3710        assert_let_timeout!(
3711            Ok(RoomEventCacheUpdate::UpdateTimelineEvents { diffs, .. }) = stream1.recv()
3712        );
3713        assert_eq!(diffs.len(), 1);
3714        assert_matches!(&diffs[0], VectorDiff::Insert { index: 0, value } => {
3715            assert_eq!(value.event_id().as_deref(), Some(evid1));
3716        });
3717
3718        assert!(stream1.is_empty());
3719
3720        // Have another subscriber.
3721        // Since it's not the first one, and the previous one loaded some more events,
3722        // the second subscribers sees them all.
3723        let (events2, stream2) = room_event_cache.subscribe().await.unwrap();
3724        assert_eq!(events2.len(), 2);
3725        assert_eq!(events2[0].event_id().as_deref(), Some(evid1));
3726        assert_eq!(events2[1].event_id().as_deref(), Some(evid2));
3727        assert!(stream2.is_empty());
3728
3729        // Drop the first stream, and wait a bit.
3730        drop(stream1);
3731        yield_now().await;
3732
3733        // The second stream remains undisturbed.
3734        assert!(stream2.is_empty());
3735
3736        // Now drop the second stream, and wait a bit.
3737        drop(stream2);
3738        yield_now().await;
3739
3740        // The linked chunk must have auto-shrunk by now.
3741
3742        {
3743            // Check the inner state: there's no more shared auto-shrinker.
3744            let state = room_event_cache.inner.state.read().await.unwrap();
3745            assert_eq!(state.subscriber_count().load(std::sync::atomic::Ordering::SeqCst), 0);
3746        }
3747
3748        // Getting the events will only give us the latest chunk.
3749        let events3 = room_event_cache.events().await.unwrap();
3750        assert_eq!(events3.len(), 1);
3751        assert_eq!(events3[0].event_id().as_deref(), Some(evid2));
3752    }
3753
3754    #[async_test]
3755    async fn test_rfind_map_event_in_memory_by() {
3756        let user_id = user_id!("@mnt_io:matrix.org");
3757        let room_id = room_id!("!raclette:patate.ch");
3758        let client = MockClientBuilder::new(None).build().await;
3759
3760        let event_factory = EventFactory::new().room(room_id);
3761
3762        let event_id_0 = event_id!("$ev0");
3763        let event_id_1 = event_id!("$ev1");
3764        let event_id_2 = event_id!("$ev2");
3765        let event_id_3 = event_id!("$ev3");
3766
3767        let event_0 =
3768            event_factory.text_msg("hello").sender(*BOB).event_id(event_id_0).into_event();
3769        let event_1 =
3770            event_factory.text_msg("world").sender(*ALICE).event_id(event_id_1).into_event();
3771        let event_2 = event_factory.text_msg("!").sender(*ALICE).event_id(event_id_2).into_event();
3772        let event_3 =
3773            event_factory.text_msg("eh!").sender(user_id).event_id(event_id_3).into_event();
3774
3775        // Fill the event cache store with an initial linked chunk of 2 chunks, and 4
3776        // events.
3777        {
3778            client
3779                .event_cache_store()
3780                .lock()
3781                .await
3782                .expect("Could not acquire the event cache lock")
3783                .as_clean()
3784                .expect("Could not acquire a clean event cache lock")
3785                .handle_linked_chunk_updates(
3786                    LinkedChunkId::Room(room_id),
3787                    vec![
3788                        Update::NewItemsChunk {
3789                            previous: None,
3790                            new: ChunkIdentifier::new(0),
3791                            next: None,
3792                        },
3793                        Update::PushItems {
3794                            at: Position::new(ChunkIdentifier::new(0), 0),
3795                            items: vec![event_3],
3796                        },
3797                        Update::NewItemsChunk {
3798                            previous: Some(ChunkIdentifier::new(0)),
3799                            new: ChunkIdentifier::new(1),
3800                            next: None,
3801                        },
3802                        Update::PushItems {
3803                            at: Position::new(ChunkIdentifier::new(1), 0),
3804                            items: vec![event_0, event_1, event_2],
3805                        },
3806                    ],
3807                )
3808                .await
3809                .unwrap();
3810        }
3811
3812        let event_cache = client.event_cache();
3813        event_cache.subscribe().unwrap();
3814
3815        client.base_client().get_or_create_room(room_id, matrix_sdk_base::RoomState::Joined);
3816        let room = client.get_room(room_id).unwrap();
3817        let (room_event_cache, _drop_handles) = room.event_cache().await.unwrap();
3818
3819        // Look for an event from `BOB`: it must be `event_0`.
3820        assert_matches!(
3821            room_event_cache
3822                .rfind_map_event_in_memory_by(|event, previous_event_id| {
3823                    (event.raw().get_field::<OwnedUserId>("sender").unwrap().as_deref() == Some(*BOB)).then(|| (event.event_id(), previous_event_id))
3824                })
3825                .await,
3826            Ok(Some((event_id, previous_event_id))) => {
3827                assert_eq!(event_id.as_deref(), Some(event_id_0));
3828                assert!(previous_event_id.is_none());
3829            }
3830        );
3831
3832        // Look for an event from `ALICE`: it must be `event_2`, right before `event_1`
3833        // because events are looked for in reverse order.
3834        assert_matches!(
3835            room_event_cache
3836                .rfind_map_event_in_memory_by(|event, previous_event_id| {
3837                    (event.raw().get_field::<OwnedUserId>("sender").unwrap().as_deref() == Some(*ALICE)).then(|| (event.event_id(), previous_event_id))
3838                })
3839                .await,
3840            Ok(Some((event_id, previous_event_id))) => {
3841                assert_eq!(event_id.as_deref(), Some(event_id_2));
3842                assert_eq!(previous_event_id.as_deref(), Some(event_id_1));
3843            }
3844        );
3845
3846        // Look for an event that is inside the storage, but not loaded.
3847        assert!(
3848            room_event_cache
3849                .rfind_map_event_in_memory_by(|event, _| {
3850                    (event.raw().get_field::<OwnedUserId>("sender").unwrap().as_deref()
3851                        == Some(user_id))
3852                    .then(|| event.event_id())
3853                })
3854                .await
3855                .unwrap()
3856                .is_none()
3857        );
3858
3859        // Look for an event that doesn't exist.
3860        assert!(
3861            room_event_cache
3862                .rfind_map_event_in_memory_by(|_, _| None::<()>)
3863                .await
3864                .unwrap()
3865                .is_none()
3866        );
3867    }
3868
3869    #[async_test]
3870    async fn test_reload_when_dirty() {
3871        let user_id = user_id!("@mnt_io:matrix.org");
3872        let room_id = room_id!("!raclette:patate.ch");
3873
3874        // The storage shared by the two clients.
3875        let event_cache_store = MemoryStore::new();
3876
3877        // Client for the process 0.
3878        let client_p0 = MockClientBuilder::new(None)
3879            .on_builder(|builder| {
3880                builder.store_config(
3881                    StoreConfig::new("process #0".to_owned())
3882                        .event_cache_store(event_cache_store.clone()),
3883                )
3884            })
3885            .build()
3886            .await;
3887
3888        // Client for the process 1.
3889        let client_p1 = MockClientBuilder::new(None)
3890            .on_builder(|builder| {
3891                builder.store_config(
3892                    StoreConfig::new("process #1".to_owned()).event_cache_store(event_cache_store),
3893                )
3894            })
3895            .build()
3896            .await;
3897
3898        let event_factory = EventFactory::new().room(room_id).sender(user_id);
3899
3900        let ev_id_0 = event_id!("$ev_0");
3901        let ev_id_1 = event_id!("$ev_1");
3902
3903        let ev_0 = event_factory.text_msg("comté").event_id(ev_id_0).into_event();
3904        let ev_1 = event_factory.text_msg("morbier").event_id(ev_id_1).into_event();
3905
3906        // Add events to the storage (shared by the two clients!).
3907        client_p0
3908            .event_cache_store()
3909            .lock()
3910            .await
3911            .expect("[p0] Could not acquire the event cache lock")
3912            .as_clean()
3913            .expect("[p0] Could not acquire a clean event cache lock")
3914            .handle_linked_chunk_updates(
3915                LinkedChunkId::Room(room_id),
3916                vec![
3917                    Update::NewItemsChunk {
3918                        previous: None,
3919                        new: ChunkIdentifier::new(0),
3920                        next: None,
3921                    },
3922                    Update::PushItems {
3923                        at: Position::new(ChunkIdentifier::new(0), 0),
3924                        items: vec![ev_0],
3925                    },
3926                    Update::NewItemsChunk {
3927                        previous: Some(ChunkIdentifier::new(0)),
3928                        new: ChunkIdentifier::new(1),
3929                        next: None,
3930                    },
3931                    Update::PushItems {
3932                        at: Position::new(ChunkIdentifier::new(1), 0),
3933                        items: vec![ev_1],
3934                    },
3935                ],
3936            )
3937            .await
3938            .unwrap();
3939
3940        // Subscribe the event caches, and create the room.
3941        let (room_event_cache_p0, room_event_cache_p1) = {
3942            let event_cache_p0 = client_p0.event_cache();
3943            event_cache_p0.subscribe().unwrap();
3944
3945            let event_cache_p1 = client_p1.event_cache();
3946            event_cache_p1.subscribe().unwrap();
3947
3948            client_p0.base_client().get_or_create_room(room_id, matrix_sdk_base::RoomState::Joined);
3949            client_p1.base_client().get_or_create_room(room_id, matrix_sdk_base::RoomState::Joined);
3950
3951            let (room_event_cache_p0, _drop_handles) =
3952                client_p0.get_room(room_id).unwrap().event_cache().await.unwrap();
3953            let (room_event_cache_p1, _drop_handles) =
3954                client_p1.get_room(room_id).unwrap().event_cache().await.unwrap();
3955
3956            (room_event_cache_p0, room_event_cache_p1)
3957        };
3958
3959        // Okay. We are ready for the test!
3960        //
3961        // First off, let's check `room_event_cache_p0` has access to the first event
3962        // loaded in-memory, then do a pagination, and see more events.
3963        let mut updates_stream_p0 = {
3964            let room_event_cache = &room_event_cache_p0;
3965
3966            let (initial_updates, mut updates_stream) =
3967                room_event_cache_p0.subscribe().await.unwrap();
3968
3969            // Initial updates contain `ev_id_1` only.
3970            assert_eq!(initial_updates.len(), 1);
3971            assert_eq!(initial_updates[0].event_id().as_deref(), Some(ev_id_1));
3972            assert!(updates_stream.is_empty());
3973
3974            // `ev_id_1` must be loaded in memory.
3975            assert!(event_loaded(room_event_cache, ev_id_1).await);
3976
3977            // `ev_id_0` must NOT be loaded in memory.
3978            assert!(event_loaded(room_event_cache, ev_id_0).await.not());
3979
3980            // Load one more event with a backpagination.
3981            room_event_cache.pagination().run_backwards_once(1).await.unwrap();
3982
3983            // A new update for `ev_id_0` must be present.
3984            assert_matches!(
3985                updates_stream.recv().await.unwrap(),
3986                RoomEventCacheUpdate::UpdateTimelineEvents { diffs, .. } => {
3987                    assert_eq!(diffs.len(), 1, "{diffs:#?}");
3988                    assert_matches!(
3989                        &diffs[0],
3990                        VectorDiff::Insert { index: 0, value: event } => {
3991                            assert_eq!(event.event_id().as_deref(), Some(ev_id_0));
3992                        }
3993                    );
3994                }
3995            );
3996
3997            // `ev_id_0` must now be loaded in memory.
3998            assert!(event_loaded(room_event_cache, ev_id_0).await);
3999
4000            updates_stream
4001        };
4002
4003        // Second, let's check `room_event_cache_p1` has the same accesses.
4004        let mut updates_stream_p1 = {
4005            let room_event_cache = &room_event_cache_p1;
4006            let (initial_updates, mut updates_stream) =
4007                room_event_cache_p1.subscribe().await.unwrap();
4008
4009            // Initial updates contain `ev_id_1` only.
4010            assert_eq!(initial_updates.len(), 1);
4011            assert_eq!(initial_updates[0].event_id().as_deref(), Some(ev_id_1));
4012            assert!(updates_stream.is_empty());
4013
4014            // `ev_id_1` must be loaded in memory.
4015            assert!(event_loaded(room_event_cache, ev_id_1).await);
4016
4017            // `ev_id_0` must NOT be loaded in memory.
4018            assert!(event_loaded(room_event_cache, ev_id_0).await.not());
4019
4020            // Load one more event with a backpagination.
4021            room_event_cache.pagination().run_backwards_once(1).await.unwrap();
4022
4023            // A new update for `ev_id_0` must be present.
4024            assert_matches!(
4025                updates_stream.recv().await.unwrap(),
4026                RoomEventCacheUpdate::UpdateTimelineEvents { diffs, .. } => {
4027                    assert_eq!(diffs.len(), 1, "{diffs:#?}");
4028                    assert_matches!(
4029                        &diffs[0],
4030                        VectorDiff::Insert { index: 0, value: event } => {
4031                            assert_eq!(event.event_id().as_deref(), Some(ev_id_0));
4032                        }
4033                    );
4034                }
4035            );
4036
4037            // `ev_id_0` must now be loaded in memory.
4038            assert!(event_loaded(room_event_cache, ev_id_0).await);
4039
4040            updates_stream
4041        };
4042
4043        // Do this a couple times, for the fun.
4044        for _ in 0..3 {
4045            // Third, because `room_event_cache_p1` has locked the store, the lock
4046            // is dirty for `room_event_cache_p0`, so it will shrink to its last
4047            // chunk!
4048            {
4049                let room_event_cache = &room_event_cache_p0;
4050                let updates_stream = &mut updates_stream_p0;
4051
4052                // `ev_id_1` must be loaded in memory, just like before.
4053                assert!(event_loaded(room_event_cache, ev_id_1).await);
4054
4055                // However, `ev_id_0` must NOT be loaded in memory. It WAS loaded, but the
4056                // state has been reloaded to its last chunk.
4057                assert!(event_loaded(room_event_cache, ev_id_0).await.not());
4058
4059                // The reload can be observed via the updates too.
4060                assert_matches!(
4061                    updates_stream.recv().await.unwrap(),
4062                    RoomEventCacheUpdate::UpdateTimelineEvents { diffs, .. } => {
4063                        assert_eq!(diffs.len(), 2, "{diffs:#?}");
4064                        assert_matches!(&diffs[0], VectorDiff::Clear);
4065                        assert_matches!(
4066                            &diffs[1],
4067                            VectorDiff::Append { values: events } => {
4068                                assert_eq!(events.len(), 1);
4069                                assert_eq!(events[0].event_id().as_deref(), Some(ev_id_1));
4070                            }
4071                        );
4072                    }
4073                );
4074
4075                // Load one more event with a backpagination.
4076                room_event_cache.pagination().run_backwards_once(1).await.unwrap();
4077
4078                // `ev_id_0` must now be loaded in memory.
4079                assert!(event_loaded(room_event_cache, ev_id_0).await);
4080
4081                // The pagination can be observed via the updates too.
4082                assert_matches!(
4083                    updates_stream.recv().await.unwrap(),
4084                    RoomEventCacheUpdate::UpdateTimelineEvents { diffs, .. } => {
4085                        assert_eq!(diffs.len(), 1, "{diffs:#?}");
4086                        assert_matches!(
4087                            &diffs[0],
4088                            VectorDiff::Insert { index: 0, value: event } => {
4089                                assert_eq!(event.event_id().as_deref(), Some(ev_id_0));
4090                            }
4091                        );
4092                    }
4093                );
4094            }
4095
4096            // Fourth, because `room_event_cache_p0` has locked the store again, the lock
4097            // is dirty for `room_event_cache_p1` too!, so it will shrink to its last
4098            // chunk!
4099            {
4100                let room_event_cache = &room_event_cache_p1;
4101                let updates_stream = &mut updates_stream_p1;
4102
4103                // `ev_id_1` must be loaded in memory, just like before.
4104                assert!(event_loaded(room_event_cache, ev_id_1).await);
4105
4106                // However, `ev_id_0` must NOT be loaded in memory. It WAS loaded, but the
4107                // state has shrunk to its last chunk.
4108                assert!(event_loaded(room_event_cache, ev_id_0).await.not());
4109
4110                // The reload can be observed via the updates too.
4111                assert_matches!(
4112                    updates_stream.recv().await.unwrap(),
4113                    RoomEventCacheUpdate::UpdateTimelineEvents { diffs, .. } => {
4114                        assert_eq!(diffs.len(), 2, "{diffs:#?}");
4115                        assert_matches!(&diffs[0], VectorDiff::Clear);
4116                        assert_matches!(
4117                            &diffs[1],
4118                            VectorDiff::Append { values: events } => {
4119                                assert_eq!(events.len(), 1);
4120                                assert_eq!(events[0].event_id().as_deref(), Some(ev_id_1));
4121                            }
4122                        );
4123                    }
4124                );
4125
4126                // Load one more event with a backpagination.
4127                room_event_cache.pagination().run_backwards_once(1).await.unwrap();
4128
4129                // `ev_id_0` must now be loaded in memory.
4130                assert!(event_loaded(room_event_cache, ev_id_0).await);
4131
4132                // The pagination can be observed via the updates too.
4133                assert_matches!(
4134                    updates_stream.recv().await.unwrap(),
4135                    RoomEventCacheUpdate::UpdateTimelineEvents { diffs, .. } => {
4136                        assert_eq!(diffs.len(), 1, "{diffs:#?}");
4137                        assert_matches!(
4138                            &diffs[0],
4139                            VectorDiff::Insert { index: 0, value: event } => {
4140                                assert_eq!(event.event_id().as_deref(), Some(ev_id_0));
4141                            }
4142                        );
4143                    }
4144                );
4145            }
4146        }
4147
4148        // Repeat that with an explicit read lock (so that we don't rely on
4149        // `event_loaded` to trigger the dirty detection).
4150        for _ in 0..3 {
4151            {
4152                let room_event_cache = &room_event_cache_p0;
4153                let updates_stream = &mut updates_stream_p0;
4154
4155                let guard = room_event_cache.inner.state.read().await.unwrap();
4156
4157                // Guard is kept alive, to ensure we can have multiple read guards alive with a
4158                // shared access.
4159                // See `RoomEventCacheStateLock::read` to learn more.
4160
4161                // The lock is no longer marked as dirty, it's been cleaned.
4162                assert!(guard.is_dirty().not());
4163
4164                // The reload can be observed via the updates too.
4165                assert_matches!(
4166                    updates_stream.recv().await.unwrap(),
4167                    RoomEventCacheUpdate::UpdateTimelineEvents { diffs, .. } => {
4168                        assert_eq!(diffs.len(), 2, "{diffs:#?}");
4169                        assert_matches!(&diffs[0], VectorDiff::Clear);
4170                        assert_matches!(
4171                            &diffs[1],
4172                            VectorDiff::Append { values: events } => {
4173                                assert_eq!(events.len(), 1);
4174                                assert_eq!(events[0].event_id().as_deref(), Some(ev_id_1));
4175                            }
4176                        );
4177                    }
4178                );
4179
4180                assert!(event_loaded(room_event_cache, ev_id_1).await);
4181                assert!(event_loaded(room_event_cache, ev_id_0).await.not());
4182
4183                // Ensure `guard` is alive up to this point (in case this test is refactored, I
4184                // want to make this super explicit).
4185                //
4186                // We drop need to drop it before the pagination because the pagination needs to
4187                // obtain a write lock.
4188                drop(guard);
4189
4190                room_event_cache.pagination().run_backwards_once(1).await.unwrap();
4191                assert!(event_loaded(room_event_cache, ev_id_0).await);
4192
4193                // The pagination can be observed via the updates too.
4194                assert_matches!(
4195                    updates_stream.recv().await.unwrap(),
4196                    RoomEventCacheUpdate::UpdateTimelineEvents { diffs, .. } => {
4197                        assert_eq!(diffs.len(), 1, "{diffs:#?}");
4198                        assert_matches!(
4199                            &diffs[0],
4200                            VectorDiff::Insert { index: 0, value: event } => {
4201                                assert_eq!(event.event_id().as_deref(), Some(ev_id_0));
4202                            }
4203                        );
4204                    }
4205                );
4206            }
4207
4208            {
4209                let room_event_cache = &room_event_cache_p1;
4210                let updates_stream = &mut updates_stream_p1;
4211
4212                let guard = room_event_cache.inner.state.read().await.unwrap();
4213
4214                // Guard is kept alive, to ensure we can have multiple read guards alive with a
4215                // shared access.
4216
4217                // The lock is no longer marked as dirty, it's been cleaned.
4218                assert!(guard.is_dirty().not());
4219
4220                // The reload can be observed via the updates too.
4221                assert_matches!(
4222                    updates_stream.recv().await.unwrap(),
4223                    RoomEventCacheUpdate::UpdateTimelineEvents { diffs, .. } => {
4224                        assert_eq!(diffs.len(), 2, "{diffs:#?}");
4225                        assert_matches!(&diffs[0], VectorDiff::Clear);
4226                        assert_matches!(
4227                            &diffs[1],
4228                            VectorDiff::Append { values: events } => {
4229                                assert_eq!(events.len(), 1);
4230                                assert_eq!(events[0].event_id().as_deref(), Some(ev_id_1));
4231                            }
4232                        );
4233                    }
4234                );
4235
4236                assert!(event_loaded(room_event_cache, ev_id_1).await);
4237                assert!(event_loaded(room_event_cache, ev_id_0).await.not());
4238
4239                // Ensure `guard` is alive up to this point (in case this test is refactored, I
4240                // want to make this super explicit).
4241                //
4242                // We drop need to drop it before the pagination because the pagination needs to
4243                // obtain a write lock.
4244                drop(guard);
4245
4246                room_event_cache.pagination().run_backwards_once(1).await.unwrap();
4247                assert!(event_loaded(room_event_cache, ev_id_0).await);
4248
4249                // The pagination can be observed via the updates too.
4250                assert_matches!(
4251                    updates_stream.recv().await.unwrap(),
4252                    RoomEventCacheUpdate::UpdateTimelineEvents { diffs, .. } => {
4253                        assert_eq!(diffs.len(), 1, "{diffs:#?}");
4254                        assert_matches!(
4255                            &diffs[0],
4256                            VectorDiff::Insert { index: 0, value: event } => {
4257                                assert_eq!(event.event_id().as_deref(), Some(ev_id_0));
4258                            }
4259                        );
4260                    }
4261                );
4262            }
4263        }
4264
4265        // Repeat that with an explicit write lock.
4266        for _ in 0..3 {
4267            {
4268                let room_event_cache = &room_event_cache_p0;
4269                let updates_stream = &mut updates_stream_p0;
4270
4271                let guard = room_event_cache.inner.state.write().await.unwrap();
4272
4273                // The lock is no longer marked as dirty, it's been cleaned.
4274                assert!(guard.is_dirty().not());
4275
4276                // The reload can be observed via the updates too.
4277                assert_matches!(
4278                    updates_stream.recv().await.unwrap(),
4279                    RoomEventCacheUpdate::UpdateTimelineEvents { diffs, .. } => {
4280                        assert_eq!(diffs.len(), 2, "{diffs:#?}");
4281                        assert_matches!(&diffs[0], VectorDiff::Clear);
4282                        assert_matches!(
4283                            &diffs[1],
4284                            VectorDiff::Append { values: events } => {
4285                                assert_eq!(events.len(), 1);
4286                                assert_eq!(events[0].event_id().as_deref(), Some(ev_id_1));
4287                            }
4288                        );
4289                    }
4290                );
4291
4292                // Guard isn't kept alive, otherwise `event_loaded` couldn't run because it
4293                // needs to obtain a read lock.
4294                drop(guard);
4295
4296                assert!(event_loaded(room_event_cache, ev_id_1).await);
4297                assert!(event_loaded(room_event_cache, ev_id_0).await.not());
4298
4299                room_event_cache.pagination().run_backwards_once(1).await.unwrap();
4300                assert!(event_loaded(room_event_cache, ev_id_0).await);
4301
4302                // The pagination can be observed via the updates too.
4303                assert_matches!(
4304                    updates_stream.recv().await.unwrap(),
4305                    RoomEventCacheUpdate::UpdateTimelineEvents { diffs, .. } => {
4306                        assert_eq!(diffs.len(), 1, "{diffs:#?}");
4307                        assert_matches!(
4308                            &diffs[0],
4309                            VectorDiff::Insert { index: 0, value: event } => {
4310                                assert_eq!(event.event_id().as_deref(), Some(ev_id_0));
4311                            }
4312                        );
4313                    }
4314                );
4315            }
4316
4317            {
4318                let room_event_cache = &room_event_cache_p1;
4319                let updates_stream = &mut updates_stream_p1;
4320
4321                let guard = room_event_cache.inner.state.write().await.unwrap();
4322
4323                // The lock is no longer marked as dirty, it's been cleaned.
4324                assert!(guard.is_dirty().not());
4325
4326                // The reload can be observed via the updates too.
4327                assert_matches!(
4328                    updates_stream.recv().await.unwrap(),
4329                    RoomEventCacheUpdate::UpdateTimelineEvents { diffs, .. } => {
4330                        assert_eq!(diffs.len(), 2, "{diffs:#?}");
4331                        assert_matches!(&diffs[0], VectorDiff::Clear);
4332                        assert_matches!(
4333                            &diffs[1],
4334                            VectorDiff::Append { values: events } => {
4335                                assert_eq!(events.len(), 1);
4336                                assert_eq!(events[0].event_id().as_deref(), Some(ev_id_1));
4337                            }
4338                        );
4339                    }
4340                );
4341
4342                // Guard isn't kept alive, otherwise `event_loaded` couldn't run because it
4343                // needs to obtain a read lock.
4344                drop(guard);
4345
4346                assert!(event_loaded(room_event_cache, ev_id_1).await);
4347                assert!(event_loaded(room_event_cache, ev_id_0).await.not());
4348
4349                room_event_cache.pagination().run_backwards_once(1).await.unwrap();
4350                assert!(event_loaded(room_event_cache, ev_id_0).await);
4351
4352                // The pagination can be observed via the updates too.
4353                assert_matches!(
4354                    updates_stream.recv().await.unwrap(),
4355                    RoomEventCacheUpdate::UpdateTimelineEvents { diffs, .. } => {
4356                        assert_eq!(diffs.len(), 1, "{diffs:#?}");
4357                        assert_matches!(
4358                            &diffs[0],
4359                            VectorDiff::Insert { index: 0, value: event } => {
4360                                assert_eq!(event.event_id().as_deref(), Some(ev_id_0));
4361                            }
4362                        );
4363                    }
4364                );
4365            }
4366        }
4367    }
4368
4369    #[async_test]
4370    async fn test_load_when_dirty() {
4371        let room_id_0 = room_id!("!raclette:patate.ch");
4372        let room_id_1 = room_id!("!morbiflette:patate.ch");
4373
4374        // The storage shared by the two clients.
4375        let event_cache_store = MemoryStore::new();
4376
4377        // Client for the process 0.
4378        let client_p0 = MockClientBuilder::new(None)
4379            .on_builder(|builder| {
4380                builder.store_config(
4381                    StoreConfig::new("process #0".to_owned())
4382                        .event_cache_store(event_cache_store.clone()),
4383                )
4384            })
4385            .build()
4386            .await;
4387
4388        // Client for the process 1.
4389        let client_p1 = MockClientBuilder::new(None)
4390            .on_builder(|builder| {
4391                builder.store_config(
4392                    StoreConfig::new("process #1".to_owned()).event_cache_store(event_cache_store),
4393                )
4394            })
4395            .build()
4396            .await;
4397
4398        // Subscribe the event caches, and create the room.
4399        let (room_event_cache_0_p0, room_event_cache_0_p1) = {
4400            let event_cache_p0 = client_p0.event_cache();
4401            event_cache_p0.subscribe().unwrap();
4402
4403            let event_cache_p1 = client_p1.event_cache();
4404            event_cache_p1.subscribe().unwrap();
4405
4406            client_p0
4407                .base_client()
4408                .get_or_create_room(room_id_0, matrix_sdk_base::RoomState::Joined);
4409            client_p0
4410                .base_client()
4411                .get_or_create_room(room_id_1, matrix_sdk_base::RoomState::Joined);
4412
4413            client_p1
4414                .base_client()
4415                .get_or_create_room(room_id_0, matrix_sdk_base::RoomState::Joined);
4416            client_p1
4417                .base_client()
4418                .get_or_create_room(room_id_1, matrix_sdk_base::RoomState::Joined);
4419
4420            let (room_event_cache_0_p0, _drop_handles) =
4421                client_p0.get_room(room_id_0).unwrap().event_cache().await.unwrap();
4422            let (room_event_cache_0_p1, _drop_handles) =
4423                client_p1.get_room(room_id_0).unwrap().event_cache().await.unwrap();
4424
4425            (room_event_cache_0_p0, room_event_cache_0_p1)
4426        };
4427
4428        // Let's make the cross-process lock over the store dirty.
4429        {
4430            drop(room_event_cache_0_p0.inner.state.read().await.unwrap());
4431            drop(room_event_cache_0_p1.inner.state.read().await.unwrap());
4432        }
4433
4434        // Create the `RoomEventCache` for `room_id_1`. During its creation, the
4435        // cross-process lock over the store MUST be dirty, which makes no difference as
4436        // a clean one: the state is just loaded, not reloaded.
4437        let (room_event_cache_1_p0, _) =
4438            client_p0.get_room(room_id_1).unwrap().event_cache().await.unwrap();
4439
4440        // Check the lock isn't dirty because it's been cleared.
4441        {
4442            let guard = room_event_cache_1_p0.inner.state.read().await.unwrap();
4443            assert!(guard.is_dirty().not());
4444        }
4445
4446        // The only way to test this behaviour is to see that the dirty block in
4447        // `RoomEventCacheStateLock` is covered by this test.
4448    }
4449
4450    async fn event_loaded(room_event_cache: &RoomEventCache, event_id: &EventId) -> bool {
4451        room_event_cache
4452            .rfind_map_event_in_memory_by(|event, _previous_event_id| {
4453                (event.event_id().as_deref() == Some(event_id)).then_some(())
4454            })
4455            .await
4456            .unwrap()
4457            .is_some()
4458    }
4459}