use crate::{captcha_as_wav_base64, Perform};
use actix_web::web::Data;
-use anyhow::Context;
use bcrypt::verify;
use captcha::{gen, Difficulty};
use chrono::Duration;
is_admin,
password_length_check,
person::*,
+ remove_user_data,
send_email_verification_success,
send_password_reset_email,
send_verification_email,
};
+use lemmy_apub::{
+ activities::block::SiteOrCommunity,
+ protocol::activities::block::{block_user::BlockUser, undo_block_user::UndoBlockUser},
+};
use lemmy_db_schema::{
diesel_option_overwrite,
diesel_option_overwrite_to_url,
naive_now,
source::{
comment::Comment,
- community::Community,
email_verification::EmailVerification,
local_user::{LocalUser, LocalUserForm},
moderator::*,
person::*,
person_block::{PersonBlock, PersonBlockForm},
person_mention::*,
- post::Post,
private_message::PrivateMessage,
site::*,
},
private_message_view::PrivateMessageView,
};
use lemmy_db_views_actor::{
- community_moderator_view::CommunityModeratorView,
person_mention_view::{PersonMentionQueryBuilder, PersonMentionView},
person_view::PersonViewSafe,
};
use lemmy_utils::{
claims::Claims,
- location_info,
utils::{is_valid_display_name, is_valid_matrix_id, naive_from_unix},
ConnectionId,
LemmyError,
return Err(LemmyError::from_message("password_incorrect"));
}
- let site = blocking(context.pool(), Site::read_simple).await??;
+ let site = blocking(context.pool(), Site::read_local_site).await??;
if site.require_email_verification && !local_user_view.local_user.email_verified {
return Err(LemmyError::from_message("email_not_verified"));
}
// When the site requires email, make sure email is not Some(None). IE, an overwrite to a None value
if let Some(email) = &email {
- let site_fut = blocking(context.pool(), Site::read_simple);
+ let site_fut = blocking(context.pool(), Site::read_local_site);
if email.is_none() && site_fut.await??.require_email_verification {
return Err(LemmyError::from_message("email_required"));
}
shared_inbox_url: None,
matrix_user_id,
bot_account,
+ ban_expires: None,
};
blocking(context.pool(), move |conn| {
blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
- let site_creator_id = blocking(context.pool(), move |conn| {
- Site::read(conn, 1).map(|s| s.creator_id)
- })
- .await??;
-
- let mut admins = blocking(context.pool(), PersonViewSafe::admins).await??;
- let creator_index = admins
- .iter()
- .position(|r| r.person.id == site_creator_id)
- .context(location_info!())?;
- let creator_person = admins.remove(creator_index);
- admins.insert(0, creator_person);
+ let admins = blocking(context.pool(), PersonViewSafe::admins).await??;
let res = AddAdminResponse { admins };
let ban = data.ban;
let banned_person_id = data.person_id;
- let ban_person = move |conn: &'_ _| Person::ban_person(conn, banned_person_id, ban);
- blocking(context.pool(), ban_person)
+ let expires = data.expires.map(naive_from_unix);
+
+ let ban_person = move |conn: &'_ _| Person::ban_person(conn, banned_person_id, ban, expires);
+ let person = blocking(context.pool(), ban_person)
.await?
.map_err(LemmyError::from)
.map_err(|e| e.with_message("couldnt_update_user"))?;
// Remove their data if that's desired
- if data.remove_data.unwrap_or(false) {
- // Posts
- blocking(context.pool(), move |conn: &'_ _| {
- Post::update_removed_for_creator(conn, banned_person_id, None, true)
- })
- .await??;
-
- // Communities
- // Remove all communities where they're the top mod
- // for now, remove the communities manually
- let first_mod_communities = blocking(context.pool(), move |conn: &'_ _| {
- CommunityModeratorView::get_community_first_mods(conn)
- })
- .await??;
-
- // Filter to only this banned users top communities
- let banned_user_first_communities: Vec<CommunityModeratorView> = first_mod_communities
- .into_iter()
- .filter(|fmc| fmc.moderator.id == banned_person_id)
- .collect();
-
- for first_mod_community in banned_user_first_communities {
- blocking(context.pool(), move |conn: &'_ _| {
- Community::update_removed(conn, first_mod_community.community.id, true)
- })
- .await??;
- }
-
- // Comments
- blocking(context.pool(), move |conn: &'_ _| {
- Comment::update_removed_for_creator(conn, banned_person_id, true)
- })
- .await??;
+ let remove_data = data.remove_data.unwrap_or(false);
+ if remove_data {
+ remove_user_data(person.id, context.pool()).await?;
}
// Mod tables
- let expires = data.expires.map(naive_from_unix);
-
let form = ModBanForm {
mod_person_id: local_user_view.person.id,
other_person_id: data.person_id,
})
.await??;
+ let site = SiteOrCommunity::Site(
+ blocking(context.pool(), Site::read_local_site)
+ .await??
+ .into(),
+ );
+ // if the action affects a local user, federate to other instances
+ if person.local {
+ if ban {
+ BlockUser::send(
+ &site,
+ &person.into(),
+ &local_user_view.person.into(),
+ remove_data,
+ data.reason.clone(),
+ expires,
+ context,
+ )
+ .await?;
+ } else {
+ UndoBlockUser::send(
+ &site,
+ &person.into(),
+ &local_user_view.person.into(),
+ data.reason.clone(),
+ context,
+ )
+ .await?;
+ }
+ }
+
let res = BanPersonResponse {
person_view,
banned: data.ban,
}
}
+#[async_trait::async_trait(?Send)]
+impl Perform for GetBannedPersons {
+ type Response = BannedPersonsResponse;
+
+ async fn perform(
+ &self,
+ context: &Data<LemmyContext>,
+ _websocket_id: Option<ConnectionId>,
+ ) -> Result<Self::Response, LemmyError> {
+ let data: &GetBannedPersons = self;
+ let local_user_view =
+ get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
+
+ // Make sure user is an admin
+ is_admin(&local_user_view)?;
+
+ let banned = blocking(context.pool(), PersonViewSafe::banned).await??;
+
+ let res = Self::Response { banned };
+
+ Ok(res)
+ }
+}
+
#[async_trait::async_trait(?Send)]
impl Perform for BlockPerson {
type Response = BlockPersonResponse;