From 2e9c292bb989ffff2c99aa2a6062962c913b3586 Mon Sep 17 00:00:00 2001 From: Vika Date: Tue, 9 Jul 2024 22:43:21 +0300 Subject: database: use Url to represent user authorities This makes the interface more consistent and resistant to misuse. --- src/database/file/mod.rs | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) (limited to 'src/database/file') diff --git a/src/database/file/mod.rs b/src/database/file/mod.rs index 46660ab..f6715e1 100644 --- a/src/database/file/mod.rs +++ b/src/database/file/mod.rs @@ -210,7 +210,8 @@ impl FileStorage { async fn hydrate_author( feed: &mut serde_json::Value, - user: &'_ Option, + // Unused? + user: Option<&url::Url>, storage: &S, ) { let url = feed["properties"]["uid"][0] @@ -226,6 +227,7 @@ async fn hydrate_author( let author_list: Vec = stream::iter(author.iter()) .then(|i| async move { if let Some(i) = i.as_str() { + // BUG: Use `user` to sanitize? match storage.get_post(i).await { Ok(post) => match post { Some(post) => post, @@ -319,7 +321,7 @@ impl Storage for FileStorage { } #[tracing::instrument(skip(self))] - async fn put_post(&self, post: &'_ serde_json::Value, user: &'_ str) -> Result<()> { + async fn put_post(&self, post: &'_ serde_json::Value, user: &url::Url) -> Result<()> { let key = post["properties"]["uid"][0] .as_str() .expect("Tried to save a post without UID"); @@ -358,7 +360,7 @@ impl Storage for FileStorage { .unwrap_or_default() ) }; - if url != key && url_domain == user { + if url != key && url_domain == user.authority() { let link = url_to_path(&self.root_dir, url); debug!("Creating a symlink at {:?}", link); let orig = path.clone(); @@ -386,7 +388,7 @@ impl Storage for FileStorage { // Add the h-feed to the channel list let path = { let mut path = relative_path::RelativePathBuf::new(); - path.push(user); + path.push(user.authority()); path.push("channels"); path.to_path(&self.root_dir) @@ -487,9 +489,9 @@ impl Storage for FileStorage { } #[tracing::instrument(skip(self))] - async fn get_channels(&self, user: &'_ str) -> Result> { + async fn get_channels(&self, user: &url::Url) -> Result> { let mut path = relative_path::RelativePathBuf::new(); - path.push(user); + path.push(user.authority()); path.push("channels"); let path = path.to_path(&self.root_dir); @@ -521,13 +523,13 @@ impl Storage for FileStorage { url: &'_ str, cursor: Option<&'_ str>, limit: usize, - user: Option<&'_ str> + user: Option<&url::Url> ) -> Result)>> { Ok(self.read_feed_with_limit( url, - &cursor.map(|v| v.to_owned()), + cursor, limit, - &user.map(|v| v.to_owned()) + user ).await? .map(|feed| { tracing::debug!("Feed: {:#}", serde_json::Value::Array( @@ -555,9 +557,9 @@ impl Storage for FileStorage { async fn read_feed_with_limit( &self, url: &'_ str, - after: &'_ Option, + after: Option<&str>, limit: usize, - user: &'_ Option, + user: Option<&url::Url>, ) -> Result> { if let Some(mut feed) = self.get_post(url).await? { if feed["children"].is_array() { @@ -627,10 +629,10 @@ impl Storage for FileStorage { } #[tracing::instrument(skip(self))] - async fn get_setting, 'a>(&self, user: &'_ str) -> Result { + async fn get_setting, 'a>(&self, user: &url::Url) -> Result { debug!("User for getting settings: {}", user); let mut path = relative_path::RelativePathBuf::new(); - path.push(user); + path.push(user.authority()); path.push("settings"); let path = path.to_path(&self.root_dir); @@ -648,9 +650,9 @@ impl Storage for FileStorage { } #[tracing::instrument(skip(self))] - async fn set_setting + 'a, 'a>(&self, user: &'a str, value: S::Data) -> Result<()> { + async fn set_setting + 'a, 'a>(&self, user: &'a url::Url, value: S::Data) -> Result<()> { let mut path = relative_path::RelativePathBuf::new(); - path.push(user); + path.push(user.authority()); path.push("settings"); let path = path.to_path(&self.root_dir); -- cgit 1.4.1