feat: support for (un)blocking communities

This commit is contained in:
Bnyro 2023-08-07 16:05:55 +02:00
parent 3ccbdfd903
commit 58c77072ed
3 changed files with 53 additions and 13 deletions

View File

@ -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<GetCommunityRespons
}
pub fn follow_community(
community_id: i32,
community_id: CommunityId,
follow: bool,
) -> Result<CommunityResponse, reqwest::Error> {
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<BlockCommunityResponse, reqwest::Error> {
let params = BlockCommunity {
community_id,
block,
auth: settings::get_current_account().jwt.unwrap(),
};
super::post("/community/block", &params)
}

View File

@ -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
},
};

View File

@ -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 => {}
}
}