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,
|
2023-10-19 17:21:05 +00:00
|
|
|
person::BanPerson,
|
2023-08-02 16:52:41 +00:00
|
|
|
send_activity::{ActivityChannel, SendActivityData},
|
2023-10-17 17:25:35 +00:00
|
|
|
utils::{check_expire_time, is_admin, remove_user_data},
|
2023-10-19 17:21:05 +00:00
|
|
|
SuccessResponse,
|
2022-04-13 18:12:25 +00:00
|
|
|
};
|
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::{
|
2023-10-09 10:46:12 +00:00
|
|
|
login_token::LoginToken,
|
2022-04-13 18:12:25 +00:00
|
|
|
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-10-17 17:25:35 +00:00
|
|
|
utils::validation::is_valid_body_field,
|
2023-04-15 14:45:11 +00:00
|
|
|
};
|
2023-10-17 17:25:35 +00:00
|
|
|
|
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-10-19 17:21:05 +00:00
|
|
|
) -> Result<Json<SuccessResponse>, LemmyError> {
|
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-10-17 17:25:35 +00:00
|
|
|
let expires = check_expire_time(data.expires)?;
|
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-10-09 10:46:12 +00:00
|
|
|
let local_user_id = LocalUserView::read_person(&mut context.pool(), data.person_id)
|
|
|
|
.await?
|
|
|
|
.local_user
|
|
|
|
.id;
|
|
|
|
LoginToken::invalidate_all(&mut context.pool(), local_user_id).await?;
|
|
|
|
|
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-10-11 14:48:19 +00:00
|
|
|
reason: data.reason.clone(),
|
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-10-19 17:21:05 +00:00
|
|
|
Ok(Json(SuccessResponse::default()))
|
2022-04-13 18:12:25 +00:00
|
|
|
}
|