~vpzom/hitide

03f0616a7c2b00ac0e47247795308c0a35a12e88 — Colin Reeder a month ago 5a0cd2b
Add UI for removing community moderators
2 files changed, 67 insertions(+), 1 deletions(-)

M res/lang/en.ftl
M src/routes/communities.rs
M res/lang/en.ftl => res/lang/en.ftl +1 -0
@@ 59,6 59,7 @@ post_not_approved = This post has not been approved by the community.
preview = Preview
register = Register
remote = Remote
remove = remove
reply = reply
reply_submit = Reply
reply_to = Reply to

M src/routes/communities.rs => src/routes/communities.rs +66 -1
@@ 478,7 478,24 @@ async fn page_community_moderators(
                {
                    api_res.iter().map(|user| {
                        render::rsx! {
                            <li><a href={format!("/users/{}", user.id)}>{user.username.as_ref()}</a></li>
                            <li>
                                <a href={format!("/users/{}", user.id)}>{user.username.as_ref()}</a>
                                {
                                    if community_info.you_are_moderator == Some(true) {
                                        Some(render::rsx! {
                                            <>
                                                {" "}
                                                <form class={"inline"} method={"POST"} action={format!("/communities/{}/moderators/remove", community_id)}>
                                                    <input type={"hidden"} name={"user"} value={user.id.to_string()} />
                                                    <button type={"submit"}>{lang.tr("remove", None)}</button>
                                                </form>
                                            </>
                                        })
                                    } else {
                                        None
                                    }
                                }
                            </li>
                        }
                    })
                    .collect::<Vec<_>>()


@@ 550,6 567,49 @@ async fn handler_community_moderators_add(
        .body("Successfully added.".into())?)
}

async fn handler_community_moderators_remove(
    params: (i64,),
    ctx: Arc<crate::RouteContext>,
    req: hyper::Request<hyper::Body>,
) -> Result<hyper::Response<hyper::Body>, crate::Error> {
    let (community_id,) = params;

    let (req_parts, body) = req.into_parts();

    let cookies = get_cookie_map_for_headers(&req_parts.headers)?;

    #[derive(Deserialize)]
    struct ModeratorsRemoveParams {
        user: i64,
    }

    let body = hyper::body::to_bytes(body).await?;
    let body: ModeratorsRemoveParams = serde_urlencoded::from_bytes(&body)?;

    res_to_error(
        ctx.http_client
            .request(for_client(
                hyper::Request::delete(format!(
                    "{}/api/unstable/communities/{}/moderators/{}",
                    ctx.backend_host, community_id, body.user,
                ))
                .body(Default::default())?,
                &req_parts.headers,
                &cookies,
            )?)
            .await?,
    )
    .await?;

    Ok(hyper::Response::builder()
        .status(hyper::StatusCode::SEE_OTHER)
        .header(
            hyper::header::LOCATION,
            format!("/communities/{}/moderators", community_id),
        )
        .body("Successfully removed.".into())?)
}

async fn handler_community_post_approve(
    params: (i64, i64),
    ctx: Arc<crate::RouteContext>,


@@ 868,6 928,11 @@ pub fn route_communities() -> crate::RouteNode<()> {
                            "add",
                            crate::RouteNode::new()
                                .with_handler_async("POST", handler_community_moderators_add),
                        )
                        .with_child(
                            "remove",
                            crate::RouteNode::new()
                                .with_handler_async("POST", handler_community_moderators_remove),
                        ),
                )
                .with_child(