2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 community::{BlockCommunity, BlockCommunityResponse},
5 utils::{blocking, get_local_user_view_from_jwt},
7 use lemmy_apub::protocol::activities::following::undo_follow::UndoFollowCommunity;
10 community::{Community, CommunityFollower, CommunityFollowerForm},
11 community_block::{CommunityBlock, CommunityBlockForm},
13 traits::{Blockable, Crud, Followable},
15 use lemmy_db_views_actor::structs::CommunityView;
16 use lemmy_utils::{error::LemmyError, ConnectionId};
17 use lemmy_websocket::LemmyContext;
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 let block = move |conn: &mut _| CommunityBlock::block(conn, &community_block_form);
42 blocking(context.pool(), block)
44 .map_err(|e| LemmyError::from_error_message(e, "community_block_already_exists"))?;
46 // Also, unfollow the community, and send a federated unfollow
47 let community_follower_form = CommunityFollowerForm {
48 community_id: data.community_id,
52 blocking(context.pool(), move |conn: &mut _| {
53 CommunityFollower::unfollow(conn, &community_follower_form)
57 let community = blocking(context.pool(), move |conn| {
58 Community::read(conn, community_id)
61 UndoFollowCommunity::send(&local_user_view.person.into(), &community.into(), context).await?;
63 let unblock = move |conn: &mut _| CommunityBlock::unblock(conn, &community_block_form);
64 blocking(context.pool(), unblock)
66 .map_err(|e| LemmyError::from_error_message(e, "community_block_already_exists"))?;
69 let community_view = blocking(context.pool(), move |conn| {
70 CommunityView::read(conn, community_id, Some(person_id))
74 Ok(BlockCommunityResponse {