diff options
author | Colin Reeder <colin@vpzom.click> | 2020-07-12 22:48:59 -0600 |
---|---|---|
committer | Colin Reeder <colin@vpzom.click> | 2020-07-12 22:48:59 -0600 |
commit | 44e988eb6cbcf8fdc25f89deb16ddab8e9ee7bfc (patch) | |
tree | 47ec99e5d74fb28ab6855031e298076e21cdae8c | |
parent | 46aaf2ca30384e05f9a387097078076103f7116c (diff) |
Split up home and all, add messages for empty result
-rw-r--r-- | src/components/mod.rs | 1 | ||||
-rw-r--r-- | src/routes/mod.rs | 84 |
2 files changed, 79 insertions, 6 deletions
diff --git a/src/components/mod.rs b/src/components/mod.rs index a44bfb8..ffa7756 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -177,6 +177,7 @@ pub fn HTPage<'a, Children: render::Render>( <header class={"mainHeader"}> <div class={"left actionList"}> <a href={"/"} class={"siteName"}>{"lotide"}</a> + <a href={"/all"}>{"All"}</a> <a href={"/communities"}>{"Communities"}</a> <a href={"/about"}>{"About"}</a> </div> diff --git a/src/routes/mod.rs b/src/routes/mod.rs index c27b4a2..93e667b 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -921,16 +921,17 @@ async fn page_home( let base_data = fetch_base_data(&ctx.backend_host, &ctx.http_client, &cookies).await?; - let for_user = base_data.login.is_some(); + if base_data.login.is_none() { + return page_all_inner(&cookies, &base_data, ctx).await; + } let api_res = res_to_error( ctx.http_client .request(with_auth( - hyper::Request::get(if for_user { - format!("{}/api/unstable/users/me/following:posts", ctx.backend_host,) - } else { - format!("{}/api/unstable/posts", ctx.backend_host,) - }) + hyper::Request::get(format!( + "{}/api/unstable/users/me/following:posts", + ctx.backend_host + )) .body(Default::default())?, &cookies, )?) @@ -943,6 +944,73 @@ async fn page_home( Ok(html_response(render::html! { <HTPage base_data={&base_data} title={"lotide"}> + { + if api_res.is_empty() { + Some(render::rsx! { + <p> + {"Looks like there's nothing here. Why not "} + <a href={"/communities"}>{"follow some communities"}</a> + {"?"} + </p> + }) + } else { + None + } + } + <ul> + {api_res.iter().map(|post| { + PostItem { post, in_community: false } + }).collect::<Vec<_>>()} + </ul> + </HTPage> + })) +} + +async fn page_all( + _: (), + 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?; + + page_all_inner(&cookies, &base_data, ctx).await +} + +async fn page_all_inner( + cookies: &CookieMap<'_>, + base_data: &crate::PageBaseData, + ctx: Arc<crate::RouteContext>, +) -> Result<hyper::Response<hyper::Body>, crate::Error> { + let api_res = res_to_error( + ctx.http_client + .request(with_auth( + hyper::Request::get(format!("{}/api/unstable/posts", ctx.backend_host)) + .body(Default::default())?, + &cookies, + )?) + .await?, + ) + .await?; + + let api_res = hyper::body::to_bytes(api_res.into_body()).await?; + let api_res: Vec<RespPostListPost<'_>> = serde_json::from_slice(&api_res)?; + + Ok(html_response(render::html! { + <HTPage base_data={&base_data} title={"lotide"}> + <h1>{"The Whole Known Network"}</h1> + { + if api_res.is_empty() { + Some(render::rsx! { + <p> + {"Looks like there's nothing here (yet!)."} + </p> + }) + } else { + None + } + } <ul> {api_res.iter().map(|post| { PostItem { post, in_community: false } @@ -960,6 +1028,10 @@ pub fn route_root() -> crate::RouteNode<()> { crate::RouteNode::new().with_handler_async("GET", page_about), ) .with_child( + "all", + crate::RouteNode::new().with_handler_async("GET", page_all), + ) + .with_child( "comments", crate::RouteNode::new().with_child_parse::<i64, _>( crate::RouteNode::new() |