diff --git a/benchmarks/benches/room_bench.rs b/benchmarks/benches/room_bench.rs index 8b591ef54..350c6e50e 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_or_reload_session( + .block_on(base_client.activate( 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 7337c8713..3be3cf986 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_or_reload_session`] + /// [`BaseClient::activate`] #[cfg(feature = "e2e-encryption")] olm_machine: Arc>>, @@ -289,16 +289,21 @@ impl BaseClient { &self.event_cache_store } - /// Is the client logged in. - pub fn logged_in(&self) -> bool { + /// Check whether the client has been activated. + /// + /// See [`BaseClient::activate`] to know what it means. + pub fn is_activated(&self) -> bool { self.state_store.session_meta().is_some() } - /// Set the [`SessionMeta`] for this client, and if a state exists for this - /// session, reload it. + /// Activate the client. /// - /// If encryption is enabled, this also initializes or restores the - /// `OlmMachine`. + /// A client is considered active when: + /// + /// 1. It has a `SessionMeta` (user ID, device ID and access token), + /// 2. Has loaded cached data from storage, + /// 3. If encryption is enabled, it also initialized or restored its + /// `OlmMachine`. /// /// # Arguments /// @@ -317,14 +322,14 @@ impl BaseClient { /// # Panics /// /// This method panics if it is called twice. - pub async fn set_or_reload_session( + pub async fn activate( &self, session_meta: SessionMeta, #[cfg(feature = "e2e-encryption")] custom_account: Option< crate::crypto::vodozemac::olm::Account, >, ) -> Result<()> { - debug!(user_id = ?session_meta.user_id, device_id = ?session_meta.device_id, "Restoring login"); + debug!(user_id = ?session_meta.user_id, device_id = ?session_meta.device_id, "Activating the client"); self.state_store .set_or_reload_session(session_meta.clone(), &self.room_info_notable_update_sender) .await?; @@ -814,7 +819,7 @@ impl BaseClient { } else { // 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 - // `Self::set_or_reload_session()`. + // `Self::activate()`. Ok(encryption_sync_changes.to_device_events) } } @@ -2260,7 +2265,7 @@ mod tests { let client = BaseClient::new(StoreConfig::new("cross-process-store-locks-holder-name".to_owned())); client - .set_or_reload_session( + .activate( SessionMeta { user_id: user_id.to_owned(), device_id: "FOOBAR".into() }, #[cfg(feature = "e2e-encryption")] None, @@ -2319,7 +2324,7 @@ mod tests { let client = BaseClient::new(StoreConfig::new("cross-process-store-locks-holder-name".to_owned())); client - .set_or_reload_session( + .activate( SessionMeta { user_id: user_id.to_owned(), device_id: "FOOBAR".into() }, #[cfg(feature = "e2e-encryption")] None, @@ -2380,7 +2385,7 @@ mod tests { let client = BaseClient::new(StoreConfig::new("cross-process-store-locks-holder-name".to_owned())); client - .set_or_reload_session( + .activate( SessionMeta { user_id: user_id.to_owned(), device_id: "FOOBAR".into() }, #[cfg(feature = "e2e-encryption")] None, @@ -2451,7 +2456,7 @@ mod tests { let client = BaseClient::new(StoreConfig::new("cross-process-store-locks-holder-name".to_owned())); client - .set_or_reload_session( + .activate( 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 d5679bd56..eb2661ac2 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_or_reload_session( + .activate( 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_or_reload_session( + .activate( 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_or_reload_session( + .activate( SessionMeta { user_id: user_id!("@alice:example.org").into(), device_id: ruma::device_id!("AYEAYEAYE").into(), diff --git a/crates/matrix-sdk-base/src/test_utils.rs b/crates/matrix-sdk-base/src/test_utils.rs index 6b0ab2783..d1cc54bf2 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_or_reload_session( + .activate( SessionMeta { user_id: user_id.to_owned(), device_id: "FOOBAR".into() }, #[cfg(feature = "e2e-encryption")] None, ) .await - .expect("`set_or_reload_session` failed!"); + .expect("`activate` failed!"); client } diff --git a/crates/matrix-sdk/src/authentication/matrix/mod.rs b/crates/matrix-sdk/src/authentication/matrix/mod.rs index 776ae9a87..3334f8c8b 100644 --- a/crates/matrix-sdk/src/authentication/matrix/mod.rs +++ b/crates/matrix-sdk/src/authentication/matrix/mod.rs @@ -761,7 +761,7 @@ impl MatrixAuth { self.client.auth_ctx().set_session_tokens(session.tokens); self.client .base_client() - .set_or_reload_session( + .activate( 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 d89408b34..313c43324 100644 --- a/crates/matrix-sdk/src/authentication/oauth/mod.rs +++ b/crates/matrix-sdk/src/authentication/oauth/mod.rs @@ -807,7 +807,7 @@ impl OAuth { self.client.auth_ctx().set_session_tokens(tokens.clone()); self.client .base_client() - .set_or_reload_session( + .activate( meta, #[cfg(feature = "e2e-encryption")] None, @@ -1046,7 +1046,7 @@ impl OAuth { } else { self.client .base_client() - .set_or_reload_session( + .activate( 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 dbab91f11..703b3e517 100644 --- a/crates/matrix-sdk/src/authentication/oauth/qrcode/login.rs +++ b/crates/matrix-sdk/src/authentication/oauth/qrcode/login.rs @@ -200,7 +200,7 @@ impl<'a> IntoFuture for LoginWithQrCode<'a> { self.client.whoami().await.map_err(QRCodeLoginError::UserIdDiscovery)?; self.client .base_client() - .set_or_reload_session( + .activate( SessionMeta { user_id: whoami_response.user_id, device_id: OwnedDeviceId::from(device_id.to_base64()), diff --git a/crates/matrix-sdk/src/client/mod.rs b/crates/matrix-sdk/src/client/mod.rs index b1daeaea3..20ebe5a52 100644 --- a/crates/matrix-sdk/src/client/mod.rs +++ b/crates/matrix-sdk/src/client/mod.rs @@ -502,9 +502,17 @@ impl Client { self.inner.http_client.request_config } - /// Is the client logged in. - pub fn logged_in(&self) -> bool { - self.inner.base_client.logged_in() + /// Check whether the client has been activated. + /// + /// A client is considered active when: + /// + /// 1. It has a `SessionMeta` (user ID, device ID and access token), i.e. it + /// is logged in, + /// 2. Has loaded cached data from storage, + /// 3. If encryption is enabled, it also initialized or restored its + /// `OlmMachine`. + pub fn is_activated(&self) -> bool { + self.inner.base_client.is_activated() } /// The server used by the client. diff --git a/crates/matrix-sdk/tests/integration/matrix_auth.rs b/crates/matrix-sdk/tests/integration/matrix_auth.rs index d62968374..58cbbe85a 100644 --- a/crates/matrix-sdk/tests/integration/matrix_auth.rs +++ b/crates/matrix-sdk/tests/integration/matrix_auth.rs @@ -73,7 +73,7 @@ async fn test_login() { let auth = client.matrix_auth(); auth.login_username("example", "wordpass").send().await.unwrap(); - assert!(client.logged_in(), "Client should be logged in"); + assert!(client.is_activated(), "Client should be activated"); assert!(auth.logged_in(), "Client should be logged in with the MatrixAuth API"); assert_matches!(client.auth_api(), Some(AuthApi::Matrix(_))); @@ -94,9 +94,7 @@ async fn test_login_with_discovery() { client.matrix_auth().login_username("example", "wordpass").send().await.unwrap(); - let logged_in = client.logged_in(); - assert!(logged_in, "Client should be logged in"); - + assert!(client.is_activated(), "Client should be activated"); assert_eq!(client.homeserver().as_str(), "https://example.org/"); } @@ -112,9 +110,7 @@ async fn test_login_no_discovery() { client.matrix_auth().login_username("example", "wordpass").send().await.unwrap(); - let logged_in = client.logged_in(); - assert!(logged_in, "Client should be logged in"); - + assert!(client.is_activated(), "Client should be activated"); assert_eq!(client.homeserver(), Url::parse(&server.uri()).unwrap()); } @@ -152,8 +148,7 @@ async fn test_login_with_sso() { .await .unwrap(); - let logged_in = client.logged_in(); - assert!(logged_in, "Client should be logged in"); + assert!(client.is_activated(), "Client should be activated"); } #[async_test] @@ -187,8 +182,7 @@ async fn test_login_with_sso_token() { auth.login_token("averysmalltoken").send().await.unwrap(); - let logged_in = client.logged_in(); - assert!(logged_in, "Client should be logged in"); + assert!(client.is_activated(), "Client should be activated"); } #[async_test] @@ -223,8 +217,7 @@ async fn test_login_with_sso_callback() { let callback_url = Url::parse("http://127.0.0.1:3030?loginToken=averysmalltoken").unwrap(); auth.login_with_sso_callback(callback_url).unwrap().await.unwrap(); - let logged_in = client.logged_in(); - assert!(logged_in, "Client should be logged in"); + assert!(client.is_activated(), "Client should be activated"); } #[async_test] @@ -484,7 +477,7 @@ async fn test_login_with_cross_signing_bootstrapping() { let auth = client.matrix_auth(); auth.login_username("example", "hunter2").send().await.unwrap(); - assert!(client.logged_in(), "Client should be logged in"); + assert!(client.is_activated(), "Client should be activated"); assert!(auth.logged_in(), "Client should be logged in with the MatrixAuth API"); client.encryption().wait_for_e2ee_initialization_tasks().await; @@ -537,7 +530,7 @@ async fn test_login_with_cross_signing_bootstrapping() { let auth = client.matrix_auth(); auth.login_token("HUNTER2").send().await.unwrap(); - assert!(client.logged_in(), "Client should be logged in"); + assert!(client.is_activated(), "Client should be activated"); assert!(auth.logged_in(), "Client should be logged in with the MatrixAuth API"); client.encryption().wait_for_e2ee_initialization_tasks().await; @@ -613,7 +606,7 @@ async fn test_login_doesnt_fail_if_cross_signing_bootstrapping_failed() { let auth = client.matrix_auth(); auth.login_username("example", "hunter2").send().await.unwrap(); - assert!(client.logged_in(), "Client should be logged in"); + assert!(client.is_activated(), "Client should be activated"); assert!(auth.logged_in(), "Client should be logged in with the MatrixAuth API"); let me = client.user_id().expect("we are now logged in"); @@ -734,6 +727,6 @@ async fn test_login_with_cross_signing_bootstrapping_already_bootstrapped() { let auth = client.matrix_auth(); auth.login_username("example", "hunter2").send().await.unwrap(); - assert!(client.logged_in(), "Client should be logged in"); + assert!(client.is_activated(), "Client should be activated"); assert!(auth.logged_in(), "Client should be logged in with the MatrixAuth API"); } diff --git a/crates/matrix-sdk/tests/integration/refresh_token.rs b/crates/matrix-sdk/tests/integration/refresh_token.rs index 29df957e0..065801ce7 100644 --- a/crates/matrix-sdk/tests/integration/refresh_token.rs +++ b/crates/matrix-sdk/tests/integration/refresh_token.rs @@ -63,8 +63,7 @@ async fn test_login_username_refresh_token() { .await .unwrap(); - let logged_in = client.logged_in(); - assert!(logged_in, "Client should be logged in"); + assert!(client.is_activated(), "Client should be activated"); res.refresh_token.unwrap(); } @@ -108,8 +107,7 @@ async fn test_login_sso_refresh_token() { .await .unwrap(); - let logged_in = client.logged_in(); - assert!(logged_in, "Client should be logged in"); + assert!(client.is_activated(), "Client should be activated"); res.refresh_token.unwrap(); }