aboutsummaryrefslogtreecommitdiff
path: root/server/src/routes/items.rs
blob: e841d1b5eb4fad4feab8434f9997a1dc07aaa56c (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
/*
    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 crate::{
    request_info::RequestInfo,
    responders::UiPage,
    routes::{error::MyResult, node::create_nku},
};
use anyhow::anyhow;
use base64::{Engine, prelude::BASE64_URL_SAFE};
use jellycommon::{jellyobject::Path, *};
use jellydb::{Filter, MultiBehaviour, Query, Sort, SortOrder, ValueSort};
use jellyui::components::items::Items;
use rocket::get;

#[get("/items?<cont>")]
pub fn r_items(ri: RequestInfo, cont: Option<&str>) -> MyResult<UiPage> {
    let cont_in = cont
        .map(|s| BASE64_URL_SAFE.decode(s))
        .transpose()
        .map_err(|_| anyhow!("invalid contination token"))?;

    let mut items = Vec::new();
    let mut cont_out = None;
    ri.state.database.transaction(&mut |txn| {
        let rows = txn
            .query(Query {
                filter: Filter::All(vec![
                    Filter::Match(Path(vec![NO_KIND.0]), KIND_VIDEO.into()),
                    Filter::Match(Path(vec![NO_VISIBILITY.0]), VISI_VISIBLE.into()),
                ]),
                sort: Sort::Value(ValueSort {
                    path: Path(vec![NO_RELEASEDATE.0]),
                    multi: MultiBehaviour::First,
                    order: SortOrder::Descending,
                    offset: None,
                }),
                continuation: cont_in.clone(),
            })?
            .take(64)
            .collect::<Result<Vec<_>, _>>()?;

        items.clear();
        cont_out = None;
        for (row, is) in rows {
            items.push(create_nku(txn, row)?);
            cont_out = Some(is)
        }
        Ok(())
    })?;

    Ok(ri.respond_ui(&Items {
        ri: &ri.render_info(),
        items: &items,
        cont: cont_out.map(|x| BASE64_URL_SAFE.encode(x)),
    }))
}