From 1c9e1a95cfdd1394724f4edb48a5598438e0d163 Mon Sep 17 00:00:00 2001 From: Colin Reeder Date: Mon, 29 Jun 2020 23:14:29 -0600 Subject: Split posts routes into module --- src/routes/mod.rs | 264 +-------------------------------------------------- src/routes/posts.rs | 267 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 269 insertions(+), 262 deletions(-) create mode 100644 src/routes/posts.rs diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 61bba5d..6e3a7d8 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -3,6 +3,7 @@ use std::borrow::Cow; use std::sync::Arc; mod communities; +mod posts; mod r#static; const COOKIE_AGE: u32 = 60 * 60 * 24 * 365; @@ -836,242 +837,6 @@ async fn handler_new_community_submit( .body("Successfully created.".into())?) } -async fn page_post( - params: (i64,), - ctx: Arc, - req: hyper::Request, -) -> Result, crate::Error> { - let (post_id,) = params; - - let cookies = get_cookie_map_for_req(&req)?; - - let base_data = fetch_base_data(&ctx.backend_host, &ctx.http_client, &cookies).await?; - - let api_res = res_to_error( - ctx.http_client - .request(with_auth( - hyper::Request::get(format!( - "{}/api/unstable/posts/{}", - ctx.backend_host, post_id - )) - .body(Default::default())?, - &cookies, - )?) - .await?, - ) - .await?; - let api_res = hyper::body::to_bytes(api_res.into_body()).await?; - - let post: RespPostInfo = serde_json::from_slice(&api_res)?; - - Ok(html_response(render::html! { - -

{post.as_ref().title.as_ref()}

-

- {post.score.to_string()}{" points"} - {" "} - { - if base_data.login.is_some() { - Some(render::rsx! { -

- -
- }) - } else { - None - } - } -

-

- {"Submitted by "} - {" to "} -

- { - match &post.as_ref().href { - None => None, - Some(href) => { - Some(render::rsx! { -

{href.as_ref()}

- }) - } - } - } - - { - if author_is_me(&post.as_ref().author, &base_data.login) { - Some(render::rsx! { -

- {"delete"} -

- }) - } else { - None - } - } -
-

{"Comments"}

- { - if base_data.login.is_some() { - Some(render::rsx! { -
-
- -
- -
- }) - } else { - None - } - } -
    - { - post.comments.iter().map(|comment| { - render::rsx! { - - } - }).collect::>() - } -
