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
|
/*
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) 2024 metamuffin <metamuffin.org>
*/
use super::{
account::session::Session,
layout::LayoutPage,
node::{DatabaseNodeUserDataExt, NodeCard},
};
use crate::{
database::DataAcid,
routes::ui::{error::MyResult, layout::DynLayoutPage},
};
use anyhow::Context;
use chrono::{Datelike, Utc};
use jellybase::{
database::{redb::ReadableTable, TableExt, T_NODE, T_USER_NODE},
CONF,
};
use jellycommon::{user::WatchedState, Rating};
use rocket::{get, State};
use tokio::fs::read_to_string;
#[get("/")]
pub fn r_home(sess: Session, db: &State<DataAcid>) -> MyResult<DynLayoutPage> {
let mut items = {
let txn = db.begin_read()?;
let nodes = txn.open_table(T_NODE)?;
let node_users = txn.open_table(T_USER_NODE)?;
let i = nodes
.iter()?
.map(|a| {
let (x, y) = a.unwrap();
let (x, y) = (x.value().to_owned(), y.value().0);
let z = node_users
.get(&(sess.user.name.as_str(), x.as_str()))
.unwrap()
.map(|z| z.value().0)
.unwrap_or_default();
let y = y.public;
(x, y, z)
})
.collect::<Vec<_>>();
drop(nodes);
i
};
let random = (0..16)
.flat_map(|i| Some(items[cheap_daily_random(i).checked_rem(items.len())?].clone()))
.collect::<Vec<_>>();
let toplevel = T_NODE
.get(&db, "library")?
.context("root node missing")?
.public
.children
.into_iter()
.map(|n| db.get_node_with_userdata(&n, &sess))
.collect::<anyhow::Result<Vec<_>>>()?
.into_iter()
.collect::<Vec<_>>();
items.sort_by_key(|(_, n, _)| {
n.ratings
.get(&Rating::Tmdb)
.map(|x| (*x * -1000.) as i32)
.unwrap_or(0)
});
let top_rated = items
.iter()
.take(16)
.filter(|(_, n, _)| n.ratings.contains_key(&Rating::Tmdb))
.map(|k| k.to_owned())
.collect::<Vec<_>>();
items.sort_by_key(|(_, n, _)| n.release_date.map(|d| -d).unwrap_or(i64::MAX));
let latest = items
.iter()
.take(16)
.map(|k| k.to_owned())
.collect::<Vec<_>>();
let continue_watching = items
.iter()
.filter(|(_, _, u)| matches!(u.watched, WatchedState::Progress(_)))
.map(|k| k.to_owned())
.collect::<Vec<_>>();
let watchlist = items
.iter()
.filter(|(_, _, u)| matches!(u.watched, WatchedState::Pending))
.map(|k| k.to_owned())
.collect::<Vec<_>>();
Ok(LayoutPage {
title: "Home".to_string(),
content: markup::new! {
h2 { "Explore " @CONF.brand }
ul.children.hlist {@for (id, node, udata) in &toplevel {
li { @NodeCard { id, node, udata } }
}}
@if !continue_watching.is_empty() {
h2 { "Continue Watching" }
ul.children.hlist {@for (id, node, udata) in &continue_watching {
li { @NodeCard { id, node, udata } }
}}
}
@if !watchlist.is_empty() {
h2 { "Watchlist" }
ul.children.hlist {@for (id, node, udata) in &watchlist {
li { @NodeCard { id, node, udata } }
}}
}
h2 { "Today's Picks" }
ul.children.hlist {@for (id, node, udata) in &random {
li { @NodeCard { id, node, udata } }
}}
h2 { "Latest Releases" }
ul.children.hlist {@for (id, node, udata) in &latest {
li { @NodeCard { id, node, udata } }
}}
@if !top_rated.is_empty() {
h2 { "Top Rated" }
ul.children.hlist {@for (id, node, udata) in &top_rated {
li { @NodeCard { id, 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()
})
}
fn cheap_daily_random(i: usize) -> usize {
xorshift(xorshift(Utc::now().num_days_from_ce() as u64) + i as u64) as usize
}
fn xorshift(mut x: u64) -> u64 {
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
x
}
|