diff options
Diffstat (limited to 'server/src/routes/ui/home.rs')
-rw-r--r-- | server/src/routes/ui/home.rs | 89 |
1 files changed, 45 insertions, 44 deletions
diff --git a/server/src/routes/ui/home.rs b/server/src/routes/ui/home.rs index ee98bd2..3e002ee 100644 --- a/server/src/routes/ui/home.rs +++ b/server/src/routes/ui/home.rs @@ -10,17 +10,23 @@ use super::{ }; use crate::{ database::Database, - routes::ui::{error::MyResult, layout::DynLayoutPage}, + routes::{ + api::AcceptJson, + 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; +use jellycommon::{api::ApiHomeResponse, user::WatchedState, NodeID, NodeKind, Rating, Visibility}; +use rocket::{get, serde::json::Json, Either, State}; -#[get("/")] -pub fn r_home(sess: Session, db: &State<Database>) -> MyResult<DynLayoutPage> { +#[get("/home")] +pub fn r_home( + sess: Session, + db: &State<Database>, + aj: AcceptJson, +) -> MyResult<Either<DynLayoutPage, Json<ApiHomeResponse>>> { let mut items = db.list_nodes_with_udata(&sess.user.name)?; let mut toplevel = db @@ -31,10 +37,10 @@ pub fn r_home(sess: Session, db: &State<Database>) -> MyResult<DynLayoutPage> { .collect::<anyhow::Result<Vec<_>>>()?; toplevel.sort_by_key(|(n, _)| n.index.unwrap_or(usize::MAX)); - let mut categories = Vec::<(&'static str, Vec<_>)>::new(); + let mut categories = Vec::<(String, Vec<_>)>::new(); categories.push(( - "Continue Watching", + "Continue Watching".to_string(), items .iter() .filter(|(_, u)| matches!(u.watched, WatchedState::Progress(_))) @@ -42,7 +48,7 @@ pub fn r_home(sess: Session, db: &State<Database>) -> MyResult<DynLayoutPage> { .collect(), )); categories.push(( - "Your Watchlist", + "Your Watchlist".to_string(), items .iter() .filter(|(_, u)| matches!(u.watched, WatchedState::Pending)) @@ -55,7 +61,7 @@ pub fn r_home(sess: Session, db: &State<Database>) -> MyResult<DynLayoutPage> { items.sort_by_key(|(n, _)| n.release_date.map(|d| -d).unwrap_or(i64::MAX)); categories.push(( - "Latest in Videos", + "Latest in Videos".to_string(), items .iter() .filter(|(n, _)| matches!(n.kind, NodeKind::Video)) @@ -64,7 +70,7 @@ pub fn r_home(sess: Session, db: &State<Database>) -> MyResult<DynLayoutPage> { .collect(), )); categories.push(( - "Latest in Music", + "Latest in Music".to_string(), items .iter() .filter(|(n, _)| matches!(n.kind, NodeKind::Music)) @@ -73,7 +79,7 @@ pub fn r_home(sess: Session, db: &State<Database>) -> MyResult<DynLayoutPage> { .collect(), )); categories.push(( - "Latest in Short form", + "Latest in Short form".to_string(), items .iter() .filter(|(n, _)| matches!(n.kind, NodeKind::ShortFormVideo)) @@ -90,7 +96,7 @@ pub fn r_home(sess: Session, db: &State<Database>) -> MyResult<DynLayoutPage> { }); categories.push(( - "Top Rated", + "Top Rated".to_string(), items .iter() .take(16) @@ -107,7 +113,7 @@ pub fn r_home(sess: Session, db: &State<Database>) -> MyResult<DynLayoutPage> { }); categories.push(( - "Today's Picks", + "Today's Picks".to_string(), (0..16) .flat_map(|i| Some(items[cheap_daily_random(i).checked_rem(items.len())?].clone())) .collect(), @@ -117,7 +123,7 @@ pub fn r_home(sess: Session, db: &State<Database>) -> MyResult<DynLayoutPage> { let mut items = items.clone(); items.retain(|(_, u)| matches!(u.watched, WatchedState::Watched)); categories.push(( - "Watch again", + "Watch again".to_string(), (0..16) .flat_map(|i| Some(items[cheap_daily_random(i).checked_rem(items.len())?].clone())) .collect(), @@ -126,41 +132,36 @@ pub fn r_home(sess: Session, db: &State<Database>) -> MyResult<DynLayoutPage> { items.retain(|(n, _)| matches!(n.kind, NodeKind::Music)); categories.push(( - "Discover Music", + "Discover Music".to_string(), (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 } } - }} + Ok(if *aj { + Either::Right(Json(ApiHomeResponse { + toplevel, + categories, + })) + } else { + Either::Left(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<DynLayoutPage<'static>> { - 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() + }, + ..Default::default() + }) }) } |