-
-
- })) -} - -async fn page_post_delete( - params: (i64,), - ctx: Arc, - req: hyper::Request, -) -> Result, crate::Error> { - let (post_id,) = params; - - let cookies = get_cookie_map_for_req(&req)?; - - let base_data = fetch_base_data(&ctx.backend_host, &ctx.http_client, &cookies).await?; - - let api_res = res_to_error( - ctx.http_client - .request(with_auth( - hyper::Request::get(format!( - "{}/api/unstable/posts/{}", - ctx.backend_host, post_id - )) - .body(Default::default())?, - &cookies, - )?) - .await?, - ) - .await?; - let api_res = hyper::body::to_bytes(api_res.into_body()).await?; - - let post: RespPostInfo = serde_json::from_slice(&api_res)?; - - Ok(html_response(render::html! { - -

{post.as_ref().title.as_ref()}

-

{"Delete this post?"}

-
- {"No, cancel"} - {" "} - -
-
- })) -} - -async fn handler_post_delete_confirm( - params: (i64,), - ctx: Arc, - req: hyper::Request, -) -> Result, crate::Error> { - let (post_id,) = params; - - let cookies = get_cookie_map_for_req(&req)?; - - res_to_error( - ctx.http_client - .request(with_auth( - hyper::Request::delete(format!( - "{}/api/unstable/posts/{}", - ctx.backend_host, post_id, - )) - .body("".into())?, - &cookies, - )?) - .await?, - ) - .await?; - - Ok(hyper::Response::builder() - .status(hyper::StatusCode::SEE_OTHER) - .header(hyper::header::LOCATION, "/") - .body("Successfully deleted.".into())?) -} - -async fn handler_post_like( - params: (i64,), - ctx: Arc, - req: hyper::Request, -) -> Result, crate::Error> { - let (post_id,) = params; - - let cookies = get_cookie_map_for_req(&req)?; - - res_to_error( - ctx.http_client - .request(with_auth( - hyper::Request::post(format!( - "{}/api/unstable/posts/{}/like", - ctx.backend_host, post_id - )) - .body(Default::default())?, - &cookies, - )?) - .await?, - ) - .await?; - - Ok(hyper::Response::builder() - .status(hyper::StatusCode::SEE_OTHER) - .header(hyper::header::LOCATION, format!("/posts/{}", post_id)) - .body("Successfully liked.".into())?) -} - -async fn handler_post_submit_reply( - params: (i64,), - ctx: Arc, - req: hyper::Request, -) -> Result, crate::Error> { - let (post_id,) = params; - - let cookies_string = get_cookies_string(&req)?.map(ToOwned::to_owned); - let cookies_string = cookies_string.as_deref(); - let cookies = get_cookie_map(cookies_string)?; - - let body = hyper::body::to_bytes(req.into_body()).await?; - let body: serde_json::Value = serde_urlencoded::from_bytes(&body)?; - let body = serde_json::to_vec(&body)?; - - res_to_error( - ctx.http_client - .request(with_auth( - hyper::Request::post(format!( - "{}/api/unstable/posts/{}/replies", - ctx.backend_host, post_id - )) - .body(body.into())?, - &cookies, - )?) - .await?, - ) - .await?; - - Ok(hyper::Response::builder() - .status(hyper::StatusCode::SEE_OTHER) - .header(hyper::header::LOCATION, format!("/posts/{}", post_id)) - .body("Successfully posted.".into())?) -} - async fn page_signup( _: (), ctx: Arc, @@ -1229,32 +994,7 @@ pub fn route_root() -> crate::RouteNode<()> { .with_handler_async("POST", handler_new_community_submit), ), ) - .with_child( - "posts", - crate::RouteNode::new().with_child_parse::( - crate::RouteNode::new() - .with_handler_async("GET", page_post) - .with_child( - "delete", - crate::RouteNode::new() - .with_handler_async("GET", page_post_delete) - .with_child( - "confirm", - crate::RouteNode::new() - .with_handler_async("POST", handler_post_delete_confirm), - ), - ) - .with_child( - "like", - crate::RouteNode::new().with_handler_async("POST", handler_post_like), - ) - .with_child( - "submit_reply", - crate::RouteNode::new() - .with_handler_async("POST", handler_post_submit_reply), - ), - ), - ) + .with_child("posts", posts::route_posts()) .with_child( "signup", crate::RouteNode::new() diff --git a/src/routes/posts.rs b/src/routes/posts.rs new file mode 100644 index 0000000..8efde41 --- /dev/null +++ b/src/routes/posts.rs @@ -0,0 +1,267 @@ +use super::{ + author_is_me, fetch_base_data, get_cookie_map, get_cookie_map_for_req, get_cookies_string, + html_response, res_to_error, with_auth, Comment, CommunityLink, Content, HTPage, RespPostInfo, + UserLink, +}; +use std::sync::Arc; + +async fn page_post( + params: (i64,), + ctx: Arc, + req: hyper::Request, +) -> Result, crate::Error> { + let (post_id,) = params; + + let cookies = get_cookie_map_for_req(&req)?; + + let base_data = fetch_base_data(&ctx.backend_host, &ctx.http_client, &cookies).await?; + + let api_res = res_to_error( + ctx.http_client + .request(with_auth( + hyper::Request::get(format!( + "{}/api/unstable/posts/{}", + ctx.backend_host, post_id + )) + .body(Default::default())?, + &cookies, + )?) + .await?, + ) + .await?; + let api_res = hyper::body::to_bytes(api_res.into_body()).await?; + + let post: RespPostInfo = serde_json::from_slice(&api_res)?; + + Ok(html_response(render::html! { + +

{post.as_ref().title.as_ref()}

+

+ {post.score.to_string()}{" points"} + {" "} + { + if base_data.login.is_some() { + Some(render::rsx! { +

+ +
+ }) + } else { + None + } + } +

+

+ {"Submitted by "} + {" to "} +

+ { + match &post.as_ref().href { + None => None, + Some(href) => { + Some(render::rsx! { +

{href.as_ref()}

+ }) + } + } + } + + { + if author_is_me(&post.as_ref().author, &base_data.login) { + Some(render::rsx! { +

+ {"delete"} +

+ }) + } else { + None + } + } +
+

{"Comments"}

+ { + if base_data.login.is_some() { + Some(render::rsx! { +
+
+ +
+ +
+ }) + } else { + None + } + } +
    + { + post.comments.iter().map(|comment| { + render::rsx! { + + } + }).collect::>() + } +
+
+
+ })) +} + +async fn page_post_delete( + params: (i64,), + ctx: Arc, + req: hyper::Request, +) -> Result, crate::Error> { + let (post_id,) = params; + + let cookies = get_cookie_map_for_req(&req)?; + + let base_data = fetch_base_data(&ctx.backend_host, &ctx.http_client, &cookies).await?; + + let api_res = res_to_error( + ctx.http_client + .request(with_auth( + hyper::Request::get(format!( + "{}/api/unstable/posts/{}", + ctx.backend_host, post_id + )) + .body(Default::default())?, + &cookies, + )?) + .await?, + ) + .await?; + let api_res = hyper::body::to_bytes(api_res.into_body()).await?; + + let post: RespPostInfo = serde_json::from_slice(&api_res)?; + + Ok(html_response(render::html! { + +

{post.as_ref().title.as_ref()}

+

{"Delete this post?"}

+
+ {"No, cancel"} + {" "} + +
+
+ })) +} + +async fn handler_post_delete_confirm( + params: (i64,), + ctx: Arc, + req: hyper::Request, +) -> Result, crate::Error> { + let (post_id,) = params; + + let cookies = get_cookie_map_for_req(&req)?; + + res_to_error( + ctx.http_client + .request(with_auth( + hyper::Request::delete(format!( + "{}/api/unstable/posts/{}", + ctx.backend_host, post_id, + )) + .body("".into())?, + &cookies, + )?) + .await?, + ) + .await?; + + Ok(hyper::Response::builder() + .status(hyper::StatusCode::SEE_OTHER) + .header(hyper::header::LOCATION, "/") + .body("Successfully deleted.".into())?) +} + +async fn handler_post_like( + params: (i64,), + ctx: Arc, + req: hyper::Request, +) -> Result, crate::Error> { + let (post_id,) = params; + + let cookies = get_cookie_map_for_req(&req)?; + + res_to_error( + ctx.http_client + .request(with_auth( + hyper::Request::post(format!( + "{}/api/unstable/posts/{}/like", + ctx.backend_host, post_id + )) + .body(Default::default())?, + &cookies, + )?) + .await?, + ) + .await?; + + Ok(hyper::Response::builder() + .status(hyper::StatusCode::SEE_OTHER) + .header(hyper::header::LOCATION, format!("/posts/{}", post_id)) + .body("Successfully liked.".into())?) +} + +async fn handler_post_submit_reply( + params: (i64,), + ctx: Arc, + req: hyper::Request, +) -> Result, crate::Error> { + let (post_id,) = params; + + let cookies_string = get_cookies_string(&req)?.map(ToOwned::to_owned); + let cookies_string = cookies_string.as_deref(); + let cookies = get_cookie_map(cookies_string)?; + + let body = hyper::body::to_bytes(req.into_body()).await?; + let body: serde_json::Value = serde_urlencoded::from_bytes(&body)?; + let body = serde_json::to_vec(&body)?; + + res_to_error( + ctx.http_client + .request(with_auth( + hyper::Request::post(format!( + "{}/api/unstable/posts/{}/replies", + ctx.backend_host, post_id + )) + .body(body.into())?, + &cookies, + )?) + .await?, + ) + .await?; + + Ok(hyper::Response::builder() + .status(hyper::StatusCode::SEE_OTHER) + .header(hyper::header::LOCATION, format!("/posts/{}", post_id)) + .body("Successfully posted.".into())?) +} + +pub fn route_posts() -> crate::RouteNode<()> { + crate::RouteNode::new().with_child_parse::( + crate::RouteNode::new() + .with_handler_async("GET", page_post) + .with_child( + "delete", + crate::RouteNode::new() + .with_handler_async("GET", page_post_delete) + .with_child( + "confirm", + crate::RouteNode::new() + .with_handler_async("POST", handler_post_delete_confirm), + ), + ) + .with_child( + "like", + crate::RouteNode::new().with_handler_async("POST", handler_post_like), + ) + .with_child( + "submit_reply", + crate::RouteNode::new().with_handler_async("POST", handler_post_submit_reply), + ), + ) +} -- cgit v1.2.3