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