From 1e4f2ee6fbe3e8b3fb5d7d0a5edc4de0bb437b8e Mon Sep 17 00:00:00 2001 From: Colin Reeder Date: Thu, 25 Jan 2024 10:52:35 -0700 Subject: [PATCH] Merge community lists into tab view, including mine (#221) --- res/lang/en.ftl | 7 +- src/routes/communities.rs | 250 ++++++++++++++------------------------ 2 files changed, 98 insertions(+), 159 deletions(-) diff --git a/res/lang/en.ftl b/res/lang/en.ftl index 2a2b671..0d8d775 100644 --- a/res/lang/en.ftl +++ b/res/lang/en.ftl @@ -30,10 +30,11 @@ comment_reply_attachment_missing_content_type = Missing Content-Type for image u comment_reply_image_prompt = Attach Image (optional): comment_submit = Post Comment communities = Communities -communities_all_view = See All -communities_local_more = More Local Communities -communities_remote_more = More Remote Communities communities_page_next = View More +communities_filter_all = all +communities_filter_local = local +communities_filter_remote = remote +communities_filter_mine = mine community_add_moderator = Add Moderator community_create = Create Community community_create_not_allowed = You are not allowed to create communities diff --git a/src/routes/communities.rs b/src/routes/communities.rs index f1c14c6..8d10157 100644 --- a/src/routes/communities.rs +++ b/src/routes/communities.rs @@ -16,6 +16,7 @@ use crate::routes::{ use serde_derive::{Deserialize, Serialize}; use std::borrow::Cow; use std::collections::HashMap; +use std::fmt::Write; use std::ops::Deref; use std::sync::Arc; @@ -29,81 +30,52 @@ async fn page_communities( let base_data = fetch_base_data(&ctx.backend_host, &ctx.http_client, req.headers(), &cookies).await?; - #[derive(Deserialize)] + #[derive(Deserialize, Serialize)] struct Query<'a> { local: Option, + #[serde(rename = "your_follow.accepted")] + your_follow_accepted: Option, page: Option>, } let query: Query = serde_urlencoded::from_str(req.uri().query().unwrap_or(""))?; - let page_param = if let Some(page) = query.page { - Cow::Owned(format!("&page={}", page)) - } else { - Cow::Borrowed("") - }; - - let remote_communities_api_res = if query.local == Some(true) { - None - } else { - Some( - hyper::body::to_bytes( - res_to_error( - ctx.http_client - .request( - hyper::Request::get(format!( - "{}/api/unstable/communities?local=false{}", - ctx.backend_host, page_param - )) - .body(Default::default())?, - ) - .await?, - ) - .await? - .into_body(), - ) - .await?, - ) - }; - let remote_communities: Option> = remote_communities_api_res - .map(|value| serde_json::from_slice(&value)) - .transpose()?; - - let local_communities_api_res = if query.local == Some(false) { - None - } else { - Some( - hyper::body::to_bytes( - res_to_error( - ctx.http_client - .request( - hyper::Request::get(format!( - "{}/api/unstable/communities?local=true{}", - ctx.backend_host, page_param - )) - .body(Default::default())?, - ) - .await?, - ) - .await? - .into_body(), - ) - .await?, + let api_res = hyper::body::to_bytes( + res_to_error( + ctx.http_client + .request(for_client( + hyper::Request::get(format!( + "{}/api/unstable/communities?{}", + ctx.backend_host, + // for now this works but will need to change if we add other page parameters + serde_urlencoded::to_string(&query)?, + )) + .body(Default::default())?, + req.headers(), + &cookies, + )?) + .await?, ) - }; - let local_communities: Option> = local_communities_api_res - .map(|value| serde_json::from_slice(&value)) - .transpose()?; - - let title = if let Some(local) = query.local { - if local { - lang.tr(&lang::COMMUNITIES_LOCAL_MORE) - } else { - lang.tr(&lang::COMMUNITIES_REMOTE_MORE) - } - } else { - lang.tr(&lang::COMMUNITIES) - }; + .await? + .into_body(), + ) + .await?; + + let communities: RespList = serde_json::from_slice(&api_res)?; + + let title = lang.tr(&lang::COMMUNITIES); + + let filter_options: &[(lang::LangKey, bool, Option, Option)] = &[ + (lang::COMMUNITIES_FILTER_ALL, true, None, None), + (lang::COMMUNITIES_FILTER_LOCAL, true, Some(true), None), + (lang::COMMUNITIES_FILTER_REMOTE, true, Some(false), None), + ( + lang::COMMUNITIES_FILTER_MINE, + base_data.login.is_some(), + None, + Some(true), + ), + ]; Ok(html_response(render::html! {

{title.as_ref()}

{ - if query.local.is_some() { - Some(render::rsx! { - {lang.tr(&lang::COMMUNITIES_ALL_VIEW)} - }) + if let Some(login) = &base_data.login { + if login.permissions.create_community.allowed { + Some(render::rsx! { {lang.tr(&lang::COMMUNITY_CREATE)} }) + } else { + None + } } else { None } } - { - if let Some(local_communities) = &local_communities { - Some(render::rsx! { -
- { - if query.local.is_none() { - Some(render::rsx! { -

{lang.tr(&lang::LOCAL)}

- }) +
+ + {" "} + +
+
+ { + filter_options.iter() + .map(|(key, show, local, followed)| { + if *show { + let name = lang.tr(key); + Ok(Some(if &query.local == local && &query.your_follow_accepted == followed { + render::rsx! { {name} } } else { - None - } - } - { - if let Some(login) = &base_data.login { - if login.permissions.create_community.allowed { - Some(render::rsx! { {lang.tr(&lang::COMMUNITY_CREATE)} }) - } else { - None + let mut href = "/communities".to_owned(); + if let Some(local) = local { + if followed.is_some() { + return Err(crate::Error::InternalStrStatic("Unimplemented")); + } + write!(href, "?local={}", local).unwrap(); + } else if let Some(followed) = followed { + write!(href, "?your_follow.accepted={}", followed).unwrap(); } - } else { - None - } + + render::rsx! { {name} } + })) + } else { + Ok(None) } -
    - { - local_communities.items.iter() - .map(|community| { - render::rsx! { -
  • - } - }) - .collect::>() - } -
- { - local_communities.next_page.as_ref().map(|next_page| { - render::rsx! { - - {lang.tr(&lang::COMMUNITIES_PAGE_NEXT)} - - } - }) + }) + .collect::, _>>()? + } +
+
    + { + communities.items.iter() + .map(|community| { + render::rsx! { +
  • } -
- }) - } else { - None + }) + .collect::>() } - } + { - if let Some(remote_communities) = &remote_communities { + if let Some(next_page) = communities.next_page { Some(render::rsx! { -
- { - if query.local.is_none() { - Some(render::rsx! { -

{lang.tr(&lang::REMOTE)}

- }) - } else { - None - } - } -
- - {" "} - -
-
    - { - remote_communities.items.iter() - .map(|community| { - render::rsx! { -
  • - } - }) - .collect::>() - } -
- { - remote_communities.next_page.as_ref().map(|next_page| { - render::rsx! { - - {lang.tr(&lang::COMMUNITIES_PAGE_NEXT)} - - } - }) - } -
+ + {lang.tr(&lang::COMMUNITIES_PAGE_NEXT)} + }) } else { None -- 2.45.2