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