From 1313c3da3c709796103b4e045321852cc9ecf92d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Tue, 22 Dec 2020 10:47:21 +0100 Subject: [PATCH] client: Restore the membership based get room methods. --- matrix_sdk/src/client.rs | 148 +++++++++++++++---------------- matrix_sdk_base/src/client.rs | 30 +++---- matrix_sdk_base/src/rooms/mod.rs | 16 ++++ matrix_sdk_base/src/store.rs | 12 ++- 4 files changed, 108 insertions(+), 98 deletions(-) diff --git a/matrix_sdk/src/client.rs b/matrix_sdk/src/client.rs index 65d17d20..f63301f3 100644 --- a/matrix_sdk/src/client.rs +++ b/matrix_sdk/src/client.rs @@ -41,7 +41,8 @@ use tracing::{debug, warn}; use tracing::{error, info, instrument}; use matrix_sdk_base::{ - responses::SyncResponse, BaseClient, BaseClientConfig, EventEmitter, JoinedRoom, Session, Store, + responses::SyncResponse, BaseClient, BaseClientConfig, EventEmitter, InvitedRoom, JoinedRoom, + LeftRoom, Session, Store, }; #[cfg(feature = "encryption")] @@ -569,26 +570,26 @@ impl Client { /// /// `room_id` - The unique id of the room that should be fetched. pub fn get_joined_room(&self, room_id: &RoomId) -> Option { - self.base_client.get_joined_room(room_id) + self.store().get_joined_room(room_id) } - ///// Get an invited room with the given room id. - ///// - ///// # Arguments - ///// - ///// `room_id` - The unique id of the room that should be fetched. - //pub async fn get_invited_room(&self, room_id: &RoomId) -> Option>> { - // self.base_client.get_invited_room(room_id).await - //} + /// Get an invited room with the given room id. + /// + /// # Arguments + /// + /// `room_id` - The unique id of the room that should be fetched. + pub fn get_invited_room(&self, room_id: &RoomId) -> Option { + self.store().get_invited_room(room_id) + } - ///// Get a left room with the given room id. - ///// - ///// # Arguments - ///// - ///// `room_id` - The unique id of the room that should be fetched. - //pub async fn get_left_room(&self, room_id: &RoomId) -> Option>> { - // self.base_client.get_left_room(room_id).await - //} + /// Get a left room with the given room id. + /// + /// # Arguments + /// + /// `room_id` - The unique id of the room that should be fetched. + pub fn get_left_room(&self, room_id: &RoomId) -> Option { + self.store().get_left_room(room_id) + } /// Login to the server. /// @@ -2298,78 +2299,73 @@ mod test { assert!(client.devices().await.is_ok()); } - // #[tokio::test] - // async fn test_join_leave_room() { - // let homeserver = Url::from_str(&mockito::server_url()).unwrap(); + #[tokio::test] + async fn test_join_leave_room() { + let homeserver = Url::from_str(&mockito::server_url()).unwrap(); - // let room_id = room_id!("!SVkFJHzfwvuaIEawgC:localhost"); + let room_id = room_id!("!SVkFJHzfwvuaIEawgC:localhost"); - // let session = Session { - // access_token: "1234".to_owned(), - // user_id: user_id!("@example:localhost"), - // device_id: "DEVICEID".into(), - // }; + let session = Session { + access_token: "1234".to_owned(), + user_id: user_id!("@example:localhost"), + device_id: "DEVICEID".into(), + }; - // let _m = mock( - // "GET", - // Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_string()), - // ) - // .with_status(200) - // .with_body(test_json::SYNC.to_string()) - // .create(); + let _m = mock( + "GET", + Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_string()), + ) + .with_status(200) + .with_body(test_json::SYNC.to_string()) + .create(); - // let dir = tempdir().unwrap(); - // let path: &Path = dir.path(); - // let store = Box::new(JsonStore::open(path).unwrap()); + let client = Client::new(homeserver.clone()).unwrap(); + client.restore_login(session.clone()).await.unwrap(); - // let config = ClientConfig::default().state_store(store); - // let client = Client::new_with_config(homeserver.clone(), config).unwrap(); - // client.restore_login(session.clone()).await.unwrap(); + let room = client.get_joined_room(&room_id); + assert!(room.is_none()); - // let room = client.get_joined_room(&room_id).await; - // assert!(room.is_none()); + client.sync_once(SyncSettings::default()).await.unwrap(); - // client.sync_once(SyncSettings::default()).await.unwrap(); + let room = client.get_left_room(&room_id); + assert!(room.is_none()); - // let room = client.get_left_room(&room_id).await; - // assert!(room.is_none()); + let room = client.get_joined_room(&room_id); + assert!(room.is_some()); - // let room = client.get_joined_room(&room_id).await; - // assert!(room.is_some()); + // test store reloads with correct room state from JsonStore + // let store = Box::new(JsonStore::open(path).unwrap()); + // let config = ClientConfig::default().state_store(store); + // let joined_client = Client::new_with_config(homeserver, config).unwrap(); + // joined_client.restore_login(session).await.unwrap(); - // // test store reloads with correct room state from JsonStore - // let store = Box::new(JsonStore::open(path).unwrap()); - // let config = ClientConfig::default().state_store(store); - // let joined_client = Client::new_with_config(homeserver, config).unwrap(); - // joined_client.restore_login(session).await.unwrap(); + // // joined room reloaded from state store + // joined_client + // .sync_once(SyncSettings::default()) + // .await + // .unwrap(); + // let room = joined_client.get_joined_room(&room_id).await; + // assert!(room.is_some()); - // // joined room reloaded from state store - // joined_client - // .sync_once(SyncSettings::default()) - // .await - // .unwrap(); - // let room = joined_client.get_joined_room(&room_id).await; - // assert!(room.is_some()); + // let _m = mock( + // "GET", + // Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_string()), + // ) + // .with_status(200) + // .with_body(test_json::LEAVE_SYNC_EVENT.to_string()) + // .create(); - // let _m = mock( - // "GET", - // Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_string()), - // ) - // .with_status(200) - // .with_body(test_json::LEAVE_SYNC_EVENT.to_string()) - // .create(); + // joined_client + // .sync_once(SyncSettings::default()) + // .await + // .unwrap(); - // joined_client - // .sync_once(SyncSettings::default()) - // .await - // .unwrap(); + // let room = joined_client.get_joined_room(&room_id).await; + // assert!(room.is_none()); - // let room = joined_client.get_joined_room(&room_id).await; - // assert!(room.is_none()); - - // let room = joined_client.get_left_room(&room_id).await; - // assert!(room.is_some()); - // } + // let room = joined_client.get_left_room(&room_id).await; + // assert!(room.is_some()); + } #[tokio::test] async fn account_data() { diff --git a/matrix_sdk_base/src/client.rs b/matrix_sdk_base/src/client.rs index 35b4260e..1b07335c 100644 --- a/matrix_sdk_base/src/client.rs +++ b/matrix_sdk_base/src/client.rs @@ -58,14 +58,13 @@ use crate::{ error::Result, event_emitter::Emitter, responses::{ - AccountData, Ephemeral, InviteState, InvitedRoom as InvitedRoomResponse, - JoinedRoom as JoinedRoomResponse, LeftRoom as LeftRoomResponse, MemberEvent, Presence, - Rooms, State, StrippedMemberEvent, SyncResponse, Timeline, + AccountData, Ephemeral, InviteState, InvitedRoom, JoinedRoom, LeftRoom, MemberEvent, + Presence, Rooms, State, StrippedMemberEvent, SyncResponse, Timeline, }, rooms::{RoomInfo, RoomType, StrippedRoomInfo}, session::Session, store::{SledStore, StateChanges, Store}, - EventEmitter, JoinedRoom, RoomState, + EventEmitter, RoomState, }; pub type Token = String; @@ -497,6 +496,7 @@ impl BaseClient { timeline } + #[allow(clippy::type_complexity)] fn handle_invited_state( &self, events: Vec>, @@ -543,6 +543,7 @@ impl BaseClient { ) } + #[allow(clippy::type_complexity)] fn handle_state( &self, events: Vec>, @@ -722,13 +723,7 @@ impl BaseClient { rooms.join.insert( room_id, - JoinedRoomResponse::new( - timeline, - state, - account_data, - ephemeral, - notification_count, - ), + JoinedRoom::new(timeline, state, account_data, ephemeral, notification_count), ); changes.add_room(room_info); @@ -762,10 +757,9 @@ impl BaseClient { .handle_room_account_data(&room_id, &new_info.account_data.events, &mut changes) .await; - rooms.leave.insert( - room_id, - LeftRoomResponse::new(timeline, state, account_data), - ); + rooms + .leave + .insert(room_id, LeftRoom::new(timeline, state, account_data)); } for (room_id, new_info) in response.rooms.invite { @@ -788,7 +782,7 @@ impl BaseClient { changes.stripped_members.insert(room_id.clone(), members); changes.stripped_state.insert(room_id.clone(), state_events); - let room = InvitedRoomResponse { + let room = InvitedRoom { invite_state: state, }; @@ -1029,10 +1023,6 @@ impl BaseClient { } } - pub fn get_joined_room(&self, room_id: &RoomId) -> Option { - self.store.get_joined_room(room_id) - } - pub fn get_room(&self, room_id: &RoomId) -> Option { self.store.get_room(room_id) } diff --git a/matrix_sdk_base/src/rooms/mod.rs b/matrix_sdk_base/src/rooms/mod.rs index 1d847eac..4bddea02 100644 --- a/matrix_sdk_base/src/rooms/mod.rs +++ b/matrix_sdk_base/src/rooms/mod.rs @@ -42,6 +42,22 @@ impl RoomState { } } + pub fn invited(self) -> Option { + if let RoomState::Invited(r) = self { + Some(r) + } else { + None + } + } + + pub fn left(self) -> Option { + if let RoomState::Left(r) = self { + Some(r) + } else { + None + } + } + pub fn is_encrypted(&self) -> bool { match self { RoomState::Joined(r) => r.inner.is_encrypted(), diff --git a/matrix_sdk_base/src/store.rs b/matrix_sdk_base/src/store.rs index 1518457f..d5e3d4c0 100644 --- a/matrix_sdk_base/src/store.rs +++ b/matrix_sdk_base/src/store.rs @@ -45,11 +45,19 @@ impl Store { self.rooms.get(room_id).map(|r| r.clone()) } - pub(crate) fn get_joined_room(&self, room_id: &RoomId) -> Option { + pub fn get_joined_room(&self, room_id: &RoomId) -> Option { self.get_room(room_id).map(|r| r.joined()).flatten() } - pub(crate) fn get_room(&self, room_id: &RoomId) -> Option { + pub fn get_invited_room(&self, room_id: &RoomId) -> Option { + self.get_room(room_id).map(|r| r.invited()).flatten() + } + + pub fn get_left_room(&self, room_id: &RoomId) -> Option { + self.get_room(room_id).map(|r| r.left()).flatten() + } + + pub fn get_room(&self, room_id: &RoomId) -> Option { self.get_bare_room(room_id) .map(|r| match r.room_type() { RoomType::Joined => Some(RoomState::Joined(JoinedRoom { inner: r })),