2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 community::{BanFromCommunity, BanFromCommunityResponse},
5 utils::{get_local_user_view_from_jwt, is_mod_or_admin, remove_user_data_in_community},
6 websocket::{messages::SendCommunityRoomMessage, UserOperation},
10 activities::block::SiteOrCommunity,
11 objects::{community::ApubCommunity, person::ApubPerson},
12 protocol::activities::block::{block_user::BlockUser, undo_block_user::UndoBlockUser},
14 use lemmy_db_schema::{
19 CommunityFollowerForm,
21 CommunityPersonBanForm,
23 moderator::{ModBanFromCommunity, ModBanFromCommunityForm},
26 traits::{Bannable, Crud, Followable},
28 use lemmy_db_views_actor::structs::PersonViewSafe;
29 use lemmy_utils::{error::LemmyError, utils::naive_from_unix, ConnectionId};
31 #[async_trait::async_trait(?Send)]
32 impl Perform for BanFromCommunity {
33 type Response = BanFromCommunityResponse;
35 #[tracing::instrument(skip(context, websocket_id))]
38 context: &Data<LemmyContext>,
39 websocket_id: Option<ConnectionId>,
40 ) -> Result<BanFromCommunityResponse, LemmyError> {
41 let data: &BanFromCommunity = self;
43 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
45 let community_id = data.community_id;
46 let banned_person_id = data.person_id;
47 let remove_data = data.remove_data.unwrap_or(false);
48 let expires = data.expires.map(naive_from_unix);
50 // Verify that only mods or admins can ban
51 is_mod_or_admin(context.pool(), local_user_view.person.id, community_id).await?;
53 let community_user_ban_form = CommunityPersonBanForm {
54 community_id: data.community_id,
55 person_id: data.person_id,
56 expires: Some(expires),
59 let community: ApubCommunity = Community::read(context.pool(), community_id).await?.into();
60 let banned_person: ApubPerson = Person::read(context.pool(), banned_person_id).await?.into();
63 CommunityPersonBan::ban(context.pool(), &community_user_ban_form)
65 .map_err(|e| LemmyError::from_error_message(e, "community_user_already_banned"))?;
67 // Also unsubscribe them from the community, if they are subscribed
68 let community_follower_form = CommunityFollowerForm {
69 community_id: data.community_id,
70 person_id: banned_person_id,
74 CommunityFollower::unfollow(context.pool(), &community_follower_form)
79 &SiteOrCommunity::Community(community),
81 &local_user_view.person.clone().into(),
89 CommunityPersonBan::unban(context.pool(), &community_user_ban_form)
91 .map_err(|e| LemmyError::from_error_message(e, "community_user_already_banned"))?;
93 &SiteOrCommunity::Community(community),
95 &local_user_view.person.clone().into(),
102 // Remove/Restore their data if that's desired
104 remove_user_data_in_community(community_id, banned_person_id, context.pool()).await?;
108 let form = ModBanFromCommunityForm {
109 mod_person_id: local_user_view.person.id,
110 other_person_id: data.person_id,
111 community_id: data.community_id,
112 reason: data.reason.clone(),
113 banned: Some(data.ban),
117 ModBanFromCommunity::create(context.pool(), &form).await?;
119 let person_id = data.person_id;
120 let person_view = PersonViewSafe::read(context.pool(), person_id).await?;
122 let res = BanFromCommunityResponse {
127 context.chat_server().do_send(SendCommunityRoomMessage {
128 op: UserOperation::BanFromCommunity,
129 response: res.clone(),