summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorColin Reeder <colin@vpzom.click>2020-06-27 22:31:26 -0600
committerColin Reeder <colin@vpzom.click>2020-06-27 22:31:26 -0600
commita8b43334cc9f26f98abca82366ad8b5da58d093c (patch)
treeb3d17c4bd64a542d4f270edf85f3b1cd7e51a3eb
parent6201f0eb7ec03dd4821d76f8561ec64f54852390 (diff)
Introduce lookup form
-rw-r--r--Cargo.lock7
-rw-r--r--Cargo.toml1
-rw-r--r--src/routes/communities.rs8
-rw-r--r--src/routes/mod.rs76
4 files changed, 92 insertions, 0 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 685d7d2..166551a 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -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"
diff --git a/Cargo.toml b/Cargo.toml
index b851858..7360a5f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -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"
diff --git a/src/routes/communities.rs b/src/routes/communities.rs
index f8d1415..886a21f 100644
--- a/src/routes/communities.rs
+++ b/src/routes/communities.rs
@@ -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()
diff --git a/src/routes/mod.rs b/src/routes/mod.rs
index 082a616..60c6968 100644
--- a/src/routes/mod.rs
+++ b/src/routes/mod.rs
@@ -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)