diff options
author | cel 🌸 <cel@blos.sm> | 2023-06-22 15:35:18 +0100 |
---|---|---|
committer | cel 🌸 <cel@blos.sm> | 2023-06-22 15:35:18 +0100 |
commit | a135acf943ba0fc442dca69f43339921728b1ed4 (patch) | |
tree | 77d8ad93d8064db1f0b71601e8b13b165c787cb9 /src/main.rs | |
parent | 139c26158bba4ec09e7ba690ba6c9c11da620707 (diff) | |
download | blossom-a135acf943ba0fc442dca69f43339921728b1ed4.tar.gz blossom-a135acf943ba0fc442dca69f43339921728b1ed4.tar.bz2 blossom-a135acf943ba0fc442dca69f43339921728b1ed4.zip |
implement blog tag filtering
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 46 |
1 files changed, 28 insertions, 18 deletions
diff --git a/src/main.rs b/src/main.rs index c08c2e1..40f546d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ mod scrobbles; mod skweets; use std::borrow::Cow; +use std::collections::HashSet; use rocket::fs::{relative, FileServer}; use rocket::http::Status; @@ -47,24 +48,6 @@ async fn home(clients: &State<Clients>) -> Template { ) } -#[get("/blog")] -async fn blog() -> Template { - let mut blogposts = posts::get_blogposts().await.unwrap_or_default(); - let tags = posts::get_tags(&blogposts); - for blogpost in &mut blogposts { - blogpost.render().await; - } - let reverse = "reverse".to_owned(); - Template::render( - "blog", - context! { - reverse, - blogposts, - tags, - }, - ) -} - #[get("/blog/<blogpost>")] async fn blogpost(blogpost: &str) -> Result<Template> { let mut blogpost = posts::get_blogpost(blogpost).await?; @@ -77,6 +60,33 @@ async fn blogpost(blogpost: &str) -> Result<Template> { )) } +#[get("/blog?<filter>")] +async fn blog(filter: Vec<String>) -> Result<Template> { + let mut blogposts = posts::get_blogposts().await?; + let tags: Vec<String> = posts::get_tags(&blogposts) + .into_iter() + .map(|tag| tag.to_owned()) + .collect(); + let mut filter_hashset: HashSet<String> = HashSet::new(); + if !filter.is_empty() { + filter_hashset.extend(filter.into_iter()); + blogposts = posts::filter_by_tags(blogposts, &filter_hashset); + } + for blogpost in &mut blogposts { + blogpost.render().await?; + } + let reverse = true; + Ok(Template::render( + "blog", + context! { + reverse, + tags, + filter_hashset, + blogposts, + }, + )) +} + #[get("/contact")] async fn contact() -> Template { Template::render("contact", context! {}) |