~vpzom/hitide

d09c97c6840bff1ffe8495fa59e4924157d58f3b — Colin Reeder 17 days ago 63e2996 master
Add UI for making posts sticky
5 files changed, 109 insertions(+), 14 deletions(-)

M res/lang/en.ftl
M src/components/mod.rs
M src/resp_types.rs
M src/routes/communities.rs
M src/routes/posts.rs
M res/lang/en.ftl => res/lang/en.ftl +2 -0
@@ 70,6 70,8 @@ post_comments_count =
post_delete_question = Delete this post?
post_delete_title = Delete Post
post_likes_nothing = Looks like nobody has liked this post yet.
post_make_sticky = Sticky
post_make_not_sticky = Unsticky
post_new = New Post
post_new_href_conflict = Cannot specify both URL and Image
post_new_missing_content_type = Missing Content-Type for image upload

M src/components/mod.rs => src/components/mod.rs +1 -1
@@ 345,7 345,7 @@ pub fn PostItem<'a>(
    lang: &'a crate::Translator,
) {
    render::rsx! {
        <li class={if post.sticky { "sticky" } else { "" }}>
        <li class={if post.as_ref().sticky { "sticky" } else { "" }}>
            <div class={"titleLine"}>
                <a href={format!("/posts/{}", post.as_ref().as_ref().id)}>
                    {post.as_ref().as_ref().title.as_ref()}

M src/resp_types.rs => src/resp_types.rs +1 -1
@@ 26,6 26,7 @@ pub struct RespSomePostInfo<'a> {
    pub created: Cow<'a, str>,
    #[serde(borrow)]
    pub community: RespMinimalCommunityInfo<'a>,
    pub sticky: bool,
}

impl<'a> AsRef<RespMinimalPostInfo<'a>> for RespSomePostInfo<'a> {


@@ 39,7 40,6 @@ pub struct RespPostListPost<'a> {
    #[serde(flatten, borrow)]
    pub base: RespSomePostInfo<'a>,
    pub replies_count_total: i64,
    pub sticky: bool,
}

impl<'a> AsRef<RespSomePostInfo<'a>> for RespPostListPost<'a> {

M src/routes/communities.rs => src/routes/communities.rs +72 -0
@@ 741,6 741,66 @@ async fn handler_community_post_unapprove(
        .body("Successfully unapproved.".into())?)
}

async fn handler_community_post_make_sticky(
    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("{\"sticky\": 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 stickied.".into())?)
}

async fn handler_community_post_make_unsticky(
    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("{\"sticky\": 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 unstickied.".into())?)
}

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


@@ 1141,6 1201,18 @@ pub fn route_communities() -> crate::RouteNode<()> {
                                    .with_handler_async("POST", handler_community_post_approve),
                            )
                            .with_child(
                                "make_sticky",
                                crate::RouteNode::new()
                                    .with_handler_async("POST", handler_community_post_make_sticky),
                            )
                            .with_child(
                                "make_unsticky",
                                crate::RouteNode::new().with_handler_async(
                                    "POST",
                                    handler_community_post_make_unsticky,
                                ),
                            )
                            .with_child(
                                "unapprove",
                                crate::RouteNode::new()
                                    .with_handler_async("POST", handler_community_post_unapprove),

M src/routes/posts.rs => src/routes/posts.rs +33 -12
@@ 179,18 179,39 @@ async fn page_post_inner(
                {" "}
                {
                    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>
                            }
                        Some(render::rsx! {
                            <>
                                {
                                    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>
                                        }
                                    }
                                }
                                {
                                    if post.as_ref().sticky {
                                        render::rsx! {
                                            <form method={"POST"} action={format!("/communities/{}/posts/{}/make_unsticky", post.as_ref().community.id, post_id)}>
                                                <button type={"submit"}>{lang.tr("post_make_not_sticky", None)}</button>
                                            </form>
                                        }
                                    } else {
                                        render::rsx! {
                                            <form method={"POST"} action={format!("/communities/{}/posts/{}/make_sticky", post.as_ref().community.id, post_id)}>
                                                <button type={"submit"}>{lang.tr("post_make_sticky", None)}</button>
                                            </form>
                                        }
                                    }
                                }
                            </>
                        })
                    } else {
                        None