diff --git a/src/api/community.rs b/src/api/community.rs index 1817fac..0c3b6c9 100644 --- a/src/api/community.rs +++ b/src/api/community.rs @@ -1,5 +1,5 @@ use lemmy_api_common::{ - community::{CommunityResponse, FollowCommunity, GetCommunity, GetCommunityResponse}, + community::{CommunityResponse, FollowCommunity, GetCommunity, GetCommunityResponse, BlockCommunityResponse, BlockCommunity}, lemmy_db_schema::newtypes::CommunityId, }; @@ -16,11 +16,11 @@ pub fn get_community(id: CommunityId) -> std::result::Result Result { let params = FollowCommunity { - community_id: CommunityId(community_id), + community_id: community_id, follow, auth: settings::get_current_account().jwt.unwrap(), }; @@ -30,3 +30,16 @@ pub fn follow_community( pub fn default_community() -> GetCommunityResponse { serde_json::from_str(include_str!("../examples/community.json")).unwrap() } + +pub fn block_community( + community_id: CommunityId, + block: bool, +) -> std::result::Result { + let params = BlockCommunity { + community_id, + block, + auth: settings::get_current_account().jwt.unwrap(), + }; + + super::post("/community/block", ¶ms) +} \ No newline at end of file diff --git a/src/api/user.rs b/src/api/user.rs index 1516ba1..a76b20a 100644 --- a/src/api/user.rs +++ b/src/api/user.rs @@ -2,7 +2,7 @@ use lemmy_api_common::{ lemmy_db_schema::{newtypes::PersonId, CommentSortType}, person::{ GetPersonDetails, GetPersonDetailsResponse, GetPersonMentions, GetPersonMentionsResponse, - GetReplies, GetRepliesResponse, MarkAllAsRead, + GetReplies, GetRepliesResponse, MarkAllAsRead }, }; diff --git a/src/components/community_page.rs b/src/components/community_page.rs index 8838656..f86c7f8 100644 --- a/src/components/community_page.rs +++ b/src/components/community_page.rs @@ -36,6 +36,8 @@ pub enum CommunityInput { ToggleSubscription, UpdateSubscriptionState(SubscribedType), UpdateOrder(SortType), + ToggleBlocked, + UpdateBlocked(bool), None, } @@ -91,6 +93,7 @@ impl SimpleComponent for CommunityPage { set_orientation: gtk::Orientation::Horizontal, set_halign: gtk::Align::Center, set_margin_top: 10, + set_spacing: 10, #[watch] set_visible: settings::get_current_account().jwt.is_some(), @@ -113,9 +116,20 @@ impl SimpleComponent for CommunityPage { } } }, + gtk::Button { + set_label: "Block", + #[watch] + set_visible: !model.info.blocked, + connect_clicked => CommunityInput::ToggleBlocked, + }, + gtk::Button { + set_label: "Unblock", + #[watch] + set_visible: model.info.blocked, + connect_clicked => CommunityInput::ToggleBlocked, + }, gtk::Button { set_label: "Create post", - set_margin_start: 10, connect_clicked => CommunityInput::OpenCreatePostDialog, } }, @@ -236,21 +250,19 @@ impl SimpleComponent for CommunityPage { }); } CommunityInput::ToggleSubscription => { - let community_id = self.info.community.id.0; + let community_id = self.info.community.id; let new_state = matches!(self.info.subscribed, SubscribedType::NotSubscribed); std::thread::spawn(move || { - let message = match api::community::follow_community(community_id, new_state) { - Ok(community) => Some(CommunityInput::UpdateSubscriptionState( + match api::community::follow_community(community_id, new_state) { + Ok(community) => { + sender.input(CommunityInput::UpdateSubscriptionState( community.community_view.subscribed, - )), + )); + }, Err(err) => { println!("{}", err); - None } }; - if let Some(message) = message { - sender.input(message) - }; }); } CommunityInput::UpdateSubscriptionState(state) => { @@ -262,6 +274,21 @@ impl SimpleComponent for CommunityPage { self.posts.guard().clear(); sender.input_sender().emit(CommunityInput::FetchPosts); } + CommunityInput::ToggleBlocked => { + let community_id = self.info.community.id; + let blocked = self.info.blocked; + std::thread::spawn(move || { + match api::community::block_community(community_id, !blocked) { + Ok(resp) => { + sender.input(CommunityInput::UpdateBlocked(resp.blocked)); + } + Err(err) => { + println!("{}", err); + } + } + }); + } + CommunityInput::UpdateBlocked(blocked) => self.info.blocked = blocked, CommunityInput::None => {} } }