From 35afd65705af9e57feb9f8c94fadf449966ea0a9 Mon Sep 17 00:00:00 2001 From: Devin R Date: Thu, 7 May 2020 07:09:22 -0400 Subject: [PATCH] base/async_client: remove deadcode, remove get room name methods from AsyncClient --- matrix_sdk/src/async_client.rs | 30 ++++-------- matrix_sdk/src/base_client.rs | 63 ++++---------------------- matrix_sdk/src/models/room.rs | 6 ++- matrix_sdk/tests/async_client_tests.rs | 18 +++++--- 4 files changed, 33 insertions(+), 84 deletions(-) diff --git a/matrix_sdk/src/async_client.rs b/matrix_sdk/src/async_client.rs index 1c99ec58..9510c4c0 100644 --- a/matrix_sdk/src/async_client.rs +++ b/matrix_sdk/src/async_client.rs @@ -315,24 +315,6 @@ impl AsyncClient { self.base_client.add_event_emitter(emitter).await; } - /// Returns an `Option` of the room name from a `RoomId`. - /// - /// This is a human readable room name. - pub async fn get_room_name(&self, room_id: &RoomId) -> Option { - // TODO do we want to use the `RoomStateType` enum here or should we have - // 3 separate `room_name` methods. The other option is to remove this and have - // the user get a `Room` and use `Room::calculate_name` method? - self.base_client.calculate_room_name(room_id).await - } - - /// Returns a `Vec` of the room names this client knows about. - /// - /// This is a human readable list of room names. - pub async fn get_room_names(&self) -> Vec { - // TODO same as get_room_name - self.base_client.calculate_room_names().await - } - /// Returns the joined rooms this client knows about. /// /// A `HashMap` of room id to `matrix::models::Room` @@ -396,14 +378,18 @@ impl AsyncClient { /// # let homeserver = Url::parse("http://example.com").unwrap(); /// let store = JsonStore::open("path/to/store").unwrap(); /// let config = AsyncClientConfig::new().state_store(Box::new(store)); - /// let mut cli = AsyncClient::new(homeserver, None).unwrap(); + /// let mut client = AsyncClient::new(homeserver, None).unwrap(); /// # use futures::executor::block_on; /// # block_on(async { - /// let _ = cli.login("name", "password", None, None).await.unwrap(); + /// let _ = client.login("name", "password", None, None).await.unwrap(); /// // returns true when a state store sync is successful - /// assert!(cli.sync_with_state_store().await.unwrap()); + /// assert!(client.sync_with_state_store().await.unwrap()); /// // now state is restored without a request to the server - /// assert_eq!(vec!["room".to_string(), "names".to_string()], cli.get_room_names().await) + /// let mut names = vec![]; + /// for r in client.joined_rooms().read().await.values() { + /// names.push(r.read().await.calculate_name()); + /// } + /// assert_eq!(vec!["room".to_string(), "names".to_string()], names) /// # }); /// ``` pub async fn sync_with_state_store(&self) -> Result { diff --git a/matrix_sdk/src/base_client.rs b/matrix_sdk/src/base_client.rs index 3a7d5a01..34504e6c 100644 --- a/matrix_sdk/src/base_client.rs +++ b/matrix_sdk/src/base_client.rs @@ -267,24 +267,6 @@ impl Client { Ok(()) } - pub(crate) async fn calculate_room_name(&self, room_id: &RoomId) -> Option { - if let Some(room) = self.joined_rooms.read().await.get(room_id) { - let room = room.read().await; - Some(room.room_name.calculate_name(&room.members)) - } else { - None - } - } - - pub(crate) async fn calculate_room_names(&self) -> Vec { - let mut res = Vec::new(); - for room in self.joined_rooms.read().await.values() { - let room = room.read().await; - res.push(room.room_name.calculate_name(&room.members)) - } - res - } - pub(crate) async fn get_or_create_joined_room(&self, room_id: &RoomId) -> Arc> { #[allow(clippy::or_fun_call)] let mut rooms = self.joined_rooms.write().await; @@ -510,33 +492,15 @@ impl Client { pub async fn receive_left_timeline_event( &self, room_id: &RoomId, - event: &mut EventJson, - ) -> (Option>, bool) { + event: &EventJson, + ) -> bool { match event.deserialize() { - #[allow(unused_mut)] - Ok(mut e) => { - #[cfg(feature = "encryption")] - let mut decrypted_event = None; - #[cfg(not(feature = "encryption"))] - let decrypted_event = None; - - #[cfg(feature = "encryption")] - { - if let RoomEvent::RoomEncrypted(ref mut e) = e { - e.room_id = Some(room_id.to_owned()); - let mut olm = self.olm.lock().await; - - if let Some(o) = &mut *olm { - decrypted_event = o.decrypt_room_event(&e).await.ok(); - } - } - } - + Ok(e) => { let room_lock = self.get_or_create_left_room(room_id).await; let mut room = room_lock.write().await; - (decrypted_event, room.receive_timeline_event(&e)) + room.receive_timeline_event(&e) } - _ => (None, false), + _ => false, } } @@ -652,7 +616,7 @@ impl Client { } } - // when events change state updated signals to state store to update database + // when events change state, updated signals to StateStore to update database let mut updated = self.iter_joined_rooms(response).await?; if self.iter_invited_rooms(&response).await? { @@ -808,19 +772,10 @@ impl Client { } for mut event in &mut left_room.timeline.events { - let decrypted_event = { - let (decrypt_ev, timeline_update) = - self.receive_left_timeline_event(room_id, &mut event).await; - if timeline_update { - updated = true; - }; - decrypt_ev + if self.receive_left_timeline_event(room_id, &mut event).await { + updated = true; }; - if let Some(e) = decrypted_event { - *event = e; - } - if let Ok(e) = event.deserialize() { self.emit_timeline_event(&room_id, &e, RoomStateType::Left) .await; @@ -853,7 +808,7 @@ impl Client { } } - self.get_or_create_left_room(&room_id).await.clone() + self.get_or_create_invited_room(&room_id).await.clone() }; for event in &invited_room.invite_state.events { diff --git a/matrix_sdk/src/models/room.rs b/matrix_sdk/src/models/room.rs index e397b6bd..20d8ccaa 100644 --- a/matrix_sdk/src/models/room.rs +++ b/matrix_sdk/src/models/room.rs @@ -633,6 +633,10 @@ mod test { let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)); let _response = client.sync(sync_settings).await.unwrap(); - assert_eq!(vec!["example, example2"], client.get_room_names().await); + let mut names = vec![]; + for r in client.joined_rooms().read().await.values() { + names.push(r.read().await.calculate_name()); + } + assert_eq!(vec!["example, example2"], names); } } diff --git a/matrix_sdk/tests/async_client_tests.rs b/matrix_sdk/tests/async_client_tests.rs index ae5baf6f..04f0bf56 100644 --- a/matrix_sdk/tests/async_client_tests.rs +++ b/matrix_sdk/tests/async_client_tests.rs @@ -81,11 +81,15 @@ async fn room_names() { let _response = client.sync(sync_settings).await.unwrap(); - assert_eq!(vec!["tutorial"], client.get_room_names().await); - assert_eq!( - Some("tutorial".into()), - client - .get_room_name(&RoomId::try_from("!SVkFJHzfwvuaIEawgC:localhost").unwrap()) - .await - ); + let mut names = vec![]; + for r in client.joined_rooms().read().await.values() { + names.push(r.read().await.calculate_name()); + } + assert_eq!(vec!["tutorial"], names); + let room = client + .get_joined_room(&RoomId::try_from("!SVkFJHzfwvuaIEawgC:localhost").unwrap()) + .await + .unwrap(); + + assert_eq!("tutorial".to_string(), room.read().await.calculate_name()); }