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
|
/*
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 <metamuffin.org>
*/
use super::{error::MyResult, sort::filter_and_sort_nodes};
use crate::{api::AcceptJson, database::Database, locale::AcceptLanguage, logic::session::Session};
use anyhow::{anyhow, Result};
use jellycommon::{
api::{ApiNodeResponse, NodeFilterSort, SortOrder, SortProperty},
user::NodeUserData,
Node, NodeID, NodeKind, Visibility,
};
use rocket::{get, serde::json::Json, Either, State};
use std::{cmp::Reverse, collections::BTreeMap, sync::Arc};
/// This function is a stub and only useful for use in the uri! macro.
#[get("/n/<id>")]
pub fn r_library_node(id: NodeID) {
let _ = id;
}
#[get("/n/<id>?<parents>&<children>&<filter..>")]
pub async fn r_library_node_filter<'a>(
session: Session,
id: NodeID,
db: &'a State<Database>,
aj: AcceptJson,
filter: NodeFilterSort,
lang: AcceptLanguage,
parents: bool,
children: bool,
) -> MyResult<Either<DynLayoutPage<'a>, Json<ApiNodeResponse>>> {
let AcceptLanguage(lang) = lang;
let (node, udata) = db.get_node_with_userdata(id, &session)?;
let mut children = if !*aj || children {
db.get_node_children(id)?
.into_iter()
.map(|c| db.get_node_with_userdata(c, &session))
.collect::<anyhow::Result<Vec<_>>>()?
} else {
Vec::new()
};
let mut parents = if !*aj || parents {
node.parents
.iter()
.map(|pid| db.get_node_with_userdata(*pid, &session))
.collect::<anyhow::Result<Vec<_>>>()?
} else {
Vec::new()
};
let mut similar = get_similar_media(&node, db, &session)?;
similar.retain(|(n, _)| n.visibility >= Visibility::Reduced);
children.retain(|(n, _)| n.visibility >= Visibility::Reduced);
parents.retain(|(n, _)| n.visibility >= Visibility::Reduced);
filter_and_sort_nodes(
&filter,
match node.kind {
NodeKind::Channel => (SortProperty::ReleaseDate, SortOrder::Descending),
NodeKind::Season | NodeKind::Show => (SortProperty::Index, SortOrder::Ascending),
_ => (SortProperty::Title, SortOrder::Ascending),
},
&mut children,
);
Ok(if *aj {
Either::Right(Json(ApiNodeResponse {
children,
parents,
node,
userdata: udata,
}))
} else {
Either::Left(LayoutPage {
title: node.title.clone().unwrap_or_default(),
content: markup::new!(@NodePage {
node: &node,
udata: &udata,
children: &children,
parents: &parents,
filter: &filter,
player: false,
similar: &similar,
lang: &lang,
}),
..Default::default()
})
})
}
pub fn get_similar_media(
node: &Node,
db: &Database,
session: &Session,
) -> Result<Vec<(Arc<Node>, NodeUserData)>> {
let this_id = NodeID::from_slug(&node.slug);
let mut ranking = BTreeMap::<NodeID, usize>::new();
for tag in &node.tags {
let nodes = db.get_tag_nodes(tag)?;
let weight = 1_000_000 / nodes.len();
for n in nodes {
if n != this_id {
*ranking.entry(n).or_default() += weight;
}
}
}
let mut ranking = ranking.into_iter().collect::<Vec<_>>();
ranking.sort_by_key(|(_, k)| Reverse(*k));
ranking
.into_iter()
.take(32)
.map(|(pid, _)| db.get_node_with_userdata(pid, session))
.collect::<anyhow::Result<Vec<_>>>()
}
pub trait DatabaseNodeUserDataExt {
fn get_node_with_userdata(
&self,
id: NodeID,
session: &Session,
) -> Result<(Arc<Node>, NodeUserData)>;
}
impl DatabaseNodeUserDataExt for Database {
fn get_node_with_userdata(
&self,
id: NodeID,
session: &Session,
) -> Result<(Arc<Node>, NodeUserData)> {
Ok((
self.get_node(id)?.ok_or(anyhow!("node does not exist"))?,
self.get_node_udata(id, &session.user.name)?
.unwrap_or_default(),
))
}
}
|