From d1b452eaa2e9f24397a08ecd8734d7b8a5fe555d Mon Sep 17 00:00:00 2001 From: Colin Reeder Date: Wed, 8 Jul 2020 23:17:54 -0600 Subject: Handle delayed follow acceptance --- src/resp_types.rs | 7 ++++- src/routes/communities.rs | 80 +++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 77 insertions(+), 10 deletions(-) diff --git a/src/resp_types.rs b/src/resp_types.rs index d81c6ef..53e0935 100644 --- a/src/resp_types.rs +++ b/src/resp_types.rs @@ -73,11 +73,16 @@ pub struct RespLoginInfo { #[derive(Deserialize, Debug)] pub struct Empty {} +#[derive(Deserialize, Debug)] +pub struct RespYourFollow { + pub accepted: bool, +} + #[derive(Deserialize)] pub struct RespCommunityInfoMaybeYour<'a> { #[serde(flatten)] pub base: RespMinimalCommunityInfo<'a>, - pub your_follow: Option, + pub your_follow: Option, } impl<'a> AsRef> for RespCommunityInfoMaybeYour<'a> { diff --git a/src/routes/communities.rs b/src/routes/communities.rs index ce289cc..aec64d2 100644 --- a/src/routes/communities.rs +++ b/src/routes/communities.rs @@ -1,5 +1,7 @@ -use crate::components::{BoolSubmitButton, CommunityLink, HTPage, PostItem}; -use crate::resp_types::{RespCommunityInfoMaybeYour, RespMinimalCommunityInfo, RespPostListPost}; +use crate::components::{CommunityLink, HTPage, PostItem}; +use crate::resp_types::{ + RespCommunityInfoMaybeYour, RespMinimalCommunityInfo, RespPostListPost, RespYourFollow, +}; use crate::routes::{ fetch_base_data, get_cookie_map, get_cookie_map_for_req, html_response, res_to_error, with_auth, }; @@ -132,20 +134,43 @@ async fn page_community( let posts: Vec> = serde_json::from_slice(&posts_api_res)?; - let follow_url = format!("/communities/{}/follow", community_id); let new_post_url = format!("/communities/{}/new_post", community_id); let title = community_info.as_ref().name.as_ref(); - let following = community_info.your_follow.is_some(); - Ok(html_response(render::html! {

{title}

-

- - + { + if base_data.login.is_some() { + Some(match community_info.your_follow { + Some(RespYourFollow { accepted: true }) => { + render::rsx! { +
+ +
+ } + }, + Some(RespYourFollow { accepted: false }) => { + render::rsx! { +
+ +
+ } + }, + None => { + render::rsx! { +
+ +
+ } + } + }) + } else { + None + } + }

{"New Post"} @@ -175,7 +200,8 @@ async fn handler_community_follow( "{}/api/unstable/communities/{}/follow", ctx.backend_host, community_id )) - .body(Default::default())?, + .header(hyper::header::CONTENT_TYPE, "application/json") + .body("{\"try_wait_for_accept\":true}".into())?, &cookies, )?) .await?, @@ -191,6 +217,38 @@ async fn handler_community_follow( .body("Successfully followed".into())?) } +async fn handler_community_unfollow( + params: (i64,), + ctx: Arc, + req: hyper::Request, +) -> Result, crate::Error> { + let (community_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/communities/{}/unfollow", + ctx.backend_host, community_id + )) + .body(Default::default())?, + &cookies, + )?) + .await?, + ) + .await?; + + Ok(hyper::Response::builder() + .status(hyper::StatusCode::SEE_OTHER) + .header( + hyper::header::LOCATION, + format!("/communities/{}", community_id), + ) + .body("Successfully unfollowed".into())?) +} + async fn page_community_new_post( params: (i64,), ctx: Arc, @@ -291,6 +349,10 @@ pub fn route_communities() -> crate::RouteNode<()> { "follow", crate::RouteNode::new().with_handler_async("POST", handler_community_follow), ) + .with_child( + "unfollow", + crate::RouteNode::new().with_handler_async("POST", handler_community_unfollow), + ) .with_child( "new_post", crate::RouteNode::new() -- cgit v1.2.3