mirror of
https://github.com/LemmyNet/lemmy.git
synced 2025-02-10 02:54:49 +00:00
![Dessalines](/assets/img/avatar_default.png)
* Allow empty string to clear URL-type DB fields. - To address difficulties with clearing URL-type fields like avatars, banners, site icons, this PR turns the URL type form fields into strings. - This allows an empty string to be used as a "clear data", as in the case with the regular text form fields. - Also includes various cleanups. - Fixes #4777 - Context: #2287 * Fixing comment. * Use Option<&str> and deref. --------- Co-authored-by: SleeplessOne1917 <28871516+SleeplessOne1917@users.noreply.github.com>
107 lines
2.7 KiB
Rust
107 lines
2.7 KiB
Rust
use crate::ban_nonlocal_user_from_local_communities;
|
|
use activitypub_federation::config::Data;
|
|
use actix_web::web::Json;
|
|
use lemmy_api_common::{
|
|
context::LemmyContext,
|
|
person::{BanPerson, BanPersonResponse},
|
|
send_activity::{ActivityChannel, SendActivityData},
|
|
utils::{check_expire_time, is_admin, remove_user_data},
|
|
};
|
|
use lemmy_db_schema::{
|
|
source::{
|
|
login_token::LoginToken,
|
|
moderator::{ModBan, ModBanForm},
|
|
person::{Person, PersonUpdateForm},
|
|
},
|
|
traits::Crud,
|
|
};
|
|
use lemmy_db_views::structs::LocalUserView;
|
|
use lemmy_db_views_actor::structs::PersonView;
|
|
use lemmy_utils::{
|
|
error::{LemmyErrorExt, LemmyErrorType, LemmyResult},
|
|
utils::validation::is_valid_body_field,
|
|
};
|
|
|
|
#[tracing::instrument(skip(context))]
|
|
pub async fn ban_from_site(
|
|
data: Json<BanPerson>,
|
|
context: Data<LemmyContext>,
|
|
local_user_view: LocalUserView,
|
|
) -> LemmyResult<Json<BanPersonResponse>> {
|
|
// Make sure user is an admin
|
|
is_admin(&local_user_view)?;
|
|
|
|
if let Some(reason) = &data.reason {
|
|
is_valid_body_field(reason, false)?;
|
|
}
|
|
|
|
let expires = check_expire_time(data.expires)?;
|
|
|
|
let person = Person::update(
|
|
&mut context.pool(),
|
|
data.person_id,
|
|
&PersonUpdateForm {
|
|
banned: Some(data.ban),
|
|
ban_expires: Some(expires),
|
|
..Default::default()
|
|
},
|
|
)
|
|
.await
|
|
.with_lemmy_type(LemmyErrorType::CouldntUpdateUser)?;
|
|
|
|
// if its a local user, invalidate logins
|
|
let local_user = LocalUserView::read_person(&mut context.pool(), person.id).await;
|
|
if let Ok(Some(local_user)) = local_user {
|
|
LoginToken::invalidate_all(&mut context.pool(), local_user.local_user.id).await?;
|
|
}
|
|
|
|
// Remove their data if that's desired
|
|
let remove_data = data.remove_data.unwrap_or(false);
|
|
if remove_data {
|
|
remove_user_data(person.id, &context).await?;
|
|
}
|
|
|
|
// Mod tables
|
|
let form = ModBanForm {
|
|
mod_person_id: local_user_view.person.id,
|
|
other_person_id: person.id,
|
|
reason: data.reason.clone(),
|
|
banned: Some(data.ban),
|
|
expires,
|
|
};
|
|
|
|
ModBan::create(&mut context.pool(), &form).await?;
|
|
|
|
let person_view = PersonView::read(&mut context.pool(), person.id)
|
|
.await?
|
|
.ok_or(LemmyErrorType::CouldntFindPerson)?;
|
|
|
|
ban_nonlocal_user_from_local_communities(
|
|
&local_user_view,
|
|
&person,
|
|
data.ban,
|
|
&data.reason,
|
|
&data.remove_data,
|
|
&data.expires,
|
|
&context,
|
|
)
|
|
.await?;
|
|
|
|
ActivityChannel::submit_activity(
|
|
SendActivityData::BanFromSite {
|
|
moderator: local_user_view.person,
|
|
banned_user: person_view.person.clone(),
|
|
reason: data.reason.clone(),
|
|
remove_data: data.remove_data,
|
|
ban: data.ban,
|
|
expires: data.expires,
|
|
},
|
|
&context,
|
|
)
|
|
.await?;
|
|
|
|
Ok(Json(BanPersonResponse {
|
|
person_view,
|
|
banned: data.ban,
|
|
}))
|
|
}
|