base/async_client: remove deadcode, remove get room name methods from AsyncClient

This commit is contained in:
Devin R 2020-05-07 07:09:22 -04:00
parent fa4a99504e
commit 35afd65705
4 changed files with 33 additions and 84 deletions

View file

@ -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<String> {
// 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<String> {
// 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<bool> {

View file

@ -267,24 +267,6 @@ impl Client {
Ok(())
}
pub(crate) async fn calculate_room_name(&self, room_id: &RoomId) -> Option<String> {
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<String> {
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<RwLock<Room>> {
#[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<RoomEvent>,
) -> (Option<EventJson<RoomEvent>>, bool) {
event: &EventJson<RoomEvent>,
) -> 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 {

View file

@ -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);
}
}

View file

@ -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());
}