diff --git a/benchmarks/benches/room_bench.rs b/benchmarks/benches/room_bench.rs index 7bf583482..8b591ef54 100644 --- a/benchmarks/benches/room_bench.rs +++ b/benchmarks/benches/room_bench.rs @@ -61,7 +61,7 @@ pub fn receive_all_members_benchmark(c: &mut Criterion) { ); runtime - .block_on(base_client.set_session_meta( + .block_on(base_client.set_or_reload_session( SessionMeta { user_id: user_id!("@somebody:example.com").to_owned(), device_id: device_id!("DEVICE_ID").to_owned(), diff --git a/crates/matrix-sdk-base/src/client.rs b/crates/matrix-sdk-base/src/client.rs index 47f04894e..a2b8e2782 100644 --- a/crates/matrix-sdk-base/src/client.rs +++ b/crates/matrix-sdk-base/src/client.rs @@ -115,7 +115,7 @@ pub struct BaseClient { /// The olm-machine that is created once the /// [`SessionMeta`][crate::session::SessionMeta] is set via - /// [`BaseClient::set_session_meta`] + /// [`BaseClient::set_or_reload_session`] #[cfg(feature = "e2e-encryption")] olm_machine: Arc>>, @@ -222,7 +222,10 @@ impl BaseClient { if let Some(session_meta) = self.session_meta().cloned() { copy.state_store - .set_session_meta(session_meta, ©.room_info_notable_update_sender) + .set_session_meta_and_reload_state( + session_meta, + ©.room_info_notable_update_sender, + ) .await?; } @@ -293,7 +296,8 @@ impl BaseClient { self.state_store.session_meta().is_some() } - /// Set the meta of the session. + /// Set the [`SessionMeta`] for this client, and if a state exists for this + /// session, reload it. /// /// If encryption is enabled, this also initializes or restores the /// `OlmMachine`. @@ -312,8 +316,10 @@ impl BaseClient { /// useful if one wishes to create identity keys before knowing the /// user/device IDs, e.g., to use the identity key as the device ID. /// + /// # Panics + /// /// This method panics if it is called twice. - pub async fn set_session_meta( + pub async fn set_or_reload_session( &self, session_meta: SessionMeta, #[cfg(feature = "e2e-encryption")] custom_account: Option< @@ -322,7 +328,7 @@ impl BaseClient { ) -> Result<()> { debug!(user_id = ?session_meta.user_id, device_id = ?session_meta.device_id, "Restoring login"); self.state_store - .set_session_meta(session_meta.clone(), &self.room_info_notable_update_sender) + .set_or_reload_session(session_meta.clone(), &self.room_info_notable_update_sender) .await?; #[cfg(feature = "e2e-encryption")] @@ -808,9 +814,9 @@ impl BaseClient { Ok(events) } else { - // If we have no OlmMachine, just return the events that were passed in. + // If we have no `OlmMachine`, just return the events that were passed in. // This should not happen unless we forget to set things up by calling - // set_session_meta(). + // `Self::set_or_reload_session()`. Ok(encryption_sync_changes.to_device_events) } } @@ -2256,7 +2262,7 @@ mod tests { let client = BaseClient::new(StoreConfig::new("cross-process-store-locks-holder-name".to_owned())); client - .set_session_meta( + .set_or_reload_session( SessionMeta { user_id: user_id.to_owned(), device_id: "FOOBAR".into() }, #[cfg(feature = "e2e-encryption")] None, @@ -2315,7 +2321,7 @@ mod tests { let client = BaseClient::new(StoreConfig::new("cross-process-store-locks-holder-name".to_owned())); client - .set_session_meta( + .set_or_reload_session( SessionMeta { user_id: user_id.to_owned(), device_id: "FOOBAR".into() }, #[cfg(feature = "e2e-encryption")] None, @@ -2376,7 +2382,7 @@ mod tests { let client = BaseClient::new(StoreConfig::new("cross-process-store-locks-holder-name".to_owned())); client - .set_session_meta( + .set_or_reload_session( SessionMeta { user_id: user_id.to_owned(), device_id: "FOOBAR".into() }, #[cfg(feature = "e2e-encryption")] None, @@ -2447,7 +2453,7 @@ mod tests { let client = BaseClient::new(StoreConfig::new("cross-process-store-locks-holder-name".to_owned())); client - .set_session_meta( + .set_or_reload_session( SessionMeta { user_id: user_id.to_owned(), device_id: "FOOBAR".into() }, #[cfg(feature = "e2e-encryption")] None, diff --git a/crates/matrix-sdk-base/src/rooms/normal.rs b/crates/matrix-sdk-base/src/rooms/normal.rs index bbc40ffca..d5679bd56 100644 --- a/crates/matrix-sdk-base/src/rooms/normal.rs +++ b/crates/matrix-sdk-base/src/rooms/normal.rs @@ -2538,7 +2538,7 @@ mod tests { BaseClient::new(StoreConfig::new("cross-process-store-locks-holder-name".to_owned())); client - .set_session_meta( + .set_or_reload_session( SessionMeta { user_id: user_id!("@alice:example.org").into(), device_id: ruma::device_id!("AYEAYEAYE").into(), @@ -2617,7 +2617,7 @@ mod tests { BaseClient::new(StoreConfig::new("cross-process-store-locks-holder-name".to_owned())); client - .set_session_meta( + .set_or_reload_session( SessionMeta { user_id: user_id!("@alice:example.org").into(), device_id: ruma::device_id!("AYEAYEAYE").into(), @@ -3201,7 +3201,7 @@ mod tests { BaseClient::new(StoreConfig::new("cross-process-store-locks-holder-name".to_owned())); client - .set_session_meta( + .set_or_reload_session( SessionMeta { user_id: user_id!("@alice:example.org").into(), device_id: ruma::device_id!("AYEAYEAYE").into(), diff --git a/crates/matrix-sdk-base/src/store/mod.rs b/crates/matrix-sdk-base/src/store/mod.rs index f431dea59..48118f515 100644 --- a/crates/matrix-sdk-base/src/store/mod.rs +++ b/crates/matrix-sdk-base/src/store/mod.rs @@ -205,13 +205,15 @@ impl BaseStateStore { Ok(room_infos) } - /// Set the meta of the session. + /// Set a [`SessionMeta`] and, if a previous state exists, reload the + /// session. /// - /// Restores the state of this `Store` from the given `SessionMeta` and the - /// inner `StateStore`. + /// Reloading a session means: reload all rooms from the state store. + /// + /// # Panics /// /// This method panics if it is called twice. - pub async fn set_session_meta( + pub async fn set_or_reload_session( &self, session_meta: SessionMeta, room_info_notable_update_sender: &broadcast::Sender, diff --git a/crates/matrix-sdk-base/src/test_utils.rs b/crates/matrix-sdk-base/src/test_utils.rs index d69686623..6b0ab2783 100644 --- a/crates/matrix-sdk-base/src/test_utils.rs +++ b/crates/matrix-sdk-base/src/test_utils.rs @@ -28,12 +28,12 @@ pub(crate) async fn logged_in_base_client(user_id: Option<&UserId>) -> BaseClien let user_id = user_id.map(|user_id| user_id.to_owned()).unwrap_or_else(|| owned_user_id!("@u:e.uk")); client - .set_session_meta( + .set_or_reload_session( SessionMeta { user_id: user_id.to_owned(), device_id: "FOOBAR".into() }, #[cfg(feature = "e2e-encryption")] None, ) .await - .expect("set_session_meta failed!"); + .expect("`set_or_reload_session` failed!"); client } diff --git a/crates/matrix-sdk/src/authentication/matrix/mod.rs b/crates/matrix-sdk/src/authentication/matrix/mod.rs index 61373d9f5..776ae9a87 100644 --- a/crates/matrix-sdk/src/authentication/matrix/mod.rs +++ b/crates/matrix-sdk/src/authentication/matrix/mod.rs @@ -760,7 +760,8 @@ impl MatrixAuth { .expect("Client authentication data was already set"); self.client.auth_ctx().set_session_tokens(session.tokens); self.client - .set_session_meta( + .base_client() + .set_or_reload_session( session.meta, #[cfg(feature = "e2e-encryption")] None, diff --git a/crates/matrix-sdk/src/authentication/oauth/mod.rs b/crates/matrix-sdk/src/authentication/oauth/mod.rs index b6a5c310d..d89408b34 100644 --- a/crates/matrix-sdk/src/authentication/oauth/mod.rs +++ b/crates/matrix-sdk/src/authentication/oauth/mod.rs @@ -290,8 +290,7 @@ impl OAuth { lock_value: String, ) -> Result<(), OAuthError> { // FIXME: it must be deferred only because we're using the crypto store and it's - // initialized only in `set_session_meta`, not if we use a dedicated - // store. + // initialized only in `set_or_reload_session`, not if we use a dedicated store. let mut lock = self.ctx().deferred_cross_process_lock_init.lock().await; if lock.is_some() { return Err(CrossProcessRefreshLockError::DuplicatedLock.into()); @@ -304,7 +303,7 @@ impl OAuth { /// Performs a deferred cross-process refresh-lock, if needs be, after an /// olm machine has been initialized. /// - /// Must be called after `set_session_meta`. + /// Must be called after [`BaseClient::set_or_reload_session`]. #[cfg(feature = "e2e-encryption")] async fn deferred_enable_cross_process_refresh_lock(&self) { let deferred_init_lock = self.ctx().deferred_cross_process_lock_init.lock().await; @@ -807,7 +806,8 @@ impl OAuth { self.client.auth_ctx().set_session_tokens(tokens.clone()); self.client - .set_session_meta( + .base_client() + .set_or_reload_session( meta, #[cfg(feature = "e2e-encryption")] None, @@ -1045,7 +1045,8 @@ impl OAuth { } } else { self.client - .set_session_meta( + .base_client() + .set_or_reload_session( new_session, #[cfg(feature = "e2e-encryption")] None, diff --git a/crates/matrix-sdk/src/authentication/oauth/qrcode/login.rs b/crates/matrix-sdk/src/authentication/oauth/qrcode/login.rs index 985c587cc..dbab91f11 100644 --- a/crates/matrix-sdk/src/authentication/oauth/qrcode/login.rs +++ b/crates/matrix-sdk/src/authentication/oauth/qrcode/login.rs @@ -199,7 +199,8 @@ impl<'a> IntoFuture for LoginWithQrCode<'a> { let whoami_response = self.client.whoami().await.map_err(QRCodeLoginError::UserIdDiscovery)?; self.client - .set_session_meta( + .base_client() + .set_or_reload_session( SessionMeta { user_id: whoami_response.user_id, device_id: OwnedDeviceId::from(device_id.to_base64()), @@ -207,7 +208,7 @@ impl<'a> IntoFuture for LoginWithQrCode<'a> { Some(account), ) .await - .map_err(QRCodeLoginError::SessionTokens)?; + .map_err(|error| QRCodeLoginError::SessionTokens(error.into()))?; self.client.oauth().enable_cross_process_lock().await?; diff --git a/crates/matrix-sdk/src/client/mod.rs b/crates/matrix-sdk/src/client/mod.rs index a932bf075..b1daeaea3 100644 --- a/crates/matrix-sdk/src/client/mod.rs +++ b/crates/matrix-sdk/src/client/mod.rs @@ -1271,22 +1271,6 @@ impl Client { } } - pub(crate) async fn set_session_meta( - &self, - session_meta: SessionMeta, - #[cfg(feature = "e2e-encryption")] custom_account: Option, - ) -> Result<()> { - self.base_client() - .set_session_meta( - session_meta, - #[cfg(feature = "e2e-encryption")] - custom_account, - ) - .await?; - - Ok(()) - } - /// Refresh the access token using the authentication API used to log into /// this session. ///