base: Don't expose the client fields publicly.

master
Damir Jelić 2020-05-06 15:55:18 +02:00
parent 1639f0fdd8
commit d8b9dc8519
4 changed files with 41 additions and 23 deletions

View File

@ -329,11 +329,11 @@ impl AsyncClient {
self.base_client.calculate_room_names().await 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` /// A `HashMap` of room id to `matrix::models::Room`
pub async fn get_rooms(&self) -> Arc<RwLock<HashMap<RoomId, Arc<tokio::sync::RwLock<Room>>>>> { pub fn joined_rooms(&self) -> Arc<RwLock<HashMap<RoomId, Arc<tokio::sync::RwLock<Room>>>>> {
self.base_client.joined_rooms.clone() self.base_client.joined_rooms()
} }
/// This allows `AsyncClient` to manually sync state with the provided `StateStore`. /// 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 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() { if let Some(session) = session.as_ref() {
request_builder.bearer_auth(&session.access_token) request_builder.bearer_auth(&session.access_token)
@ -884,7 +884,7 @@ impl AsyncClient {
#[cfg(feature = "encryption")] #[cfg(feature = "encryption")]
{ {
let encrypted = { 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 { match room {
Some(r) => r.read().await.is_encrypted(), Some(r) => r.read().await.is_encrypted(),
@ -894,7 +894,7 @@ impl AsyncClient {
if encrypted { if encrypted {
let missing_sessions = { 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 room = room.as_ref().unwrap().read().await;
let users = room.members.keys(); let users = room.members.keys();
self.base_client.get_missing_sessions(users).await? self.base_client.get_missing_sessions(users).await?

View File

@ -67,23 +67,23 @@ pub type Token = String;
pub struct Client { pub struct Client {
/// The current client session containing our user id, device id and access /// The current client session containing our user id, device id and access
/// token. /// token.
pub session: Arc<RwLock<Option<Session>>>, session: Arc<RwLock<Option<Session>>>,
/// The current sync token that should be used for the next sync call. /// The current sync token that should be used for the next sync call.
pub sync_token: Arc<RwLock<Option<Token>>>, pub(crate) sync_token: Arc<RwLock<Option<Token>>>,
/// A map of the rooms our user is joined in. /// A map of the rooms our user is joined in.
pub joined_rooms: Arc<RwLock<HashMap<RoomId, Arc<RwLock<Room>>>>>, joined_rooms: Arc<RwLock<HashMap<RoomId, Arc<RwLock<Room>>>>>,
/// A list of ignored users. /// A list of ignored users.
pub ignored_users: Arc<RwLock<Vec<UserId>>>, pub(crate) ignored_users: Arc<RwLock<Vec<UserId>>>,
/// The push ruleset for the logged in user. /// The push ruleset for the logged in user.
pub push_ruleset: Arc<RwLock<Option<Ruleset>>>, pub(crate) push_ruleset: Arc<RwLock<Option<Ruleset>>>,
/// Any implementor of EventEmitter will act as the callbacks for various /// Any implementor of EventEmitter will act as the callbacks for various
/// events. /// events.
pub event_emitter: Arc<RwLock<Option<Box<dyn EventEmitter>>>>, event_emitter: Arc<RwLock<Option<Box<dyn EventEmitter>>>>,
/// Any implementor of `StateStore` will be called to save `Room` and /// Any implementor of `StateStore` will be called to save `Room` and
/// some `BaseClient` state during `AsyncClient::sync` calls. /// some `BaseClient` state during `AsyncClient::sync` calls.
/// ///
/// There is a default implementation `JsonStore` that saves JSON to disk. /// There is a default implementation `JsonStore` that saves JSON to disk.
pub state_store: Arc<RwLock<Option<Box<dyn StateStore>>>>, state_store: Arc<RwLock<Option<Box<dyn StateStore>>>>,
/// Does the `Client` need to sync with the state store. /// Does the `Client` need to sync with the state store.
needs_state_store_sync: Arc<AtomicBool>, needs_state_store_sync: Arc<AtomicBool>,
@ -152,6 +152,12 @@ impl Client {
}) })
} }
/// The current client session containing our user id, device id and access
/// token.
pub fn session(&self) -> &Arc<RwLock<Option<Session>>> {
&self.session
}
/// Is the client logged in. /// Is the client logged in.
pub async fn logged_in(&self) -> bool { pub async fn logged_in(&self) -> bool {
// TODO turn this into a atomic bool so this method doesn't need to be // TODO turn this into a atomic bool so this method doesn't need to be
@ -267,10 +273,22 @@ impl Client {
.clone() .clone()
} }
pub(crate) async fn get_room(&self, room_id: &RoomId) -> Option<Arc<RwLock<Room>>> { /// 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<Arc<RwLock<Room>>> {
self.joined_rooms.read().await.get(room_id).cloned() 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<RwLock<HashMap<RoomId, Arc<tokio::sync::RwLock<Room>>>>> {
self.joined_rooms.clone()
}
/// Handle a m.ignored_user_list event, updating the room state if necessary. /// Handle a m.ignored_user_list event, updating the room state if necessary.
/// ///
/// Returns true if the room name changed, false otherwise. /// 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. /// * `event` - The event that should be handled by the client.
pub async fn receive_presence_event(&self, room_id: &RoomId, event: &PresenceEvent) -> bool { 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. // 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; let mut room = room.write().await;
room.receive_presence_event(event) room.receive_presence_event(event)
} else { } else {
@ -631,7 +649,7 @@ impl Client {
&self, &self,
room_id: &RoomId, room_id: &RoomId,
) -> Result<Vec<send_event_to_device::Request>> { ) -> Result<Vec<send_event_to_device::Request>> {
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; let mut olm = self.olm.lock().await;
match &mut *olm { match &mut *olm {
@ -756,7 +774,7 @@ impl Client {
return; 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) Arc::clone(&room)
} else { } else {
return; return;
@ -797,7 +815,7 @@ impl Client {
return; 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) Arc::clone(&room)
} else { } else {
return; return;
@ -834,7 +852,7 @@ impl Client {
return; 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) Arc::clone(&room)
} else { } else {
return; return;
@ -867,7 +885,7 @@ impl Client {
return; 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) Arc::clone(&room)
} else { } else {
return; return;
@ -894,7 +912,7 @@ impl Client {
pub(crate) async fn emit_presence_event(&self, room_id: &RoomId, event: &PresenceEvent) { 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(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; ee.on_presence_event(Arc::clone(&room), &event).await;
} }
} }

View File

@ -493,7 +493,7 @@ mod test {
let _response = client.sync(sync_settings).await.unwrap(); 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 rooms = rooms_lock.read().await;
let room = &rooms let room = &rooms
.get(&RoomId::try_from("!SVkFJHzfwvuaIEawgC:localhost").unwrap()) .get(&RoomId::try_from("!SVkFJHzfwvuaIEawgC:localhost").unwrap())

View File

@ -290,7 +290,7 @@ mod test {
let base_client = &client.base_client; let base_client = &client.base_client;
// assert the synced client and the logged in client are equal // 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!( assert_eq!(
base_client.sync_token().await, base_client.sync_token().await,
Some("s526_47314_0_7_1_1_1_11444_1".to_string()) Some("s526_47314_0_7_1_1_1_11444_1".to_string())