diff options
author | Vika <vika@fireburn.ru> | 2023-06-22 21:35:22 +0300 |
---|---|---|
committer | Vika <vika@fireburn.ru> | 2023-06-22 21:35:22 +0300 |
commit | 858c0ddd9cc36af0acc72efb1ff1bdc1d8e28b0a (patch) | |
tree | 5b46bcc88e37fa0e23fea4f49c0242ad21662bca /kittybox-rs/src/database/file/mod.rs | |
parent | d61e1f6a8e5ad5b7c14b1f9ab3101496f3f9ea00 (diff) | |
download | kittybox-858c0ddd9cc36af0acc72efb1ff1bdc1d8e28b0a.tar.zst |
database: use domains instead of authorities as owner key
This allows disregarding http/https comparisons and simplifies some database designs.
Diffstat (limited to 'kittybox-rs/src/database/file/mod.rs')
-rw-r--r-- | kittybox-rs/src/database/file/mod.rs | 53 |
1 files changed, 29 insertions, 24 deletions
diff --git a/kittybox-rs/src/database/file/mod.rs b/kittybox-rs/src/database/file/mod.rs index a9de63e..3b373d8 100644 --- a/kittybox-rs/src/database/file/mod.rs +++ b/kittybox-rs/src/database/file/mod.rs @@ -108,9 +108,16 @@ fn url_to_path(root: &Path, url: &str) -> PathBuf { } fn url_to_relative_path(url: &str) -> relative_path::RelativePathBuf { - let url = axum::http::Uri::try_from(url).expect("Couldn't parse a URL"); + let url = url::Url::try_from(url).expect("Couldn't parse a URL"); let mut path = relative_path::RelativePathBuf::new(); - path.push(url.authority().unwrap().to_string() + url.path() + ".json"); + let user_domain = format!( + "{}{}", + url.host_str().unwrap(), + url.port() + .map(|port| format!(":{}", port)) + .unwrap_or_default() + ); + path.push(user_domain + url.path() + ".json"); path } @@ -353,7 +360,17 @@ impl Storage for FileStorage { if let Some(urls) = post["properties"]["url"].as_array() { for url in urls.iter().map(|i| i.as_str().unwrap()) { - if url != key && url.starts_with(user) { + let url_domain = { + let url = url::Url::parse(url).unwrap(); + format!( + "{}{}", + url.host_str().unwrap(), + url.port() + .map(|port| format!(":{}", port)) + .unwrap_or_default() + ) + }; + if url != key && url_domain == user { let link = url_to_path(&self.root_dir, url); debug!("Creating a symlink at {:?}", link); let orig = path.clone(); @@ -377,20 +394,14 @@ impl Storage for FileStorage { .iter() .any(|s| s.as_str() == Some("h-feed")) { - println!("Adding to channel list..."); + tracing::debug!("Adding to channel list..."); // Add the h-feed to the channel list let mut path = relative_path::RelativePathBuf::new(); - path.push( - axum::http::Uri::try_from(user.to_string()) - .unwrap() - .authority() - .unwrap() - .to_string(), - ); + path.push(user); path.push("channels"); - let path = path.to_path(&self.root_dir); - let tempfilename = (&path).with_extension("tmp"); + tracing::debug!("Channels file path: {}", path.display()); + let tempfilename = path.with_extension("tmp"); let channel_name = post["properties"]["name"][0] .as_str() .map(|s| s.to_string()) @@ -468,16 +479,12 @@ impl Storage for FileStorage { #[tracing::instrument(skip(self))] async fn get_channels(&self, user: &'_ str) -> Result<Vec<super::MicropubChannel>> { let mut path = relative_path::RelativePathBuf::new(); - path.push( - axum::http::Uri::try_from(user.to_string()) - .unwrap() - .authority() - .unwrap() - .to_string(), - ); + path.push(user); path.push("channels"); let path = path.to_path(&self.root_dir); + tracing::debug!("Channels file path: {}", path.display()); + match File::open(&path).await { Ok(mut f) => { let mut content = String::new(); @@ -595,9 +602,8 @@ impl Storage for FileStorage { #[tracing::instrument(skip(self))] async fn get_setting<S: settings::Setting<'a>, 'a>(&self, user: &'_ str) -> Result<S> { debug!("User for getting settings: {}", user); - let url = axum::http::Uri::try_from(user).expect("Couldn't parse a URL"); let mut path = relative_path::RelativePathBuf::new(); - path.push(url.authority().unwrap().to_string()); + path.push(user); path.push("settings"); let path = path.to_path(&self.root_dir); @@ -616,9 +622,8 @@ impl Storage for FileStorage { #[tracing::instrument(skip(self))] async fn set_setting<S: settings::Setting<'a> + 'a, 'a>(&self, user: &'a str, value: S::Data) -> Result<()> { - let url = axum::http::Uri::try_from(user).expect("Couldn't parse a URL"); let mut path = relative_path::RelativePathBuf::new(); - path.push(url.authority().unwrap().to_string()); + path.push(user); path.push("settings"); let path = path.to_path(&self.root_dir); |