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
|
/*
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) 2026 metamuffin <metamuffin.org>
*/
use super::error::MyResult;
use crate::request_info::RequestInfo;
use anyhow::{Result, anyhow};
use jellycommon::{
jellyobject::{EMPTY, Object, Path, Tag},
*,
};
use jellydb::{Filter, MultiBehaviour, Query, Sort, SortOrder, Transaction, ValueSort};
use jellyui::components::node_page::NodePage;
use rocket::{get, response::content::RawHtml};
use std::{borrow::Cow, collections::BTreeMap};
#[get("/n/<slug>")]
pub fn r_node(ri: RequestInfo<'_>, slug: &str) -> MyResult<RawHtml<String>> {
ri.require_user()?;
let mut nku = None;
let mut children = Vec::new();
let mut credits = Vec::new();
let mut credited = Vec::new();
ri.state.database.transaction(&mut |txn| {
if let Some(row) = txn.query_single(Query {
filter: Filter::Match(Path(vec![NO_SLUG.0]), slug.into()),
..Default::default()
})? {
let n = txn.get(row)?.unwrap();
children = c_children(txn, row, &n)?;
credits = c_credits(txn, &n)?;
credited = c_credited(txn, row)?;
nku = Some(Nku {
node: Cow::Owned(n),
userdata: Cow::Borrowed(EMPTY),
role: None,
});
}
Ok(())
})?;
let Some(nku) = nku else {
Err(anyhow!("no such node"))?
};
Ok(ri.respond_ui(&NodePage {
ri: &ri.render_info(),
nku,
children: &children,
credited: &credited,
credits: &credits,
}))
}
fn c_children(txn: &mut dyn Transaction, row: u64, node: &Object) -> Result<Vec<Nku<'static>>> {
let kind = node.get(NO_KIND).unwrap_or(KIND_COLLECTION);
let (order, path) = match kind {
KIND_CHANNEL => (SortOrder::Descending, Path(vec![NO_RELEASEDATE.0])),
KIND_SEASON | KIND_SHOW => (SortOrder::Ascending, Path(vec![NO_INDEX.0])),
_ => (SortOrder::Ascending, Path(vec![NO_TITLE.0])),
};
let children_rows = txn
.query(Query {
sort: Sort::Value(ValueSort {
multi: MultiBehaviour::First,
offset: None,
order,
path,
}),
filter: Filter::All(vec![
Filter::Match(Path(vec![NO_VISIBILITY.0]), VISI_VISIBLE.into()),
Filter::Match(Path(vec![NO_PARENT.0]), row.into()),
]),
..Default::default()
})?
.collect::<Result<Vec<_>>>()?;
let mut list = Vec::new();
for (row, _) in children_rows {
list.push(Nku {
node: Cow::Owned(txn.get(row)?.unwrap()),
role: None,
userdata: Cow::Borrowed(EMPTY),
});
}
Ok(list)
}
fn c_credits(txn: &mut dyn Transaction, node: &Object) -> Result<Vec<(Tag, Vec<Nku<'static>>)>> {
if !node.has(NO_CREDIT.0) {
return Ok(Vec::new());
}
let mut cats = BTreeMap::<_, Vec<_>>::new();
for cred in node.iter(NO_CREDIT) {
let Some(row) = cred.get(CR_NODE) else {
continue;
};
cred.get(CR_ROLE);
cats.entry(cred.get(CR_KIND).unwrap_or(CRCAT_CREW))
.or_default()
.push(Nku {
node: Cow::Owned(txn.get(row)?.unwrap()),
role: cred.get(CR_ROLE).map(String::from).map(Cow::Owned),
userdata: Cow::Borrowed(EMPTY),
});
}
let mut cats = cats.into_iter().collect::<Vec<_>>();
cats.sort_by_key(|(c, _)| match *c {
CRCAT_CAST => 0,
CRCAT_CREW => 1,
_ => 100,
});
Ok(cats)
}
fn c_credited(txn: &mut dyn Transaction, row: u64) -> Result<Vec<Nku<'static>>> {
let children_rows = txn
.query(Query {
sort: Sort::Value(ValueSort {
multi: MultiBehaviour::First,
offset: None,
order: SortOrder::Ascending,
path: Path(vec![NO_TITLE.0]),
}),
filter: Filter::All(vec![
Filter::Match(Path(vec![NO_VISIBILITY.0]), VISI_VISIBLE.into()),
Filter::Match(Path(vec![NO_CREDIT.0, CR_NODE.0]), row.into()),
]),
..Default::default()
})?
.collect::<Result<Vec<_>>>()?;
let mut list = Vec::new();
for (row, _) in children_rows {
list.push(Nku {
node: Cow::Owned(txn.get(row)?.unwrap()),
role: None,
userdata: Cow::Borrowed(EMPTY),
});
}
Ok(list)
}
|