1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
/*
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 <metamuffin.org>
*/
use super::{
assets::rocket_uri_macro_r_item_assets,
error::MyError,
player::player_uri,
sort::{filter_and_sort_nodes, NodeFilterSort, NodeFilterSortForm},
};
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};
/// This function is a stub and only useful for use in the uri! macro.
#[get("/n/<id>")]
pub fn r_library_node(id: String) -> () {
drop(id)
}
#[get("/n/<id>?<filter..>")]
pub async fn r_library_node_filter(
_sess: Session,
id: String,
db: &State<Database>,
aj: AcceptJson,
filter: NodeFilterSort,
) -> Result<Either<DynLayoutPage<'_>, Json<NodePublic>>, 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 mut children = node
.children
.iter()
.map(|c| {
Ok((
c.to_owned(),
db.node
.get(c)?
.ok_or(anyhow!("child does not exist"))?
.public,
))
})
.collect::<anyhow::Result<Vec<_>>>()?
.into_iter()
.collect();
filter_and_sort_nodes(&filter, &mut children);
Ok(Either::Left(LayoutPage {
title: node.title.to_string(),
show_back: true, //- !matches!(node.kind, NodeKind::Collection),
content: markup::new! {
@NodePage { node: &node, id: &id, children: &children, filter: &filter }
},
..Default::default()
}))
}
markup::define! {
NodeCard<'a>(id: &'a str, node: &'a NodePublic) {
@let cls = format!("node card poster {}", match node.kind {NodeKind::Channel => "poster-square", NodeKind::Video => "thumb-land", NodeKind::Collection => "poster-land", _ => "poster-port"});
div[class=cls] {
.poster {
a[href=uri!(r_library_node(id))] {
img[src=uri!(r_item_assets(id, AssetRole::Poster, Some(1024)))];
}
@if matches!(node.kind, NodeKind::Collection | NodeKind::Channel) {
.cardhover.open { a[href=&uri!(r_library_node(id))] { "Open" } }
} else {
.cardhover.item {
a.play[href=&player_uri(id)] { "▶" }
@Props { node }
}
}
}
p.title {
a[href=uri!(r_library_node(id))] {
@node.title
}
}
}
}
NodePage<'a>(id: &'a str, node: &'a NodePublic, children: &'a Vec<(String, NodePublic)>, filter: &'a NodeFilterSort) {
@if !matches!(node.kind, NodeKind::Collection) {
img.backdrop[src=uri!(r_item_assets(id, AssetRole::Backdrop, Some(2048)))];
}
.page.node {
@if !matches!(node.kind, NodeKind::Collection) {
div.bigposter { img[src=uri!(r_item_assets(id, AssetRole::Poster, Some(2048)))]; }
}
.title {
h1 { @node.title }
@if node.media.is_some() { a.play[href=&player_uri(id)] { "Watch now" }}
}
.details {
@Props { node }
h3 { @node.tagline }
@if let Some(description) = &node.description {
p { @for line in description.lines() { @line br; } }
}
}
@if let NodeKind::Collection = node.kind {
@if let Some(parent) = &node.parent {
a.dirup[href=uri!(r_library_node(parent))] { "Go up" }
@NodeFilterSortForm { f: filter }
}
}
@match node.kind {
NodeKind::Collection | NodeKind::Channel => {
ul.children {@for (id, node) in children.iter() {
li { @NodeCard { id, node } }
}}
}
NodeKind::Series => {
ol { @for (id, c) in children.iter() {
li { a[href=uri!(r_library_node(id))] { @c.title } }
}}
}
_ => {}
}
}
}
Props<'a>(node: &'a NodePublic) {
.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" }
Rating::YoutubeFollowers(n) => { @format_count(*n) " Subscribers" }
Rating::RottenTomatoes(n) => { @n " Tomatoes" }
Rating::Metacritic(n) => { "Metacritic Score: " @n }
Rating::Imdb(n) => { "IMDb Rating: " @n }
} }
}
}
}
}
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<usize>) -> 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}")
}
}
|