aboutsummaryrefslogtreecommitdiff
path: root/server/src/ui/node.rs
blob: d510c7147db3590e5a055d0d153b4c79f67fd874 (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
/*
    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, ui_responder::UiResponse};
use anyhow::{Result, anyhow};
use jellycommon::{
    jellyobject::{Object, Path},
    *,
};
use jellydb::{Filter, Query, Sort};
use rocket::get;

#[get("/n/<slug>")]
pub fn r_node(ri: RequestInfo<'_>, slug: &str) -> MyResult<UiResponse> {
    ri.require_user()?;

    let mut node = None;
    let mut children = None;
    ri.state.database.transaction(&mut |txn| {
        if let Some(row) = txn.query_single(Query {
            filter: Filter::Match(Path(vec![NO_SLUG.0]), slug.as_bytes().to_vec()),
            sort: Sort::None,
        })? {
            node = Some(Object::EMPTY.insert(NKU_NODE, txn.get(row)?.unwrap().as_object()));

            let rows = txn
                .query(Query {
                    sort: Sort::None,
                    filter: Filter::Match(Path(vec![NO_PARENT.0]), row.to_be_bytes().to_vec()),
                })?
                .collect::<Result<Vec<_>>>()?;

            children = Some(
                rows.into_iter()
                    .map(|(row, _)| {
                        Ok(Object::EMPTY.insert(NKU_NODE, txn.get(row)?.unwrap().as_object()))
                    })
                    .collect::<Result<Vec<_>>>()?,
            );
        }
        Ok(())
    })?;

    let (Some(node), Some(children)) = (node, children) else {
        Err(anyhow!("not found"))?
    };

    let children = children.iter().map(|c| c.as_object()).collect::<Vec<_>>();

    let mut children_list = Object::EMPTY.insert(NODELIST_DISPLAYSTYLE, NLSTYLE_GRID);
    children_list = children_list
        .as_object()
        .insert_multi(NODELIST_ITEM, &children);

    Ok(ri.respond_ui(
        Object::EMPTY
            .insert(VIEW_NODE_PAGE, node.as_object())
            .as_object()
            .insert(VIEW_NODE_LIST, children_list.as_object()),
    ))
}