]> Untitled Git - lemmy.git/blob - crates/api/src/community/block.rs
Split apart api files (#2216)
[lemmy.git] / crates / api / src / community / block.rs
1 use crate::Perform;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   blocking,
5   community::{BlockCommunity, BlockCommunityResponse},
6   get_local_user_view_from_jwt,
7 };
8 use lemmy_apub::protocol::activities::following::undo_follow::UndoFollowCommunity;
9 use lemmy_db_schema::{
10   source::{
11     community::{Community, CommunityFollower, CommunityFollowerForm},
12     community_block::{CommunityBlock, CommunityBlockForm},
13   },
14   traits::{Blockable, Crud, Followable},
15 };
16 use lemmy_db_views_actor::community_view::CommunityView;
17 use lemmy_utils::{ConnectionId, LemmyError};
18 use lemmy_websocket::LemmyContext;
19
20 #[async_trait::async_trait(?Send)]
21 impl Perform for BlockCommunity {
22   type Response = BlockCommunityResponse;
23
24   #[tracing::instrument(skip(context, _websocket_id))]
25   async fn perform(
26     &self,
27     context: &Data<LemmyContext>,
28     _websocket_id: Option<ConnectionId>,
29   ) -> Result<BlockCommunityResponse, LemmyError> {
30     let data: &BlockCommunity = self;
31     let local_user_view =
32       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
33
34     let community_id = data.community_id;
35     let person_id = local_user_view.person.id;
36     let community_block_form = CommunityBlockForm {
37       person_id,
38       community_id,
39     };
40
41     if data.block {
42       let block = move |conn: &'_ _| CommunityBlock::block(conn, &community_block_form);
43       blocking(context.pool(), block)
44         .await?
45         .map_err(|e| LemmyError::from_error_message(e, "community_block_already_exists"))?;
46
47       // Also, unfollow the community, and send a federated unfollow
48       let community_follower_form = CommunityFollowerForm {
49         community_id: data.community_id,
50         person_id,
51         pending: false,
52       };
53       blocking(context.pool(), move |conn: &'_ _| {
54         CommunityFollower::unfollow(conn, &community_follower_form)
55       })
56       .await?
57       .ok();
58       let community = blocking(context.pool(), move |conn| {
59         Community::read(conn, community_id)
60       })
61       .await??;
62       UndoFollowCommunity::send(&local_user_view.person.into(), &community.into(), context).await?;
63     } else {
64       let unblock = move |conn: &'_ _| CommunityBlock::unblock(conn, &community_block_form);
65       blocking(context.pool(), unblock)
66         .await?
67         .map_err(|e| LemmyError::from_error_message(e, "community_block_already_exists"))?;
68     }
69
70     let community_view = blocking(context.pool(), move |conn| {
71       CommunityView::read(conn, community_id, Some(person_id))
72     })
73     .await??;
74
75     Ok(BlockCommunityResponse {
76       blocked: data.block,
77       community_view,
78     })
79   }
80 }