2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 community::{BlockCommunity, BlockCommunityResponse},
6 get_local_user_view_from_jwt,
8 use lemmy_apub::protocol::activities::following::undo_follow::UndoFollowCommunity;
11 community::{Community, CommunityFollower, CommunityFollowerForm},
12 community_block::{CommunityBlock, CommunityBlockForm},
14 traits::{Blockable, Crud, Followable},
16 use lemmy_db_views_actor::community_view::CommunityView;
17 use lemmy_utils::{ConnectionId, LemmyError};
18 use lemmy_websocket::LemmyContext;
20 #[async_trait::async_trait(?Send)]
21 impl Perform for BlockCommunity {
22 type Response = BlockCommunityResponse;
24 #[tracing::instrument(skip(context, _websocket_id))]
27 context: &Data<LemmyContext>,
28 _websocket_id: Option<ConnectionId>,
29 ) -> Result<BlockCommunityResponse, LemmyError> {
30 let data: &BlockCommunity = self;
32 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
34 let community_id = data.community_id;
35 let person_id = local_user_view.person.id;
36 let community_block_form = CommunityBlockForm {
42 let block = move |conn: &'_ _| CommunityBlock::block(conn, &community_block_form);
43 blocking(context.pool(), block)
45 .map_err(|e| LemmyError::from_error_message(e, "community_block_already_exists"))?;
47 // Also, unfollow the community, and send a federated unfollow
48 let community_follower_form = CommunityFollowerForm {
49 community_id: data.community_id,
53 blocking(context.pool(), move |conn: &'_ _| {
54 CommunityFollower::unfollow(conn, &community_follower_form)
58 let community = blocking(context.pool(), move |conn| {
59 Community::read(conn, community_id)
62 UndoFollowCommunity::send(&local_user_view.person.into(), &community.into(), context).await?;
64 let unblock = move |conn: &'_ _| CommunityBlock::unblock(conn, &community_block_form);
65 blocking(context.pool(), unblock)
67 .map_err(|e| LemmyError::from_error_message(e, "community_block_already_exists"))?;
70 let community_view = blocking(context.pool(), move |conn| {
71 CommunityView::read(conn, community_id, Some(person_id))
75 Ok(BlockCommunityResponse {