Skip to main content

matrix_sdk_base/store/
mod.rs

1// Copyright 2021 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//! The state store holds the overall state for rooms, users and their
16//! profiles and their timelines. It is an overall cache for faster access
17//! and convenience- accessible through `Store`.
18//!
19//! Implementing the `StateStore` trait, you can plug any storage backend
20//! into the store for the actual storage. By default this brings an in-memory
21//! store.
22
23use std::{
24    borrow::Borrow,
25    collections::{BTreeMap, BTreeSet, HashMap, HashSet},
26    fmt,
27    ops::Deref,
28    result::Result as StdResult,
29    str::{FromStr, Utf8Error},
30    sync::{Arc, OnceLock, RwLock as StdRwLock},
31};
32
33use eyeball_im::{Vector, VectorDiff};
34use futures_util::Stream;
35use matrix_sdk_common::ROOM_VERSION_RULES_FALLBACK;
36
37#[cfg(any(test, feature = "testing"))]
38#[macro_use]
39pub mod integration_tests;
40mod observable_map;
41mod traits;
42
43use matrix_sdk_common::{cross_process_lock::CrossProcessLockConfig, locks::Mutex as SyncMutex};
44#[cfg(feature = "e2e-encryption")]
45use matrix_sdk_crypto::store::{DynCryptoStore, IntoCryptoStore};
46pub use matrix_sdk_store_encryption::Error as StoreEncryptionError;
47use observable_map::ObservableMap;
48use ruma::{
49    EventId, OwnedEventId, OwnedRoomId, OwnedUserId, RoomId, UserId,
50    events::{
51        AnyGlobalAccountDataEvent, AnyRoomAccountDataEvent, AnyStrippedStateEvent,
52        AnySyncStateEvent, EmptyStateKey, GlobalAccountDataEventType, RedactContent,
53        RedactedStateEventContent, RoomAccountDataEventType, StateEventType, StaticEventContent,
54        StaticStateEventContent, StrippedStateEvent, SyncStateEvent,
55        presence::PresenceEvent,
56        receipt::ReceiptEventContent,
57        room::{
58            create::RoomCreateEventContent,
59            member::{RoomMemberEventContent, StrippedRoomMemberEvent},
60            power_levels::{RoomPowerLevels, RoomPowerLevelsEventContent},
61            redaction::SyncRoomRedactionEvent,
62        },
63    },
64    profile::UserProfileUpdate,
65    serde::Raw,
66};
67use serde::de::DeserializeOwned;
68use tokio::sync::{Mutex, RwLock, broadcast};
69use tracing::warn;
70pub use traits::compare_thread_subscription_bump_stamps;
71
72use crate::{
73    MinimalRoomMemberEvent, Room, RoomCreateWithCreatorEventContent, RoomStateFilter, SessionMeta,
74    deserialized_responses::DisplayName,
75    event_cache::store as event_cache_store,
76    media::store as media_store,
77    room::{RoomInfo, RoomInfoNotableUpdate, RoomState},
78};
79
80pub(crate) mod ambiguity_map;
81mod avatar_cache;
82mod memory_store;
83pub mod migration_helpers;
84mod send_queue;
85
86pub use avatar_cache::AvatarCache;
87
88#[cfg(any(test, feature = "testing"))]
89pub use self::integration_tests::StateStoreIntegrationTests;
90#[cfg(feature = "unstable-msc4274")]
91pub use self::send_queue::{AccumulatedSentMediaInfo, FinishGalleryItemInfo};
92pub use self::{
93    memory_store::MemoryStore,
94    send_queue::{
95        ChildTransactionId, DependentQueuedRequest, DependentQueuedRequestKind,
96        FinishUploadThumbnailInfo, QueueWedgeError, QueuedRequest, QueuedRequestKind,
97        SentMediaInfo, SentRequestKey, SerializableEventContent,
98    },
99    traits::{
100        ComposerDraft, ComposerDraftType, DraftAttachment, DraftAttachmentContent, DraftThumbnail,
101        DynStateStore, IncorrectMutexGuardError, IntoStateStore, SaveLockedStateStore, StateStore,
102        StateStoreDataKey, StateStoreDataValue, StateStoreExt, SupportedVersionsResponse,
103        ThreadSubscriptionCatchupToken, WellKnownResponse,
104    },
105};
106
107/// State store specific error type.
108#[derive(Debug, thiserror::Error)]
109pub enum StoreError {
110    /// An error happened in the underlying database backend.
111    #[error(transparent)]
112    Backend(Box<dyn std::error::Error + Send + Sync>),
113
114    /// An error happened while serializing or deserializing some data.
115    #[error(transparent)]
116    Json(#[from] serde_json::Error),
117
118    /// An error happened while deserializing a Matrix identifier, e.g. an user
119    /// id.
120    #[error(transparent)]
121    Identifier(#[from] ruma::IdParseError),
122
123    /// The store is locked with a passphrase and an incorrect passphrase was
124    /// given.
125    #[error("The store failed to be unlocked")]
126    StoreLocked,
127
128    /// An unencrypted store was tried to be unlocked with a passphrase.
129    #[error("The store is not encrypted but was tried to be opened with a passphrase")]
130    UnencryptedStore,
131
132    /// The store failed to encrypt or decrypt some data.
133    #[error("Error encrypting or decrypting data from the store: {0}")]
134    Encryption(#[from] StoreEncryptionError),
135
136    /// The store failed to encode or decode some data.
137    #[error("Error encoding or decoding data from the store: {0}")]
138    Codec(#[from] Utf8Error),
139
140    /// The database format has changed in a backwards incompatible way.
141    #[error(
142        "The database format changed in an incompatible way, current \
143        version: {0}, latest version: {1}"
144    )]
145    UnsupportedDatabaseVersion(usize, usize),
146
147    /// Redacting an event in the store has failed.
148    ///
149    /// This should never happen.
150    #[error("Redaction failed: {0}")]
151    Redaction(#[source] ruma::canonical_json::CanonicalJsonFieldError),
152
153    /// The store contains invalid data.
154    #[error("The store contains invalid data: {details}")]
155    InvalidData {
156        /// Details about which data is invalid, and how.
157        details: String,
158    },
159}
160
161impl StoreError {
162    /// Create a new [`Backend`][Self::Backend] error.
163    ///
164    /// Shorthand for `StoreError::Backend(Box::new(error))`.
165    #[inline]
166    pub fn backend<E>(error: E) -> Self
167    where
168        E: std::error::Error + Send + Sync + 'static,
169    {
170        Self::Backend(Box::new(error))
171    }
172}
173
174/// A `StateStore` specific result type.
175pub type Result<T, E = StoreError> = std::result::Result<T, E>;
176
177/// A state store wrapper for the SDK.
178///
179/// This adds additional higher level store functionality on top of a
180/// `StateStore` implementation.
181#[derive(Clone)]
182pub(crate) struct BaseStateStore {
183    pub(super) inner: SaveLockedStateStore,
184    session_meta: Arc<OnceLock<SessionMeta>>,
185    room_load_settings: Arc<RwLock<RoomLoadSettings>>,
186
187    /// A sender that is used to communicate changes to room information. Each
188    /// tick contains the room ID and the reasons that have generated this tick.
189    pub(crate) room_info_notable_update_sender: broadcast::Sender<RoomInfoNotableUpdate>,
190
191    /// The current sync token that should be used for the next sync call.
192    pub(super) sync_token: Arc<RwLock<Option<String>>>,
193
194    /// All rooms the store knows about.
195    rooms: Arc<StdRwLock<ObservableMap<OwnedRoomId, Room>>>,
196
197    /// Which rooms have already logged a log line about missing room info, in
198    /// the context of response processors?
199    pub(crate) already_logged_missing_room: Arc<SyncMutex<HashSet<OwnedRoomId>>>,
200}
201
202impl BaseStateStore {
203    /// Create a new store, wrapping the given `StateStore`
204    pub fn new(inner: Arc<DynStateStore>) -> Self {
205        // Create the channel to receive `RoomInfoNotableUpdate`.
206        //
207        // Let's consider the channel will receive 5 updates for 100 rooms
208        // maximum. This is unrealistic in practise, as the sync mechanism is
209        // pretty unlikely to trigger such amount of updates, it's a safe value.
210        //
211        // Also, note that it must not be zero, because (i) it will panic, (ii)
212        // a new user has no room, but can create rooms; remember that the
213        // channel's capacity is immutable.
214        let (room_info_notable_update_sender, _room_info_notable_update_receiver) =
215            broadcast::channel(500);
216
217        Self {
218            inner: SaveLockedStateStore::new(inner),
219            session_meta: Default::default(),
220            room_load_settings: Default::default(),
221            room_info_notable_update_sender,
222            sync_token: Default::default(),
223            rooms: Arc::new(StdRwLock::new(ObservableMap::new())),
224            already_logged_missing_room: Default::default(),
225        }
226    }
227
228    /// Get access to the syncing lock.
229    pub fn lock(&self) -> &Mutex<()> {
230        self.inner.lock()
231    }
232
233    /// Set the [`SessionMeta`] into [`BaseStateStore::session_meta`].
234    ///
235    /// # Panics
236    ///
237    /// Panics if called twice.
238    pub(crate) fn set_session_meta(&self, session_meta: SessionMeta) {
239        self.session_meta.set(session_meta).expect("`SessionMeta` was already set");
240    }
241
242    /// Loads rooms from the given [`DynStateStore`] (in
243    /// [`BaseStateStore::new`]) into [`BaseStateStore::rooms`].
244    pub(crate) async fn load_rooms(
245        &self,
246        user_id: &UserId,
247        room_load_settings: RoomLoadSettings,
248    ) -> Result<()> {
249        *self.room_load_settings.write().await = room_load_settings.clone();
250
251        let room_infos = self.load_and_migrate_room_infos(room_load_settings).await?;
252
253        let mut rooms = self.rooms.write().unwrap();
254
255        for room_info in room_infos {
256            let new_room = Room::restore(
257                user_id,
258                self.inner.clone(),
259                room_info,
260                self.room_info_notable_update_sender.clone(),
261            );
262            let new_room_id = new_room.room_id().to_owned();
263
264            rooms.insert(new_room_id, new_room);
265        }
266
267        Ok(())
268    }
269
270    /// Load room infos from the [`StateStore`] and applies migrations onto
271    /// them.
272    async fn load_and_migrate_room_infos(
273        &self,
274        room_load_settings: RoomLoadSettings,
275    ) -> Result<Vec<RoomInfo>> {
276        let mut room_infos = self.inner.get_room_infos(&room_load_settings).await?;
277        let mut migrated_room_infos = Vec::with_capacity(room_infos.len());
278
279        for room_info in room_infos.iter_mut() {
280            if room_info.apply_migrations(self.inner.clone()).await {
281                migrated_room_infos.push(room_info.clone());
282            }
283        }
284
285        if !migrated_room_infos.is_empty() {
286            let changes = StateChanges {
287                room_infos: migrated_room_infos
288                    .into_iter()
289                    .map(|room_info| (room_info.room_id.clone(), room_info))
290                    .collect(),
291                ..Default::default()
292            };
293
294            if let Err(error) = self.inner.save_changes(&changes).await {
295                warn!("Failed to save migrated room infos: {error}");
296            }
297        }
298
299        Ok(room_infos)
300    }
301
302    /// Load sync token from the [`StateStore`], and put it in
303    /// [`BaseStateStore::sync_token`].
304    pub(crate) async fn load_sync_token(&self) -> Result<()> {
305        let token =
306            self.get_kv_data(StateStoreDataKey::SyncToken).await?.and_then(|s| s.into_sync_token());
307        *self.sync_token.write().await = token;
308
309        Ok(())
310    }
311
312    /// Restore the session meta, sync token and rooms from an existing
313    /// [`BaseStateStore`].
314    #[cfg(any(feature = "e2e-encryption", test))]
315    pub(crate) async fn derive_from_other(&self, other: &Self) -> Result<()> {
316        let Some(session_meta) = other.session_meta.get() else {
317            return Ok(());
318        };
319
320        let room_load_settings = other.room_load_settings.read().await.clone();
321
322        self.load_rooms(&session_meta.user_id, room_load_settings).await?;
323        self.load_sync_token().await?;
324        self.set_session_meta(session_meta.clone());
325
326        Ok(())
327    }
328
329    /// The current [`SessionMeta`] containing our user ID and device ID.
330    pub fn session_meta(&self) -> Option<&SessionMeta> {
331        self.session_meta.get()
332    }
333
334    /// Get all the rooms this store knows about.
335    pub fn rooms(&self) -> Vec<Room> {
336        self.rooms.read().unwrap().iter().cloned().collect()
337    }
338
339    /// Get all the rooms this store knows about, filtered by state.
340    pub fn rooms_filtered(&self, filter: RoomStateFilter) -> Vec<Room> {
341        self.rooms
342            .read()
343            .unwrap()
344            .iter()
345            .filter(|room| filter.matches(room.state()))
346            .cloned()
347            .collect()
348    }
349
350    /// Get a stream of all the rooms changes, in addition to the existing
351    /// rooms.
352    pub fn rooms_stream(
353        &self,
354    ) -> (Vector<Room>, impl Stream<Item = Vec<VectorDiff<Room>>> + use<>) {
355        self.rooms.read().unwrap().stream()
356    }
357
358    /// Get the room with the given room id.
359    pub fn room(&self, room_id: &RoomId) -> Option<Room> {
360        self.rooms.read().unwrap().get(room_id).cloned()
361    }
362
363    /// Check if a room exists.
364    pub(crate) fn room_exists(&self, room_id: &RoomId) -> bool {
365        self.rooms.read().unwrap().get(room_id).is_some()
366    }
367
368    /// Lookup the `Room` for the given `RoomId`, or create one, if it didn't
369    /// exist yet in the store
370    pub fn get_or_create_room(&self, room_id: &RoomId, room_state: RoomState) -> Room {
371        let user_id =
372            &self.session_meta.get().expect("Creating room while not being logged in").user_id;
373
374        self.rooms
375            .write()
376            .unwrap()
377            .get_or_create(room_id, || {
378                Room::new(
379                    user_id,
380                    self.inner.clone(),
381                    room_id,
382                    room_state,
383                    self.room_info_notable_update_sender.clone(),
384                )
385            })
386            .clone()
387    }
388
389    /// Forget the room with the given room ID.
390    ///
391    /// # Arguments
392    ///
393    /// * `room_id` - The id of the room that should be forgotten.
394    pub(crate) async fn forget_room(&self, room_id: &RoomId) -> Result<()> {
395        self.inner.remove_room(room_id).await?;
396        self.rooms.write().unwrap().remove(room_id);
397        Ok(())
398    }
399}
400
401#[cfg(not(tarpaulin_include))]
402impl fmt::Debug for BaseStateStore {
403    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
404        f.debug_struct("Store")
405            .field("inner", &self.inner)
406            .field("session_meta", &self.session_meta)
407            .field("sync_token", &self.sync_token)
408            .field("rooms", &self.rooms)
409            .finish_non_exhaustive()
410    }
411}
412
413impl Deref for BaseStateStore {
414    type Target = SaveLockedStateStore;
415
416    fn deref(&self) -> &Self::Target {
417        &self.inner
418    }
419}
420
421/// Configure how many rooms will be restored when restoring the session with
422/// `BaseStateStore::load_rooms`.
423///
424/// <div class="warning">
425///
426/// # ⚠️ be careful!
427///
428/// When loading a single room with [`RoomLoadSettings::One`], the in-memory
429/// state may not reflect the store state (in the databases). Thus, when one
430/// will get a room that exists in the store state but _not_ in the in-memory
431/// state, it will be created from scratch and, when saved, will override the
432/// data in the store state (in the databases). This can lead to weird
433/// behaviours.
434///
435/// This option is expected to be used as follows:
436///
437/// 1. Create a `BaseStateStore` with a [`StateStore`] based on SQLite for
438///    example,
439/// 2. Restore a session and load one room from the [`StateStore`] (in the case
440///    of dealing with a notification for example),
441/// 3. Derive the `BaseStateStore`, with `BaseStateStore::derive_from_other`,
442///    into another one with an in-memory [`StateStore`], such as
443///    [`MemoryStore`],
444/// 4. Work on this derived `BaseStateStore`.
445///
446/// Now, all operations happen in the [`MemoryStore`], not on the original store
447/// (SQLite in this example), thus protecting original data.
448///
449/// From a higher-level point of view, this is what
450/// [`BaseClient::clone_with_in_memory_state_store`] does.
451///
452/// </div>
453///
454/// [`BaseClient::clone_with_in_memory_state_store`]: crate::BaseClient::clone_with_in_memory_state_store
455#[derive(Clone, Debug, Default)]
456pub enum RoomLoadSettings {
457    /// Load all rooms from the [`StateStore`] into the in-memory state store
458    /// `BaseStateStore`.
459    ///
460    /// This is the default variant.
461    #[default]
462    All,
463
464    /// Load a single room from the [`StateStore`] into the in-memory state
465    /// store `BaseStateStore`.
466    ///
467    /// Please, be careful with this option. Read the documentation of
468    /// [`RoomLoadSettings`].
469    One(OwnedRoomId),
470}
471
472/// The subscription status of a thread.
473///
474/// We keep unsubscriptions in the database, because we need the bumpstamp
475/// information (in `ThreadSubscription`) to be around to order subscriptions
476/// and unsubscriptions.
477#[derive(Clone, Copy, Debug, PartialEq, Eq)]
478pub enum ThreadSubscriptionStatus {
479    /// The user is subscribed to the related thread.
480    Subscribed {
481        /// Whether the subscription was made automatically by a client, not by
482        /// manual user choice.
483        automatic: bool,
484    },
485
486    /// The user has been unsubscribed to the related thread.
487    Unsubscribed,
488}
489
490impl FromStr for ThreadSubscriptionStatus {
491    type Err = ();
492
493    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
494        match s {
495            "automatic" => Ok(ThreadSubscriptionStatus::Subscribed { automatic: true }),
496            "manual" => Ok(ThreadSubscriptionStatus::Subscribed { automatic: false }),
497            "unsubscribed" => Ok(ThreadSubscriptionStatus::Unsubscribed),
498            _ => Err(()),
499        }
500    }
501}
502
503impl ThreadSubscriptionStatus {
504    /// Represent the status as a static string ref, for it to be stored into a
505    /// persistent format.
506    ///
507    /// Note: this is serialized in some databases implementations, so make sure
508    /// to not change it lightly, and keep it in sync with [`Self::from_str`].
509    pub fn as_str(&self) -> &'static str {
510        match self {
511            ThreadSubscriptionStatus::Subscribed { automatic } => {
512                if *automatic {
513                    "automatic"
514                } else {
515                    "manual"
516                }
517            }
518            ThreadSubscriptionStatus::Unsubscribed => "unsubscribed",
519        }
520    }
521}
522
523/// A thread subscription, as saved in the state store.
524#[derive(Clone, Copy, Debug, PartialEq, Eq)]
525pub struct StoredThreadSubscription {
526    /// Current status of the subscription.
527    pub status: ThreadSubscriptionStatus,
528
529    /// An optional bump stamp, as defined in the MSC; the higher the value, the
530    /// most recent the thread subscription information is, and should be
531    /// remembered.
532    ///
533    /// If not set, this means it's a user-provided thread subscription, for
534    /// which we're waiting validation from a server (e.g. through a remote echo
535    /// via sync).
536    pub bump_stamp: Option<u64>,
537}
538
539/// Store state changes and pass them to the StateStore.
540#[derive(Clone, Debug, Default)]
541pub struct StateChanges {
542    /// The sync token that relates to this update.
543    pub sync_token: Option<String>,
544    /// A mapping of event type string to `AnyBasicEvent`.
545    pub account_data: BTreeMap<GlobalAccountDataEventType, Raw<AnyGlobalAccountDataEvent>>,
546    /// A mapping of `UserId` to `PresenceEvent`.
547    pub presence: BTreeMap<OwnedUserId, Raw<PresenceEvent>>,
548
549    /// A mapping of `RoomId` to a map of users and their
550    /// `MinimalRoomMemberEvent`.
551    pub profiles: BTreeMap<OwnedRoomId, BTreeMap<OwnedUserId, MinimalRoomMemberEvent>>,
552
553    /// A mapping of room profiles to delete.
554    ///
555    /// These are deleted *before* other room profiles are inserted.
556    pub profiles_to_delete: BTreeMap<OwnedRoomId, Vec<OwnedUserId>>,
557
558    /// A mapping of `RoomId` to a map of event type string to a state key and
559    /// `AnySyncStateEvent`.
560    pub state:
561        BTreeMap<OwnedRoomId, BTreeMap<StateEventType, BTreeMap<String, Raw<AnySyncStateEvent>>>>,
562    /// A mapping of `RoomId` to a map of event type string to `AnyBasicEvent`.
563    pub room_account_data:
564        BTreeMap<OwnedRoomId, BTreeMap<RoomAccountDataEventType, Raw<AnyRoomAccountDataEvent>>>,
565
566    /// A map of `OwnedRoomId` to `RoomInfo`.
567    pub room_infos: BTreeMap<OwnedRoomId, RoomInfo>,
568
569    /// A map of `RoomId` to `ReceiptEventContent`.
570    pub receipts: BTreeMap<OwnedRoomId, ReceiptEventContent>,
571
572    /// A map of `RoomId` to maps of `OwnedEventId` to be redacted by
573    /// `SyncRoomRedactionEvent`.
574    pub redactions: BTreeMap<OwnedRoomId, BTreeMap<OwnedEventId, Raw<SyncRoomRedactionEvent>>>,
575
576    /// A mapping of `RoomId` to a map of event type to a map of state key to
577    /// `StrippedState`.
578    pub stripped_state: BTreeMap<
579        OwnedRoomId,
580        BTreeMap<StateEventType, BTreeMap<String, Raw<AnyStrippedStateEvent>>>,
581    >,
582
583    /// A map from room id to a map of a display name and a set of user ids that
584    /// share that display name in the given room.
585    pub ambiguity_maps: BTreeMap<OwnedRoomId, HashMap<DisplayName, BTreeSet<OwnedUserId>>>,
586
587    /// A mapping of `UserId` to global `UserProfile` updates, as defined in
588    /// MSC4262.
589    ///
590    /// These follow the MSC4262 update pattern: fields with an explicit `null`
591    /// value are removed, while fields that aren't present are left unchanged.
592    pub global_profiles: BTreeMap<OwnedUserId, UserProfileUpdate>,
593}
594
595impl StateChanges {
596    /// Create a new `StateChanges` struct with the given sync_token.
597    pub fn new(sync_token: String) -> Self {
598        Self { sync_token: Some(sync_token), ..Default::default() }
599    }
600
601    /// Update the `StateChanges` struct with the given `PresenceEvent`.
602    pub fn add_presence_event(&mut self, event: PresenceEvent, raw_event: Raw<PresenceEvent>) {
603        self.presence.insert(event.sender, raw_event);
604    }
605
606    /// Update the `StateChanges` struct with the given `RoomInfo`.
607    pub fn add_room(&mut self, room: RoomInfo) {
608        self.room_infos.insert(room.room_id.clone(), room);
609    }
610
611    /// Update the `StateChanges` struct with the given room with a new
612    /// `AnyBasicEvent`.
613    pub fn add_room_account_data(
614        &mut self,
615        room_id: &RoomId,
616        event: AnyRoomAccountDataEvent,
617        raw_event: Raw<AnyRoomAccountDataEvent>,
618    ) {
619        self.room_account_data
620            .entry(room_id.to_owned())
621            .or_default()
622            .insert(event.event_type(), raw_event);
623    }
624
625    /// Update the `StateChanges` struct with the given room with a new
626    /// `StrippedMemberEvent`.
627    pub fn add_stripped_member(
628        &mut self,
629        room_id: &RoomId,
630        user_id: &UserId,
631        event: Raw<StrippedRoomMemberEvent>,
632    ) {
633        self.stripped_state
634            .entry(room_id.to_owned())
635            .or_default()
636            .entry(StateEventType::RoomMember)
637            .or_default()
638            .insert(user_id.into(), event.cast());
639    }
640
641    /// Update the `StateChanges` struct with the given room with a new
642    /// `AnySyncStateEvent`.
643    pub fn add_state_event(
644        &mut self,
645        room_id: &RoomId,
646        event: AnySyncStateEvent,
647        raw_event: Raw<AnySyncStateEvent>,
648    ) {
649        self.state
650            .entry(room_id.to_owned())
651            .or_default()
652            .entry(event.event_type())
653            .or_default()
654            .insert(event.state_key().to_owned(), raw_event);
655    }
656
657    /// Redact an event in the room
658    pub fn add_redaction(
659        &mut self,
660        room_id: &RoomId,
661        redacted_event_id: &EventId,
662        redaction: Raw<SyncRoomRedactionEvent>,
663    ) {
664        self.redactions
665            .entry(room_id.to_owned())
666            .or_default()
667            .insert(redacted_event_id.to_owned(), redaction);
668    }
669
670    /// Update the `StateChanges` struct with the given room with a new
671    /// `Receipts`.
672    pub fn add_receipts(&mut self, room_id: &RoomId, event: ReceiptEventContent) {
673        self.receipts.insert(room_id.to_owned(), event);
674    }
675
676    /// Get a specific state event of statically-known type with the given state
677    /// key in the given room, if it is present in the `state` map of these
678    /// `StateChanges`.
679    pub(crate) fn state_static_for_key<C, K>(
680        &self,
681        room_id: &RoomId,
682        state_key: &K,
683    ) -> Option<&Raw<SyncStateEvent<C>>>
684    where
685        C: StaticEventContent<IsPrefix = ruma::events::False>
686            + StaticStateEventContent
687            + RedactContent,
688        C::Redacted: RedactedStateEventContent,
689        C::StateKey: Borrow<K>,
690        K: AsRef<str> + ?Sized,
691    {
692        self.state
693            .get(room_id)?
694            .get(&C::TYPE.into())?
695            .get(state_key.as_ref())
696            .map(Raw::cast_ref_unchecked)
697    }
698
699    /// Get a specific stripped state event of statically-known type with the
700    /// given state key in the given room, if it is present in the
701    /// `stripped_state` map of these `StateChanges`.
702    pub(crate) fn stripped_state_static_for_key<C, K>(
703        &self,
704        room_id: &RoomId,
705        state_key: &K,
706    ) -> Option<&Raw<StrippedStateEvent<C::PossiblyRedacted>>>
707    where
708        C: StaticEventContent<IsPrefix = ruma::events::False> + StaticStateEventContent,
709        C::StateKey: Borrow<K>,
710        K: AsRef<str> + ?Sized,
711    {
712        self.stripped_state
713            .get(room_id)?
714            .get(&C::TYPE.into())?
715            .get(state_key.as_ref())
716            .map(Raw::cast_ref_unchecked)
717    }
718
719    /// Get a specific state event of statically-known type with the given state
720    /// key in the given room, if it is present in the `state` or
721    /// `stripped_state` map of these `StateChanges` and it deserializes
722    /// successfully.
723    pub(crate) fn any_state_static_for_key<C, K>(
724        &self,
725        room_id: &RoomId,
726        state_key: &K,
727    ) -> Option<StrippedStateEvent<C::PossiblyRedacted>>
728    where
729        C: StaticEventContent<IsPrefix = ruma::events::False>
730            + StaticStateEventContent
731            + RedactContent,
732        C::Redacted: RedactedStateEventContent,
733        C::PossiblyRedacted: StaticEventContent + DeserializeOwned,
734        C::StateKey: Borrow<K>,
735        K: AsRef<str> + ?Sized,
736    {
737        self.state_static_for_key::<C, K>(room_id, state_key)
738            .map(Raw::cast_ref)
739            .or_else(|| self.stripped_state_static_for_key::<C, K>(room_id, state_key))?
740            .deserialize()
741            .ok()
742    }
743
744    /// Get the member for the given user in the given room from an event
745    /// contained in these `StateChanges`, if any.
746    pub(crate) fn member(
747        &self,
748        room_id: &RoomId,
749        user_id: &UserId,
750    ) -> Option<StrippedRoomMemberEvent> {
751        self.any_state_static_for_key::<RoomMemberEventContent, _>(room_id, user_id)
752    }
753
754    /// Get the create event for the given room from an event contained in these
755    /// `StateChanges`, if any.
756    pub(crate) fn create(&self, room_id: &RoomId) -> Option<RoomCreateWithCreatorEventContent> {
757        self.any_state_static_for_key::<RoomCreateEventContent, _>(room_id, &EmptyStateKey)
758            .map(|event| {
759                RoomCreateWithCreatorEventContent::from_event_content(event.content, event.sender)
760            })
761            // Fallback to the content in the room info.
762            .or_else(|| self.room_infos.get(room_id)?.create().cloned())
763    }
764
765    /// Get the power levels for the given room from an event contained in these
766    /// `StateChanges`, if any.
767    pub(crate) fn power_levels(&self, room_id: &RoomId) -> Option<RoomPowerLevels> {
768        let power_levels_content = self
769            .any_state_static_for_key::<RoomPowerLevelsEventContent, _>(room_id, &EmptyStateKey)?;
770
771        let create_content = self.create(room_id)?;
772        let rules = create_content.room_version.rules().unwrap_or(ROOM_VERSION_RULES_FALLBACK);
773        let creators = create_content.creators();
774
775        Some(power_levels_content.power_levels(&rules.authorization, creators))
776    }
777}
778
779/// Configuration for the various stores.
780///
781/// By default, this always includes a state store and an event cache store.
782/// When the `e2e-encryption` feature is enabled, this also includes a crypto
783/// store.
784///
785/// # Examples
786///
787/// ```
788/// # use matrix_sdk_common::cross_process_lock::CrossProcessLockConfig;
789/// # use matrix_sdk_base::store::StoreConfig;
790/// #
791/// let store_config = StoreConfig::new(CrossProcessLockConfig::MultiProcess {
792///     holder_name: "cross-process-store-locks-holder-name".to_owned(),
793/// });
794/// ```
795#[derive(Clone)]
796pub struct StoreConfig {
797    #[cfg(feature = "e2e-encryption")]
798    pub(crate) crypto_store: Arc<DynCryptoStore>,
799    pub(crate) state_store: Arc<DynStateStore>,
800    pub(crate) event_cache_store: event_cache_store::EventCacheStoreLock,
801    pub(crate) media_store: media_store::MediaStoreLock,
802    cross_process_lock_config: CrossProcessLockConfig,
803}
804
805#[cfg(not(tarpaulin_include))]
806impl fmt::Debug for StoreConfig {
807    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> StdResult<(), fmt::Error> {
808        fmt.debug_struct("StoreConfig").finish()
809    }
810}
811
812impl StoreConfig {
813    /// Create a new default `StoreConfig`.
814    ///
815    /// To learn more about `cross_process_lock_config`, please read
816    /// [`CrossProcessLock::new`](matrix_sdk_common::cross_process_lock::CrossProcessLock::new).
817    #[must_use]
818    pub fn new(cross_process_lock_config: CrossProcessLockConfig) -> Self {
819        Self {
820            #[cfg(feature = "e2e-encryption")]
821            crypto_store: matrix_sdk_crypto::store::MemoryStore::new().into_crypto_store(),
822            state_store: Arc::new(MemoryStore::new()),
823            event_cache_store: event_cache_store::EventCacheStoreLock::new(
824                event_cache_store::MemoryStore::new(),
825                cross_process_lock_config.clone(),
826            ),
827            media_store: media_store::MediaStoreLock::new(
828                media_store::MemoryMediaStore::new(),
829                cross_process_lock_config.clone(),
830            ),
831            cross_process_lock_config,
832        }
833    }
834
835    /// Set a custom implementation of a `CryptoStore`.
836    ///
837    /// The crypto store must be opened before being set.
838    #[cfg(feature = "e2e-encryption")]
839    pub fn crypto_store(mut self, store: impl IntoCryptoStore) -> Self {
840        self.crypto_store = store.into_crypto_store();
841        self
842    }
843
844    /// Set a custom implementation of a `StateStore`.
845    pub fn state_store(mut self, store: impl IntoStateStore) -> Self {
846        self.state_store = store.into_state_store();
847        self
848    }
849
850    /// Set a custom implementation of an `EventCacheStore`.
851    pub fn event_cache_store<S>(mut self, event_cache_store: S) -> Self
852    where
853        S: event_cache_store::IntoEventCacheStore,
854    {
855        self.event_cache_store = event_cache_store::EventCacheStoreLock::new(
856            event_cache_store,
857            self.cross_process_lock_config.clone(),
858        );
859        self
860    }
861
862    /// Set a custom implementation of an `MediaStore`.
863    pub fn media_store<S>(mut self, media_store: S) -> Self
864    where
865        S: media_store::IntoMediaStore,
866    {
867        self.media_store =
868            media_store::MediaStoreLock::new(media_store, self.cross_process_lock_config.clone());
869        self
870    }
871}
872
873#[cfg(test)]
874mod tests {
875    use std::{ops::Not, sync::Arc};
876
877    use assert_matches::assert_matches;
878    use matrix_sdk_test::async_test;
879    use ruma::{owned_device_id, owned_user_id, room_id, user_id};
880
881    use super::{BaseStateStore, MemoryStore, RoomLoadSettings};
882    use crate::{RoomInfo, RoomState, SessionMeta, StateChanges, StateStore};
883
884    #[async_test]
885    async fn test_set_session_meta() {
886        let store = BaseStateStore::new(Arc::new(MemoryStore::new()));
887
888        let session_meta = SessionMeta {
889            user_id: owned_user_id!("@mnt_io:matrix.org"),
890            device_id: owned_device_id!("HELLOYOU"),
891        };
892
893        assert!(store.session_meta.get().is_none());
894
895        store.set_session_meta(session_meta.clone());
896
897        assert_eq!(store.session_meta.get(), Some(&session_meta));
898    }
899
900    #[async_test]
901    #[should_panic]
902    async fn test_set_session_meta_twice() {
903        let store = BaseStateStore::new(Arc::new(MemoryStore::new()));
904
905        let session_meta = SessionMeta {
906            user_id: owned_user_id!("@mnt_io:matrix.org"),
907            device_id: owned_device_id!("HELLOYOU"),
908        };
909
910        store.set_session_meta(session_meta.clone());
911        // Kaboom.
912        store.set_session_meta(session_meta);
913    }
914
915    #[async_test]
916    async fn test_derive_from_other() {
917        // The first store.
918        let other = BaseStateStore::new(Arc::new(MemoryStore::new()));
919
920        let session_meta = SessionMeta {
921            user_id: owned_user_id!("@mnt_io:matrix.org"),
922            device_id: owned_device_id!("HELLOYOU"),
923        };
924        let room_id_0 = room_id!("!r0");
925
926        other
927            .load_rooms(&session_meta.user_id, RoomLoadSettings::One(room_id_0.to_owned()))
928            .await
929            .unwrap();
930        other.set_session_meta(session_meta.clone());
931
932        // Derive another store.
933        let store = BaseStateStore::new(Arc::new(MemoryStore::new()));
934        store.derive_from_other(&other).await.unwrap();
935
936        // `SessionMeta` is derived.
937        assert_eq!(store.session_meta.get(), Some(&session_meta));
938        // `RoomLoadSettings` is derived.
939        assert_matches!(*store.room_load_settings.read().await, RoomLoadSettings::One(ref room_id) => {
940            assert_eq!(room_id, room_id_0);
941        });
942
943        // The `RoomInfoNotableUpdate` is not derived. Every one has its own
944        // channel.
945        assert!(
946            store
947                .room_info_notable_update_sender
948                .same_channel(&other.room_info_notable_update_sender)
949                .not()
950        );
951    }
952
953    #[test]
954    fn test_room_load_settings_default() {
955        assert_matches!(RoomLoadSettings::default(), RoomLoadSettings::All);
956    }
957
958    #[async_test]
959    async fn test_load_all_rooms() {
960        let room_id_0 = room_id!("!r0");
961        let room_id_1 = room_id!("!r1");
962        let user_id = user_id!("@mnt_io:matrix.org");
963
964        let memory_state_store = Arc::new(MemoryStore::new());
965
966        // Initial state.
967        {
968            let store = BaseStateStore::new(memory_state_store.clone());
969            let mut changes = StateChanges::default();
970            changes.add_room(RoomInfo::new(room_id_0, RoomState::Joined));
971            changes.add_room(RoomInfo::new(room_id_1, RoomState::Joined));
972
973            store.inner.save_changes(&changes).await.unwrap();
974        }
975
976        // Check a `BaseStateStore` is able to load all rooms.
977        {
978            let store = BaseStateStore::new(memory_state_store.clone());
979
980            // Default value.
981            assert_matches!(*store.room_load_settings.read().await, RoomLoadSettings::All);
982
983            // Load rooms.
984            store.load_rooms(user_id, RoomLoadSettings::All).await.unwrap();
985
986            // Check the last room load settings.
987            assert_matches!(*store.room_load_settings.read().await, RoomLoadSettings::All);
988
989            // Check the loaded rooms.
990            let mut rooms = store.rooms();
991            rooms.sort_by(|a, b| a.room_id().cmp(b.room_id()));
992
993            assert_eq!(rooms.len(), 2);
994
995            assert_eq!(rooms[0].room_id(), room_id_0);
996            assert_eq!(rooms[0].own_user_id(), user_id);
997
998            assert_eq!(rooms[1].room_id(), room_id_1);
999            assert_eq!(rooms[1].own_user_id(), user_id);
1000        }
1001    }
1002
1003    #[async_test]
1004    async fn test_load_one_room() {
1005        let room_id_0 = room_id!("!r0");
1006        let room_id_1 = room_id!("!r1");
1007        let user_id = user_id!("@mnt_io:matrix.org");
1008
1009        let memory_state_store = Arc::new(MemoryStore::new());
1010
1011        // Initial state.
1012        {
1013            let store = BaseStateStore::new(memory_state_store.clone());
1014            let mut changes = StateChanges::default();
1015            changes.add_room(RoomInfo::new(room_id_0, RoomState::Joined));
1016            changes.add_room(RoomInfo::new(room_id_1, RoomState::Joined));
1017
1018            store.inner.save_changes(&changes).await.unwrap();
1019        }
1020
1021        // Check a `BaseStateStore` is able to load one room.
1022        {
1023            let store = BaseStateStore::new(memory_state_store.clone());
1024
1025            // Default value.
1026            assert_matches!(*store.room_load_settings.read().await, RoomLoadSettings::All);
1027
1028            // Load rooms.
1029            store.load_rooms(user_id, RoomLoadSettings::One(room_id_1.to_owned())).await.unwrap();
1030
1031            // Check the last room load settings.
1032            assert_matches!(
1033                *store.room_load_settings.read().await,
1034                RoomLoadSettings::One(ref room_id) => {
1035                    assert_eq!(room_id, room_id_1);
1036                }
1037            );
1038
1039            // Check the loaded rooms.
1040            let rooms = store.rooms();
1041            assert_eq!(rooms.len(), 1);
1042
1043            assert_eq!(rooms[0].room_id(), room_id_1);
1044            assert_eq!(rooms[0].own_user_id(), user_id);
1045        }
1046    }
1047}