2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 community::{BanFromCommunity, BanFromCommunityResponse},
6 get_local_user_view_from_jwt,
8 remove_user_data_in_community,
11 activities::block::SiteOrCommunity,
12 objects::{community::ApubCommunity, person::ApubPerson},
13 protocol::activities::block::{block_user::BlockUser, undo_block_user::UndoBlockUser},
15 use lemmy_db_schema::{
20 CommunityFollowerForm,
22 CommunityPersonBanForm,
24 moderator::{ModBanFromCommunity, ModBanFromCommunityForm},
27 traits::{Bannable, Crud, Followable},
29 use lemmy_db_views_actor::person_view::PersonViewSafe;
30 use lemmy_utils::{utils::naive_from_unix, ConnectionId, LemmyError};
31 use lemmy_websocket::{messages::SendCommunityRoomMessage, LemmyContext, UserOperation};
33 #[async_trait::async_trait(?Send)]
34 impl Perform for BanFromCommunity {
35 type Response = BanFromCommunityResponse;
37 #[tracing::instrument(skip(context, websocket_id))]
40 context: &Data<LemmyContext>,
41 websocket_id: Option<ConnectionId>,
42 ) -> Result<BanFromCommunityResponse, LemmyError> {
43 let data: &BanFromCommunity = self;
45 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
47 let community_id = data.community_id;
48 let banned_person_id = data.person_id;
49 let remove_data = data.remove_data.unwrap_or(false);
50 let expires = data.expires.map(naive_from_unix);
52 // Verify that only mods or admins can ban
53 is_mod_or_admin(context.pool(), local_user_view.person.id, community_id).await?;
55 let community_user_ban_form = CommunityPersonBanForm {
56 community_id: data.community_id,
57 person_id: data.person_id,
58 expires: Some(expires),
61 let community: ApubCommunity = blocking(context.pool(), move |conn: &'_ _| {
62 Community::read(conn, community_id)
66 let banned_person: ApubPerson = blocking(context.pool(), move |conn: &'_ _| {
67 Person::read(conn, banned_person_id)
73 let ban = move |conn: &'_ _| CommunityPersonBan::ban(conn, &community_user_ban_form);
74 blocking(context.pool(), ban)
76 .map_err(|e| LemmyError::from_error_message(e, "community_user_already_banned"))?;
78 // Also unsubscribe them from the community, if they are subscribed
79 let community_follower_form = CommunityFollowerForm {
80 community_id: data.community_id,
81 person_id: banned_person_id,
84 blocking(context.pool(), move |conn: &'_ _| {
85 CommunityFollower::unfollow(conn, &community_follower_form)
91 &SiteOrCommunity::Community(community),
93 &local_user_view.person.clone().into(),
101 let unban = move |conn: &'_ _| CommunityPersonBan::unban(conn, &community_user_ban_form);
102 blocking(context.pool(), unban)
104 .map_err(|e| LemmyError::from_error_message(e, "community_user_already_banned"))?;
106 &SiteOrCommunity::Community(community),
108 &local_user_view.person.clone().into(),
115 // Remove/Restore their data if that's desired
117 remove_user_data_in_community(community_id, banned_person_id, context.pool()).await?;
121 let form = ModBanFromCommunityForm {
122 mod_person_id: local_user_view.person.id,
123 other_person_id: data.person_id,
124 community_id: data.community_id,
125 reason: data.reason.to_owned(),
126 banned: Some(data.ban),
129 blocking(context.pool(), move |conn| {
130 ModBanFromCommunity::create(conn, &form)
134 let person_id = data.person_id;
135 let person_view = blocking(context.pool(), move |conn| {
136 PersonViewSafe::read(conn, person_id)
140 let res = BanFromCommunityResponse {
145 context.chat_server().do_send(SendCommunityRoomMessage {
146 op: UserOperation::BanFromCommunity,
147 response: res.clone(),