From 324794a9e8d60c9e187dd8c24f9cae076e933142 Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Thu, 1 Jan 2026 22:11:02 +0100 Subject: new home feed --- src/blog.rs | 12 ++++++------ src/main.rs | 38 ++++++++++++++++++++++++++++++++++++++ src/templates.rs | 5 ++++- 3 files changed, 48 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/blog.rs b/src/blog.rs index 9acb5e2..36204e7 100644 --- a/src/blog.rs +++ b/src/blog.rs @@ -17,12 +17,12 @@ static DIRECTORY: &str = "./blog"; #[derive(Clone)] pub struct Blogpost { - file_name: String, - title: String, - published_at: DateTime, - updated_at: Option>, - tags: Vec, - content: String, + pub file_name: String, + pub title: String, + pub published_at: DateTime, + pub updated_at: Option>, + pub tags: Vec, + pub content: String, } #[derive(Deserialize)] diff --git a/src/main.rs b/src/main.rs index 0f14e3c..7247c33 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,6 +15,7 @@ mod utils; use std::{collections::HashSet, time::Duration}; +use chrono::{DateTime, Utc}; use i18n::set_language; use poem::http::StatusCode; use poem::i18n::unic_langid::langid; @@ -64,14 +65,51 @@ async fn home(Data(reqwest): Data<&reqwest::Client>, locale: Locale) -> template let listenbrainz = listenbrainz.unwrap_or_default(); let blogposts = blogposts.unwrap_or_default(); let poems = poems.unwrap_or_default(); + + 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::>(); + + 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, + } +} + +pub enum HomeFeedItem { + Blogpost(Blogpost), + Poem(Poem), +} + +impl HomeFeedItem { + pub fn published_at(&self) -> DateTime { + match self { + HomeFeedItem::Blogpost(post) => post.published_at, + HomeFeedItem::Poem(poem) => poem.published_at, + } } } diff --git a/src/templates.rs b/src/templates.rs index b4ea98d..c086487 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -5,9 +5,9 @@ use poem::http::StatusCode; use rand::{thread_rng, Rng}; use crate::i18n::Locale; -use crate::poetry; use crate::posts::Post; use crate::{blog, scrobbles::NowPlayingData}; +use crate::{poetry, HomeFeedItem}; mod filters { pub fn mytruncate(s: impl std::fmt::Display, length: usize) -> ::askama::Result { @@ -23,9 +23,12 @@ pub struct Home { pub title: String, pub is_live: bool, pub listenbrainz: NowPlayingData, + pub feed: Vec, pub blogposts: Vec, pub poem: Option, pub locale: Locale, + pub filter_tags: HashSet, + pub jiggle: isize, } #[derive(Template)] -- cgit