refactor(base): Rename BaseClient::set_or_reload_session and ::logged_in.

This patch renames `BaseClient::set_or_reload_session`
to `BaseClient::activate`, and `BaseClient::logged_in` to
`BaseClient::is_activated`.

The idea behind these renamings is to introduce a “state” for the
`BaseClient`: it is activated when is has a `SessionMeta`, has loaded
its data from the storages, and has an `OlmMachine`. Consequently, the
`logged_in` method is renamed `is_activated` for the symmetry. If one
wants to know if the client is logged in, it can use `is_activated`, or
also `MatrixAuth::logged_in`.
This commit is contained in:
Ivan Enderlin
2025-03-28 15:05:39 +01:00
parent f11eec4caf
commit de615f2ffe
10 changed files with 52 additions and 48 deletions
+1 -1
View File
@@ -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(),
+19 -14
View File
@@ -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<RwLock<Option<OlmMachine>>>,
@@ -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,
+3 -3
View File
@@ -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(),
+2 -2
View File
@@ -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
}
@@ -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,
@@ -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,
@@ -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()),
+11 -3
View File
@@ -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.
@@ -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");
}
@@ -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();
}