state_store: add load_all_rooms, fix clippy warnings

This commit is contained in:
Devin R 2020-04-19 15:25:25 -04:00
parent 9fb4bd9d19
commit 8f89e8335c
3 changed files with 57 additions and 7 deletions

View file

@ -257,8 +257,8 @@ impl Room {
invited_member_count,
} = summary;
self.room_name.heroes = heroes.clone();
self.room_name.invited_member_count = invited_member_count.clone();
self.room_name.joined_member_count = joined_member_count.clone();
self.room_name.invited_member_count = *invited_member_count;
self.room_name.joined_member_count = *joined_member_count;
}
/// Handle a room.member updating the room state if necessary.
@ -453,7 +453,7 @@ mod test {
.await;
assert_eq!(2, room.members.len());
for (_id, member) in &room.members {
for member in room.members.values() {
assert_eq!(MembershipState::Join, member.membership);
}

View file

@ -13,6 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use std::collections::HashMap;
use std::path::Path;
pub mod state_store;
@ -50,13 +51,17 @@ pub trait StateStore: Send + Sync {
/// Set up connections or open files to load/save state.
fn open(&self, path: &Path) -> Result<(), Self::IoError>;
///
/// Loads the state of `BaseClient` through `StateStore::Store` type.
fn load_client_state(&self) -> Result<Self::Store, Self::IoError>;
///
/// Load the state of a single `Room` by `RoomId`.
fn load_room_state(&self, room_id: &RoomId) -> Result<Room, Self::IoError>;
/// Load the state of all `Room`s.
///
/// This will be mapped over in the client in order to store `Room`s in an async safe way.
fn load_all_rooms(&self) -> Result<HashMap<RoomId, Room>, Self::IoError>;
/// Save the current state of the `BaseClient` using the `StateStore::Store` type.
fn store_client_state(&self, _: Self::Store) -> Result<(), Self::IoError>;
///
/// Save the state a single `Room`.
fn store_room_state(&self, _: &Room) -> Result<(), Self::IoError>;
}

View file

@ -1,4 +1,5 @@
use std::fs::OpenOptions;
use std::collections::HashMap;
use std::fs::{self, OpenOptions};
use std::io::{BufReader, BufWriter, Write};
use std::path::Path;
@ -42,6 +43,33 @@ impl StateStore for JsonStore {
}
}
fn load_all_rooms(&self) -> Result<HashMap<RoomId, Room>> {
if let Some(mut path) = dirs::home_dir() {
path.push(".matrix_store/rooms/");
let mut rooms_map = HashMap::new();
for file in fs::read_dir(&path)? {
let file = file?.path();
if file.is_dir() {
continue;
}
let f_hdl = OpenOptions::new().read(true).open(&file)?;
let reader = BufReader::new(f_hdl);
let room = serde_json::from_reader::<_, Room>(reader).map_err(Error::from)?;
let room_id = room.room_id.clone();
rooms_map.insert(room_id, room);
}
Ok(rooms_map)
} else {
todo!("Error maybe")
}
}
fn store_client_state(&self, state: ClientState) -> Result<()> {
if let Some(mut path) = dirs::home_dir() {
path.push(".matrix_store/client.json");
@ -146,4 +174,21 @@ mod test {
fn store_room_state() {
run_and_cleanup(test_store_room_state);
}
fn test_load_rooms() {
let store = JsonStore;
let id = RoomId::try_from("!roomid:example.com").unwrap();
let user = UserId::try_from("@example:example.com").unwrap();
let room = Room::new(&id, &user);
store.store_room_state(&room).unwrap();
let loaded = store.load_all_rooms().unwrap();
println!("{:?}", loaded);
}
#[test]
fn load_rooms() {
run_and_cleanup(test_load_rooms);
}
}