refactor(base): Rename set_session_meta to set_or_reload_session.

This patch renames the various `set_session_meta` methods to
`set_or_reload_session`. The idea is to highlight that the method is not
a simple setter: it sets but it _also_ updates the store' state.

The private shortcut method in `matrix_sdk::Client::set_session_meta`
is removed, and caller uses `client.base_client().set_or_reload_session`
instead. Why removing this `Client::set_session_meta` shortcut
method? Because it was creating confusion with another method:
`Client::restoring_session`. This private shortcut method wasn't used in
a lot of places, then I believe it's a nice improvement.
This commit is contained in:
Ivan Enderlin
2025-03-28 10:57:39 +01:00
parent e2e5b39afa
commit db97d616f6
9 changed files with 40 additions and 45 deletions
+1 -1
View File
@@ -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(),
+17 -11
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_session_meta`]
/// [`BaseClient::set_or_reload_session`]
#[cfg(feature = "e2e-encryption")]
olm_machine: Arc<RwLock<Option<OlmMachine>>>,
@@ -222,7 +222,10 @@ impl BaseClient {
if let Some(session_meta) = self.session_meta().cloned() {
copy.state_store
.set_session_meta(session_meta, &copy.room_info_notable_update_sender)
.set_session_meta_and_reload_state(
session_meta,
&copy.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,
+3 -3
View File
@@ -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(),
+6 -4
View File
@@ -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<RoomInfoNotableUpdate>,
+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_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
}
@@ -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,
@@ -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,
@@ -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?;
-16
View File
@@ -1271,22 +1271,6 @@ impl Client {
}
}
pub(crate) async fn set_session_meta(
&self,
session_meta: SessionMeta,
#[cfg(feature = "e2e-encryption")] custom_account: Option<vodozemac::olm::Account>,
) -> 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.
///