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