client: Restore the membership based get room methods.

master
Damir Jelić 2020-12-22 10:47:21 +01:00
parent 1bfb2d08a6
commit 1313c3da3c
4 changed files with 108 additions and 98 deletions

View File

@ -41,7 +41,8 @@ use tracing::{debug, warn};
use tracing::{error, info, instrument}; use tracing::{error, info, instrument};
use matrix_sdk_base::{ 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")] #[cfg(feature = "encryption")]
@ -569,26 +570,26 @@ impl Client {
/// ///
/// `room_id` - The unique id of the room that should be fetched. /// `room_id` - The unique id of the room that should be fetched.
pub fn get_joined_room(&self, room_id: &RoomId) -> Option<JoinedRoom> { pub fn get_joined_room(&self, room_id: &RoomId) -> Option<JoinedRoom> {
self.base_client.get_joined_room(room_id) self.store().get_joined_room(room_id)
} }
///// Get an invited room with the given room id. /// Get an invited room with the given room id.
///// ///
///// # Arguments /// # Arguments
///// ///
///// `room_id` - The unique id of the room that should be fetched. /// `room_id` - The unique id of the room that should be fetched.
//pub async fn get_invited_room(&self, room_id: &RoomId) -> Option<Arc<RwLock<Room>>> { pub fn get_invited_room(&self, room_id: &RoomId) -> Option<InvitedRoom> {
// self.base_client.get_invited_room(room_id).await self.store().get_invited_room(room_id)
//} }
///// Get a left room with the given room id. /// Get a left room with the given room id.
///// ///
///// # Arguments /// # Arguments
///// ///
///// `room_id` - The unique id of the room that should be fetched. /// `room_id` - The unique id of the room that should be fetched.
//pub async fn get_left_room(&self, room_id: &RoomId) -> Option<Arc<RwLock<Room>>> { pub fn get_left_room(&self, room_id: &RoomId) -> Option<LeftRoom> {
// self.base_client.get_left_room(room_id).await self.store().get_left_room(room_id)
//} }
/// Login to the server. /// Login to the server.
/// ///
@ -2298,46 +2299,41 @@ mod test {
assert!(client.devices().await.is_ok()); assert!(client.devices().await.is_ok());
} }
// #[tokio::test] #[tokio::test]
// async fn test_join_leave_room() { async fn test_join_leave_room() {
// let homeserver = Url::from_str(&mockito::server_url()).unwrap(); 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 { let session = Session {
// access_token: "1234".to_owned(), access_token: "1234".to_owned(),
// user_id: user_id!("@example:localhost"), user_id: user_id!("@example:localhost"),
// device_id: "DEVICEID".into(), device_id: "DEVICEID".into(),
// }; };
// let _m = mock( let _m = mock(
// "GET", "GET",
// Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_string()), Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_string()),
// ) )
// .with_status(200) .with_status(200)
// .with_body(test_json::SYNC.to_string()) .with_body(test_json::SYNC.to_string())
// .create(); .create();
// let dir = tempdir().unwrap(); let client = Client::new(homeserver.clone()).unwrap();
// let path: &Path = dir.path(); client.restore_login(session.clone()).await.unwrap();
// let store = Box::new(JsonStore::open(path).unwrap());
// let config = ClientConfig::default().state_store(store); let room = client.get_joined_room(&room_id);
// let client = Client::new_with_config(homeserver.clone(), config).unwrap(); assert!(room.is_none());
// client.restore_login(session.clone()).await.unwrap();
// let room = client.get_joined_room(&room_id).await; client.sync_once(SyncSettings::default()).await.unwrap();
// assert!(room.is_none());
// 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; let room = client.get_joined_room(&room_id);
// assert!(room.is_none()); assert!(room.is_some());
// let room = client.get_joined_room(&room_id).await; // test store reloads with correct room state from JsonStore
// assert!(room.is_some());
// // test store reloads with correct room state from JsonStore
// let store = Box::new(JsonStore::open(path).unwrap()); // let store = Box::new(JsonStore::open(path).unwrap());
// let config = ClientConfig::default().state_store(store); // let config = ClientConfig::default().state_store(store);
// let joined_client = Client::new_with_config(homeserver, config).unwrap(); // let joined_client = Client::new_with_config(homeserver, config).unwrap();
@ -2369,7 +2365,7 @@ mod test {
// let room = joined_client.get_left_room(&room_id).await; // let room = joined_client.get_left_room(&room_id).await;
// assert!(room.is_some()); // assert!(room.is_some());
// } }
#[tokio::test] #[tokio::test]
async fn account_data() { async fn account_data() {

View File

@ -58,14 +58,13 @@ use crate::{
error::Result, error::Result,
event_emitter::Emitter, event_emitter::Emitter,
responses::{ responses::{
AccountData, Ephemeral, InviteState, InvitedRoom as InvitedRoomResponse, AccountData, Ephemeral, InviteState, InvitedRoom, JoinedRoom, LeftRoom, MemberEvent,
JoinedRoom as JoinedRoomResponse, LeftRoom as LeftRoomResponse, MemberEvent, Presence, Presence, Rooms, State, StrippedMemberEvent, SyncResponse, Timeline,
Rooms, State, StrippedMemberEvent, SyncResponse, Timeline,
}, },
rooms::{RoomInfo, RoomType, StrippedRoomInfo}, rooms::{RoomInfo, RoomType, StrippedRoomInfo},
session::Session, session::Session,
store::{SledStore, StateChanges, Store}, store::{SledStore, StateChanges, Store},
EventEmitter, JoinedRoom, RoomState, EventEmitter, RoomState,
}; };
pub type Token = String; pub type Token = String;
@ -497,6 +496,7 @@ impl BaseClient {
timeline timeline
} }
#[allow(clippy::type_complexity)]
fn handle_invited_state( fn handle_invited_state(
&self, &self,
events: Vec<Raw<AnyStrippedStateEvent>>, events: Vec<Raw<AnyStrippedStateEvent>>,
@ -543,6 +543,7 @@ impl BaseClient {
) )
} }
#[allow(clippy::type_complexity)]
fn handle_state( fn handle_state(
&self, &self,
events: Vec<Raw<AnySyncStateEvent>>, events: Vec<Raw<AnySyncStateEvent>>,
@ -722,13 +723,7 @@ impl BaseClient {
rooms.join.insert( rooms.join.insert(
room_id, room_id,
JoinedRoomResponse::new( JoinedRoom::new(timeline, state, account_data, ephemeral, notification_count),
timeline,
state,
account_data,
ephemeral,
notification_count,
),
); );
changes.add_room(room_info); changes.add_room(room_info);
@ -762,10 +757,9 @@ impl BaseClient {
.handle_room_account_data(&room_id, &new_info.account_data.events, &mut changes) .handle_room_account_data(&room_id, &new_info.account_data.events, &mut changes)
.await; .await;
rooms.leave.insert( rooms
room_id, .leave
LeftRoomResponse::new(timeline, state, account_data), .insert(room_id, LeftRoom::new(timeline, state, account_data));
);
} }
for (room_id, new_info) in response.rooms.invite { 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_members.insert(room_id.clone(), members);
changes.stripped_state.insert(room_id.clone(), state_events); changes.stripped_state.insert(room_id.clone(), state_events);
let room = InvitedRoomResponse { let room = InvitedRoom {
invite_state: state, invite_state: state,
}; };
@ -1029,10 +1023,6 @@ impl BaseClient {
} }
} }
pub fn get_joined_room(&self, room_id: &RoomId) -> Option<JoinedRoom> {
self.store.get_joined_room(room_id)
}
pub fn get_room(&self, room_id: &RoomId) -> Option<RoomState> { pub fn get_room(&self, room_id: &RoomId) -> Option<RoomState> {
self.store.get_room(room_id) self.store.get_room(room_id)
} }

View File

@ -42,6 +42,22 @@ impl RoomState {
} }
} }
pub fn invited(self) -> Option<InvitedRoom> {
if let RoomState::Invited(r) = self {
Some(r)
} else {
None
}
}
pub fn left(self) -> Option<LeftRoom> {
if let RoomState::Left(r) = self {
Some(r)
} else {
None
}
}
pub fn is_encrypted(&self) -> bool { pub fn is_encrypted(&self) -> bool {
match self { match self {
RoomState::Joined(r) => r.inner.is_encrypted(), RoomState::Joined(r) => r.inner.is_encrypted(),

View File

@ -45,11 +45,19 @@ impl Store {
self.rooms.get(room_id).map(|r| r.clone()) self.rooms.get(room_id).map(|r| r.clone())
} }
pub(crate) fn get_joined_room(&self, room_id: &RoomId) -> Option<JoinedRoom> { pub fn get_joined_room(&self, room_id: &RoomId) -> Option<JoinedRoom> {
self.get_room(room_id).map(|r| r.joined()).flatten() self.get_room(room_id).map(|r| r.joined()).flatten()
} }
pub(crate) fn get_room(&self, room_id: &RoomId) -> Option<RoomState> { pub fn get_invited_room(&self, room_id: &RoomId) -> Option<InvitedRoom> {
self.get_room(room_id).map(|r| r.invited()).flatten()
}
pub fn get_left_room(&self, room_id: &RoomId) -> Option<LeftRoom> {
self.get_room(room_id).map(|r| r.left()).flatten()
}
pub fn get_room(&self, room_id: &RoomId) -> Option<RoomState> {
self.get_bare_room(room_id) self.get_bare_room(room_id)
.map(|r| match r.room_type() { .map(|r| match r.room_type() {
RoomType::Joined => Some(RoomState::Joined(JoinedRoom { inner: r })), RoomType::Joined => Some(RoomState::Joined(JoinedRoom { inner: r })),