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 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<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.
/////
///// # Arguments
/////
///// `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>>> {
// 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<InvitedRoom> {
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<Arc<RwLock<Room>>> {
// 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<LeftRoom> {
self.store().get_left_room(room_id)
}
/// Login to the server.
///
@ -2298,46 +2299,41 @@ 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
// 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();
@ -2369,7 +2365,7 @@ mod test {
// let room = joined_client.get_left_room(&room_id).await;
// assert!(room.is_some());
// }
}
#[tokio::test]
async fn account_data() {

View File

@ -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<Raw<AnyStrippedStateEvent>>,
@ -543,6 +543,7 @@ impl BaseClient {
)
}
#[allow(clippy::type_complexity)]
fn handle_state(
&self,
events: Vec<Raw<AnySyncStateEvent>>,
@ -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<JoinedRoom> {
self.store.get_joined_room(room_id)
}
pub fn get_room(&self, room_id: &RoomId) -> Option<RoomState> {
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 {
match self {
RoomState::Joined(r) => r.inner.is_encrypted(),

View File

@ -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<JoinedRoom> {
pub fn get_joined_room(&self, room_id: &RoomId) -> Option<JoinedRoom> {
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)
.map(|r| match r.room_type() {
RoomType::Joined => Some(RoomState::Joined(JoinedRoom { inner: r })),