diff --git a/matrix_sdk/src/async_client.rs b/matrix_sdk/src/async_client.rs index f253baa8..38f9791d 100644 --- a/matrix_sdk/src/async_client.rs +++ b/matrix_sdk/src/async_client.rs @@ -300,9 +300,7 @@ impl AsyncClient { /// Is the client logged in. pub async fn logged_in(&self) -> bool { - // TODO turn this into a atomic bool so this method doesn't need to be - // async. - self.base_client.read().await.logged_in() + self.base_client.read().await.logged_in().await } /// The Homeserver of the client. @@ -806,8 +804,9 @@ impl AsyncClient { let request_builder = if Request::METADATA.requires_authentication { let client = self.base_client.read().await; + let session = client.session.read().await; - if let Some(ref session) = client.session { + if let Some(session) = session.as_ref() { request_builder.bearer_auth(&session.access_token) } else { return Err(Error::AuthenticationRequired); diff --git a/matrix_sdk/src/base_client.rs b/matrix_sdk/src/base_client.rs index 4055c5bf..2acf8e5d 100644 --- a/matrix_sdk/src/base_client.rs +++ b/matrix_sdk/src/base_client.rs @@ -66,7 +66,7 @@ pub type Token = String; pub struct Client { /// The current client session containing our user id, device id and access /// token. - pub session: Option, + pub session: Arc>>, /// The current sync token that should be used for the next sync call. pub sync_token: Arc>>, /// A map of the rooms our user is joined in. @@ -118,7 +118,7 @@ impl Client { }; Ok(Client { - session, + session: Arc::new(RwLock::new(session)), sync_token: Arc::new(RwLock::new(None)), joined_rooms: Arc::new(RwLock::new(HashMap::new())), ignored_users: Arc::new(RwLock::new(Vec::new())), @@ -132,8 +132,10 @@ impl Client { } /// Is the client logged in. - pub fn logged_in(&self) -> bool { - self.session.is_some() + pub async fn logged_in(&self) -> bool { + // TODO turn this into a atomic bool so this method doesn't need to be + // async. + self.session.read().await.is_some() } /// Add `EventEmitter` to `Client`. @@ -153,7 +155,7 @@ impl Client { /// Returns `true` when a state store sync has successfully completed. pub(crate) async fn sync_with_state_store(&mut self) -> Result { if let Some(store) = self.state_store.as_ref() { - if let Some(sess) = self.session.as_ref() { + if let Some(sess) = self.session.read().await.as_ref() { if let Some(client_state) = store.load_client_state(sess).await? { let ClientState { sync_token, @@ -198,7 +200,7 @@ impl Client { device_id: response.device_id.clone(), user_id: response.user_id.clone(), }; - self.session = Some(session); + *self.session.write().await = Some(session); #[cfg(feature = "encryption")] { @@ -236,6 +238,8 @@ impl Client { room_id, &self .session + .read() + .await .as_ref() .expect("Receiving events while not being logged in") .user_id, diff --git a/matrix_sdk/src/state/state_store.rs b/matrix_sdk/src/state/state_store.rs index 99e2ac9a..67880ce5 100644 --- a/matrix_sdk/src/state/state_store.rs +++ b/matrix_sdk/src/state/state_store.rs @@ -290,7 +290,7 @@ mod test { let base_client = client.base_client.read().await; // assert the synced client and the logged in client are equal - assert_eq!(base_client.session, Some(session)); + assert_eq!(*base_client.session.read().await, Some(session)); assert_eq!( base_client.sync_token().await, Some("s526_47314_0_7_1_1_1_11444_1".to_string())