2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 person::{BanPerson, BanPersonResponse},
6 utils::{is_admin, local_user_view_from_jwt, remove_user_data},
10 moderator::{ModBan, ModBanForm},
11 person::{Person, PersonUpdateForm},
15 use lemmy_db_views_actor::structs::PersonView;
17 error::{LemmyError, LemmyErrorExt, LemmyErrorType},
18 utils::{time::naive_from_unix, validation::is_valid_body_field},
21 #[async_trait::async_trait(?Send)]
22 impl Perform for BanPerson {
23 type Response = BanPersonResponse;
25 #[tracing::instrument(skip(context))]
26 async fn perform(&self, context: &Data<LemmyContext>) -> Result<BanPersonResponse, LemmyError> {
27 let data: &BanPerson = self;
28 let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
30 // Make sure user is an admin
31 is_admin(&local_user_view)?;
33 is_valid_body_field(&data.reason, false)?;
36 let banned_person_id = data.person_id;
37 let expires = data.expires.map(naive_from_unix);
39 let person = Person::update(
42 &PersonUpdateForm::builder()
44 .ban_expires(Some(expires))
48 .with_lemmy_type(LemmyErrorType::CouldntUpdateUser)?;
50 // Remove their data if that's desired
51 let remove_data = data.remove_data.unwrap_or(false);
63 let form = ModBanForm {
64 mod_person_id: local_user_view.person.id,
65 other_person_id: data.person_id,
66 reason: data.reason.clone(),
67 banned: Some(data.ban),
71 ModBan::create(&mut context.pool(), &form).await?;
73 let person_id = data.person_id;
74 let person_view = PersonView::read(&mut context.pool(), person_id).await?;
76 Ok(BanPersonResponse {