diff options
-rw-r--r-- | res/lang/en.ftl | 2 | ||||
-rw-r--r-- | res/lang/eo.ftl | 2 | ||||
-rw-r--r-- | src/routes/communities.rs | 76 | ||||
-rw-r--r-- | src/routes/posts.rs | 46 |
4 files changed, 124 insertions, 2 deletions
diff --git a/res/lang/en.ftl b/res/lang/en.ftl index 2040948..e475611 100644 --- a/res/lang/en.ftl +++ b/res/lang/en.ftl @@ -48,6 +48,8 @@ on = on on_your_post = on your post or_start = Or password_prompt = Password: +post_approve = Approve +post_approve_undo = Remove from Community post_delete_question = Delete this post? post_delete_title = Delete Post post_likes_nothing = Looks like nobody has liked this post yet. diff --git a/res/lang/eo.ftl b/res/lang/eo.ftl index e62db7c..3ea23e0 100644 --- a/res/lang/eo.ftl +++ b/res/lang/eo.ftl @@ -48,6 +48,8 @@ on = sur on_your_post = sur via poŝto or_start = Aŭ password_prompt = Pasvorto: +post_approve = Aprobi +post_approve_undo = Forpreni el komunumo post_delete_question = Ĉu vi volas forigi ĉi tiun poŝton? post_delete_title = Forigi Poŝton post_likes_nothing = Ŝajnas, ke neniu ankoraŭ ŝatis ĉi tion poŝton. diff --git a/src/routes/communities.rs b/src/routes/communities.rs index 7a1e906..4ffefce 100644 --- a/src/routes/communities.rs +++ b/src/routes/communities.rs @@ -414,6 +414,66 @@ async fn handler_community_follow( .body("Successfully followed".into())?) } +async fn handler_community_post_approve( + params: (i64, i64), + ctx: Arc<crate::RouteContext>, + req: hyper::Request<hyper::Body>, +) -> Result<hyper::Response<hyper::Body>, crate::Error> { + let (community_id, post_id) = params; + + let cookies = get_cookie_map_for_req(&req)?; + + res_to_error( + ctx.http_client + .request(for_client( + hyper::Request::patch(format!( + "{}/api/unstable/communities/{}/posts/{}", + ctx.backend_host, community_id, post_id + )) + .body("{\"approved\": true}".into())?, + req.headers(), + &cookies, + )?) + .await?, + ) + .await?; + + Ok(hyper::Response::builder() + .status(hyper::StatusCode::SEE_OTHER) + .header(hyper::header::LOCATION, format!("/posts/{}", post_id)) + .body("Successfully approved.".into())?) +} + +async fn handler_community_post_unapprove( + params: (i64, i64), + ctx: Arc<crate::RouteContext>, + req: hyper::Request<hyper::Body>, +) -> Result<hyper::Response<hyper::Body>, crate::Error> { + let (community_id, post_id) = params; + + let cookies = get_cookie_map_for_req(&req)?; + + res_to_error( + ctx.http_client + .request(for_client( + hyper::Request::patch(format!( + "{}/api/unstable/communities/{}/posts/{}", + ctx.backend_host, community_id, post_id + )) + .body("{\"approved\": false}".into())?, + req.headers(), + &cookies, + )?) + .await?, + ) + .await?; + + Ok(hyper::Response::builder() + .status(hyper::StatusCode::SEE_OTHER) + .header(hyper::header::LOCATION, format!("/posts/{}", post_id)) + .body("Successfully unapproved.".into())?) +} + async fn handler_community_unfollow( params: (i64,), ctx: Arc<crate::RouteContext>, @@ -599,6 +659,22 @@ pub fn route_communities() -> crate::RouteNode<()> { crate::RouteNode::new().with_handler_async("POST", handler_community_follow), ) .with_child( + "posts", + crate::RouteNode::new().with_child_parse::<i64, _>( + crate::RouteNode::new() + .with_child( + "approve", + crate::RouteNode::new() + .with_handler_async("POST", handler_community_post_approve), + ) + .with_child( + "unapprove", + crate::RouteNode::new() + .with_handler_async("POST", handler_community_post_unapprove), + ), + ), + ) + .with_child( "unfollow", crate::RouteNode::new().with_handler_async("POST", handler_community_unfollow), ) diff --git a/src/routes/posts.rs b/src/routes/posts.rs index 2f26686..7bb85bf 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -3,7 +3,7 @@ use super::{ res_to_error, }; use crate::components::{Comment, CommunityLink, Content, HTPage, TimeAgo, UserLink}; -use crate::resp_types::{JustUser, RespList, RespPostInfo}; +use crate::resp_types::{JustUser, RespCommunityInfoMaybeYour, RespList, RespPostInfo}; use crate::util::author_is_me; use std::sync::Arc; @@ -41,9 +41,32 @@ async fn page_post( ) .await?; let api_res = hyper::body::to_bytes(api_res.into_body()).await?; - let post: RespPostInfo = serde_json::from_slice(&api_res)?; + let is_community_moderator = if base_data.login.is_some() { + let api_res = res_to_error( + ctx.http_client + .request(for_client( + hyper::Request::get(format!( + "{}/api/unstable/communities/{}?include_your=true", + ctx.backend_host, + post.as_ref().community.id, + )) + .body(Default::default())?, + req.headers(), + &cookies, + )?) + .await?, + ) + .await?; + let api_res = hyper::body::to_bytes(api_res.into_body()).await?; + + let info: RespCommunityInfoMaybeYour = serde_json::from_slice(&api_res)?; + info.you_are_moderator.unwrap() + } else { + false + }; + let title = post.as_ref().as_ref().title.as_ref(); Ok(html_response(render::html! { @@ -80,6 +103,25 @@ async fn page_post( None } } + { + if is_community_moderator { + Some(if post.approved { + render::rsx! { + <form method={"POST"} action={format!("/communities/{}/posts/{}/unapprove", post.as_ref().community.id, post_id)}> + <button type={"submit"}>{lang.tr("post_approve_undo", None)}</button> + </form> + } + } else { + render::rsx! { + <form method={"POST"} action={format!("/communities/{}/posts/{}/approve", post.as_ref().community.id, post_id)}> + <button type={"submit"}>{lang.tr("post_approve", None)}</button> + </form> + } + }) + } else { + None + } + } </p> <p> {lang.tr("submitted", None)} |