1 use activitypub_federation::config::Data;
2 use actix_web::web::Json;
3 use lemmy_api_common::{
4 community::{BlockCommunity, BlockCommunityResponse},
6 send_activity::{ActivityChannel, SendActivityData},
7 utils::local_user_view_from_jwt,
11 community::{CommunityFollower, CommunityFollowerForm},
12 community_block::{CommunityBlock, CommunityBlockForm},
14 traits::{Blockable, Followable},
16 use lemmy_db_views_actor::structs::CommunityView;
17 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
19 #[tracing::instrument(skip(context))]
20 pub async fn block_community(
21 data: Json<BlockCommunity>,
22 context: Data<LemmyContext>,
23 ) -> Result<Json<BlockCommunityResponse>, LemmyError> {
24 let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
26 let community_id = data.community_id;
27 let person_id = local_user_view.person.id;
28 let community_block_form = CommunityBlockForm {
34 CommunityBlock::block(&mut context.pool(), &community_block_form)
36 .with_lemmy_type(LemmyErrorType::CommunityBlockAlreadyExists)?;
38 // Also, unfollow the community, and send a federated unfollow
39 let community_follower_form = CommunityFollowerForm {
40 community_id: data.community_id,
45 CommunityFollower::unfollow(&mut context.pool(), &community_follower_form)
49 CommunityBlock::unblock(&mut context.pool(), &community_block_form)
51 .with_lemmy_type(LemmyErrorType::CommunityBlockAlreadyExists)?;
55 CommunityView::read(&mut context.pool(), community_id, Some(person_id), None).await?;
57 ActivityChannel::submit_activity(
58 SendActivityData::FollowCommunity(
59 community_view.community.clone(),
60 local_user_view.person.clone(),
67 Ok(Json(BlockCommunityResponse {