aboutsummaryrefslogtreecommitdiff
path: root/server/src/routes/ui/home.rs
blob: d9d1100fa960e6b4224911a7fa1b0f010382ae5f (plain)
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
/*
    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::{account::session::Session, layout::LayoutPage, node::NodeCard};
use crate::{
    database::Database,
    routes::ui::{error::MyResult, layout::DynLayoutPage},
};
use anyhow::Context;
use chrono::{Datelike, Utc};
use jellybase::CONF;
use jellycommon::NodePublic;
use rocket::{get, State};
use tokio::fs::read_to_string;

#[get("/")]
pub fn r_home(sess: Session, db: &State<Database>) -> MyResult<DynLayoutPage> {
    let mut items = db
        .node
        .iter()
        .map(|e| e.context("listing"))
        .collect::<anyhow::Result<Vec<_>>>()?
        .into_iter()
        .map(|(k, n)| (k, n.public))
        .collect::<Vec<(String, NodePublic)>>();

    let random = (0..16)
        .flat_map(|i| Some(items[cheap_daily_random(i).checked_rem(items.len())?].clone()))
        .collect::<Vec<_>>();

    let toplevel = db
        .node
        .get(&"library".to_string())?
        .context("root node missing")?
        .public
        .children
        .into_iter()
        .map(|n| {
            Ok((
                n.clone(),
                db.node.get(&n)?.context("child does not exist")?.public,
            ))
        })
        .collect::<anyhow::Result<Vec<_>>>()?
        .into_iter()
        .collect::<Vec<_>>();

    items.sort_by_key(|(_, n)| {
        n.release_date
            .map(|d| d.naive_utc().timestamp())
            .unwrap_or(0)
    });

    let latest = items
        .iter()
        .take(16)
        .map(|k| k.to_owned())
        .collect::<Vec<_>>();

    Ok(LayoutPage {
        title: "Home".to_string(),
        content: markup::new! {
            p { "Welcome back " @sess.user.display_name  }
            h2 { "Explore " @CONF.brand }
            .homelist { ul {@for (id, node) in &toplevel {
                li { @NodeCard { id, node } }
            }}}
            h2 { "Latest Releases" }
            .homelist { ul {@for (id, node) in &latest {
                li { @NodeCard { id, node } }
            }}}
            h2 { "Today's Picks" }
            .homelist { ul {@for (id, node) in &random {
                li { @NodeCard { id, node } }
            }}}
            p.error { "TODO: continue watching" }
            p.error { "TODO: recently added" }
            p.error { "TODO: best rating" }
        },
        ..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
}