/* 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) 2023 metamuffin */ use super::{assets::rocket_uri_macro_r_item_assets, error::MyError, player::player_uri}; use crate::{ database::Database, routes::{ api::AcceptJson, ui::{ account::session::Session, assets::AssetRole, layout::{DynLayoutPage, LayoutPage}, }, }, uri, }; use anyhow::{anyhow, Context}; use jellycommon::{MediaInfo, NodeKind, NodePublic, Rating, SourceTrackKind}; use rocket::{get, serde::json::Json, Either, State}; #[get("/n/")] pub async fn r_library_node( _sess: Session, id: String, db: &State, aj: AcceptJson, ) -> Result, Json>, MyError> { let node = db .node .get(&id) .context("retrieving library node")? .ok_or(anyhow!("node does not exist"))? .public; if *aj { return Ok(Either::Right(Json(node))); } let children = node .children .iter() .map(|c| { Ok(( c.to_owned(), db.node .get(c)? .ok_or(anyhow!("child does not exist"))? .public, )) }) .collect::>>()? .into_iter() .collect(); Ok(Either::Left(LayoutPage { title: node.title.to_string(), show_back: matches!(node.kind, NodeKind::Movie | NodeKind::Episode), content: markup::new! { @NodePage { node: &node, id: &id, children: &children } }, ..Default::default() })) } markup::define! { NodePage<'a>(id: &'a str, node: &'a NodePublic, children: &'a Vec<(String, NodePublic)>) { @match node.kind { NodeKind::Collection | NodeKind::Show | NodeKind::Season => { @DirectoryPage { node, _id: id, children } } NodeKind::Series => { @SeriesPage { node, children, id } } NodeKind::Movie | NodeKind::Episode | NodeKind::Video => { @ItemPage { node, id } } } } NodeCard<'a>(id: &'a str, node: &'a NodePublic) { @PosterCard { wide: matches!(node.kind, NodeKind::Collection | NodeKind::Video), dir: !matches!(node.kind, NodeKind::Movie | NodeKind::Video | NodeKind::Episode), id, title: &node.title } } DirectoryPage<'a>(_id: &'a str, node: &'a NodePublic, children: &'a Vec<(String, NodePublic)>) { div.page.dir { h1 { @node.title } @if let Some(parent) = &node.parent { a.dirup[href=uri!(r_library_node(parent))] { "Go up" } } ul.directorylisting { @for (id, node) in children.iter() { li { @NodeCard { id, node } } } } } } PosterCard<'a>(id: &'a str, title: &'a str, wide: bool, dir: bool) { div[class=if *wide {"card wide poster"} else {"card poster"}] { div.banner { a[href=uri!(r_library_node(id))] { img[src=uri!(r_item_assets(id, AssetRole::Poster))]; } @if *dir { div.hoverdir { a[href=&uri!(r_library_node(id))] { "Open" } } } else { div.hoveritem { a[href=&player_uri(id)] { "▶" } } } } p.title { a[href=uri!(r_library_node(id))] { @title } } } } ItemPage<'a>(id: &'a str, node: &'a NodePublic) { // TODO different image here img.backdrop[src=uri!(r_item_assets(id, AssetRole::Backdrop))]; div.page.item { div.banner { img[src=uri!(r_item_assets(id, AssetRole::Poster))]; } div.title { h1 { @node.title } a.play[href=&player_uri(id)] { "Watch now" } } div.details { div.props { @if let Some(m) = &node.media { p { @format_duration(m.duration) } p { @m.resolution_name() } } @for r in &node.ratings { p { @match r { Rating::YoutubeLikes(n) => { @format_count(*n) " Likes" } Rating::YoutubeViews(n) => { @format_count(*n) " Views" } _ => { "Unknown Rating" } } } } } h3 { @node.tagline } p { @node.description } } } } SeriesPage<'a>(id: &'a str, node: &'a NodePublic, children: &'a Vec<(String, NodePublic)>) { // TODO different image here img.backdrop[src=uri!(r_item_assets(id, AssetRole::Backdrop))]; div.page.item { div.banner { img[src=uri!(r_item_assets(id, AssetRole::Poster))]; } div.title { h1 { @node.title } } div.details { h3 { @node.tagline } p { @node.description } } ol { @for (id, c) in children.iter() { li { a[href=uri!(r_library_node(id))] { @c.title } } } } } } } pub fn format_duration(mut d: f64) -> String { let mut s = String::new(); for (unit, k) in [("h", 60. * 60.), ("m", 60.), ("s", 1.)] { let mut h = 0; while d > k { d -= k; h += 1; } if h > 0 { s += &format!("{h}{unit}") } } s } trait MediaInfoExt { fn resolution_name(&self) -> &'static str; } impl MediaInfoExt for MediaInfo { fn resolution_name(&self) -> &'static str { let mut maxw = 0; for t in &self.tracks { match &t.kind { SourceTrackKind::Video { width, .. } => maxw = maxw.max(*width), _ => (), } } match maxw { 7680.. => "8K", 3840.. => "4K", 2560.. => "WQHD", 1920.. => "Full HD", 1280.. => "HD 720p", 640.. => "NTSC", _ => "Unkown", } } } fn format_count(n: impl Into) -> String { let n: usize = n.into(); if n >= 1_000_000 { format!("{:.1}M", n as f32 / 1_000_000.) } else if n >= 1_000 { format!("{:.1}k", n as f32 / 1_000.) } else { format!("{n}") } }