From 24b109448bc8f1d30c88bbb9d01a05eec74b3d51 Mon Sep 17 00:00:00 2001 From: Vika Date: Sun, 5 Dec 2021 22:05:36 +0300 Subject: frontend: Added listing feeds to the header bar --- src/database/file/mod.rs | 6 +++--- src/database/mod.rs | 10 +++------- src/frontend/mod.rs | 11 +++++++++-- src/frontend/templates/mod.rs | 8 ++++++-- src/micropub/get.rs | 4 ++-- 5 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/database/file/mod.rs b/src/database/file/mod.rs index 1e0102a..67b3549 100644 --- a/src/database/file/mod.rs +++ b/src/database/file/mod.rs @@ -346,12 +346,12 @@ impl Storage for FileStorage { Ok(()) } - async fn get_channels( + async fn get_channels<'a>( &self, - user: &crate::indieauth::User, + user: &'a str, ) -> Result> { let mut path = relative_path::RelativePathBuf::new(); - path.push(&user.me.to_string()); + path.push(user.to_string()); path.push("channels"); let path = path.to_path(&self.root_dir); diff --git a/src/database/mod.rs b/src/database/mod.rs index 339439d..a57e243 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -210,8 +210,8 @@ pub trait Storage: Clone + Send + Sync { /// The last update always wins. async fn update_post<'a>(&self, url: &'a str, update: serde_json::Value) -> Result<()>; - /// Get a list of channels available for the user represented by the `user` object to write. - async fn get_channels(&self, user: &User) -> Result>; + /// Get a list of channels available for the user represented by the URL `user` to write to. + async fn get_channels<'a>(&self, user: &'a str) -> Result>; /// Fetch a feed at `url` and return a an h-feed object containing /// `limit` posts after a post by url `after`, filtering the content @@ -386,11 +386,7 @@ mod tests { .await .unwrap(); let chans = backend - .get_channels(&crate::indieauth::User::new( - "https://fireburn.ru/", - "https://quill.p3k.io/", - "create update media", - )) + .get_channels("https://fireburn.ru/") .await .unwrap(); assert_eq!(chans.len(), 1); diff --git a/src/frontend/mod.rs b/src/frontend/mod.rs index 890879c..6194249 100644 --- a/src/frontend/mod.rs +++ b/src/frontend/mod.rs @@ -257,6 +257,7 @@ pub async fn mainpage(mut req: Request>) -> Resu webmention: None, microsub: None, }, + feeds: Vec::default(), content: OnboardingPage {}.to_string(), } .to_string(), @@ -281,6 +282,7 @@ pub async fn mainpage(mut req: Request>) -> Resu webmention: None, microsub: None, }, + feeds: backend.get_channels(hcard_url).await.unwrap_or_else(|_| Vec::default()), content: MainPage { feed: &feed?, card: &card?, @@ -335,6 +337,7 @@ pub async fn render_post(mut req: Request>) -> R } }; let origin = url.origin(); + let owner = origin.ascii_serialization() + "/"; Ok(Response::builder(200) .content_type("text/html; charset=utf-8") @@ -346,7 +349,7 @@ pub async fn render_post(mut req: Request>) -> R blog_name: &req .state() .storage - .get_setting("site_name", &(origin.ascii_serialization() + "/")) // XXX I'm pretty sure this is bound to cause issues with IDN-style domains + .get_setting("site_name", &owner) // XXX I'm pretty sure this is bound to cause issues with IDN-style domains .await .unwrap_or_else(|_| "Kitty Box!".to_string()), endpoints: IndiewebEndpoints { @@ -355,6 +358,7 @@ pub async fn render_post(mut req: Request>) -> R webmention: None, microsub: None, }, + feeds: req.state().storage.get_channels(&owner).await.unwrap_or_else(|e| Vec::default()), content: template, } .to_string(), @@ -376,12 +380,14 @@ where ) -> Result { let authorization_endpoint = request.state().authorization_endpoint.to_string(); let token_endpoint = request.state().token_endpoint.to_string(); + let owner = request.url().origin().ascii_serialization().clone() + "/"; let site_name = &request .state() .storage - .get_setting("site_name", &(request.url().origin().ascii_serialization().clone() + "/")) + .get_setting("site_name", &owner) .await .unwrap_or_else(|_| "Kitty Box!".to_string()); + let feeds = request.state().storage.get_channels(&owner).await.unwrap_or_else(|_| Vec::default()); let mut res = next.run(request).await; let mut code: Option = None; if let Some(err) = res.downcast_error::() { @@ -406,6 +412,7 @@ where webmention: None, microsub: None, }, + feeds: feeds, content: ErrorPage { code }.to_string(), } .to_string(), diff --git a/src/frontend/templates/mod.rs b/src/frontend/templates/mod.rs index 290e215..a7c01e0 100644 --- a/src/frontend/templates/mod.rs +++ b/src/frontend/templates/mod.rs @@ -1,4 +1,5 @@ -use super::IndiewebEndpoints; +use crate::database::MicropubChannel; +use crate::frontend::IndiewebEndpoints; use ellipse::Ellipse; use http_types::StatusCode; use log::error; @@ -24,7 +25,7 @@ mod onboarding; pub use onboarding::OnboardingPage; markup::define! { - Template<'a>(title: &'a str, blog_name: &'a str, endpoints: IndiewebEndpoints, content: String) { + Template<'a>(title: &'a str, blog_name: &'a str, endpoints: IndiewebEndpoints, feeds: Vec, content: String) { @markup::doctype() html { head { @@ -48,6 +49,9 @@ markup::define! { ul { // TODO print a list of feeds and allow jumping to them li { a#homepage[href="/"] { @blog_name } } + @for feed in feeds.iter() { + li { a[href=&feed.uid] { @feed.name } } + } li.shiftright { a#login[href="/login"] { "Login" } } } } diff --git a/src/micropub/get.rs b/src/micropub/get.rs index 5db99f7..9732281 100644 --- a/src/micropub/get.rs +++ b/src/micropub/get.rs @@ -24,7 +24,7 @@ where match &*query.q { "config" => { let channels: Vec; - match backend.get_channels(&user).await { + match backend.get_channels(&user.me.as_str()).await { Ok(chans) => channels = chans, Err(err) => return Ok(err.into()) } @@ -36,7 +36,7 @@ where }, "channel" => { let channels: Vec; - match backend.get_channels(&user).await { + match backend.get_channels(&user.me.as_str()).await { Ok(chans) => channels = chans, Err(err) => return Ok(err.into()) } -- cgit 1.4.1