aboutsummaryrefslogtreecommitdiff
path: root/server/src/routes/search.rs
blob: 6e7bab1d77ea60a5be496cae1bb6c0f06c49994e (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
/*
    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, responders::UiPage, routes::node::create_nku};
use jellycommon::{jellyobject::Path, *};
use jellydb::{Filter, Query, Sort};
use jellyui::components::search::Search;
use log::info;
use rocket::get;
use std::time::Instant;

#[get("/search?<q>")]
pub async fn r_search(ri: RequestInfo<'_>, q: Option<&str>) -> MyResult<UiPage> {
    ri.require_user()?;

    let mut items = Vec::new();
    if let Some(q) = q {
        let t = Instant::now();
        ri.state.database.transaction(&mut |txn| {
            let rows = txn
                .query(Query {
                    filter: Filter::Match(Path(vec![NO_VISIBILITY.0]), VISI_VISIBLE.into()),
                    sort: Sort::TextSearch(
                        vec![
                            Path(vec![NO_TITLE.0]),
                            Path(vec![NO_DESCRIPTION.0]),
                            Path(vec![NO_TAGLINE.0]),
                            Path(vec![NO_SUBTITLE.0]),
                        ],
                        q.to_owned(),
                    ),
                    ..Default::default()
                })?
                .take(64)
                .collect::<Result<Vec<_>, _>>()?;

            items.clear();
            for (row, _is) in rows {
                items.push(create_nku(txn, row)?);
            }
            Ok(())
        })?;
        info!("search {q:?} took {:?}", t.elapsed());
    }

    Ok(ri.respond_ui(&Search {
        query: q.unwrap_or_default(),
        items: &items,
        ri: &ri.render_info(),
        cont: None,
    }))
}