diff --git a/matrix_sdk/src/async_client.rs b/matrix_sdk/src/async_client.rs index 745b5b71..f884dd6e 100644 --- a/matrix_sdk/src/async_client.rs +++ b/matrix_sdk/src/async_client.rs @@ -329,11 +329,11 @@ impl AsyncClient { self.base_client.calculate_room_names().await } - /// Returns the rooms this client knows about. + /// Returns the joined rooms this client knows about. /// /// A `HashMap` of room id to `matrix::models::Room` - pub async fn get_rooms(&self) -> Arc>>>> { - self.base_client.joined_rooms.clone() + pub fn joined_rooms(&self) -> Arc>>>> { + self.base_client.joined_rooms() } /// This allows `AsyncClient` to manually sync state with the provided `StateStore`. @@ -798,7 +798,7 @@ impl AsyncClient { }; let request_builder = if Request::METADATA.requires_authentication { - let session = self.base_client.session.read().await; + let session = self.base_client.session().read().await; if let Some(session) = session.as_ref() { request_builder.bearer_auth(&session.access_token) @@ -884,7 +884,7 @@ impl AsyncClient { #[cfg(feature = "encryption")] { let encrypted = { - let room = self.base_client.get_room(room_id).await; + let room = self.base_client.get_joined_room(room_id).await; match room { Some(r) => r.read().await.is_encrypted(), @@ -894,7 +894,7 @@ impl AsyncClient { if encrypted { let missing_sessions = { - let room = self.base_client.get_room(room_id).await; + let room = self.base_client.get_joined_room(room_id).await; let room = room.as_ref().unwrap().read().await; let users = room.members.keys(); self.base_client.get_missing_sessions(users).await? diff --git a/matrix_sdk/src/base_client.rs b/matrix_sdk/src/base_client.rs index 3d38ecee..5b9a96bc 100644 --- a/matrix_sdk/src/base_client.rs +++ b/matrix_sdk/src/base_client.rs @@ -67,23 +67,23 @@ pub type Token = String; pub struct Client { /// The current client session containing our user id, device id and access /// token. - pub session: Arc>>, + session: Arc>>, /// The current sync token that should be used for the next sync call. - pub sync_token: Arc>>, + pub(crate) sync_token: Arc>>, /// A map of the rooms our user is joined in. - pub joined_rooms: Arc>>>>, + joined_rooms: Arc>>>>, /// A list of ignored users. - pub ignored_users: Arc>>, + pub(crate) ignored_users: Arc>>, /// The push ruleset for the logged in user. - pub push_ruleset: Arc>>, + pub(crate) push_ruleset: Arc>>, /// Any implementor of EventEmitter will act as the callbacks for various /// events. - pub event_emitter: Arc>>>, + event_emitter: Arc>>>, /// Any implementor of `StateStore` will be called to save `Room` and /// some `BaseClient` state during `AsyncClient::sync` calls. /// /// There is a default implementation `JsonStore` that saves JSON to disk. - pub state_store: Arc>>>, + state_store: Arc>>>, /// Does the `Client` need to sync with the state store. needs_state_store_sync: Arc, @@ -152,6 +152,12 @@ impl Client { }) } + /// The current client session containing our user id, device id and access + /// token. + pub fn session(&self) -> &Arc>> { + &self.session + } + /// 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 @@ -267,10 +273,22 @@ impl Client { .clone() } - pub(crate) async fn get_room(&self, room_id: &RoomId) -> Option>> { + /// Get a joined room with the given room id. + /// + /// # Arguments + /// + /// `room_id` - The unique id of the room that should be fetched. + pub(crate) async fn get_joined_room(&self, room_id: &RoomId) -> Option>> { self.joined_rooms.read().await.get(room_id).cloned() } + /// Returns the joined rooms this client knows about. + /// + /// A `HashMap` of room id to `matrix::models::Room` + pub fn joined_rooms(&self) -> Arc>>>> { + self.joined_rooms.clone() + } + /// Handle a m.ignored_user_list event, updating the room state if necessary. /// /// Returns true if the room name changed, false otherwise. @@ -373,7 +391,7 @@ impl Client { /// * `event` - The event that should be handled by the client. pub async fn receive_presence_event(&self, room_id: &RoomId, event: &PresenceEvent) -> bool { // this should be the room that was just created in the `Client::sync` loop. - if let Some(room) = self.get_room(room_id).await { + if let Some(room) = self.get_joined_room(room_id).await { let mut room = room.write().await; room.receive_presence_event(event) } else { @@ -631,7 +649,7 @@ impl Client { &self, room_id: &RoomId, ) -> Result> { - let room = self.get_room(room_id).await.expect("No room found"); + let room = self.get_joined_room(room_id).await.expect("No room found"); let mut olm = self.olm.lock().await; match &mut *olm { @@ -756,7 +774,7 @@ impl Client { return; }; - let room = if let Some(room) = self.get_room(&room_id).await { + let room = if let Some(room) = self.get_joined_room(&room_id).await { Arc::clone(&room) } else { return; @@ -797,7 +815,7 @@ impl Client { return; }; - let room = if let Some(room) = self.get_room(&room_id).await { + let room = if let Some(room) = self.get_joined_room(&room_id).await { Arc::clone(&room) } else { return; @@ -834,7 +852,7 @@ impl Client { return; }; - let room = if let Some(room) = self.get_room(&room_id).await { + let room = if let Some(room) = self.get_joined_room(&room_id).await { Arc::clone(&room) } else { return; @@ -867,7 +885,7 @@ impl Client { return; }; - let room = if let Some(room) = self.get_room(&room_id).await { + let room = if let Some(room) = self.get_joined_room(&room_id).await { Arc::clone(&room) } else { return; @@ -894,7 +912,7 @@ impl Client { pub(crate) async fn emit_presence_event(&self, room_id: &RoomId, event: &PresenceEvent) { if let Some(ee) = &self.event_emitter.read().await.as_ref() { - if let Some(room) = self.get_room(&room_id).await { + if let Some(room) = self.get_joined_room(&room_id).await { ee.on_presence_event(Arc::clone(&room), &event).await; } } diff --git a/matrix_sdk/src/models/room.rs b/matrix_sdk/src/models/room.rs index a5c25f40..da7aba1b 100644 --- a/matrix_sdk/src/models/room.rs +++ b/matrix_sdk/src/models/room.rs @@ -493,7 +493,7 @@ mod test { let _response = client.sync(sync_settings).await.unwrap(); - let rooms_lock = &client.base_client.joined_rooms; + let rooms_lock = &client.base_client.joined_rooms(); let rooms = rooms_lock.read().await; let room = &rooms .get(&RoomId::try_from("!SVkFJHzfwvuaIEawgC:localhost").unwrap()) diff --git a/matrix_sdk/src/state/state_store.rs b/matrix_sdk/src/state/state_store.rs index 7bb91aac..75c9771c 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; // assert the synced client and the logged in client are equal - assert_eq!(*base_client.session.read().await, 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())