aboutsummaryrefslogtreecommitdiff
path: root/logic/src/items.rs
blob: c618b9b6cf27989b1642aba3c566a1e0f4800415 (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
/*
    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::{DATABASE, filter_sort::filter_and_sort_nodes, session::Session};
use anyhow::Result;

pub fn all_items(
    session: &Session,
    page: Option<usize>,
    filter: NodeFilterSort,
) -> Result<ApiItemsResponse> {
    let mut items = DATABASE.list_nodes_with_udata(session.user.name.as_str())?;

    items.retain(|(n, _)| matches!(n.visibility, Visibility::Visible));

    filter_and_sort_nodes(
        &filter,
        (SortProperty::Title, SortOrder::Ascending),
        &mut items,
    );

    let page_size = 100;
    let page = page.unwrap_or(0);
    let offset = page * page_size;
    let from = offset.min(items.len());
    let to = (offset + page_size).min(items.len());
    let max_page = items.len().div_ceil(page_size);
    Ok(ApiItemsResponse {
        count: items.len(),
        pages: max_page,
        items: items[from..to].to_vec(),
    })
}