2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 community::{BlockCommunity, BlockCommunityResponse},
6 utils::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;
29 let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
31 let community_id = data.community_id;
32 let person_id = local_user_view.person.id;
33 let community_block_form = CommunityBlockForm {
39 CommunityBlock::block(context.pool(), &community_block_form)
41 .map_err(|e| LemmyError::from_error_message(e, "community_block_already_exists"))?;
43 // Also, unfollow the community, and send a federated unfollow
44 let community_follower_form = CommunityFollowerForm {
45 community_id: data.community_id,
50 CommunityFollower::unfollow(context.pool(), &community_follower_form)
54 CommunityBlock::unblock(context.pool(), &community_block_form)
56 .map_err(|e| LemmyError::from_error_message(e, "community_block_already_exists"))?;
60 CommunityView::read(context.pool(), community_id, Some(person_id), None).await?;
62 Ok(BlockCommunityResponse {