~vpzom/hitide

a8b43334cc9f26f98abca82366ad8b5da58d093c — Colin Reeder 1 year, 5 months ago 6201f0e
Introduce lookup form
4 files changed, 92 insertions(+), 0 deletions(-)

M Cargo.lock
M Cargo.toml
M src/routes/communities.rs
M src/routes/mod.rs
M Cargo.lock => Cargo.lock +7 -0
@@ 214,6 214,7 @@ dependencies = [
 "serde_urlencoded",
 "tokio",
 "trout",
 "urlencoding",
]

[[package]]


@@ 1019,6 1020,12 @@ dependencies = [
]

[[package]]
name = "urlencoding"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9232eb53352b4442e40d7900465dfc534e8cb2dc8f18656fcb2ac16112b5593"

[[package]]
name = "utf-8"
version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"

M Cargo.toml => Cargo.toml +1 -0
@@ 20,3 20,4 @@ serde = "1.0.111"
fallible-iterator = "0.2.0"
ginger = "0.1.0"
ammonia = "3.1.0"
urlencoding = "1.1.1"

M src/routes/communities.rs => src/routes/communities.rs +8 -0
@@ 52,6 52,14 @@ async fn page_communities(
            </div>
            <div>
                <h2>{"Remote"}</h2>
                <form method={"GET"} action={"/lookup"}>
                    <label>
                        {"Add by ID: "}
                        <input r#type={"text"} name={"query"} placeholder={"group@example.com"} />
                    </label>
                    {" "}
                    <button r#type={"submit"}>{"Fetch"}</button>
                </form>
                <ul>
                    {
                        communities.iter()

M src/routes/mod.rs => src/routes/mod.rs +76 -0
@@ 648,6 648,78 @@ async fn handler_login_submit(
        .body("Successfully logged in.".into())?)
}

async fn page_lookup(
    _: (),
    ctx: Arc<crate::RouteContext>,
    req: hyper::Request<hyper::Body>,
) -> Result<hyper::Response<hyper::Body>, crate::Error> {
    let cookies = get_cookie_map_for_req(&req)?;
    let base_data = fetch_base_data(&ctx.backend_host, &ctx.http_client, &cookies).await?;

    #[derive(Deserialize)]
    struct LookupQuery<'a> {
        query: Option<Cow<'a, str>>,
    }

    let query: LookupQuery<'_> = serde_urlencoded::from_str(req.uri().query().unwrap_or(""))?;
    let query = query.query;

    #[derive(Deserialize)]
    struct LookupResult {
        id: i64,
    }

    let api_res: Option<Vec<LookupResult>> = if let Some(query) = &query {
        let api_res = res_to_error(
            ctx.http_client
                .request(
                    hyper::Request::get(format!(
                        "{}/api/unstable/actors:lookup/{}",
                        ctx.backend_host,
                        urlencoding::encode(&query)
                    ))
                    .body(Default::default())?,
                )
                .await?,
        )
        .await?;

        let api_res = hyper::body::to_bytes(api_res.into_body()).await?;
        Some(serde_json::from_slice(&api_res)?)
    } else {
        None
    };

    match api_res {
        Some(items) if !items.is_empty() => Ok(hyper::Response::builder()
            .status(hyper::StatusCode::FOUND)
            .header(
                hyper::header::LOCATION,
                format!("/communities/{}", items[0].id),
            )
            .body("Redirecting…".into())?),
        _ => {
            Ok(html_response(render::html! {
                <HTPage base_data={&base_data}>
                    <h1>{"Lookup"}</h1>
                    <form method={"GET"} action={"/lookup"}>
                        <input r#type={"text"} name={"query"} value={query.as_deref().unwrap_or("")} />
                    </form>
                    {
                        match api_res {
                            None => None,
                            Some(_) => {
                                // non-empty case is handled above
                                Some(render::rsx! { <p>{"Nothing found."}</p> })
                            },
                        }
                    }
                </HTPage>
            }))
        }
    }
}

async fn page_new_community(
    _: (),
    ctx: Arc<crate::RouteContext>,


@@ 1053,6 1125,10 @@ pub fn route_root() -> crate::RouteNode<()> {
                ),
        )
        .with_child(
            "lookup",
            crate::RouteNode::new().with_handler_async("GET", page_lookup),
        )
        .with_child(
            "new_community",
            crate::RouteNode::new()
                .with_handler_async("GET", page_new_community)