summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorColin Reeder <colin@vpzom.click>2020-06-29 23:14:29 -0600
committerColin Reeder <colin@vpzom.click>2020-06-29 23:14:29 -0600
commit1c9e1a95cfdd1394724f4edb48a5598438e0d163 (patch)
tree20e16e2fb55ee42615fe0345cff83cf041b0d981
parentd48254305a71ac734353ff22f9e08c9584e9b01a (diff)
Split posts routes into module
-rw-r--r--src/routes/mod.rs264
-rw-r--r--src/routes/posts.rs267
2 files changed, 269 insertions, 262 deletions
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<crate::RouteContext>,
- req: hyper::Request<hyper::Body>,
-) -> Result<hyper::Response<hyper::Body>, 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! {
- <HTPage base_data={&base_data}>
- <h1>{post.as_ref().title.as_ref()}</h1>
- <p>
- <em>{post.score.to_string()}{" points"}</em>
- {" "}
- {
- if base_data.login.is_some() {
- Some(render::rsx! {
- <form method={"POST"} action={format!("/posts/{}/like", post_id)}>
- <button r#type={"submit"}>{"Like"}</button>
- </form>
- })
- } else {
- None
- }
- }
- </p>
- <p>
- {"Submitted by "}<UserLink user={post.as_ref().author.as_ref()} />
- {" to "}<CommunityLink community={&post.as_ref().community} />
- </p>
- {
- match &post.as_ref().href {
- None => None,
- Some(href) => {
- Some(render::rsx! {
- <p><a href={href.as_ref()}>{href.as_ref()}</a></p>
- })
- }
- }
- }
- <Content src={post.as_ref()} />
- {
- if author_is_me(&post.as_ref().author, &base_data.login) {
- Some(render::rsx! {
- <p>
- <a href={format!("/posts/{}/delete", post_id)}>{"delete"}</a>
- </p>
- })
- } else {
- None
- }
- }
- <div>
- <h2>{"Comments"}</h2>
- {
- if base_data.login.is_some() {
- Some(render::rsx! {
- <form method={"POST"} action={format!("/posts/{}/submit_reply", post.as_ref().id)}>
- <div>
- <textarea name={"content_text"}>{()}</textarea>
- </div>
- <button r#type={"submit"}>{"Post Comment"}</button>
- </form>
- })
- } else {
- None
- }
- }
- <ul>
- {
- post.comments.iter().map(|comment| {
- render::rsx! {
- <Comment comment={comment} base_data={&base_data} />
- }
- }).collect::<Vec<_>>()
- }
- </ul>
- </div>
- </HTPage>
- }))
-}
-
-async fn page_post_delete(
- params: (i64,),
- ctx: Arc<crate::RouteContext>,
- req: hyper::Request<hyper::Body>,
-) -> Result<hyper::Response<hyper::Body>, 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! {
- <HTPage base_data={&base_data}>
- <h1>{post.as_ref().title.as_ref()}</h1>
- <h2>{"Delete this post?"}</h2>
- <form method={"POST"} action={format!("/posts/{}/delete/confirm", post.as_ref().id)}>
- <a href={format!("/posts/{}/", post.as_ref().id)}>{"No, cancel"}</a>
- {" "}
- <button r#type={"submit"}>{"Yes, delete"}</button>
- </form>
- </HTPage>
- }))
-}
-
-async fn handler_post_delete_confirm(
- params: (i64,),
- ctx: Arc<crate::RouteContext>,
- req: hyper::Request<hyper::Body>,
-) -> Result<hyper::Response<hyper::Body>, 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<crate::RouteContext>,
- req: hyper::Request<hyper::Body>,
-) -> Result<hyper::Response<hyper::Body>, 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<crate::RouteContext>,
- req: hyper::Request<hyper::Body>,
-) -> Result<hyper::Response<hyper::Body>, 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<crate::RouteContext>,
@@ -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::<i64, _>(
- 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<crate::RouteContext>,
+ req: hyper::Request<hyper::Body>,
+) -> Result<hyper::Response<hyper::Body>, 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! {
+ <HTPage base_data={&base_data}>
+ <h1>{post.as_ref().title.as_ref()}</h1>
+ <p>
+ <em>{post.score.to_string()}{" points"}</em>
+ {" "}
+ {
+ if base_data.login.is_some() {
+ Some(render::rsx! {
+ <form method={"POST"} action={format!("/posts/{}/like", post_id)}>
+ <button r#type={"submit"}>{"Like"}</button>
+ </form>
+ })
+ } else {
+ None
+ }
+ }
+ </p>
+ <p>
+ {"Submitted by "}<UserLink user={post.as_ref().author.as_ref()} />
+ {" to "}<CommunityLink community={&post.as_ref().community} />
+ </p>
+ {
+ match &post.as_ref().href {
+ None => None,
+ Some(href) => {
+ Some(render::rsx! {
+ <p><a href={href.as_ref()}>{href.as_ref()}</a></p>
+ })
+ }
+ }
+ }
+ <Content src={post.as_ref()} />
+ {
+ if author_is_me(&post.as_ref().author, &base_data.login) {
+ Some(render::rsx! {
+ <p>
+ <a href={format!("/posts/{}/delete", post_id)}>{"delete"}</a>
+ </p>
+ })
+ } else {
+ None
+ }
+ }
+ <div>
+ <h2>{"Comments"}</h2>
+ {
+ if base_data.login.is_some() {
+ Some(render::rsx! {
+ <form method={"POST"} action={format!("/posts/{}/submit_reply", post.as_ref().id)}>
+ <div>
+ <textarea name={"content_text"}>{()}</textarea>
+ </div>
+ <button r#type={"submit"}>{"Post Comment"}</button>
+ </form>
+ })
+ } else {
+ None
+ }
+ }
+ <ul>
+ {
+ post.comments.iter().map(|comment| {
+ render::rsx! {
+ <Comment comment={comment} base_data={&base_data} />
+ }
+ }).collect::<Vec<_>>()
+ }
+ </ul>
+ </div>
+ </HTPage>
+ }))
+}
+
+async fn page_post_delete(
+ params: (i64,),
+ ctx: Arc<crate::RouteContext>,
+ req: hyper::Request<hyper::Body>,
+) -> Result<hyper::Response<hyper::Body>, 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! {
+ <HTPage base_data={&base_data}>
+ <h1>{post.as_ref().title.as_ref()}</h1>
+ <h2>{"Delete this post?"}</h2>
+ <form method={"POST"} action={format!("/posts/{}/delete/confirm", post.as_ref().id)}>
+ <a href={format!("/posts/{}/", post.as_ref().id)}>{"No, cancel"}</a>
+ {" "}
+ <button r#type={"submit"}>{"Yes, delete"}</button>
+ </form>
+ </HTPage>
+ }))
+}
+
+async fn handler_post_delete_confirm(
+ params: (i64,),
+ ctx: Arc<crate::RouteContext>,
+ req: hyper::Request<hyper::Body>,
+) -> Result<hyper::Response<hyper::Body>, 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<crate::RouteContext>,
+ req: hyper::Request<hyper::Body>,
+) -> Result<hyper::Response<hyper::Body>, 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<crate::RouteContext>,
+ req: hyper::Request<hyper::Body>,
+) -> Result<hyper::Response<hyper::Body>, 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::<i64, _>(
+ 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),
+ ),
+ )
+}