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/mod.rs | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'src/database/mod.rs') diff --git a/src/database/mod.rs b/src/database/mod.rs index a6a3b46..f48b4a9 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -227,7 +227,7 @@ pub trait Storage: std::fmt::Debug + Clone + Send + Sync { /// Save a post to the database as an MF2-JSON structure. /// /// Note that the `post` object MUST have `post["properties"]["uid"][0]` defined. - 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<()>; /// Add post to feed. Some database implementations might have optimized ways to do this. #[tracing::instrument(skip(self))] @@ -258,7 +258,7 @@ pub trait Storage: std::fmt::Debug + Clone + Send + Sync { /// Get a list of channels available for the user represented by /// the `user` domain to write to. - async fn get_channels(&self, user: &'_ str) -> Result>; + async fn get_channels(&self, user: &url::Url) -> Result>; /// Fetch a feed at `url` and return an h-feed object containing /// `limit` posts after a post by url `after`, filtering the content @@ -279,9 +279,9 @@ pub trait Storage: std::fmt::Debug + Clone + Send + Sync { async fn read_feed_with_limit( &self, url: &'_ str, - after: &'_ Option, + after: Option<&str>, limit: usize, - user: &'_ Option, + user: Option<&url::Url>, ) -> Result>; /// Fetch a feed at `url` and return an h-feed object containing @@ -307,17 +307,17 @@ pub trait Storage: std::fmt::Debug + Clone + Send + Sync { url: &'_ str, cursor: Option<&'_ str>, limit: usize, - user: Option<&'_ str> + user: Option<&url::Url> ) -> Result)>>; /// Deletes a post from the database irreversibly. Must be idempotent. async fn delete_post(&self, url: &'_ str) -> Result<()>; /// Gets a setting from the setting store and passes the result. - async fn get_setting, 'a>(&'_ self, user: &'_ str) -> Result; + async fn get_setting, 'a>(&'_ self, user: &url::Url) -> Result; /// Commits a setting to the setting store. - 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<()>; /// Add (or update) a webmention on a certian post. /// @@ -359,7 +359,7 @@ mod tests { // Reading and writing backend - .put_post(&post, "fireburn.ru") + .put_post(&post, &"https://fireburn.ru/".parse().unwrap()) .await .unwrap(); if let Some(returned_post) = backend.get_post(&key).await.unwrap() { @@ -423,7 +423,7 @@ mod tests { // Reading and writing backend - .put_post(&post, "fireburn.ru") + .put_post(&post, &"https://fireburn.ru/".parse().unwrap()) .await .unwrap(); @@ -482,10 +482,10 @@ mod tests { "children": [] }); backend - .put_post(&feed, "fireburn.ru") + .put_post(&feed, &"https://fireburn.ru/".parse().unwrap()) .await .unwrap(); - let chans = backend.get_channels("fireburn.ru").await.unwrap(); + let chans = backend.get_channels(&"https://fireburn.ru/".parse().unwrap()).await.unwrap(); assert_eq!(chans.len(), 1); assert_eq!( chans[0], @@ -499,14 +499,14 @@ mod tests { async fn test_settings(backend: Backend) { backend .set_setting::( - "https://fireburn.ru/", + &"https://fireburn.ru/".parse().unwrap(), "Vika's Hideout".to_owned() ) .await .unwrap(); assert_eq!( backend - .get_setting::("https://fireburn.ru/") + .get_setting::(&"https://fireburn.ru/".parse().unwrap()) .await .unwrap() .as_ref(), @@ -594,13 +594,13 @@ mod tests { let key = feed["properties"]["uid"][0].as_str().unwrap(); backend - .put_post(&feed, "fireburn.ru") + .put_post(&feed, &"https://fireburn.ru/".parse().unwrap()) .await .unwrap(); for (i, post) in posts.iter().rev().enumerate() { backend - .put_post(post, "fireburn.ru") + .put_post(post, &"https://fireburn.ru/".parse().unwrap()) .await .unwrap(); backend.add_to_feed(key, post["properties"]["uid"][0].as_str().unwrap()).await.unwrap(); @@ -699,7 +699,7 @@ mod tests { async fn test_webmention_addition(db: Backend) { let post = gen_random_post("fireburn.ru"); - db.put_post(&post, "fireburn.ru").await.unwrap(); + db.put_post(&post, &"https://fireburn.ru/".parse().unwrap()).await.unwrap(); const TYPE: MentionType = MentionType::Reply; let target = post["properties"]["uid"][0].as_str().unwrap(); @@ -732,7 +732,7 @@ mod tests { post }; - db.put_post(&post, "fireburn.ru").await.unwrap(); + db.put_post(&post, &"https://fireburn.ru/".parse().unwrap()).await.unwrap(); for i in post["properties"]["url"].as_array().unwrap() { let (read_post, _) = db.read_feed_with_cursor(i.as_str().unwrap(), None, 20, None).await.unwrap().unwrap(); -- cgit 1.4.1