2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 community::{BlockCommunity, BlockCommunityResponse},
5 utils::get_local_user_view_from_jwt,
8 use lemmy_apub::protocol::activities::following::undo_follow::UndoFollow;
11 community::{Community, CommunityFollower, CommunityFollowerForm},
12 community_block::{CommunityBlock, CommunityBlockForm},
14 traits::{Blockable, Crud, Followable},
16 use lemmy_db_views_actor::structs::CommunityView;
17 use lemmy_utils::{error::LemmyError, ConnectionId};
19 #[async_trait::async_trait(?Send)]
20 impl Perform for BlockCommunity {
21 type Response = BlockCommunityResponse;
23 #[tracing::instrument(skip(context, _websocket_id))]
26 context: &Data<LemmyContext>,
27 _websocket_id: Option<ConnectionId>,
28 ) -> Result<BlockCommunityResponse, LemmyError> {
29 let data: &BlockCommunity = self;
31 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
33 let community_id = data.community_id;
34 let person_id = local_user_view.person.id;
35 let community_block_form = CommunityBlockForm {
41 CommunityBlock::block(context.pool(), &community_block_form)
43 .map_err(|e| LemmyError::from_error_message(e, "community_block_already_exists"))?;
45 // Also, unfollow the community, and send a federated unfollow
46 let community_follower_form = CommunityFollowerForm {
47 community_id: data.community_id,
52 CommunityFollower::unfollow(context.pool(), &community_follower_form)
55 let community = Community::read(context.pool(), community_id).await?;
56 UndoFollow::send(&local_user_view.person.into(), &community.into(), context).await?;
58 CommunityBlock::unblock(context.pool(), &community_block_form)
60 .map_err(|e| LemmyError::from_error_message(e, "community_block_already_exists"))?;
63 let community_view = CommunityView::read(context.pool(), community_id, Some(person_id)).await?;
65 Ok(BlockCommunityResponse {