2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 person::{BanPerson, BanPersonResponse},
6 utils::{get_local_user_view_from_jwt, is_admin, remove_user_data},
7 websocket::UserOperation,
11 moderator::{ModBan, ModBanForm},
12 person::{Person, PersonUpdateForm},
16 use lemmy_db_views_actor::structs::PersonView;
17 use lemmy_utils::{error::LemmyError, utils::time::naive_from_unix, ConnectionId};
19 #[async_trait::async_trait(?Send)]
20 impl Perform for BanPerson {
21 type Response = BanPersonResponse;
23 #[tracing::instrument(skip(context, websocket_id))]
26 context: &Data<LemmyContext>,
27 websocket_id: Option<ConnectionId>,
28 ) -> Result<BanPersonResponse, LemmyError> {
29 let data: &BanPerson = self;
31 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
33 // Make sure user is an admin
34 is_admin(&local_user_view)?;
37 let banned_person_id = data.person_id;
38 let expires = data.expires.map(naive_from_unix);
40 let person = Person::update(
43 &PersonUpdateForm::builder()
45 .ban_expires(Some(expires))
49 .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_user"))?;
51 // Remove their data if that's desired
52 let remove_data = data.remove_data.unwrap_or(false);
64 let form = ModBanForm {
65 mod_person_id: local_user_view.person.id,
66 other_person_id: data.person_id,
67 reason: data.reason.clone(),
68 banned: Some(data.ban),
72 ModBan::create(context.pool(), &form).await?;
74 let person_id = data.person_id;
75 let person_view = PersonView::read(context.pool(), person_id).await?;
77 let res = BanPersonResponse {
82 context.send_all_ws_message(&UserOperation::BanPerson, &res, websocket_id)?;