diff options
Diffstat (limited to '')
| -rw-r--r-- | src/main.rs | 59 |
1 files changed, 57 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs index 7253a16..bbfcbff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,9 +12,11 @@ mod scrobbles; mod skweets; mod templates; mod utils; +mod visits; use std::{collections::HashSet, time::Duration}; +use chrono::{DateTime, Utc}; use i18n::set_language; use poem::http::StatusCode; use poem::i18n::unic_langid::langid; @@ -53,25 +55,69 @@ struct Static; #[handler] async fn home(Data(reqwest): Data<&reqwest::Client>, locale: Locale) -> templates::Home { let listenbrainz_client = listenbrainz::raw::Client::new(); - let (live, listenbrainz, blogposts, poems) = tokio::join!( + let (live, listenbrainz, blogposts, poems, visits) = tokio::join!( live::get_live_status(reqwest), scrobbles::get_now_playing(&listenbrainz_client), // skweets::get_recents(&clients.skinnyverse), Blogpost::get_articles(), - Poem::get_articles() + Poem::get_articles(), + visits::get_visits(reqwest) ); let is_live = live.unwrap_or_default().online; let listenbrainz = listenbrainz.unwrap_or_default(); let blogposts = blogposts.unwrap_or_default(); let poems = poems.unwrap_or_default(); + // let visits = visits.ok().map(|v| v.count); + let visits = visits + .ok() + .map(|v| v.count.replace("\u{202f}", "")) + .and_then(|c| c.parse().ok()); + + let mut posts = blogposts + .iter() + .take(10) + .map(Clone::clone) + .map(HomeFeedItem::Blogpost) + .chain( + poems + .iter() + .take(10) + .map(Clone::clone) + .map(HomeFeedItem::Poem), + ) + .collect::<Vec<_>>(); + + posts.sort_by_key(|i| i.published_at()); + posts.reverse(); + posts.truncate(10); + let poem = poems.choose(&mut rand::thread_rng()).cloned(); + templates::Home { title: locale.text("title").unwrap(), is_live, listenbrainz, + feed: posts, blogposts, poem, locale, + filter_tags: HashSet::new(), + jiggle: 8, + visits, + } +} + +pub enum HomeFeedItem { + Blogpost(Blogpost), + Poem(Poem), +} + +impl HomeFeedItem { + pub fn published_at(&self) -> DateTime<Utc> { + match self { + HomeFeedItem::Blogpost(post) => post.published_at, + HomeFeedItem::Poem(poem) => poem.published_at, + } } } @@ -160,6 +206,14 @@ async fn contact(locale: Locale) -> templates::Contact { } #[handler] +async fn about(locale: Locale) -> templates::About { + templates::About { + title: locale.text("title-about").unwrap(), + locale, + } +} + +#[handler] async fn plants() -> Result<()> { Err(BlossomError::Unimplemented) } @@ -229,6 +283,7 @@ async fn main() -> std::result::Result<(), std::io::Error> { .at("/poetry/:poem", get(get_poem)) .at("/feed", get(feed)) .at("/contact", get(contact)) + .at("/about", get(about)) .at("/plants", get(plants)) .nest("/static/", EmbeddedFilesEndpoint::<Static>::new()) .catch_all_error(custom_error) |
