Fix api_common::utils

This commit is contained in:
dull b 2023-07-04 02:11:01 +00:00
parent 29b00d7ae8
commit 7dc73de613
7 changed files with 76 additions and 63 deletions

View File

@ -78,7 +78,7 @@ impl Perform for BanFromCommunity {
// Remove/Restore their data if that's desired // Remove/Restore their data if that's desired
if remove_data { if remove_data {
remove_user_data_in_community(community_id, banned_person_id, context.conn().await?).await?; remove_user_data_in_community(community_id, banned_person_id, context.pool()).await?;
} }
// Mod tables // Mod tables

View File

@ -52,7 +52,7 @@ impl Perform for BanPerson {
if remove_data { if remove_data {
remove_user_data( remove_user_data(
person.id, person.id,
context.conn().await?, context.pool(),
context.settings(), context.settings(),
context.client(), context.client(),
) )

View File

@ -46,7 +46,7 @@ impl Perform for PurgeCommunity {
purge_image_posts_for_community( purge_image_posts_for_community(
community_id, community_id,
context.conn().await?, context.pool(),
context.settings(), context.settings(),
context.client(), context.client(),
) )

View File

@ -45,7 +45,7 @@ impl Perform for PurgePerson {
purge_image_posts_for_person( purge_image_posts_for_person(
person_id, person_id,
context.conn().await?, context.pool(),
context.settings(), context.settings(),
context.client(), context.client(),
) )

View File

@ -50,11 +50,11 @@ use url::{ParseError, Url};
#[tracing::instrument(skip_all)] #[tracing::instrument(skip_all)]
pub async fn is_mod_or_admin( pub async fn is_mod_or_admin(
mut conn: impl DbConn, conn: impl DbConn,
person_id: PersonId, person_id: PersonId,
community_id: CommunityId, community_id: CommunityId,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let is_mod_or_admin = CommunityView::is_mod_or_admin(&mut *conn, person_id, community_id).await?; let is_mod_or_admin = CommunityView::is_mod_or_admin(conn, person_id, community_id).await?;
if !is_mod_or_admin { if !is_mod_or_admin {
return Err(LemmyError::from_message("not_a_mod_or_admin")); return Err(LemmyError::from_message("not_a_mod_or_admin"));
} }
@ -63,13 +63,13 @@ pub async fn is_mod_or_admin(
#[tracing::instrument(skip_all)] #[tracing::instrument(skip_all)]
pub async fn is_mod_or_admin_opt( pub async fn is_mod_or_admin_opt(
mut conn: impl DbConn, conn: impl DbConn,
local_user_view: Option<&LocalUserView>, local_user_view: Option<&LocalUserView>,
community_id: Option<CommunityId>, community_id: Option<CommunityId>,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
if let Some(local_user_view) = local_user_view { if let Some(local_user_view) = local_user_view {
if let Some(community_id) = community_id { if let Some(community_id) = community_id {
is_mod_or_admin(&mut *conn, local_user_view.person.id, community_id).await is_mod_or_admin(conn, local_user_view.person.id, community_id).await
} else { } else {
is_admin(local_user_view) is_admin(local_user_view)
} }
@ -101,8 +101,8 @@ pub fn is_top_mod(
} }
#[tracing::instrument(skip_all)] #[tracing::instrument(skip_all)]
pub async fn get_post(post_id: PostId, mut conn: impl DbConn) -> Result<Post, LemmyError> { pub async fn get_post(post_id: PostId, conn: impl DbConn) -> Result<Post, LemmyError> {
Post::read(&mut *conn, post_id) Post::read(conn, post_id)
.await .await
.map_err(|e| LemmyError::from_error_message(e, "couldnt_find_post")) .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_post"))
} }
@ -111,11 +111,11 @@ pub async fn get_post(post_id: PostId, mut conn: impl DbConn) -> Result<Post, Le
pub async fn mark_post_as_read( pub async fn mark_post_as_read(
person_id: PersonId, person_id: PersonId,
post_id: PostId, post_id: PostId,
mut conn: impl DbConn, conn: impl DbConn,
) -> Result<PostRead, LemmyError> { ) -> Result<PostRead, LemmyError> {
let post_read_form = PostReadForm { post_id, person_id }; let post_read_form = PostReadForm { post_id, person_id };
PostRead::mark_as_read(&mut *conn, &post_read_form) PostRead::mark_as_read(conn, &post_read_form)
.await .await
.map_err(|e| LemmyError::from_error_message(e, "couldnt_mark_post_as_read")) .map_err(|e| LemmyError::from_error_message(e, "couldnt_mark_post_as_read"))
} }
@ -124,11 +124,11 @@ pub async fn mark_post_as_read(
pub async fn mark_post_as_unread( pub async fn mark_post_as_unread(
person_id: PersonId, person_id: PersonId,
post_id: PostId, post_id: PostId,
mut conn: impl DbConn, conn: impl DbConn,
) -> Result<usize, LemmyError> { ) -> Result<usize, LemmyError> {
let post_read_form = PostReadForm { post_id, person_id }; let post_read_form = PostReadForm { post_id, person_id };
PostRead::mark_as_unread(&mut *conn, &post_read_form) PostRead::mark_as_unread(conn, &post_read_form)
.await .await
.map_err(|e| LemmyError::from_error_message(e, "couldnt_mark_post_as_read")) .map_err(|e| LemmyError::from_error_message(e, "couldnt_mark_post_as_read"))
} }
@ -197,9 +197,9 @@ pub fn check_user_valid(
pub async fn check_community_ban( pub async fn check_community_ban(
person_id: PersonId, person_id: PersonId,
community_id: CommunityId, community_id: CommunityId,
mut conn: impl DbConn, conn: impl DbConn,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let is_banned = CommunityPersonBanView::get(&mut *conn, person_id, community_id) let is_banned = CommunityPersonBanView::get(conn, person_id, community_id)
.await .await
.is_ok(); .is_ok();
if is_banned { if is_banned {
@ -212,9 +212,9 @@ pub async fn check_community_ban(
#[tracing::instrument(skip_all)] #[tracing::instrument(skip_all)]
pub async fn check_community_deleted_or_removed( pub async fn check_community_deleted_or_removed(
community_id: CommunityId, community_id: CommunityId,
mut conn: impl DbConn, conn: impl DbConn,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let community = Community::read(&mut *conn, community_id) let community = Community::read(conn, community_id)
.await .await
.map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?; .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
if community.deleted || community.removed { if community.deleted || community.removed {
@ -236,9 +236,9 @@ pub fn check_post_deleted_or_removed(post: &Post) -> Result<(), LemmyError> {
pub async fn check_person_block( pub async fn check_person_block(
my_id: PersonId, my_id: PersonId,
potential_blocker_id: PersonId, potential_blocker_id: PersonId,
mut conn: impl DbConn, conn: impl DbConn,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let is_blocked = PersonBlock::read(&mut *conn, potential_blocker_id, my_id) let is_blocked = PersonBlock::read(conn, potential_blocker_id, my_id)
.await .await
.is_ok(); .is_ok();
if is_blocked { if is_blocked {
@ -338,7 +338,7 @@ pub fn send_email_to_user(
pub async fn send_password_reset_email( pub async fn send_password_reset_email(
user: &LocalUserView, user: &LocalUserView,
mut conn: impl DbConn, conn: impl DbConn,
settings: &Settings, settings: &Settings,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
// Generate a random token // Generate a random token
@ -347,7 +347,7 @@ pub async fn send_password_reset_email(
// Insert the row // Insert the row
let token2 = token.clone(); let token2 = token.clone();
let local_user_id = user.local_user.id; let local_user_id = user.local_user.id;
PasswordResetRequest::create_token(&mut *conn, local_user_id, &token2).await?; PasswordResetRequest::create_token(conn, local_user_id, &token2).await?;
let email = &user.local_user.email.clone().expect("email"); let email = &user.local_user.email.clone().expect("email");
let lang = get_interface_language(user); let lang = get_interface_language(user);
@ -362,7 +362,7 @@ pub async fn send_password_reset_email(
pub async fn send_verification_email( pub async fn send_verification_email(
user: &LocalUserView, user: &LocalUserView,
new_email: &str, new_email: &str,
mut conn: impl DbConn, conn: impl DbConn,
settings: &Settings, settings: &Settings,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let form = EmailVerificationForm { let form = EmailVerificationForm {
@ -375,7 +375,7 @@ pub async fn send_verification_email(
settings.get_protocol_and_hostname(), settings.get_protocol_and_hostname(),
&form.verification_token &form.verification_token
); );
EmailVerification::create(&mut *conn, &form).await?; EmailVerification::create(conn, &form).await?;
let lang = get_interface_language(user); let lang = get_interface_language(user);
let subject = lang.verify_email_subject(&settings.hostname); let subject = lang.verify_email_subject(&settings.hostname);
@ -453,11 +453,11 @@ pub fn send_application_approved_email(
/// Send a new applicant email notification to all admins /// Send a new applicant email notification to all admins
pub async fn send_new_applicant_email_to_admins( pub async fn send_new_applicant_email_to_admins(
applicant_username: &str, applicant_username: &str,
mut conn: impl DbConn, conn: impl DbConn,
settings: &Settings, settings: &Settings,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
// Collect the admins with emails // Collect the admins with emails
let admins = LocalUserView::list_admins_with_emails(&mut *conn).await?; let admins = LocalUserView::list_admins_with_emails(conn).await?;
let applications_link = &format!( let applications_link = &format!(
"{}/registration_applications", "{}/registration_applications",
@ -478,11 +478,11 @@ pub async fn send_new_applicant_email_to_admins(
pub async fn send_new_report_email_to_admins( pub async fn send_new_report_email_to_admins(
reporter_username: &str, reporter_username: &str,
reported_username: &str, reported_username: &str,
mut conn: impl DbConn, conn: impl DbConn,
settings: &Settings, settings: &Settings,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
// Collect the admins with emails // Collect the admins with emails
let admins = LocalUserView::list_admins_with_emails(&mut *conn).await?; let admins = LocalUserView::list_admins_with_emails(conn).await?;
let reports_link = &format!("{}/reports", settings.get_protocol_and_hostname(),); let reports_link = &format!("{}/reports", settings.get_protocol_and_hostname(),);
@ -499,7 +499,7 @@ pub async fn send_new_report_email_to_admins(
pub async fn check_registration_application( pub async fn check_registration_application(
local_user_view: &LocalUserView, local_user_view: &LocalUserView,
local_site: &LocalSite, local_site: &LocalSite,
mut conn: impl DbConn, conn: impl DbConn,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
if (local_site.registration_mode == RegistrationMode::RequireApplication if (local_site.registration_mode == RegistrationMode::RequireApplication
|| local_site.registration_mode == RegistrationMode::Closed) || local_site.registration_mode == RegistrationMode::Closed)
@ -508,8 +508,7 @@ pub async fn check_registration_application(
{ {
// Fetch the registration, see if its denied // Fetch the registration, see if its denied
let local_user_id = local_user_view.local_user.id; let local_user_id = local_user_view.local_user.id;
let registration = let registration = RegistrationApplication::find_by_local_user_id(conn, local_user_id).await?;
RegistrationApplication::find_by_local_user_id(&mut *conn, local_user_id).await?;
if let Some(deny_reason) = registration.deny_reason { if let Some(deny_reason) = registration.deny_reason {
let lang = get_interface_language(local_user_view); let lang = get_interface_language(local_user_view);
let registration_denied_message = format!("{}: {}", lang.registration_denied(), &deny_reason); let registration_denied_message = format!("{}: {}", lang.registration_denied(), &deny_reason);
@ -534,11 +533,11 @@ pub fn check_private_instance_and_federation_enabled(
pub async fn purge_image_posts_for_person( pub async fn purge_image_posts_for_person(
banned_person_id: PersonId, banned_person_id: PersonId,
mut conn: impl DbConn, pool: &DbPool,
settings: &Settings, settings: &Settings,
client: &ClientWithMiddleware, client: &ClientWithMiddleware,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let posts = Post::fetch_pictrs_posts_for_creator(&mut *conn, banned_person_id).await?; let posts = Post::fetch_pictrs_posts_for_creator(get_conn(pool).await?, banned_person_id).await?;
for post in posts { for post in posts {
if let Some(url) = post.url { if let Some(url) = post.url {
purge_image_from_pictrs(client, settings, &url).await.ok(); purge_image_from_pictrs(client, settings, &url).await.ok();
@ -550,18 +549,23 @@ pub async fn purge_image_posts_for_person(
} }
} }
Post::remove_pictrs_post_images_and_thumbnails_for_creator(&mut *conn, banned_person_id).await?; Post::remove_pictrs_post_images_and_thumbnails_for_creator(
get_conn(pool).await?,
banned_person_id,
)
.await?;
Ok(()) Ok(())
} }
pub async fn purge_image_posts_for_community( pub async fn purge_image_posts_for_community(
banned_community_id: CommunityId, banned_community_id: CommunityId,
mut conn: impl DbConn, pool: &DbPool,
settings: &Settings, settings: &Settings,
client: &ClientWithMiddleware, client: &ClientWithMiddleware,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let posts = Post::fetch_pictrs_posts_for_community(&mut *conn, banned_community_id).await?; let posts =
Post::fetch_pictrs_posts_for_community(get_conn(pool).await?, banned_community_id).await?;
for post in posts { for post in posts {
if let Some(url) = post.url { if let Some(url) = post.url {
purge_image_from_pictrs(client, settings, &url).await.ok(); purge_image_from_pictrs(client, settings, &url).await.ok();
@ -573,7 +577,10 @@ pub async fn purge_image_posts_for_community(
} }
} }
Post::remove_pictrs_post_images_and_thumbnails_for_community(&mut *conn, banned_community_id) Post::remove_pictrs_post_images_and_thumbnails_for_community(
get_conn(pool).await?,
banned_community_id,
)
.await?; .await?;
Ok(()) Ok(())
@ -581,12 +588,12 @@ pub async fn purge_image_posts_for_community(
pub async fn remove_user_data( pub async fn remove_user_data(
banned_person_id: PersonId, banned_person_id: PersonId,
mut conn: impl DbConn, pool: &DbPool,
settings: &Settings, settings: &Settings,
client: &ClientWithMiddleware, client: &ClientWithMiddleware,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
// Purge user images // Purge user images
let person = Person::read(&mut *conn, banned_person_id).await?; let person = Person::read(get_conn(pool).await?, banned_person_id).await?;
if let Some(avatar) = person.avatar { if let Some(avatar) = person.avatar {
purge_image_from_pictrs(client, settings, &avatar) purge_image_from_pictrs(client, settings, &avatar)
.await .await
@ -600,7 +607,7 @@ pub async fn remove_user_data(
// Update the fields to None // Update the fields to None
Person::update( Person::update(
&mut *conn, get_conn(pool).await?,
banned_person_id, banned_person_id,
&PersonUpdateForm::builder() &PersonUpdateForm::builder()
.avatar(Some(None)) .avatar(Some(None))
@ -610,15 +617,16 @@ pub async fn remove_user_data(
.await?; .await?;
// Posts // Posts
Post::update_removed_for_creator(&mut *conn, banned_person_id, None, true).await?; Post::update_removed_for_creator(get_conn(pool).await?, banned_person_id, None, true).await?;
// Purge image posts // Purge image posts
purge_image_posts_for_person(banned_person_id, &mut *conn, settings, client).await?; purge_image_posts_for_person(banned_person_id, pool, settings, client).await?;
// Communities // Communities
// Remove all communities where they're the top mod // Remove all communities where they're the top mod
// for now, remove the communities manually // for now, remove the communities manually
let first_mod_communities = CommunityModeratorView::get_community_first_mods(&mut *conn).await?; let first_mod_communities =
CommunityModeratorView::get_community_first_mods(get_conn(pool).await?).await?;
// Filter to only this banned users top communities // Filter to only this banned users top communities
let banned_user_first_communities: Vec<CommunityModeratorView> = first_mod_communities let banned_user_first_communities: Vec<CommunityModeratorView> = first_mod_communities
@ -629,7 +637,7 @@ pub async fn remove_user_data(
for first_mod_community in banned_user_first_communities { for first_mod_community in banned_user_first_communities {
let community_id = first_mod_community.community.id; let community_id = first_mod_community.community.id;
Community::update( Community::update(
&mut *conn, get_conn(pool).await?,
community_id, community_id,
&CommunityUpdateForm::builder().removed(Some(true)).build(), &CommunityUpdateForm::builder().removed(Some(true)).build(),
) )
@ -646,7 +654,7 @@ pub async fn remove_user_data(
} }
// Update the fields to None // Update the fields to None
Community::update( Community::update(
&mut *conn, get_conn(pool).await?,
community_id, community_id,
&CommunityUpdateForm::builder() &CommunityUpdateForm::builder()
.icon(Some(None)) .icon(Some(None))
@ -657,7 +665,7 @@ pub async fn remove_user_data(
} }
// Comments // Comments
Comment::update_removed_for_creator(&mut *conn, banned_person_id, true).await?; Comment::update_removed_for_creator(get_conn(pool).await?, banned_person_id, true).await?;
Ok(()) Ok(())
} }
@ -665,15 +673,21 @@ pub async fn remove_user_data(
pub async fn remove_user_data_in_community( pub async fn remove_user_data_in_community(
community_id: CommunityId, community_id: CommunityId,
banned_person_id: PersonId, banned_person_id: PersonId,
mut conn: impl DbConn, pool: &DbPool,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
// Posts // Posts
Post::update_removed_for_creator(&mut *conn, banned_person_id, Some(community_id), true).await?; Post::update_removed_for_creator(
get_conn(pool).await?,
banned_person_id,
Some(community_id),
true,
)
.await?;
// Comments // Comments
// TODO Diesel doesn't allow updates with joins, so this has to be a loop // TODO Diesel doesn't allow updates with joins, so this has to be a loop
let comments = CommentQuery::builder() let comments = CommentQuery::builder()
.conn(&mut *conn) .conn(get_conn(pool).await?)
.creator_id(Some(banned_person_id)) .creator_id(Some(banned_person_id))
.community_id(Some(community_id)) .community_id(Some(community_id))
.build() .build()
@ -683,7 +697,7 @@ pub async fn remove_user_data_in_community(
for comment_view in &comments { for comment_view in &comments {
let comment_id = comment_view.comment.id; let comment_id = comment_view.comment.id;
Comment::update( Comment::update(
&mut *conn, get_conn(pool).await?,
comment_id, comment_id,
&CommentUpdateForm::builder().removed(Some(true)).build(), &CommentUpdateForm::builder().removed(Some(true)).build(),
) )
@ -695,12 +709,12 @@ pub async fn remove_user_data_in_community(
pub async fn delete_user_account( pub async fn delete_user_account(
person_id: PersonId, person_id: PersonId,
mut conn: impl DbConn, pool: &DbPool,
settings: &Settings, settings: &Settings,
client: &ClientWithMiddleware, client: &ClientWithMiddleware,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
// Delete their images // Delete their images
let person = Person::read(&mut *conn, person_id).await?; let person = Person::read(get_conn(pool).await?, person_id).await?;
if let Some(avatar) = person.avatar { if let Some(avatar) = person.avatar {
purge_image_from_pictrs(client, settings, &avatar) purge_image_from_pictrs(client, settings, &avatar)
.await .await
@ -714,22 +728,22 @@ pub async fn delete_user_account(
// No need to update avatar and banner, those are handled in Person::delete_account // No need to update avatar and banner, those are handled in Person::delete_account
// Comments // Comments
Comment::permadelete_for_creator(&mut *conn, person_id) Comment::permadelete_for_creator(get_conn(pool).await?, person_id)
.await .await
.map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?; .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
// Posts // Posts
Post::permadelete_for_creator(&mut *conn, person_id) Post::permadelete_for_creator(get_conn(pool).await?, person_id)
.await .await
.map_err(|e| LemmyError::from_error_message(e, "couldnt_update_post"))?; .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_post"))?;
// Purge image posts // Purge image posts
purge_image_posts_for_person(person_id, &mut *conn, settings, client).await?; purge_image_posts_for_person(person_id, pool, settings, client).await?;
// Leave communities they mod // Leave communities they mod
CommunityModerator::leave_all_communities(&mut *conn, person_id).await?; CommunityModerator::leave_all_communities(get_conn(pool).await?, person_id).await?;
Person::delete_account(&mut *conn, person_id).await?; Person::delete_account(get_conn(pool).await?, person_id).await?;
Ok(()) Ok(())
} }

View File

@ -166,7 +166,7 @@ impl ActivityHandler for BlockUser {
if self.remove_data.unwrap_or(false) { if self.remove_data.unwrap_or(false) {
remove_user_data( remove_user_data(
blocked_person.id, blocked_person.id,
context.conn().await?, context.pool(),
context.settings(), context.settings(),
context.client(), context.client(),
) )
@ -202,8 +202,7 @@ impl ActivityHandler for BlockUser {
.ok(); .ok();
if self.remove_data.unwrap_or(false) { if self.remove_data.unwrap_or(false) {
remove_user_data_in_community(community.id, blocked_person.id, context.conn().await?) remove_user_data_in_community(community.id, blocked_person.id, context.pool()).await?;
.await?;
} }
// write to mod log // write to mod log

View File

@ -32,7 +32,7 @@ impl SendActivity for DeleteAccount {
let actor: ApubPerson = local_user_view.person.into(); let actor: ApubPerson = local_user_view.person.into();
delete_user_account( delete_user_account(
actor.id, actor.id,
context.conn().await?, context.pool(),
context.settings(), context.settings(),
context.client(), context.client(),
) )
@ -84,7 +84,7 @@ impl ActivityHandler for DeleteUser {
let actor = self.actor.dereference(context).await?; let actor = self.actor.dereference(context).await?;
delete_user_account( delete_user_account(
actor.id, actor.id,
context.conn().await?, context.pool(),
context.settings(), context.settings(),
context.client(), context.client(),
) )