diff --git a/examples/command_bot.rs b/examples/command_bot.rs index 2b27bfe3..a238dac5 100644 --- a/examples/command_bot.rs +++ b/examples/command_bot.rs @@ -63,6 +63,7 @@ async fn login_and_sync( username: String, password: String, ) -> Result<(), matrix_sdk::Error> { + // the location for `JsonStore` to save files to let mut home = dirs::home_dir().expect("no home directory found"); home.push("party_bot"); @@ -87,7 +88,9 @@ async fn login_and_sync( println!("logged in as {}", username); - // initial sync to set up state and so our bot doesn't respond to old messages + // An initial sync to set up state and so our bot doesn't respond to old messages. + // If the `StateStore` finds saved state in the location given the initial sync will + // be skipped in favor of loading state from the store client.sync(SyncSettings::default()).await.unwrap(); // add our CommandBot to be notified of incoming messages, we do this after the initial // sync to avoid responding to messages before the bot was running. diff --git a/src/state/state_store.rs b/src/state/state_store.rs index 87bb474c..a30dfaa2 100644 --- a/src/state/state_store.rs +++ b/src/state/state_store.rs @@ -30,7 +30,7 @@ impl JsonStore { pub fn open>(path: P) -> Result { let p = path.as_ref(); if !p.exists() { - std::fs::create_dir_all(p)?; + fs::create_dir_all(p)?; } Ok(Self { path: Arc::new(RwLock::new(p.to_path_buf())), @@ -83,10 +83,7 @@ impl StateStore for JsonStore { if !self.user_path_set.load(Ordering::SeqCst) { if let Some(user) = &state.user_id { self.user_path_set.swap(true, Ordering::SeqCst); - self.path - .write() - .await - .push(format!("{}", user.localpart())) + self.path.write().await.push(user.localpart()) } } let mut path = self.path.read().await.clone(); @@ -95,7 +92,7 @@ impl StateStore for JsonStore { if !Path::new(&path).exists() { let mut dir = path.clone(); dir.pop(); - std::fs::create_dir_all(dir)?; + async_fs::create_dir_all(dir).await?; } let json = serde_json::to_string(&state).map_err(Error::from)?; @@ -122,7 +119,7 @@ impl StateStore for JsonStore { if !Path::new(&path).exists() { let mut dir = path.clone(); dir.pop(); - std::fs::create_dir_all(dir)?; + async_fs::create_dir_all(dir).await?; } let json = serde_json::to_string(&room).map_err(Error::from)?; diff --git a/src/test_builder.rs b/src/test_builder.rs index 6149f4f6..db0f616a 100644 --- a/src/test_builder.rs +++ b/src/test_builder.rs @@ -13,7 +13,7 @@ use crate::events::{ EventJson, TryFromRaw, }; use crate::identifiers::{RoomId, UserId}; -use crate::AsyncClient; +use crate::{AsyncClient, Error, SyncSettings}; use mockito::{self, mock, Matcher, Mock}; @@ -399,11 +399,11 @@ impl MockTestRunner { self } - pub async fn to_client(&mut self) -> Result<&mut AsyncClient, crate::Error> { + pub async fn to_client(&mut self) -> Result<&mut AsyncClient, Error> { self.client .as_mut() .unwrap() - .sync(crate::SyncSettings::default()) + .sync(SyncSettings::default()) .await?; Ok(self.client.as_mut().unwrap())