~vpzom/hitide

e2e37561e002fd7dbf9087a8721198ca335d33d4 — Colin Reeder 10 days ago da14414
Add UI for post removal
4 files changed, 124 insertions(+), 2 deletions(-)

M res/lang/en.ftl
M res/lang/eo.ftl
M src/routes/communities.rs
M src/routes/posts.rs
M res/lang/en.ftl => res/lang/en.ftl +2 -0
@@ 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.

M res/lang/eo.ftl => res/lang/eo.ftl +2 -0
@@ 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.

M src/routes/communities.rs => src/routes/communities.rs +76 -0
@@ 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),
                )

M src/routes/posts.rs => src/routes/posts.rs +44 -2
@@ 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)}