From 303ac513e506bece2aa1171fce2a5a53a3b8f9dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Thu, 21 Jan 2021 12:13:46 +0100 Subject: [PATCH] base: Remove some stale files from the old state store --- matrix_sdk_base/src/state/json_store.rs | 217 ------------------------ matrix_sdk_base/src/state/mod.rs | 208 ----------------------- 2 files changed, 425 deletions(-) delete mode 100644 matrix_sdk_base/src/state/json_store.rs delete mode 100644 matrix_sdk_base/src/state/mod.rs diff --git a/matrix_sdk_base/src/state/json_store.rs b/matrix_sdk_base/src/state/json_store.rs deleted file mode 100644 index 2e4b43b1..00000000 --- a/matrix_sdk_base/src/state/json_store.rs +++ /dev/null @@ -1,217 +0,0 @@ -use std::{ - collections::HashMap, - fmt, fs, - path::{Path, PathBuf}, - sync::{ - atomic::{AtomicBool, Ordering}, - Arc, - }, -}; - -use matrix_sdk_common::{async_trait, identifiers::RoomId, locks::RwLock}; -use tokio::{fs as async_fs, io::AsyncWriteExt}; - -use super::{AllRooms, ClientState, StateStore}; -use crate::{Error, Result, Room, RoomState, Session}; - -/// A default `StateStore` implementation that serializes state as json -/// and saves it to disk. -/// -/// When logged in the `JsonStore` appends the user_id to its folder path, -/// so all files are saved in `my_client/user_id_localpart/*`. -pub struct JsonStore { - path: Arc>, - user_path_set: AtomicBool, -} - -impl JsonStore { - /// Create a `JsonStore` to store the client and room state. - /// - /// Checks if the provided path exists and creates the directories if not. - pub fn open>(path: P) -> Result { - let p = path.as_ref(); - if !p.exists() { - fs::create_dir_all(p)?; - } - Ok(Self { - path: Arc::new(RwLock::new(p.to_path_buf())), - user_path_set: AtomicBool::new(false), - }) - } - - /// Build a path for a file where the Room state to be stored in. - async fn build_room_path(&self, room_state: &str, room_id: &RoomId) -> PathBuf { - let mut path = self.path.read().await.clone(); - - path.push("rooms"); - path.push(room_state); - path.push(JsonStore::sanitize_room_id(room_id)); - path.set_extension("json"); - - path - } - - /// Build a path for the file where the Client state to be stored in. - async fn build_client_path(&self) -> PathBuf { - let mut path = self.path.read().await.clone(); - path.push("client"); - path.set_extension("json"); - - path - } - - /// Replace common characters that can't be used in a file name with an - /// underscore. - fn sanitize_room_id(room_id: &RoomId) -> String { - room_id.as_str().replace( - &['.', ':', '<', '>', '"', '/', '\\', '|', '?', '*'][..], - "_", - ) - } -} - -impl fmt::Debug for JsonStore { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("JsonStore") - .field("path", &self.path) - .finish() - } -} - -#[async_trait] -impl StateStore for JsonStore { - async fn load_client_state(&self, sess: &Session) -> Result> { - if !self.user_path_set.load(Ordering::SeqCst) { - self.user_path_set.swap(true, Ordering::SeqCst); - self.path.write().await.push(sess.user_id.localpart()) - } - - let path = self.build_client_path().await; - - let json = async_fs::read_to_string(path) - .await - .map_or(String::default(), |s| s); - if json.is_empty() { - Ok(None) - } else { - serde_json::from_str(&json).map(Some).map_err(Error::from) - } - } - - async fn load_all_rooms(&self) -> Result { - let mut path = self.path.read().await.clone(); - path.push("rooms"); - - let mut joined = HashMap::new(); - let mut left = HashMap::new(); - let mut invited = HashMap::new(); - for room_state_type in &["joined", "invited", "left"] { - path.push(room_state_type); - // don't load rooms that aren't saved yet - if !path.exists() { - path.pop(); - continue; - } - - for file in fs::read_dir(&path)? { - let file = file?.path(); - - if file.is_dir() { - continue; - } - - let json = async_fs::read_to_string(&file).await?; - let room = serde_json::from_str::(&json).map_err(Error::from)?; - let room_id = room.room_id.clone(); - - match *room_state_type { - "joined" => joined.insert(room_id, room), - "invited" => invited.insert(room_id, room), - "left" => left.insert(room_id, room), - _ => unreachable!("an array with 3 const elements was altered in JsonStore"), - }; - } - path.pop(); - } - - Ok(AllRooms { - joined, - left, - invited, - }) - } - - async fn store_client_state(&self, state: ClientState) -> Result<()> { - let path = self.build_client_path().await; - - if !path.exists() { - let mut dir = path.clone(); - dir.pop(); - async_fs::create_dir_all(dir).await?; - } - - let json = serde_json::to_string(&state).map_err(Error::from)?; - - let mut file = async_fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open(path) - .await?; - file.write_all(json.as_bytes()).await.map_err(Error::from) - } - - async fn store_room_state(&self, room: RoomState<&Room>) -> Result<()> { - let (room, room_state) = match room { - RoomState::Joined(room) => (room, "joined"), - RoomState::Invited(room) => (room, "invited"), - RoomState::Left(room) => (room, "left"), - }; - - if !self.user_path_set.load(Ordering::SeqCst) { - self.user_path_set.swap(true, Ordering::SeqCst); - self.path.write().await.push(room.own_user_id.localpart()) - } - - let path = self.build_room_path(room_state, &room.room_id).await; - - if !path.exists() { - let mut dir = path.clone(); - dir.pop(); - async_fs::create_dir_all(dir).await?; - } - - let json = serde_json::to_string(&room).map_err(Error::from)?; - - let mut file = async_fs::OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open(path) - .await?; - file.write_all(json.as_bytes()).await.map_err(Error::from) - } - - async fn delete_room_state(&self, room: RoomState<&RoomId>) -> Result<()> { - let (room_id, room_state) = match &room { - RoomState::Joined(id) => (id, "joined"), - RoomState::Invited(id) => (id, "invited"), - RoomState::Left(id) => (id, "left"), - }; - - if !self.user_path_set.load(Ordering::SeqCst) { - return Err(Error::StateStore("path for JsonStore not set".into())); - } - - let path = self.build_room_path(room_state, room_id).await; - - if !path.exists() { - return Err(Error::StateStore(format!("file {:?} not found", path))); - } - - tokio::fs::remove_file(path).await.map_err(Error::from) - } -} - -#[cfg(test)] -mod test {} diff --git a/matrix_sdk_base/src/state/mod.rs b/matrix_sdk_base/src/state/mod.rs deleted file mode 100644 index 96ddcb8a..00000000 --- a/matrix_sdk_base/src/state/mod.rs +++ /dev/null @@ -1,208 +0,0 @@ -// Copyright 2020 Devin Ragotzy -// Copyright 2020 The Matrix.org Foundation C.I.C. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -use std::collections::HashMap; - -use matrix_sdk_common::{ - async_trait, - identifiers::{RoomId, UserId}, - push::Ruleset, - AsyncTraitDeps, -}; -use serde::{Deserialize, Serialize}; - -#[cfg(not(target_arch = "wasm32"))] -mod json_store; -#[cfg(not(target_arch = "wasm32"))] -pub use json_store::JsonStore; - -use crate::{ - client::{BaseClient, Token}, - Result, Room, RoomState, Session, -}; - -/// `ClientState` holds all the information to restore a `BaseClient` -/// except the `access_token` as the default store is not secure. -/// -/// When implementing `StateStore` for something other than the filesystem -/// implement `From for YourDbType` this allows for easy conversion -/// when needed in `StateStore::load/store_client_state` -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct ClientState { - /// The current sync token that should be used for the next sync call. - pub sync_token: Option, - /// A list of ignored users. - pub ignored_users: Vec, - /// The push ruleset for the logged in user. - pub push_ruleset: Option, -} - -impl PartialEq for ClientState { - fn eq(&self, other: &Self) -> bool { - self.sync_token == other.sync_token && self.ignored_users == other.ignored_users - } -} - -/// `JsonStore::load_all_rooms` returns `AllRooms`. -/// -/// `AllRooms` is made of the `joined`, `invited` and `left` room maps. -#[derive(Debug)] -pub struct AllRooms { - /// The joined room mapping of `RoomId` to `Room`. - pub joined: HashMap, - /// The invited room mapping of `RoomId` to `Room`. - pub invited: HashMap, - /// The left room mapping of `RoomId` to `Room`. - pub left: HashMap, -} - -/// Abstraction around the data store to avoid unnecessary request on client initialization. -#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] -#[cfg_attr(not(target_arch = "wasm32"), async_trait)] -pub trait StateStore: AsyncTraitDeps { - /// Loads the state of `BaseClient` through `ClientState` type. - /// - /// An `Option::None` should be returned only if the `StateStore` tries to - /// load but no state has been stored. - async fn load_client_state(&self, _: &Session) -> Result>; - - /// 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. - async fn load_all_rooms(&self) -> Result; - - /// Save the current state of the `BaseClient` using the `StateStore::Store` type. - async fn store_client_state(&self, _: ClientState) -> Result<()>; - - /// Save the state a single `Room`. - async fn store_room_state(&self, _: RoomState<&Room>) -> Result<()>; - - /// Remove state for a room. - /// - /// This is used when a user leaves a room or rejects an invitation. - async fn delete_room_state(&self, _room: RoomState<&RoomId>) -> Result<()>; -} - -#[cfg(test)] -mod test { - use super::*; - - use std::collections::HashMap; - - use crate::identifiers::{room_id, user_id}; - - #[test] - fn serialize() { - let id = room_id!("!roomid:example.com"); - let user = user_id!("@example:example.com"); - - let room = Room::new(&id, &user); - - let state = ClientState { - sync_token: Some("hello".into()), - ignored_users: vec![user], - push_ruleset: None, - }; - assert_eq!( - r#"{"sync_token":"hello","ignored_users":["@example:example.com"],"push_ruleset":null}"#, - serde_json::to_string(&state).unwrap() - ); - - let mut joined_rooms = HashMap::new(); - joined_rooms.insert(id, room); - - #[cfg(not(feature = "messages"))] - assert_eq!( - serde_json::json!({ - "!roomid:example.com": { - "room_id": "!roomid:example.com", - "room_name": { - "name": null, - "canonical_alias": null, - "aliases": [], - "heroes": [], - "joined_member_count": null, - "invited_member_count": null - }, - "own_user_id": "@example:example.com", - "creator": null, - "direct_target": null, - "joined_members": {}, - "invited_members": {}, - "typing_users": [], - "power_levels": null, - "encrypted": null, - "unread_highlight": null, - "unread_notifications": null, - "tombstone": null - } - }), - serde_json::to_value(&joined_rooms).unwrap() - ); - - #[cfg(feature = "messages")] - assert_eq!( - serde_json::json!({ - "!roomid:example.com": { - "room_id": "!roomid:example.com", - "room_name": { - "name": null, - "canonical_alias": null, - "aliases": [], - "heroes": [], - "joined_member_count": null, - "invited_member_count": null - }, - "own_user_id": "@example:example.com", - "creator": null, - "direct_target": null, - "joined_members": {}, - "invited_members": {}, - "messages": [], - "typing_users": [], - "power_levels": null, - "encrypted": null, - "unread_highlight": null, - "unread_notifications": null, - "tombstone": null - } - }), - serde_json::to_value(&joined_rooms).unwrap() - ); - } - - #[test] - fn deserialize() { - let id = room_id!("!roomid:example.com"); - let user = user_id!("@example:example.com"); - - let room = Room::new(&id, &user); - - let state = ClientState { - sync_token: Some("hello".into()), - ignored_users: vec![user], - push_ruleset: None, - }; - let json = serde_json::to_string(&state).unwrap(); - - assert_eq!(state, serde_json::from_str(&json).unwrap()); - - let mut joined_rooms = HashMap::new(); - joined_rooms.insert(id, room); - let json = serde_json::to_string(&joined_rooms).unwrap(); - - assert_eq!(joined_rooms, serde_json::from_str(&json).unwrap()); - } -}