2023-08-02 16:52:41 +00:00
|
|
|
use activitypub_federation::config::Data;
|
|
|
|
use actix_web::web::Json;
|
2022-04-13 18:12:25 +00:00
|
|
|
use lemmy_api_common::{
|
2022-11-28 14:29:33 +00:00
|
|
|
context::LemmyContext,
|
2022-04-13 18:12:25 +00:00
|
|
|
person::{BanPerson, BanPersonResponse},
|
2023-08-02 16:52:41 +00:00
|
|
|
send_activity::{ActivityChannel, SendActivityData},
|
2023-07-26 18:01:15 +00:00
|
|
|
utils::{is_admin, local_user_view_from_jwt, remove_user_data, sanitize_html_opt},
|
2022-04-13 18:12:25 +00:00
|
|
|
};
|
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::{
|
|
|
|
moderator::{ModBan, ModBanForm},
|
2022-10-27 09:24:07 +00:00
|
|
|
person::{Person, PersonUpdateForm},
|
2022-04-13 18:12:25 +00:00
|
|
|
},
|
|
|
|
traits::Crud,
|
|
|
|
};
|
2023-03-01 17:19:46 +00:00
|
|
|
use lemmy_db_views_actor::structs::PersonView;
|
2023-04-15 14:45:11 +00:00
|
|
|
use lemmy_utils::{
|
2023-07-10 14:50:07 +00:00
|
|
|
error::{LemmyError, LemmyErrorExt, LemmyErrorType},
|
2023-04-15 14:45:11 +00:00
|
|
|
utils::{time::naive_from_unix, validation::is_valid_body_field},
|
|
|
|
};
|
2023-08-02 16:52:41 +00:00
|
|
|
#[tracing::instrument(skip(context))]
|
|
|
|
pub async fn ban_from_site(
|
|
|
|
data: Json<BanPerson>,
|
|
|
|
context: Data<LemmyContext>,
|
|
|
|
) -> Result<Json<BanPersonResponse>, LemmyError> {
|
|
|
|
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
|
2022-04-13 18:12:25 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
// Make sure user is an admin
|
|
|
|
is_admin(&local_user_view)?;
|
2022-04-13 18:12:25 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
is_valid_body_field(&data.reason, false)?;
|
2022-04-13 18:12:25 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
let expires = data.expires.map(naive_from_unix);
|
2022-04-13 18:12:25 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
let person = Person::update(
|
|
|
|
&mut context.pool(),
|
|
|
|
data.person_id,
|
2023-08-08 09:41:41 +00:00
|
|
|
&PersonUpdateForm {
|
|
|
|
banned: Some(data.ban),
|
|
|
|
ban_expires: Some(expires),
|
|
|
|
..Default::default()
|
|
|
|
},
|
2023-08-02 16:52:41 +00:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
.with_lemmy_type(LemmyErrorType::CouldntUpdateUser)?;
|
2023-04-15 14:45:11 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
// Remove their data if that's desired
|
|
|
|
let remove_data = data.remove_data.unwrap_or(false);
|
|
|
|
if remove_data {
|
|
|
|
remove_user_data(
|
|
|
|
person.id,
|
2023-07-11 13:09:59 +00:00
|
|
|
&mut context.pool(),
|
2023-08-02 16:52:41 +00:00
|
|
|
context.settings(),
|
|
|
|
context.client(),
|
2022-11-09 10:05:00 +00:00
|
|
|
)
|
2023-08-02 16:52:41 +00:00
|
|
|
.await?;
|
|
|
|
}
|
2022-04-13 18:12:25 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
// Mod tables
|
|
|
|
let form = ModBanForm {
|
|
|
|
mod_person_id: local_user_view.person.id,
|
|
|
|
other_person_id: data.person_id,
|
|
|
|
reason: sanitize_html_opt(&data.reason),
|
|
|
|
banned: Some(data.ban),
|
|
|
|
expires,
|
|
|
|
};
|
2022-04-13 18:12:25 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
ModBan::create(&mut context.pool(), &form).await?;
|
2022-04-13 18:12:25 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
let person_view = PersonView::read(&mut context.pool(), data.person_id).await?;
|
2022-04-13 18:12:25 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
ActivityChannel::submit_activity(
|
|
|
|
SendActivityData::BanFromSite(
|
|
|
|
local_user_view.person,
|
|
|
|
person_view.person.clone(),
|
|
|
|
data.0.clone(),
|
|
|
|
),
|
|
|
|
&context,
|
|
|
|
)
|
|
|
|
.await?;
|
2022-04-13 18:12:25 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
Ok(Json(BanPersonResponse {
|
|
|
|
person_view,
|
|
|
|
banned: data.ban,
|
|
|
|
}))
|
2022-04-13 18:12:25 +00:00
|
|
|
}
|