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-09-21 10:42:28 +00:00
|
|
|
utils::{is_admin, remove_user_data, sanitize_html_api_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-09-21 10:42:28 +00:00
|
|
|
use lemmy_db_views::structs::LocalUserView;
|
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>,
|
2023-09-21 10:42:28 +00:00
|
|
|
local_user_view: LocalUserView,
|
2023-08-02 16:52:41 +00:00
|
|
|
) -> Result<Json<BanPersonResponse>, LemmyError> {
|
|
|
|
// 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 {
|
2023-08-28 10:23:45 +00:00
|
|
|
remove_user_data(person.id, &context).await?;
|
2023-08-02 16:52:41 +00:00
|
|
|
}
|
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,
|
2023-09-06 14:56:26 +00:00
|
|
|
reason: sanitize_html_api_opt(&data.reason),
|
2023-08-02 16:52:41 +00:00
|
|
|
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
|
|
|
}
|