Skip to main content

matrix_sdk_base/
client.rs

1// Copyright 2020 Damir Jelić
2// Copyright 2020 The Matrix.org Foundation C.I.C.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//     http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#[cfg(feature = "e2e-encryption")]
17use std::sync::Arc;
18use std::{
19    collections::{BTreeMap, BTreeSet, HashMap},
20    fmt,
21    ops::Deref,
22};
23
24use eyeball::{SharedObservable, Subscriber};
25use eyeball_im::{Vector, VectorDiff};
26use futures_util::Stream;
27use matrix_sdk_common::{cross_process_lock::CrossProcessLockConfig, timer};
28#[cfg(feature = "e2e-encryption")]
29use matrix_sdk_crypto::{
30    CollectStrategy, DecryptionSettings, EncryptionSettings, OlmError, OlmMachine,
31    TrustRequirement, store::DynCryptoStore, store::types::RoomPendingKeyBundleDetails,
32    types::requests::ToDeviceRequest,
33};
34#[cfg(doc)]
35use ruma::DeviceId;
36#[cfg(feature = "e2e-encryption")]
37use ruma::events::room::{history_visibility::HistoryVisibility, member::MembershipState};
38use ruma::{
39    OwnedRoomId, OwnedUserId, RoomId, UserId,
40    api::client::{self as api, sync::sync_events::v5},
41    events::{
42        StateEvent, StateEventType,
43        ignored_user_list::IgnoredUserListEventContent,
44        push_rules::{PushRulesEvent, PushRulesEventContent},
45        room::member::SyncRoomMemberEvent,
46    },
47    push::Ruleset,
48    time::Instant,
49};
50use tokio::sync::{Mutex, broadcast};
51#[cfg(feature = "e2e-encryption")]
52use tokio::sync::{RwLock, RwLockReadGuard};
53use tracing::{Level, debug, enabled, info, instrument, warn};
54
55#[cfg(feature = "e2e-encryption")]
56use crate::RoomMemberships;
57use crate::{
58    RoomStateFilter, SessionMeta,
59    deserialized_responses::DisplayName,
60    error::{Error, Result},
61    event_cache::store::{EventCacheStoreLock, EventCacheStoreLockState},
62    media::store::MediaStoreLock,
63    response_processors::{self as processors, Context},
64    room::{
65        Room, RoomInfoNotableUpdate, RoomInfoNotableUpdateReasons, RoomMembersUpdate, RoomState,
66    },
67    store::{
68        BaseStateStore, DynStateStore, MemoryStore, Result as StoreResult, RoomLoadSettings,
69        StateChanges, StateStoreDataKey, StateStoreDataValue, StateStoreExt, StoreConfig,
70        ambiguity_map::AmbiguityCache,
71    },
72    sync::{RoomUpdates, SyncResponse},
73};
74
75/// A no (network) IO client implementation.
76///
77/// This client is a state machine that receives responses and events and
78/// accordingly updates its state. It is not designed to be used directly, but
79/// rather through `matrix_sdk::Client`.
80///
81/// ```rust
82/// use matrix_sdk_base::{BaseClient, ThreadingSupport, store::StoreConfig};
83/// use matrix_sdk_common::cross_process_lock::CrossProcessLockConfig;
84///
85/// let client = BaseClient::new(
86///     StoreConfig::new(CrossProcessLockConfig::multi_process(
87///         "cross-process-holder-name".to_owned(),
88///     )),
89///     ThreadingSupport::Disabled,
90/// );
91/// ```
92#[derive(Clone)]
93pub struct BaseClient {
94    /// The state store.
95    pub(crate) state_store: BaseStateStore,
96
97    /// The store used by the event cache.
98    event_cache_store: EventCacheStoreLock,
99
100    /// The store used by the media cache.
101    media_store: MediaStoreLock,
102
103    /// The store used for encryption.
104    ///
105    /// This field is only meant to be used for `OlmMachine` initialization.
106    /// All operations on it happen inside the `OlmMachine`.
107    #[cfg(feature = "e2e-encryption")]
108    crypto_store: Arc<DynCryptoStore>,
109
110    /// The olm-machine that is created once the
111    /// [`SessionMeta`][crate::session::SessionMeta] is set via
112    /// [`BaseClient::activate`]
113    #[cfg(feature = "e2e-encryption")]
114    olm_machine: Arc<RwLock<Option<OlmMachine>>>,
115
116    /// Observable of when a user is ignored/unignored.
117    pub(crate) ignore_user_list_changes: SharedObservable<Vec<String>>,
118
119    /// The strategy to use for picking recipient devices, when sending an
120    /// encrypted message.
121    #[cfg(feature = "e2e-encryption")]
122    pub room_key_recipient_strategy: CollectStrategy,
123
124    /// The settings to use for decrypting events.
125    #[cfg(feature = "e2e-encryption")]
126    pub decryption_settings: DecryptionSettings,
127
128    /// If the client should handle verification events received when syncing.
129    #[cfg(feature = "e2e-encryption")]
130    pub handle_verification_events: bool,
131
132    /// Whether the client supports threads or not.
133    pub threading_support: ThreadingSupport,
134}
135
136#[cfg(not(tarpaulin_include))]
137impl fmt::Debug for BaseClient {
138    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
139        f.debug_struct("BaseClient")
140            .field("session_meta", &self.state_store.session_meta())
141            .field("sync_token", &self.state_store.sync_token)
142            .finish_non_exhaustive()
143    }
144}
145
146/// Whether this client instance supports threading or not. Currently used to
147/// determine how the client handles read receipts and unread count computations
148/// on the base SDK level.
149///
150/// Timelines on the other hand have a separate `TimelineFocus`
151/// `hide_threaded_events` associated value that can be used to hide threaded
152/// events but also to enable threaded read receipt sending. This is because
153/// certain timeline instances should ignore threading no matter what's defined
154/// at the client level. One such example are media filtered timelines which
155/// should contain all the room's media no matter what thread its in (unless
156/// explicitly opted into).
157#[derive(Clone, Copy, Debug)]
158pub enum ThreadingSupport {
159    /// Threading enabled.
160    Enabled {
161        /// Enable client-wide thread subscriptions support (MSC4306 / MSC4308).
162        ///
163        /// This may cause filtering out of thread subscriptions, and loading
164        /// the thread subscriptions via the sliding sync extension,
165        /// when the room list service is being used.
166        with_subscriptions: bool,
167    },
168    /// Threading disabled.
169    Disabled,
170}
171
172impl BaseClient {
173    /// Create a new client.
174    ///
175    /// # Arguments
176    ///
177    /// * `config` - the configuration for the stores (state store, event cache
178    ///   store and crypto store).
179    pub fn new(config: StoreConfig, threading_support: ThreadingSupport) -> Self {
180        let store = BaseStateStore::new(config.state_store);
181
182        BaseClient {
183            state_store: store,
184            event_cache_store: config.event_cache_store,
185            media_store: config.media_store,
186            #[cfg(feature = "e2e-encryption")]
187            crypto_store: config.crypto_store,
188            #[cfg(feature = "e2e-encryption")]
189            olm_machine: Default::default(),
190            ignore_user_list_changes: Default::default(),
191            #[cfg(feature = "e2e-encryption")]
192            room_key_recipient_strategy: Default::default(),
193            #[cfg(feature = "e2e-encryption")]
194            decryption_settings: DecryptionSettings {
195                sender_device_trust_requirement: TrustRequirement::Untrusted,
196            },
197            #[cfg(feature = "e2e-encryption")]
198            handle_verification_events: true,
199            threading_support,
200        }
201    }
202
203    /// Clones the current base client to use the same crypto store but a
204    /// different, in-memory store config, and resets transient state.
205    #[cfg(feature = "e2e-encryption")]
206    pub async fn clone_with_in_memory_state_store(
207        &self,
208        cross_process_mode: CrossProcessLockConfig,
209        handle_verification_events: bool,
210    ) -> Result<Self> {
211        let config = StoreConfig::new(cross_process_mode).state_store(MemoryStore::new());
212        let config = config.crypto_store(self.crypto_store.clone());
213
214        let copy = Self {
215            state_store: BaseStateStore::new(config.state_store),
216            event_cache_store: config.event_cache_store,
217            media_store: config.media_store,
218            // We copy the crypto store as well as the `OlmMachine` for two reasons:
219            // 1. The `self.crypto_store` is the same as the one used inside the `OlmMachine`.
220            // 2. We need to ensure that the parent and child use the same data and caches inside
221            //    the `OlmMachine` so the various ratchets and places where new randomness gets
222            //    introduced don't diverge, i.e. one-time keys that get generated by the Olm Account
223            //    or Olm sessions when they encrypt or decrypt messages.
224            crypto_store: self.crypto_store.clone(),
225            olm_machine: self.olm_machine.clone(),
226            ignore_user_list_changes: Default::default(),
227            room_key_recipient_strategy: self.room_key_recipient_strategy.clone(),
228            decryption_settings: self.decryption_settings.clone(),
229            handle_verification_events,
230            threading_support: self.threading_support,
231        };
232
233        copy.state_store.derive_from_other(&self.state_store).await?;
234
235        Ok(copy)
236    }
237
238    /// Clones the current base client to use the same crypto store but a
239    /// different, in-memory store config, and resets transient state.
240    #[cfg(not(feature = "e2e-encryption"))]
241    #[allow(clippy::unused_async)]
242    pub async fn clone_with_in_memory_state_store(
243        &self,
244        cross_process_store_config: CrossProcessLockConfig,
245        _handle_verification_events: bool,
246    ) -> Result<Self> {
247        let config = StoreConfig::new(cross_process_store_config).state_store(MemoryStore::new());
248        Ok(Self::new(config, ThreadingSupport::Disabled))
249    }
250
251    /// Get the session meta information.
252    ///
253    /// If the client is currently logged in, this will return a
254    /// [`SessionMeta`] object which contains the user ID and device ID.
255    /// Otherwise it returns `None`.
256    pub fn session_meta(&self) -> Option<&SessionMeta> {
257        self.state_store.session_meta()
258    }
259
260    /// Get all the rooms this client knows about.
261    pub fn rooms(&self) -> Vec<Room> {
262        self.state_store.rooms()
263    }
264
265    /// Get all the rooms this client knows about, filtered by room state.
266    pub fn rooms_filtered(&self, filter: RoomStateFilter) -> Vec<Room> {
267        self.state_store.rooms_filtered(filter)
268    }
269
270    /// Get a stream of all the rooms changes, in addition to the existing
271    /// rooms.
272    pub fn rooms_stream(
273        &self,
274    ) -> (Vector<Room>, impl Stream<Item = Vec<VectorDiff<Room>>> + use<>) {
275        self.state_store.rooms_stream()
276    }
277
278    /// Lookup the Room for the given RoomId, or create one, if it didn't exist
279    /// yet in the store
280    pub fn get_or_create_room(&self, room_id: &RoomId, room_state: RoomState) -> Room {
281        self.state_store.get_or_create_room(room_id, room_state)
282    }
283
284    /// Get a reference to the state store.
285    pub fn state_store(&self) -> &DynStateStore {
286        self.state_store.deref()
287    }
288
289    /// Get a reference to the event cache store.
290    pub fn event_cache_store(&self) -> &EventCacheStoreLock {
291        &self.event_cache_store
292    }
293
294    /// Get a reference to the media store.
295    pub fn media_store(&self) -> &MediaStoreLock {
296        &self.media_store
297    }
298
299    /// Check whether the client has been activated.
300    ///
301    /// See [`BaseClient::activate`] to know what it means.
302    pub fn is_active(&self) -> bool {
303        self.state_store.session_meta().is_some()
304    }
305
306    /// Activate the client.
307    ///
308    /// A client is considered active when:
309    ///
310    /// 1. It has a `SessionMeta` (user ID, device ID and access token),
311    /// 2. Has loaded cached data from storage,
312    /// 3. If encryption is enabled, it also initialized or restored its
313    ///    `OlmMachine`.
314    ///
315    /// # Arguments
316    ///
317    /// * `session_meta` - The meta of a session that the user already has from
318    ///   a previous login call.
319    ///
320    /// * `custom_account` - A custom
321    ///   [`matrix_sdk_crypto::vodozemac::olm::Account`] to be used for the
322    ///   identity and one-time keys of this [`BaseClient`]. If no account is
323    ///   provided, a new default one or one from the store will be used. If an
324    ///   account is provided and one already exists in the store for this
325    ///   [`UserId`]/[`DeviceId`] combination, an error will be raised. This is
326    ///   useful if one wishes to create identity keys before knowing the
327    ///   user/device IDs, e.g., to use the identity key as the device ID.
328    ///
329    /// * `room_load_settings` — Specify how many rooms must be restored; use
330    ///   `::default()` if you don't know which value to pick.
331    ///
332    /// # Panics
333    ///
334    /// This method panics if it is called twice.
335    ///
336    /// [`UserId`]: ruma::UserId
337    pub async fn activate(
338        &self,
339        session_meta: SessionMeta,
340        room_load_settings: RoomLoadSettings,
341        #[cfg(feature = "e2e-encryption")] custom_account: Option<
342            crate::crypto::vodozemac::olm::Account,
343        >,
344    ) -> Result<()> {
345        debug!(user_id = ?session_meta.user_id, device_id = ?session_meta.device_id, "Activating the client");
346
347        self.state_store.load_rooms(&session_meta.user_id, room_load_settings).await?;
348        self.state_store.load_sync_token().await?;
349        self.state_store.set_session_meta(session_meta);
350
351        #[cfg(feature = "e2e-encryption")]
352        self.regenerate_olm(custom_account).await?;
353
354        Ok(())
355    }
356
357    /// Recreate an `OlmMachine` from scratch.
358    ///
359    /// In particular, this will clear all its caches.
360    #[cfg(feature = "e2e-encryption")]
361    pub async fn regenerate_olm(
362        &self,
363        custom_account: Option<crate::crypto::vodozemac::olm::Account>,
364    ) -> Result<()> {
365        tracing::debug!("regenerating OlmMachine");
366        let session_meta = self.session_meta().ok_or(Error::OlmError(OlmError::MissingSession))?;
367
368        // Recreate the `OlmMachine` and wipe the in-memory cache in the store
369        // because we suspect it has stale data.
370        let olm_machine = OlmMachine::with_store(
371            &session_meta.user_id,
372            &session_meta.device_id,
373            self.crypto_store.clone(),
374            custom_account,
375        )
376        .await
377        .map_err(OlmError::from)?;
378
379        *self.olm_machine.write().await = Some(olm_machine);
380        Ok(())
381    }
382
383    /// Get the current, if any, sync token of the client.
384    /// This will be None if the client didn't sync at least once.
385    pub async fn sync_token(&self) -> Option<String> {
386        self.state_store.sync_token.read().await.clone()
387    }
388
389    /// User has knocked on a room.
390    ///
391    /// Update the internal and cached state accordingly. Return the final Room.
392    pub async fn room_knocked(&self, room_id: &RoomId) -> Result<Room> {
393        let room = self.state_store.get_or_create_room(room_id, RoomState::Knocked);
394
395        if room.state() != RoomState::Knocked {
396            let _state_store_lock = self.state_store_lock().lock().await;
397
398            let mut room_info = room.clone_info();
399            room_info.mark_as_knocked();
400            room_info.mark_state_partially_synced();
401            room_info.mark_members_missing(); // the own member event changed
402
403            // We are no longer joined to the room, so the invite acceptance details are no
404            // longer relevant.
405            #[cfg(feature = "e2e-encryption")]
406            if let Some(olm_machine) = self.olm_machine().await.as_ref() {
407                olm_machine.store().clear_room_pending_key_bundle(room_info.room_id()).await?
408            }
409
410            let mut changes = StateChanges::default();
411            changes.add_room(room_info.clone());
412            self.state_store.save_changes(&changes).await?; // Update the store
413            room.set_room_info(room_info, RoomInfoNotableUpdateReasons::MEMBERSHIP);
414        }
415
416        Ok(room)
417    }
418
419    /// The user has joined a room using this specific client.
420    ///
421    /// This method should be called if the user accepts an invite or if they
422    /// join a public room.
423    ///
424    /// The method will create a [`Room`] object if one does not exist yet and
425    /// set the state of the [`Room`] to [`RoomState::Joined`]. The [`Room`]
426    /// object will be persisted in the cache. Please note that the [`Room`]
427    /// will be a stub until a sync has been received with the full room
428    /// state using [`BaseClient::receive_sync_response`].
429    ///
430    /// Update the internal and cached state accordingly. Return the final Room.
431    ///
432    /// # Arguments
433    ///
434    /// * `room_id` - The unique ID identifying the joined room.
435    /// * `inviter` - When joining this room in response to an invitation, the
436    ///   inviter should be recorded before sending the join request to the
437    ///   server. Providing the inviter here ensures that the
438    ///   [`RoomPendingKeyBundleDetails`] are stored for this room.
439    ///
440    /// # Examples
441    ///
442    /// ```rust
443    /// # use matrix_sdk_base::{BaseClient, store::StoreConfig, RoomState, ThreadingSupport};
444    /// # use ruma::{OwnedRoomId, OwnedUserId, RoomId};
445    /// use matrix_sdk_common::cross_process_lock::CrossProcessLockConfig;
446    /// # async {
447    /// # let client = BaseClient::new(StoreConfig::new(CrossProcessLockConfig::multi_process("example")), ThreadingSupport::Disabled);
448    /// # async fn send_join_request() -> anyhow::Result<OwnedRoomId> { todo!() }
449    /// # async fn maybe_get_inviter(room_id: &RoomId) -> anyhow::Result<Option<OwnedUserId>> { todo!() }
450    /// # let room_id: &RoomId = todo!();
451    /// let maybe_inviter = maybe_get_inviter(room_id).await?;
452    /// let room_id = send_join_request().await?;
453    /// let room = client.room_joined(&room_id, maybe_inviter).await?;
454    ///
455    /// assert_eq!(room.state(), RoomState::Joined);
456    /// # matrix_sdk_test::TestResult::Ok(()) };
457    /// ```
458    pub async fn room_joined(
459        &self,
460        room_id: &RoomId,
461        inviter: Option<OwnedUserId>,
462    ) -> Result<Room> {
463        let room = self.state_store.get_or_create_room(room_id, RoomState::Joined);
464
465        // If the state isn't `RoomState::Joined` then this means that we knew about
466        // this room before. Let's modify the existing state now.
467        if room.state() != RoomState::Joined {
468            let _state_store_lock = self.state_store_lock().lock().await;
469
470            let mut room_info = room.clone_info();
471
472            room_info.mark_as_joined();
473            room_info.mark_state_partially_synced();
474            room_info.mark_members_missing(); // the own member event changed
475
476            #[cfg(feature = "e2e-encryption")]
477            {
478                // If our previous state was an invite and we're now in the joined state, this
479                // means that the user has explicitly accepted an invite. Let's
480                // remember some details about the invite.
481                //
482                // This is somewhat of a workaround for our lack of cryptographic membership.
483                // Later on we will decide if historic room keys should be accepted
484                // based on this info. If a user has accepted an invite and we receive a room
485                // key bundle shortly after, we might accept it. If we don't do
486                // this, the homeserver could trick us into accepting any historic room key
487                // bundle.
488                let previous_state = room.state();
489                if previous_state == RoomState::Invited
490                    && let Some(inviter) = inviter
491                    && let Some(olm_machine) = self.olm_machine().await.as_ref()
492                {
493                    olm_machine.store().store_room_pending_key_bundle(room_id, &inviter).await?
494                }
495            }
496            #[cfg(not(feature = "e2e-encryption"))]
497            {
498                // suppress unused argument warning
499                let _ = inviter;
500            }
501
502            let mut changes = StateChanges::default();
503            changes.add_room(room_info.clone());
504
505            self.state_store.save_changes(&changes).await?; // Update the store
506
507            room.set_room_info(room_info, RoomInfoNotableUpdateReasons::MEMBERSHIP);
508        }
509
510        Ok(room)
511    }
512
513    /// User has left a room.
514    ///
515    /// Update the internal and cached state accordingly.
516    pub async fn room_left(&self, room_id: &RoomId) -> Result<()> {
517        let room = self.state_store.get_or_create_room(room_id, RoomState::Left);
518
519        if room.state() != RoomState::Left {
520            let _state_store_lock = self.state_store_lock().lock().await;
521
522            let mut room_info = room.clone_info();
523            room_info.mark_as_left();
524            room_info.mark_state_partially_synced();
525            room_info.mark_members_missing(); // the own member event changed
526
527            // We are no longer joined to the room, so the invite acceptance details are no
528            // longer relevant.
529            #[cfg(feature = "e2e-encryption")]
530            if let Some(olm_machine) = self.olm_machine().await.as_ref() {
531                olm_machine.store().clear_room_pending_key_bundle(room_info.room_id()).await?
532            }
533
534            let mut changes = StateChanges::default();
535            changes.add_room(room_info.clone());
536            self.state_store.save_changes(&changes).await?; // Update the store
537            room.set_room_info(room_info, RoomInfoNotableUpdateReasons::MEMBERSHIP);
538        }
539
540        Ok(())
541    }
542
543    /// Get a lock to the state store, with an exclusive access.
544    ///
545    /// It doesn't give an access to the state store itself. It's rather a lock
546    /// to synchronise all accesses to the state store.
547    pub fn state_store_lock(&self) -> &Mutex<()> {
548        self.state_store.lock()
549    }
550
551    /// Receive a response from a sync call.
552    ///
553    /// # Arguments
554    ///
555    /// * `response` - The response that we received after a successful sync.
556    #[instrument(skip_all)]
557    pub async fn receive_sync_response(
558        &self,
559        response: api::sync::sync_events::v3::Response,
560    ) -> Result<SyncResponse> {
561        self.receive_sync_response_with_requested_required_states(
562            response,
563            &RequestedRequiredStates::default(),
564        )
565        .await
566    }
567
568    /// Receive a response from a sync call, with the requested required state
569    /// events.
570    ///
571    /// # Arguments
572    ///
573    /// * `response` - The response that we received after a successful sync.
574    /// * `requested_required_states` - The requested required state events.
575    pub async fn receive_sync_response_with_requested_required_states(
576        &self,
577        response: api::sync::sync_events::v3::Response,
578        requested_required_states: &RequestedRequiredStates,
579    ) -> Result<SyncResponse> {
580        // The server might respond multiple times with the same sync token, in
581        // that case we already received this response and there's nothing to
582        // do.
583        if self.state_store.sync_token.read().await.as_ref() == Some(&response.next_batch) {
584            info!("Got the same sync response twice");
585            return Ok(SyncResponse::default());
586        }
587
588        let now = if enabled!(Level::INFO) { Some(Instant::now()) } else { None };
589
590        let user_id = self
591            .session_meta()
592            .expect("Sync shouldn't run without an authenticated user")
593            .user_id
594            .to_owned();
595
596        #[cfg(feature = "e2e-encryption")]
597        let olm_machine = self.olm_machine().await;
598
599        let mut context = Context::new(StateChanges::new(response.next_batch.clone()));
600
601        #[cfg(feature = "e2e-encryption")]
602        let processors::e2ee::to_device::Output { processed_to_device_events: to_device } =
603            processors::e2ee::to_device::from_sync_v2(
604                &response,
605                olm_machine.as_ref(),
606                &self.decryption_settings,
607            )
608            .await?;
609
610        #[cfg(not(feature = "e2e-encryption"))]
611        let to_device = response
612            .to_device
613            .events
614            .into_iter()
615            .map(|raw| {
616                use matrix_sdk_common::deserialized_responses::{
617                    ProcessedToDeviceEvent, ToDeviceUnableToDecryptInfo,
618                    ToDeviceUnableToDecryptReason,
619                };
620
621                if let Ok(Some(event_type)) = raw.get_field::<String>("type") {
622                    if event_type == "m.room.encrypted" {
623                        ProcessedToDeviceEvent::UnableToDecrypt {
624                            encrypted_event: raw,
625                            utd_info: ToDeviceUnableToDecryptInfo {
626                                reason: ToDeviceUnableToDecryptReason::EncryptionIsDisabled,
627                            },
628                        }
629                    } else {
630                        ProcessedToDeviceEvent::PlainText(raw)
631                    }
632                } else {
633                    // Exclude events with no type
634                    ProcessedToDeviceEvent::Invalid(raw)
635                }
636            })
637            .collect();
638
639        let mut ambiguity_cache = AmbiguityCache::new(self.state_store.inner.clone());
640
641        let global_account_data_processor =
642            processors::account_data::global(&response.account_data.events);
643
644        let push_rules = self.get_push_rules(&global_account_data_processor).await?;
645
646        let mut room_updates = RoomUpdates::default();
647        let mut notifications = Default::default();
648
649        let mut updated_members_in_room: BTreeMap<OwnedRoomId, BTreeSet<OwnedUserId>> =
650            BTreeMap::new();
651
652        #[cfg(feature = "e2e-encryption")]
653        let e2ee_context = processors::e2ee::E2EE::new(
654            olm_machine.as_ref(),
655            &self.decryption_settings,
656            self.handle_verification_events,
657        );
658
659        for (room_id, joined_room) in response.rooms.join {
660            let joined_room_update = processors::room::sync_v2::update_joined_room(
661                &mut context,
662                processors::room::RoomCreationData::new(
663                    &room_id,
664                    requested_required_states,
665                    &mut ambiguity_cache,
666                ),
667                joined_room,
668                &mut updated_members_in_room,
669                processors::notification::Notification::new(
670                    &push_rules,
671                    &mut notifications,
672                    &self.state_store,
673                ),
674                #[cfg(feature = "e2e-encryption")]
675                &e2ee_context,
676            )
677            .await?;
678
679            room_updates.joined.insert(room_id, joined_room_update);
680        }
681
682        for (room_id, left_room) in response.rooms.leave {
683            let left_room_update = processors::room::sync_v2::update_left_room(
684                &mut context,
685                processors::room::RoomCreationData::new(
686                    &room_id,
687                    requested_required_states,
688                    &mut ambiguity_cache,
689                ),
690                left_room,
691                processors::notification::Notification::new(
692                    &push_rules,
693                    &mut notifications,
694                    &self.state_store,
695                ),
696                #[cfg(feature = "e2e-encryption")]
697                &e2ee_context,
698            )
699            .await?;
700
701            room_updates.left.insert(room_id, left_room_update);
702        }
703
704        for (room_id, invited_room) in response.rooms.invite {
705            let invited_room_update = processors::room::sync_v2::update_invited_room(
706                &mut context,
707                &room_id,
708                &user_id,
709                invited_room,
710                processors::notification::Notification::new(
711                    &push_rules,
712                    &mut notifications,
713                    &self.state_store,
714                ),
715                #[cfg(feature = "e2e-encryption")]
716                &e2ee_context,
717            )
718            .await?;
719
720            room_updates.invited.insert(room_id, invited_room_update);
721        }
722
723        for (room_id, knocked_room) in response.rooms.knock {
724            let knocked_room_update = processors::room::sync_v2::update_knocked_room(
725                &mut context,
726                &room_id,
727                &user_id,
728                knocked_room,
729                processors::notification::Notification::new(
730                    &push_rules,
731                    &mut notifications,
732                    &self.state_store,
733                ),
734                #[cfg(feature = "e2e-encryption")]
735                &e2ee_context,
736            )
737            .await?;
738
739            room_updates.knocked.insert(room_id, knocked_room_update);
740        }
741
742        global_account_data_processor.apply(&mut context, &self.state_store).await;
743
744        context.state_changes.presence = response
745            .presence
746            .events
747            .iter()
748            .filter_map(|e| {
749                let event = e.deserialize().ok()?;
750                Some((event.sender, e.clone()))
751            })
752            .collect();
753
754        context.state_changes.ambiguity_maps = ambiguity_cache.cache;
755
756        {
757            let _state_store_lock = self.state_store_lock().lock().await;
758
759            processors::changes::save_and_apply(
760                context,
761                &self.state_store,
762                &self.ignore_user_list_changes,
763                Some(response.next_batch.clone()),
764            )
765            .await?;
766        }
767
768        let mut context = Context::default();
769
770        // Now that all the rooms information have been saved, update the display name
771        // of the updated rooms (which relies on information stored in the database).
772        processors::room::display_name::update_for_rooms(
773            &mut context,
774            &room_updates,
775            &self.state_store,
776        )
777        .await;
778
779        // Save the new display name updates if any.
780        {
781            let _state_store_lock = self.state_store_lock().lock().await;
782
783            processors::changes::save_only(context, &self.state_store).await?;
784        }
785
786        for (room_id, member_ids) in updated_members_in_room {
787            if let Some(room) = self.get_room(&room_id) {
788                let _ =
789                    room.room_member_updates_sender.send(RoomMembersUpdate::Partial(member_ids));
790            }
791        }
792
793        if enabled!(Level::INFO) {
794            info!("Processed a sync response in {:?}", now.map(|now| now.elapsed()));
795        }
796
797        let response = SyncResponse {
798            rooms: room_updates,
799            presence: response.presence.events,
800            account_data: response.account_data.events,
801            to_device,
802            notifications,
803        };
804
805        Ok(response)
806    }
807
808    /// Receive a get member events response and convert it to a deserialized
809    /// `MembersResponse`
810    ///
811    /// This client-server request must be made without filters to make sure all
812    /// members are received. Otherwise, an error is returned.
813    ///
814    /// # Arguments
815    ///
816    /// * `room_id` - The room id this response belongs to.
817    ///
818    /// * `response` - The raw response that was received from the server.
819    #[instrument(skip_all, fields(?room_id))]
820    pub async fn receive_all_members(
821        &self,
822        room_id: &RoomId,
823        request: &api::membership::get_member_events::v3::Request,
824        response: &api::membership::get_member_events::v3::Response,
825    ) -> Result<()> {
826        if request.membership.is_some() || request.not_membership.is_some() || request.at.is_some()
827        {
828            // This function assumes all members are loaded at once to optimise how display
829            // name disambiguation works. Using it with partial member list results
830            // would produce incorrect disambiguated display name entries
831            return Err(Error::InvalidReceiveMembersParameters);
832        }
833
834        let Some(room) = self.state_store.room(room_id) else {
835            // The room is unknown to us: leave early.
836            return Ok(());
837        };
838
839        let mut chunk = Vec::with_capacity(response.chunk.len());
840        let mut context = Context::default();
841
842        #[cfg(feature = "e2e-encryption")]
843        let mut user_ids = BTreeSet::new();
844
845        let mut ambiguity_map: HashMap<DisplayName, BTreeSet<OwnedUserId>> = Default::default();
846
847        for raw_event in &response.chunk {
848            let member = match raw_event.deserialize() {
849                Ok(ev) => ev,
850                Err(e) => {
851                    let event_id: Option<String> = raw_event.get_field("event_id").ok().flatten();
852                    debug!(event_id, "Failed to deserialize member event: {e}");
853                    continue;
854                }
855            };
856
857            // TODO: All the actions in this loop used to be done only when the membership
858            // event was not in the store before. This was changed with the new room API,
859            // because e.g. leaving a room makes members events outdated and they need to be
860            // fetched by `members`. Therefore, they need to be overwritten here, even
861            // if they exist.
862            // However, this makes a new problem occur where setting the member events here
863            // potentially races with the sync.
864            // See <https://github.com/matrix-org/matrix-rust-sdk/issues/1205>.
865
866            #[cfg(feature = "e2e-encryption")]
867            match member.membership() {
868                MembershipState::Join | MembershipState::Invite => {
869                    user_ids.insert(member.state_key().to_owned());
870                }
871                _ => (),
872            }
873
874            if let StateEvent::Original(e) = &member
875                && let Some(d) = &e.content.displayname
876            {
877                let display_name = DisplayName::new(d);
878                ambiguity_map.entry(display_name).or_default().insert(member.state_key().clone());
879            }
880
881            let sync_member: SyncRoomMemberEvent = member.clone().into();
882            processors::profiles::upsert_or_delete(&mut context, room_id, &sync_member);
883
884            context
885                .state_changes
886                .state
887                .entry(room_id.to_owned())
888                .or_default()
889                .entry(member.event_type())
890                .or_default()
891                .insert(member.state_key().to_string(), raw_event.clone().cast());
892            chunk.push(member);
893        }
894
895        #[cfg(feature = "e2e-encryption")]
896        processors::e2ee::tracked_users::update(
897            self.olm_machine().await.as_ref(),
898            room.encryption_state(),
899            &user_ids,
900        )
901        .await?;
902
903        context.state_changes.ambiguity_maps.insert(room_id.to_owned(), ambiguity_map);
904
905        {
906            let _state_store_lock = self.state_store_lock().lock().await;
907
908            let mut room_info = room.clone_info();
909            room_info.mark_members_synced();
910            context.state_changes.add_room(room_info);
911
912            processors::changes::save_and_apply(
913                context,
914                &self.state_store,
915                &self.ignore_user_list_changes,
916                None,
917            )
918            .await?;
919        }
920
921        let _ = room.room_member_updates_sender.send(RoomMembersUpdate::FullReload);
922
923        Ok(())
924    }
925
926    /// Receive a successful filter upload response, the filter id will be
927    /// stored under the given name in the store.
928    ///
929    /// The filter id can later be retrieved with the [`get_filter`] method.
930    ///
931    ///
932    /// # Arguments
933    ///
934    /// * `filter_name` - The name that should be used to persist the filter id
935    ///   in the store.
936    ///
937    /// * `response` - The successful filter upload response containing the
938    ///   filter id.
939    ///
940    /// [`get_filter`]: #method.get_filter
941    pub async fn receive_filter_upload(
942        &self,
943        filter_name: &str,
944        response: &api::filter::create_filter::v3::Response,
945    ) -> Result<()> {
946        Ok(self
947            .state_store
948            .set_kv_data(
949                StateStoreDataKey::Filter(filter_name),
950                StateStoreDataValue::Filter(response.filter_id.clone()),
951            )
952            .await?)
953    }
954
955    /// Get the filter id of a previously uploaded filter.
956    ///
957    /// *Note*: A filter will first need to be uploaded and persisted using
958    /// [`receive_filter_upload`].
959    ///
960    /// # Arguments
961    ///
962    /// * `filter_name` - The name of the filter that was previously used to
963    ///   persist the filter.
964    ///
965    /// [`receive_filter_upload`]: #method.receive_filter_upload
966    pub async fn get_filter(&self, filter_name: &str) -> StoreResult<Option<String>> {
967        let filter = self
968            .state_store
969            .get_kv_data(StateStoreDataKey::Filter(filter_name))
970            .await?
971            .map(|d| d.into_filter().expect("State store data not a filter"));
972
973        Ok(filter)
974    }
975
976    /// Get a to-device request that will share a room key with users in a room.
977    #[cfg(feature = "e2e-encryption")]
978    pub async fn share_room_key(&self, room_id: &RoomId) -> Result<Vec<Arc<ToDeviceRequest>>> {
979        match self.olm_machine().await.as_ref() {
980            Some(o) => {
981                let Some(room) = self.get_room(room_id) else {
982                    return Err(Error::InsufficientData);
983                };
984
985                let history_visibility = room.history_visibility_or_default();
986                let Some(room_encryption_event) = room.encryption_settings() else {
987                    return Err(Error::EncryptionNotEnabled);
988                };
989
990                // Don't share the group session with members that are invited
991                // if the history visibility is set to `Joined`
992                let filter = if history_visibility == HistoryVisibility::Joined {
993                    RoomMemberships::JOIN
994                } else {
995                    RoomMemberships::ACTIVE
996                };
997
998                let members = self.state_store.get_user_ids(room_id, filter).await?;
999
1000                let settings = EncryptionSettings::new(
1001                    room_encryption_event,
1002                    history_visibility,
1003                    self.room_key_recipient_strategy.clone(),
1004                );
1005
1006                Ok(o.share_room_key(room_id, members.iter().map(Deref::deref), settings).await?)
1007            }
1008            None => panic!("Olm machine wasn't started"),
1009        }
1010    }
1011
1012    /// Get the room with the given room id.
1013    ///
1014    /// # Arguments
1015    ///
1016    /// * `room_id` - The id of the room that should be fetched.
1017    pub fn get_room(&self, room_id: &RoomId) -> Option<Room> {
1018        self.state_store.room(room_id)
1019    }
1020
1021    /// Forget the room with the given room ID.
1022    ///
1023    /// The room will be dropped from the room list and the store.
1024    ///
1025    /// # Arguments
1026    ///
1027    /// * `room_id` - The id of the room that should be forgotten.
1028    pub async fn forget_room(&self, room_id: &RoomId) -> Result<()> {
1029        // Forget the room in the state store.
1030        self.state_store.forget_room(room_id).await?;
1031
1032        // Remove the room in the event cache store too.
1033        match self.event_cache_store().lock().await? {
1034            // If the lock is clear, we can do the operation as expected.
1035            // If the lock is dirty, we can ignore to refresh the state, we just need to remove a
1036            // room. Also, we must not mark the lock as non-dirty because other operations may be
1037            // critical and may need to refresh the `EventCache`' state.
1038            EventCacheStoreLockState::Clean(guard) | EventCacheStoreLockState::Dirty(guard) => {
1039                guard.remove_room(room_id).await?
1040            }
1041        }
1042
1043        Ok(())
1044    }
1045
1046    /// Get the olm machine.
1047    #[cfg(feature = "e2e-encryption")]
1048    pub async fn olm_machine(&self) -> RwLockReadGuard<'_, Option<OlmMachine>> {
1049        self.olm_machine.read().await
1050    }
1051
1052    /// Get the push rules.
1053    ///
1054    /// Gets the push rules previously processed, otherwise get them from the
1055    /// store. As a fallback, uses [`Ruleset::server_default`] if the user
1056    /// is logged in.
1057    pub(crate) async fn get_push_rules(
1058        &self,
1059        global_account_data_processor: &processors::account_data::Global,
1060    ) -> Result<Ruleset> {
1061        let _timer = timer!(Level::TRACE, "get_push_rules");
1062        if let Some(event) = global_account_data_processor
1063            .push_rules()
1064            .and_then(|ev| ev.deserialize_as_unchecked::<PushRulesEvent>().ok())
1065        {
1066            Ok(event.content.global)
1067        } else if let Some(event) = self
1068            .state_store
1069            .get_account_data_event_static::<PushRulesEventContent>()
1070            .await?
1071            .and_then(|ev| ev.deserialize().ok())
1072        {
1073            Ok(event.content.global)
1074        } else if let Some(session_meta) = self.state_store.session_meta() {
1075            Ok(Ruleset::server_default(&session_meta.user_id))
1076        } else {
1077            Ok(Ruleset::new())
1078        }
1079    }
1080
1081    /// Returns a subscriber that publishes an event every time the ignore user
1082    /// list changes
1083    pub fn subscribe_to_ignore_user_list_changes(&self) -> Subscriber<Vec<String>> {
1084        self.ignore_user_list_changes.subscribe()
1085    }
1086
1087    /// Returns a new receiver that gets future room info notable updates.
1088    ///
1089    /// Learn more by reading the [`RoomInfoNotableUpdate`] type.
1090    pub fn room_info_notable_update_receiver(&self) -> broadcast::Receiver<RoomInfoNotableUpdate> {
1091        self.state_store.room_info_notable_update_sender.subscribe()
1092    }
1093
1094    /// Checks whether the provided `user_id` belongs to an ignored user.
1095    pub async fn is_user_ignored(&self, user_id: &UserId) -> bool {
1096        match self.state_store.get_account_data_event_static::<IgnoredUserListEventContent>().await
1097        {
1098            Ok(Some(raw_ignored_user_list)) => match raw_ignored_user_list.deserialize() {
1099                Ok(current_ignored_user_list) => {
1100                    current_ignored_user_list.content.ignored_users.contains_key(user_id)
1101                }
1102                Err(error) => {
1103                    warn!(?error, "Failed to deserialize the ignored user list event");
1104                    false
1105                }
1106            },
1107            Ok(None) => false,
1108            Err(error) => {
1109                warn!(?error, "Could not get the ignored user list from the state store");
1110                false
1111            }
1112        }
1113    }
1114
1115    /// Check the record of whether we are waiting for an [MSC4268] key bundle
1116    /// for the given room.
1117    ///
1118    /// [MSC4268]: https://github.com/matrix-org/matrix-spec-proposals/pull/4268
1119    #[cfg(feature = "e2e-encryption")]
1120    pub async fn get_pending_key_bundle_details_for_room(
1121        &self,
1122        room_id: &RoomId,
1123    ) -> Result<Option<RoomPendingKeyBundleDetails>> {
1124        let result = match self.olm_machine().await.as_ref() {
1125            Some(machine) => {
1126                machine.store().get_pending_key_bundle_details_for_room(room_id).await?
1127            }
1128            None => None,
1129        };
1130        Ok(result)
1131    }
1132}
1133
1134/// Represent the `required_state` values sent by a sync request.
1135///
1136/// This is useful to track what state events have been requested when handling
1137/// a response.
1138///
1139/// For example, if a sync requests the `m.room.encryption` state event, and the
1140/// server replies with nothing, if means the room **is not** encrypted. Without
1141/// knowing which state event was required by the sync, it is impossible to
1142/// interpret the absence of state event from the server as _the room's
1143/// encryption state is **not encrypted**_ or _the room's encryption state is
1144/// **unknown**_.
1145#[derive(Debug, Default)]
1146pub struct RequestedRequiredStates {
1147    default: Vec<(StateEventType, String)>,
1148    for_rooms: HashMap<OwnedRoomId, Vec<(StateEventType, String)>>,
1149}
1150
1151impl RequestedRequiredStates {
1152    /// Create a new `RequestedRequiredStates`.
1153    ///
1154    /// `default` represents the `required_state` value for all rooms.
1155    /// `for_rooms` is the `required_state` per room.
1156    pub fn new(
1157        default: Vec<(StateEventType, String)>,
1158        for_rooms: HashMap<OwnedRoomId, Vec<(StateEventType, String)>>,
1159    ) -> Self {
1160        Self { default, for_rooms }
1161    }
1162
1163    /// Get the `required_state` value for a specific room.
1164    pub fn for_room(&self, room_id: &RoomId) -> &[(StateEventType, String)] {
1165        self.for_rooms.get(room_id).unwrap_or(&self.default)
1166    }
1167}
1168
1169impl From<&v5::Request> for RequestedRequiredStates {
1170    fn from(request: &v5::Request) -> Self {
1171        // The following information is missing in the MSC4186 at the time of writing
1172        // (2025-03-12) but: the `required_state`s from all lists and from all room
1173        // subscriptions are combined by doing an union.
1174        //
1175        // Thus, we can do the same here, put the union in `default` and keep
1176        // `for_rooms` empty. The `Self::for_room` will automatically do the fallback.
1177        let mut default = BTreeSet::new();
1178
1179        for list in request.lists.values() {
1180            default.extend(BTreeSet::from_iter(list.room_details.required_state.iter().cloned()));
1181        }
1182
1183        for room_subscription in request.room_subscriptions.values() {
1184            default.extend(BTreeSet::from_iter(room_subscription.required_state.iter().cloned()));
1185        }
1186
1187        Self { default: default.into_iter().collect(), for_rooms: HashMap::new() }
1188    }
1189}
1190
1191#[cfg(test)]
1192mod tests {
1193    use std::collections::HashMap;
1194
1195    use assert_matches2::assert_let;
1196    #[cfg(feature = "e2e-encryption")]
1197    use assert_matches2::assert_matches;
1198    use futures_util::FutureExt as _;
1199    use matrix_sdk_common::cross_process_lock::CrossProcessLockConfig;
1200    use matrix_sdk_test::{
1201        BOB, InvitedRoomBuilder, LeftRoomBuilder, SyncResponseBuilder, async_test,
1202        event_factory::EventFactory, ruma_response_from_json,
1203    };
1204    use ruma::{
1205        api::client::{self as api, sync::sync_events::v5},
1206        event_id,
1207        events::{StateEventType, room::member::MembershipState},
1208        room_id,
1209        serde::Raw,
1210        user_id,
1211    };
1212    use serde_json::{json, value::to_raw_value};
1213
1214    use super::{BaseClient, RequestedRequiredStates};
1215    use crate::{
1216        RoomDisplayName, RoomState, SessionMeta,
1217        client::ThreadingSupport,
1218        store::{RoomLoadSettings, StateStoreExt, StoreConfig},
1219        test_utils::logged_in_base_client,
1220    };
1221
1222    #[test]
1223    fn test_requested_required_states() {
1224        let room_id_0 = room_id!("!r0");
1225        let room_id_1 = room_id!("!r1");
1226
1227        let requested_required_states = RequestedRequiredStates::new(
1228            vec![(StateEventType::RoomAvatar, "".to_owned())],
1229            HashMap::from([(
1230                room_id_0.to_owned(),
1231                vec![
1232                    (StateEventType::RoomMember, "foo".to_owned()),
1233                    (StateEventType::RoomEncryption, "".to_owned()),
1234                ],
1235            )]),
1236        );
1237
1238        // A special set of state events exists for `room_id_0`.
1239        assert_eq!(
1240            requested_required_states.for_room(room_id_0),
1241            &[
1242                (StateEventType::RoomMember, "foo".to_owned()),
1243                (StateEventType::RoomEncryption, "".to_owned()),
1244            ]
1245        );
1246
1247        // No special list for `room_id_1`, it should return the defaults.
1248        assert_eq!(
1249            requested_required_states.for_room(room_id_1),
1250            &[(StateEventType::RoomAvatar, "".to_owned()),]
1251        );
1252    }
1253
1254    #[test]
1255    fn test_requested_required_states_from_sync_v5_request() {
1256        let room_id_0 = room_id!("!r0");
1257        let room_id_1 = room_id!("!r1");
1258
1259        // Empty request.
1260        let mut request = v5::Request::new();
1261
1262        {
1263            let requested_required_states = RequestedRequiredStates::from(&request);
1264
1265            assert!(requested_required_states.default.is_empty());
1266            assert!(requested_required_states.for_rooms.is_empty());
1267        }
1268
1269        // One list.
1270        request.lists.insert("foo".to_owned(), {
1271            let mut list = v5::request::List::default();
1272            list.room_details.required_state = vec![
1273                (StateEventType::RoomAvatar, "".to_owned()),
1274                (StateEventType::RoomEncryption, "".to_owned()),
1275            ];
1276
1277            list
1278        });
1279
1280        {
1281            let requested_required_states = RequestedRequiredStates::from(&request);
1282
1283            assert_eq!(
1284                requested_required_states.default,
1285                &[
1286                    (StateEventType::RoomAvatar, "".to_owned()),
1287                    (StateEventType::RoomEncryption, "".to_owned())
1288                ]
1289            );
1290            assert!(requested_required_states.for_rooms.is_empty());
1291        }
1292
1293        // Two lists.
1294        request.lists.insert("bar".to_owned(), {
1295            let mut list = v5::request::List::default();
1296            list.room_details.required_state = vec![
1297                (StateEventType::RoomEncryption, "".to_owned()),
1298                (StateEventType::RoomName, "".to_owned()),
1299            ];
1300
1301            list
1302        });
1303
1304        {
1305            let requested_required_states = RequestedRequiredStates::from(&request);
1306
1307            // Union of the state events.
1308            assert_eq!(
1309                requested_required_states.default,
1310                &[
1311                    (StateEventType::RoomAvatar, "".to_owned()),
1312                    (StateEventType::RoomEncryption, "".to_owned()),
1313                    (StateEventType::RoomName, "".to_owned()),
1314                ]
1315            );
1316            assert!(requested_required_states.for_rooms.is_empty());
1317        }
1318
1319        // One room subscription.
1320        request.room_subscriptions.insert(room_id_0.to_owned(), {
1321            let mut room_subscription = v5::request::RoomSubscription::default();
1322
1323            room_subscription.required_state = vec![
1324                (StateEventType::RoomJoinRules, "".to_owned()),
1325                (StateEventType::RoomEncryption, "".to_owned()),
1326            ];
1327
1328            room_subscription
1329        });
1330
1331        {
1332            let requested_required_states = RequestedRequiredStates::from(&request);
1333
1334            // Union of state events, all in `default`, still nothing in `for_rooms`.
1335            assert_eq!(
1336                requested_required_states.default,
1337                &[
1338                    (StateEventType::RoomAvatar, "".to_owned()),
1339                    (StateEventType::RoomEncryption, "".to_owned()),
1340                    (StateEventType::RoomJoinRules, "".to_owned()),
1341                    (StateEventType::RoomName, "".to_owned()),
1342                ]
1343            );
1344            assert!(requested_required_states.for_rooms.is_empty());
1345        }
1346
1347        // Two room subscriptions.
1348        request.room_subscriptions.insert(room_id_1.to_owned(), {
1349            let mut room_subscription = v5::request::RoomSubscription::default();
1350
1351            room_subscription.required_state = vec![
1352                (StateEventType::RoomName, "".to_owned()),
1353                (StateEventType::RoomTopic, "".to_owned()),
1354            ];
1355
1356            room_subscription
1357        });
1358
1359        {
1360            let requested_required_states = RequestedRequiredStates::from(&request);
1361
1362            // Union of state events, all in `default`, still nothing in `for_rooms`.
1363            assert_eq!(
1364                requested_required_states.default,
1365                &[
1366                    (StateEventType::RoomAvatar, "".to_owned()),
1367                    (StateEventType::RoomEncryption, "".to_owned()),
1368                    (StateEventType::RoomJoinRules, "".to_owned()),
1369                    (StateEventType::RoomName, "".to_owned()),
1370                    (StateEventType::RoomTopic, "".to_owned()),
1371                ]
1372            );
1373        }
1374    }
1375
1376    #[async_test]
1377    async fn test_invite_after_leaving() {
1378        let user_id = user_id!("@alice:example.org");
1379        let room_id = room_id!("!test:example.org");
1380
1381        let client = logged_in_base_client(Some(user_id)).await;
1382        let f = EventFactory::new();
1383
1384        let mut sync_builder = SyncResponseBuilder::new();
1385
1386        let response = sync_builder
1387            .add_left_room(
1388                LeftRoomBuilder::new(room_id).add_timeline_event(
1389                    EventFactory::new()
1390                        .member(user_id)
1391                        .membership(MembershipState::Leave)
1392                        .display_name("Alice")
1393                        .event_id(event_id!("$994173582443PhrSn:example.org")),
1394                ),
1395            )
1396            .build_sync_response();
1397        client.receive_sync_response(response).await.unwrap();
1398        assert_eq!(client.get_room(room_id).unwrap().state(), RoomState::Left);
1399
1400        let response = sync_builder
1401            .add_invited_room(
1402                InvitedRoomBuilder::new(room_id).add_state_event(
1403                    f.member(user_id)
1404                        .sender(user_id!("@example:example.org"))
1405                        .membership(MembershipState::Invite)
1406                        .display_name("Alice"),
1407                ),
1408            )
1409            .build_sync_response();
1410        client.receive_sync_response(response).await.unwrap();
1411        assert_eq!(client.get_room(room_id).unwrap().state(), RoomState::Invited);
1412    }
1413
1414    #[async_test]
1415    async fn test_invite_displayname() {
1416        let user_id = user_id!("@alice:example.org");
1417        let room_id = room_id!("!ithpyNKDtmhneaTQja:example.org");
1418
1419        let client = logged_in_base_client(Some(user_id)).await;
1420
1421        let response = ruma_response_from_json(&json!({
1422            "next_batch": "asdkl;fjasdkl;fj;asdkl;f",
1423            "device_one_time_keys_count": {
1424                "signed_curve25519": 50u64
1425            },
1426            "device_unused_fallback_key_types": [
1427                "signed_curve25519"
1428            ],
1429            "rooms": {
1430                "invite": {
1431                    "!ithpyNKDtmhneaTQja:example.org": {
1432                        "invite_state": {
1433                            "events": [
1434                                {
1435                                    "content": {
1436                                        "creator": "@test:example.org",
1437                                        "room_version": "9"
1438                                    },
1439                                    "sender": "@test:example.org",
1440                                    "state_key": "",
1441                                    "type": "m.room.create"
1442                                },
1443                                {
1444                                    "content": {
1445                                        "join_rule": "invite"
1446                                    },
1447                                    "sender": "@test:example.org",
1448                                    "state_key": "",
1449                                    "type": "m.room.join_rules"
1450                                },
1451                                {
1452                                    "content": {
1453                                        "algorithm": "m.megolm.v1.aes-sha2"
1454                                    },
1455                                    "sender": "@test:example.org",
1456                                    "state_key": "",
1457                                    "type": "m.room.encryption"
1458                                },
1459                                {
1460                                    "content": {
1461                                        "avatar_url": "mxc://example.org/dcBBDwuWEUrjfrOchvkirUST",
1462                                        "displayname": "Kyra",
1463                                        "membership": "join"
1464                                    },
1465                                    "sender": "@test:example.org",
1466                                    "state_key": "@test:example.org",
1467                                    "type": "m.room.member"
1468                                },
1469                                {
1470                                    "content": {
1471                                        "avatar_url": "mxc://example.org/ABFEXSDrESxovWwEnCYdNcHT",
1472                                        "displayname": "alice",
1473                                        "is_direct": true,
1474                                        "membership": "invite"
1475                                    },
1476                                    "origin_server_ts": 1650878657984u64,
1477                                    "sender": "@test:example.org",
1478                                    "state_key": "@alice:example.org",
1479                                    "type": "m.room.member",
1480                                    "unsigned": {
1481                                        "age": 14u64
1482                                    },
1483                                    "event_id": "$fLDqltg9Puj-kWItLSFVHPGN4YkgpYQf2qImPzdmgrE"
1484                                }
1485                            ]
1486                        }
1487                    }
1488                }
1489            }
1490        }));
1491
1492        client.receive_sync_response(response).await.unwrap();
1493
1494        let room = client.get_room(room_id).expect("Room not found");
1495        assert_eq!(room.state(), RoomState::Invited);
1496        assert_eq!(
1497            room.compute_display_name().await.expect("fetching display name failed").into_inner(),
1498            RoomDisplayName::Calculated("Kyra".to_owned())
1499        );
1500    }
1501
1502    #[async_test]
1503    async fn test_deserialization_failure() {
1504        let user_id = user_id!("@alice:example.org");
1505        let room_id = room_id!("!ithpyNKDtmhneaTQja:example.org");
1506
1507        let client = BaseClient::new(
1508            StoreConfig::new(CrossProcessLockConfig::SingleProcess),
1509            ThreadingSupport::Disabled,
1510        );
1511        client
1512            .activate(
1513                SessionMeta { user_id: user_id.to_owned(), device_id: "FOOBAR".into() },
1514                RoomLoadSettings::default(),
1515                #[cfg(feature = "e2e-encryption")]
1516                None,
1517            )
1518            .await
1519            .unwrap();
1520
1521        let response = ruma_response_from_json(&json!({
1522            "next_batch": "asdkl;fjasdkl;fj;asdkl;f",
1523            "rooms": {
1524                "join": {
1525                    "!ithpyNKDtmhneaTQja:example.org": {
1526                        "state": {
1527                            "events": [
1528                                {
1529                                    "invalid": "invalid",
1530                                },
1531                                {
1532                                    "content": {
1533                                        "name": "The room name"
1534                                    },
1535                                    "event_id": "$143273582443PhrSn:example.org",
1536                                    "origin_server_ts": 1432735824653u64,
1537                                    "room_id": "!jEsUZKDJdhlrceRyVU:example.org",
1538                                    "sender": "@example:example.org",
1539                                    "state_key": "",
1540                                    "type": "m.room.name",
1541                                    "unsigned": {
1542                                        "age": 1234
1543                                    }
1544                                },
1545                            ]
1546                        }
1547                    }
1548                }
1549            }
1550        }));
1551
1552        client.receive_sync_response(response).await.unwrap();
1553        client
1554            .state_store()
1555            .get_state_event_static::<ruma::events::room::name::RoomNameEventContent>(room_id)
1556            .await
1557            .expect("Failed to fetch state event")
1558            .expect("State event not found")
1559            .deserialize()
1560            .expect("Failed to deserialize state event");
1561    }
1562
1563    #[async_test]
1564    async fn test_invited_members_arent_ignored() {
1565        let user_id = user_id!("@alice:example.org");
1566        let inviter_user_id = user_id!("@bob:example.org");
1567        let room_id = room_id!("!ithpyNKDtmhneaTQja:example.org");
1568
1569        let client = BaseClient::new(
1570            StoreConfig::new(CrossProcessLockConfig::SingleProcess),
1571            ThreadingSupport::Disabled,
1572        );
1573        client
1574            .activate(
1575                SessionMeta { user_id: user_id.to_owned(), device_id: "FOOBAR".into() },
1576                RoomLoadSettings::default(),
1577                #[cfg(feature = "e2e-encryption")]
1578                None,
1579            )
1580            .await
1581            .unwrap();
1582
1583        // Preamble: let the SDK know about the room.
1584        let mut sync_builder = SyncResponseBuilder::new();
1585        let response = sync_builder
1586            .add_joined_room(matrix_sdk_test::JoinedRoomBuilder::new(room_id))
1587            .build_sync_response();
1588        client.receive_sync_response(response).await.unwrap();
1589
1590        // When I process the result of a /members request that only contains an invited
1591        // member,
1592        let request = api::membership::get_member_events::v3::Request::new(room_id.to_owned());
1593
1594        let raw_member_event = json!({
1595            "content": {
1596                "avatar_url": "mxc://localhost/fewjilfewjil42",
1597                "displayname": "Invited Alice",
1598                "membership": "invite"
1599            },
1600            "event_id": "$151800140517rfvjc:localhost",
1601            "origin_server_ts": 151800140,
1602            "room_id": room_id,
1603            "sender": inviter_user_id,
1604            "state_key": user_id,
1605            "type": "m.room.member",
1606            "unsigned": {
1607                "age": 13374242,
1608            }
1609        });
1610        let response = api::membership::get_member_events::v3::Response::new(vec![Raw::from_json(
1611            to_raw_value(&raw_member_event).unwrap(),
1612        )]);
1613
1614        // It's correctly processed,
1615        client.receive_all_members(room_id, &request, &response).await.unwrap();
1616
1617        let room = client.get_room(room_id).unwrap();
1618
1619        // And I can get the invited member display name and avatar.
1620        let member = room.get_member(user_id).await.expect("ok").expect("exists");
1621
1622        assert_eq!(member.user_id(), user_id);
1623        assert_eq!(member.display_name().unwrap(), "Invited Alice");
1624        assert_eq!(member.avatar_url().unwrap().to_string(), "mxc://localhost/fewjilfewjil42");
1625    }
1626
1627    #[async_test]
1628    async fn test_reinvited_members_get_a_display_name() {
1629        let user_id = user_id!("@alice:example.org");
1630        let inviter_user_id = user_id!("@bob:example.org");
1631        let room_id = room_id!("!ithpyNKDtmhneaTQja:example.org");
1632
1633        let client = BaseClient::new(
1634            StoreConfig::new(CrossProcessLockConfig::SingleProcess),
1635            ThreadingSupport::Disabled,
1636        );
1637        client
1638            .activate(
1639                SessionMeta { user_id: user_id.to_owned(), device_id: "FOOBAR".into() },
1640                RoomLoadSettings::default(),
1641                #[cfg(feature = "e2e-encryption")]
1642                None,
1643            )
1644            .await
1645            .unwrap();
1646
1647        // Preamble: let the SDK know about the room, and that the invited user left it.
1648        let f = EventFactory::new().sender(user_id);
1649        let mut sync_builder = SyncResponseBuilder::new();
1650        let response = sync_builder
1651            .add_joined_room(
1652                matrix_sdk_test::JoinedRoomBuilder::new(room_id)
1653                    .add_state_event(f.member(user_id).leave()),
1654            )
1655            .build_sync_response();
1656        client.receive_sync_response(response).await.unwrap();
1657
1658        // Now, say that the user has been re-invited.
1659        let request = api::membership::get_member_events::v3::Request::new(room_id.to_owned());
1660
1661        let raw_member_event = json!({
1662            "content": {
1663                "avatar_url": "mxc://localhost/fewjilfewjil42",
1664                "displayname": "Invited Alice",
1665                "membership": "invite"
1666            },
1667            "event_id": "$151800140517rfvjc:localhost",
1668            "origin_server_ts": 151800140,
1669            "room_id": room_id,
1670            "sender": inviter_user_id,
1671            "state_key": user_id,
1672            "type": "m.room.member",
1673            "unsigned": {
1674                "age": 13374242,
1675            }
1676        });
1677        let response = api::membership::get_member_events::v3::Response::new(vec![Raw::from_json(
1678            to_raw_value(&raw_member_event).unwrap(),
1679        )]);
1680
1681        // It's correctly processed,
1682        client.receive_all_members(room_id, &request, &response).await.unwrap();
1683
1684        let room = client.get_room(room_id).unwrap();
1685
1686        // And I can get the invited member display name and avatar.
1687        let member = room.get_member(user_id).await.expect("ok").expect("exists");
1688
1689        assert_eq!(member.user_id(), user_id);
1690        assert_eq!(member.display_name().unwrap(), "Invited Alice");
1691        assert_eq!(member.avatar_url().unwrap().to_string(), "mxc://localhost/fewjilfewjil42");
1692    }
1693
1694    #[async_test]
1695    async fn test_ignored_user_list_changes() {
1696        let user_id = user_id!("@alice:example.org");
1697        let client = BaseClient::new(
1698            StoreConfig::new(CrossProcessLockConfig::SingleProcess),
1699            ThreadingSupport::Disabled,
1700        );
1701
1702        client
1703            .activate(
1704                SessionMeta { user_id: user_id.to_owned(), device_id: "FOOBAR".into() },
1705                RoomLoadSettings::default(),
1706                #[cfg(feature = "e2e-encryption")]
1707                None,
1708            )
1709            .await
1710            .unwrap();
1711
1712        let mut subscriber = client.subscribe_to_ignore_user_list_changes();
1713        assert!(subscriber.next().now_or_never().is_none());
1714
1715        let f = EventFactory::new();
1716        let mut sync_builder = SyncResponseBuilder::new();
1717        let response = sync_builder
1718            .add_global_account_data(f.ignored_user_list([(*BOB).into()]))
1719            .build_sync_response();
1720        client.receive_sync_response(response).await.unwrap();
1721
1722        assert_let!(Some(ignored) = subscriber.next().await);
1723        assert_eq!(ignored, [BOB.to_string()]);
1724
1725        // Receive the same response.
1726        let response = sync_builder
1727            .add_global_account_data(f.ignored_user_list([(*BOB).into()]))
1728            .build_sync_response();
1729        client.receive_sync_response(response).await.unwrap();
1730
1731        // No changes in the ignored list.
1732        assert!(subscriber.next().now_or_never().is_none());
1733
1734        // Now remove Bob from the ignored list.
1735        let response =
1736            sync_builder.add_global_account_data(f.ignored_user_list([])).build_sync_response();
1737        client.receive_sync_response(response).await.unwrap();
1738
1739        assert_let!(Some(ignored) = subscriber.next().await);
1740        assert!(ignored.is_empty());
1741    }
1742
1743    #[async_test]
1744    async fn test_is_user_ignored() {
1745        let ignored_user_id = user_id!("@alice:example.org");
1746        let client = logged_in_base_client(None).await;
1747
1748        let mut sync_builder = SyncResponseBuilder::new();
1749        let f = EventFactory::new();
1750        let response = sync_builder
1751            .add_global_account_data(f.ignored_user_list([ignored_user_id.to_owned()]))
1752            .build_sync_response();
1753        client.receive_sync_response(response).await.unwrap();
1754
1755        assert!(client.is_user_ignored(ignored_user_id).await);
1756    }
1757
1758    #[cfg(feature = "e2e-encryption")]
1759    #[async_test]
1760    async fn test_invite_details_are_set() {
1761        let user_id = user_id!("@alice:localhost");
1762        let client = logged_in_base_client(Some(user_id)).await;
1763        let known_room_id = room_id!("!invited:localhost");
1764        let unknown_room_id = room_id!("!unknown:localhost");
1765
1766        let mut sync_builder = SyncResponseBuilder::new();
1767        let response = sync_builder
1768            .add_invited_room(InvitedRoomBuilder::new(known_room_id))
1769            .build_sync_response();
1770        client.receive_sync_response(response).await.unwrap();
1771
1772        // Let us first check the initial state, we should have a room in the invite
1773        // state.
1774        let invited_room = client
1775            .get_room(known_room_id)
1776            .expect("The sync should have created a room in the invited state");
1777
1778        assert_eq!(invited_room.state(), RoomState::Invited);
1779        assert!(
1780            client.get_pending_key_bundle_details_for_room(known_room_id).await.unwrap().is_none()
1781        );
1782
1783        // Now we join the room.
1784        let joined_room = client
1785            .room_joined(known_room_id, Some(user_id.to_owned()))
1786            .await
1787            .expect("We should be able to mark a room as joined");
1788
1789        // Yup, we now have some invite details.
1790        assert_eq!(joined_room.state(), RoomState::Joined);
1791        assert_matches!(
1792            client.get_pending_key_bundle_details_for_room(known_room_id).await,
1793            Ok(Some(details))
1794        );
1795        assert_eq!(details.inviter, user_id);
1796
1797        // If we didn't know about the room before the join, we assume that there wasn't
1798        // an invite and we don't record the timestamp.
1799        assert!(client.get_room(unknown_room_id).is_none());
1800        let unknown_room = client
1801            .room_joined(unknown_room_id, Some(user_id.to_owned()))
1802            .await
1803            .expect("We should be able to mark a room as joined");
1804
1805        assert_eq!(unknown_room.state(), RoomState::Joined);
1806        assert!(
1807            client
1808                .get_pending_key_bundle_details_for_room(unknown_room_id)
1809                .await
1810                .unwrap()
1811                .is_none()
1812        );
1813
1814        sync_builder.clear();
1815        let response =
1816            sync_builder.add_left_room(LeftRoomBuilder::new(known_room_id)).build_sync_response();
1817        client.receive_sync_response(response).await.unwrap();
1818
1819        // Now that we left the room, we shouldn't have any details anymore.
1820        let left_room = client
1821            .get_room(known_room_id)
1822            .expect("The sync should have created a room in the invited state");
1823
1824        assert_eq!(left_room.state(), RoomState::Left);
1825        assert!(
1826            client.get_pending_key_bundle_details_for_room(known_room_id).await.unwrap().is_none()
1827        );
1828    }
1829}