2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 community::{BanFromCommunity, BanFromCommunityResponse},
6 utils::{is_mod_or_admin, local_user_view_from_jwt, remove_user_data_in_community},
12 CommunityFollowerForm,
14 CommunityPersonBanForm,
16 moderator::{ModBanFromCommunity, ModBanFromCommunityForm},
18 traits::{Bannable, Crud, Followable},
20 use lemmy_db_views_actor::structs::PersonView;
22 error::{LemmyError, LemmyErrorExt, LemmyErrorType},
23 utils::{time::naive_from_unix, validation::is_valid_body_field},
26 #[async_trait::async_trait(?Send)]
27 impl Perform for BanFromCommunity {
28 type Response = BanFromCommunityResponse;
30 #[tracing::instrument(skip(context))]
33 context: &Data<LemmyContext>,
34 ) -> Result<BanFromCommunityResponse, LemmyError> {
35 let data: &BanFromCommunity = self;
36 let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
38 let community_id = data.community_id;
39 let banned_person_id = data.person_id;
40 let remove_data = data.remove_data.unwrap_or(false);
41 let expires = data.expires.map(naive_from_unix);
43 // Verify that only mods or admins can ban
44 is_mod_or_admin(&mut context.pool(), local_user_view.person.id, community_id).await?;
45 is_valid_body_field(&data.reason, false)?;
47 let community_user_ban_form = CommunityPersonBanForm {
48 community_id: data.community_id,
49 person_id: data.person_id,
50 expires: Some(expires),
54 CommunityPersonBan::ban(&mut context.pool(), &community_user_ban_form)
56 .with_lemmy_type(LemmyErrorType::CommunityUserAlreadyBanned)?;
58 // Also unsubscribe them from the community, if they are subscribed
59 let community_follower_form = CommunityFollowerForm {
60 community_id: data.community_id,
61 person_id: banned_person_id,
65 CommunityFollower::unfollow(&mut context.pool(), &community_follower_form)
69 CommunityPersonBan::unban(&mut context.pool(), &community_user_ban_form)
71 .with_lemmy_type(LemmyErrorType::CommunityUserAlreadyBanned)?;
74 // Remove/Restore their data if that's desired
76 remove_user_data_in_community(community_id, banned_person_id, &mut context.pool()).await?;
80 let form = ModBanFromCommunityForm {
81 mod_person_id: local_user_view.person.id,
82 other_person_id: data.person_id,
83 community_id: data.community_id,
84 reason: data.reason.clone(),
85 banned: Some(data.ban),
89 ModBanFromCommunity::create(&mut context.pool(), &form).await?;
91 let person_id = data.person_id;
92 let person_view = PersonView::read(&mut context.pool(), person_id).await?;
94 Ok(BanFromCommunityResponse {