aboutsummaryrefslogtreecommitdiff
path: root/server/src/routes/ui/search.rs
blob: d020e2e089fc710e4f38096512cf49007ce9f2d8 (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
use super::{
    account::session::Session,
    error::MyResult,
    layout::{DynLayoutPage, LayoutPage},
    node::{DatabaseNodeUserDataExt, NodeCard},
};
use jellybase::database::Database;
use jellycommon::Visibility;
use rocket::{get, State};
use std::time::Instant;

#[get("/search?<query>&<page>")]
pub async fn r_search<'a>(
    session: Session,
    db: &State<Database>,
    query: Option<&str>,
    page: Option<usize>,
) -> MyResult<DynLayoutPage<'a>> {
    let timing = Instant::now();
    let results = if let Some(query) = query {
        let (count, ids) = db.search(query, 32, page.unwrap_or_default() * 32)?;
        let mut nodes = ids
            .into_iter()
            .map(|id| db.get_node_with_userdata(id, &session))
            .collect::<Result<Vec<_>, anyhow::Error>>()?;
        nodes.retain(|(n, _)| n.visibility >= Visibility::Reduced);
        Some((count, nodes))
    } else {
        None
    };
    let search_dur = timing.elapsed();
    let query = query.unwrap_or_default().to_string();

    Ok(LayoutPage {
        title: "Search".to_string(),
        class: Some("search"),
        content: markup::new! {
            h1 { "Search" }
            form[action="", method="GET"] {
                input[type="text", name="query", placeholder="Search Term", value=&query];
                input[type="submit", value="Search"];
            }
            @if let Some((count, results)) = &results {
                h2 { "Results" }
                p.stats { @format!("Found {count} nodes in {search_dur:?}.") }
                ul.children {@for (node, udata) in results.iter() {
                    li { @NodeCard { node, udata } }
                }}
                // TODO pagination
            }
        },
    })
}