/* This file is part of jellything (https://codeberg.org/metamuffin/jellything) which is licensed under the GNU Affero General Public License (version 3); see /COPYING. Copyright (C) 2025 metamuffin */ use super::{ account::session::Session, layout::LayoutPage, node::{DatabaseNodeUserDataExt, NodeCard}, }; use crate::{ database::Database, routes::ui::{error::MyResult, layout::DynLayoutPage}, }; use anyhow::Context; use chrono::{Datelike, Utc}; use jellybase::CONF; use jellycommon::{user::WatchedState, NodeID, NodeKind, Rating, Visibility}; use rocket::{get, State}; use tokio::fs::read_to_string; #[get("/")] pub fn r_home(sess: Session, db: &State) -> MyResult { let mut items = db.list_nodes_with_udata(&sess.user.name)?; let mut toplevel = db .get_node_children(NodeID::from_slug("library")) .context("root node missing")? .into_iter() .map(|n| db.get_node_with_userdata(n, &sess)) .collect::>>()?; toplevel.sort_by_key(|(n, _)| n.index.unwrap_or(usize::MAX)); let mut categories = Vec::<(&'static str, Vec<_>)>::new(); categories.push(( "Continue Watching", items .iter() .filter(|(_, u)| matches!(u.watched, WatchedState::Progress(_))) .cloned() .collect(), )); categories.push(( "Your Watchlist", items .iter() .filter(|(_, u)| matches!(u.watched, WatchedState::Pending)) .cloned() .collect(), )); items.retain(|(n, _)| matches!(n.visibility, Visibility::Visible)); items.sort_by_key(|(n, _)| n.release_date.map(|d| -d).unwrap_or(i64::MAX)); categories.push(( "Latest in Videos", items .iter() .filter(|(n, _)| matches!(n.kind, NodeKind::Video)) .take(16) .cloned() .collect(), )); categories.push(( "Latest in Music", items .iter() .filter(|(n, _)| matches!(n.kind, NodeKind::Music)) .take(16) .cloned() .collect(), )); categories.push(( "Latest in Short form", items .iter() .filter(|(n, _)| matches!(n.kind, NodeKind::ShortFormVideo)) .take(16) .cloned() .collect(), )); items.sort_by_key(|(n, _)| { n.ratings .get(&Rating::Tmdb) .map(|x| (*x * -1000.) as i32) .unwrap_or(0) }); categories.push(( "Top Rated", items .iter() .take(16) .filter(|(n, _)| n.ratings.contains_key(&Rating::Tmdb)) .cloned() .collect(), )); items.retain(|(n, _)| { matches!( n.kind, NodeKind::Video | NodeKind::Movie | NodeKind::Episode | NodeKind::Music ) }); categories.push(( "Today's Picks", (0..16) .flat_map(|i| Some(items[cheap_daily_random(i).checked_rem(items.len())?].clone())) .collect(), )); { let mut items = items.clone(); items.retain(|(_, u)| matches!(u.watched, WatchedState::Watched)); categories.push(( "Watch again", (0..16) .flat_map(|i| Some(items[cheap_daily_random(i).checked_rem(items.len())?].clone())) .collect(), )); } items.retain(|(n, _)| matches!(n.kind, NodeKind::Music)); categories.push(( "Discover Music", (0..16) .flat_map(|i| Some(items[cheap_daily_random(i).checked_rem(items.len())?].clone())) .collect(), )); Ok(LayoutPage { title: "Home".to_string(), content: markup::new! { h2 { "Explore " @CONF.brand } ul.children.hlist {@for (node, udata) in &toplevel { li { @NodeCard { node, udata } } }} @for (name, nodes) in &categories { @if !nodes.is_empty() { h2 { @name } ul.children.hlist {@for (node, udata) in nodes { li { @NodeCard { node, udata } } }} } } }, ..Default::default() }) } #[get("/", rank = 2)] pub async fn r_home_unpriv() -> MyResult> { let front = read_to_string(CONF.asset_path.join("front.htm")).await?; Ok(LayoutPage { title: "Home".to_string(), content: markup::new! { @markup::raw(&front) }, ..Default::default() }) } fn cheap_daily_random(i: usize) -> usize { xorshift(xorshift(Utc::now().num_days_from_ce() as u64) + i as u64) as usize } fn xorshift(mut x: u64) -> u64 { x ^= x << 13; x ^= x >> 7; x ^= x << 17; x }