]> Untitled Git - lemmy.git/blob - crates/api/src/community/ban.rs
33f6ef83305db2cc4e0868ad1abd66dd2a1c0668
[lemmy.git] / crates / api / src / community / ban.rs
1 use crate::Perform;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   community::{BanFromCommunity, BanFromCommunityResponse},
5   context::LemmyContext,
6   utils::{is_mod_or_admin, local_user_view_from_jwt, remove_user_data_in_community},
7 };
8 use lemmy_db_schema::{
9   source::{
10     community::{
11       CommunityFollower,
12       CommunityFollowerForm,
13       CommunityPersonBan,
14       CommunityPersonBanForm,
15     },
16     moderator::{ModBanFromCommunity, ModBanFromCommunityForm},
17   },
18   traits::{Bannable, Crud, Followable},
19 };
20 use lemmy_db_views_actor::structs::PersonView;
21 use lemmy_utils::{
22   error::{LemmyError, LemmyErrorExt, LemmyErrorType},
23   utils::{time::naive_from_unix, validation::is_valid_body_field},
24 };
25
26 #[async_trait::async_trait(?Send)]
27 impl Perform for BanFromCommunity {
28   type Response = BanFromCommunityResponse;
29
30   #[tracing::instrument(skip(context))]
31   async fn perform(
32     &self,
33     context: &Data<LemmyContext>,
34   ) -> Result<BanFromCommunityResponse, LemmyError> {
35     let data: &BanFromCommunity = self;
36     let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
37
38     let community_id = data.community_id;
39     let banned_person_id = data.person_id;
40     let remove_data = data.remove_data.unwrap_or(false);
41     let expires = data.expires.map(naive_from_unix);
42
43     // Verify that only mods or admins can ban
44     is_mod_or_admin(&mut context.pool(), local_user_view.person.id, community_id).await?;
45     is_valid_body_field(&data.reason, false)?;
46
47     let community_user_ban_form = CommunityPersonBanForm {
48       community_id: data.community_id,
49       person_id: data.person_id,
50       expires: Some(expires),
51     };
52
53     if data.ban {
54       CommunityPersonBan::ban(&mut context.pool(), &community_user_ban_form)
55         .await
56         .with_lemmy_type(LemmyErrorType::CommunityUserAlreadyBanned)?;
57
58       // Also unsubscribe them from the community, if they are subscribed
59       let community_follower_form = CommunityFollowerForm {
60         community_id: data.community_id,
61         person_id: banned_person_id,
62         pending: false,
63       };
64
65       CommunityFollower::unfollow(&mut context.pool(), &community_follower_form)
66         .await
67         .ok();
68     } else {
69       CommunityPersonBan::unban(&mut context.pool(), &community_user_ban_form)
70         .await
71         .with_lemmy_type(LemmyErrorType::CommunityUserAlreadyBanned)?;
72     }
73
74     // Remove/Restore their data if that's desired
75     if remove_data {
76       remove_user_data_in_community(community_id, banned_person_id, &mut context.pool()).await?;
77     }
78
79     // Mod tables
80     let form = ModBanFromCommunityForm {
81       mod_person_id: local_user_view.person.id,
82       other_person_id: data.person_id,
83       community_id: data.community_id,
84       reason: data.reason.clone(),
85       banned: Some(data.ban),
86       expires,
87     };
88
89     ModBanFromCommunity::create(&mut context.pool(), &form).await?;
90
91     let person_id = data.person_id;
92     let person_view = PersonView::read(&mut context.pool(), person_id).await?;
93
94     Ok(BanFromCommunityResponse {
95       person_view,
96       banned: data.ban,
97     })
98   }
99 }