2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 community::{BlockCommunity, BlockCommunityResponse},
6 utils::get_local_user_view_from_jwt,
10 community::{CommunityFollower, CommunityFollowerForm},
11 community_block::{CommunityBlock, CommunityBlockForm},
13 traits::{Blockable, Followable},
15 use lemmy_db_views_actor::structs::CommunityView;
16 use lemmy_utils::{error::LemmyError, ConnectionId};
18 #[async_trait::async_trait(?Send)]
19 impl Perform for BlockCommunity {
20 type Response = BlockCommunityResponse;
22 #[tracing::instrument(skip(context, _websocket_id))]
25 context: &Data<LemmyContext>,
26 _websocket_id: Option<ConnectionId>,
27 ) -> Result<BlockCommunityResponse, LemmyError> {
28 let data: &BlockCommunity = self;
30 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
32 let community_id = data.community_id;
33 let person_id = local_user_view.person.id;
34 let community_block_form = CommunityBlockForm {
40 CommunityBlock::block(context.pool(), &community_block_form)
42 .map_err(|e| LemmyError::from_error_message(e, "community_block_already_exists"))?;
44 // Also, unfollow the community, and send a federated unfollow
45 let community_follower_form = CommunityFollowerForm {
46 community_id: data.community_id,
51 CommunityFollower::unfollow(context.pool(), &community_follower_form)
55 CommunityBlock::unblock(context.pool(), &community_block_form)
57 .map_err(|e| LemmyError::from_error_message(e, "community_block_already_exists"))?;
61 CommunityView::read(context.pool(), community_id, Some(person_id), None).await?;
63 Ok(BlockCommunityResponse {