]> Untitled Git - lemmy.git/blob - crates/api/src/community/ban.rs
Adding check to description and body length fields. (#2805)
[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::{get_local_user_view_from_jwt, is_mod_or_admin, remove_user_data_in_community},
7   websocket::{
8     handlers::messages::SendCommunityRoomMessage,
9     serialize_websocket_message,
10     UserOperation,
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,
28   utils::{time::naive_from_unix, validation::is_valid_body_field},
29   ConnectionId,
30 };
31
32 #[async_trait::async_trait(?Send)]
33 impl Perform for BanFromCommunity {
34   type Response = BanFromCommunityResponse;
35
36   #[tracing::instrument(skip(context, websocket_id))]
37   async fn perform(
38     &self,
39     context: &Data<LemmyContext>,
40     websocket_id: Option<ConnectionId>,
41   ) -> Result<BanFromCommunityResponse, LemmyError> {
42     let data: &BanFromCommunity = self;
43     let local_user_view =
44       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
45
46     let community_id = data.community_id;
47     let banned_person_id = data.person_id;
48     let remove_data = data.remove_data.unwrap_or(false);
49     let expires = data.expires.map(naive_from_unix);
50
51     // Verify that only mods or admins can ban
52     is_mod_or_admin(context.pool(), local_user_view.person.id, community_id).await?;
53     is_valid_body_field(&data.reason)?;
54
55     let community_user_ban_form = CommunityPersonBanForm {
56       community_id: data.community_id,
57       person_id: data.person_id,
58       expires: Some(expires),
59     };
60
61     if data.ban {
62       CommunityPersonBan::ban(context.pool(), &community_user_ban_form)
63         .await
64         .map_err(|e| LemmyError::from_error_message(e, "community_user_already_banned"))?;
65
66       // Also unsubscribe them from the community, if they are subscribed
67       let community_follower_form = CommunityFollowerForm {
68         community_id: data.community_id,
69         person_id: banned_person_id,
70         pending: false,
71       };
72
73       CommunityFollower::unfollow(context.pool(), &community_follower_form)
74         .await
75         .ok();
76     } else {
77       CommunityPersonBan::unban(context.pool(), &community_user_ban_form)
78         .await
79         .map_err(|e| LemmyError::from_error_message(e, "community_user_already_banned"))?;
80     }
81
82     // Remove/Restore their data if that's desired
83     if remove_data {
84       remove_user_data_in_community(community_id, banned_person_id, context.pool()).await?;
85     }
86
87     // Mod tables
88     let form = ModBanFromCommunityForm {
89       mod_person_id: local_user_view.person.id,
90       other_person_id: data.person_id,
91       community_id: data.community_id,
92       reason: data.reason.clone(),
93       banned: Some(data.ban),
94       expires,
95     };
96
97     ModBanFromCommunity::create(context.pool(), &form).await?;
98
99     let person_id = data.person_id;
100     let person_view = PersonView::read(context.pool(), person_id).await?;
101
102     let res = BanFromCommunityResponse {
103       person_view,
104       banned: data.ban,
105     };
106
107     // A custom ban message
108     let message = serialize_websocket_message(&UserOperation::BanFromCommunity, &res)?;
109     context.chat_server().do_send(SendCommunityRoomMessage {
110       community_id,
111       message,
112       websocket_id,
113     });
114
115     Ok(res)
116   }
117 }