From a373c82595abb7199ddb02e3c1e5ba9c35f40c57 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Mon, 15 Jul 2024 13:31:19 -0400 Subject: [PATCH] Get rid of pointless wrapper functions, return lemmyresult directly. --- crates/api/src/community/add_mod.rs | 7 ++-- crates/api/src/community/ban.rs | 12 ++---- crates/api/src/local_user/add_admin.rs | 9 +++- crates/api/src/local_user/ban_person.rs | 10 ++++- crates/api/src/site/purge/comment.rs | 7 ++-- crates/api/src/site/purge/community.rs | 7 ++-- crates/api/src/site/purge/person.rs | 10 ++++- crates/api/src/site/purge/post.rs | 10 ++++- crates/api_common/src/utils.rs | 53 ------------------------ crates/api_crud/src/comment/remove.rs | 7 ++-- crates/api_crud/src/post/remove.rs | 7 ++-- crates/db_schema/src/impls/community.rs | 5 ++- crates/db_schema/src/impls/local_user.rs | 14 +++---- 13 files changed, 64 insertions(+), 94 deletions(-) diff --git a/crates/api/src/community/add_mod.rs b/crates/api/src/community/add_mod.rs index ab64a1afe..8d8826cd2 100644 --- a/crates/api/src/community/add_mod.rs +++ b/crates/api/src/community/add_mod.rs @@ -4,11 +4,12 @@ use lemmy_api_common::{ community::{AddModToCommunity, AddModToCommunityResponse}, context::LemmyContext, send_activity::{ActivityChannel, SendActivityData}, - utils::{check_community_mod_action, check_is_higher_mod_or_admin}, + utils::check_community_mod_action, }; use lemmy_db_schema::{ source::{ community::{Community, CommunityModerator, CommunityModeratorForm}, + local_user::LocalUser, moderator::{ModAddCommunity, ModAddCommunityForm}, }, traits::{Crud, Joinable}, @@ -36,10 +37,10 @@ pub async fn add_mod_to_community( // If its a mod removal, also check that you're a higher mod. if !data.added { - check_is_higher_mod_or_admin( + LocalUser::is_higher_mod_or_admin_check( &mut context.pool(), - &local_user_view, community_id, + local_user_view.person.id, vec![data.person_id], ) .await?; diff --git a/crates/api/src/community/ban.rs b/crates/api/src/community/ban.rs index fb51c91ba..8e527d2ac 100644 --- a/crates/api/src/community/ban.rs +++ b/crates/api/src/community/ban.rs @@ -4,12 +4,7 @@ use lemmy_api_common::{ community::{BanFromCommunity, BanFromCommunityResponse}, context::LemmyContext, send_activity::{ActivityChannel, SendActivityData}, - utils::{ - check_community_mod_action, - check_expire_time, - check_is_higher_mod_or_admin, - remove_user_data_in_community, - }, + utils::{check_community_mod_action, check_expire_time, remove_user_data_in_community}, }; use lemmy_db_schema::{ source::{ @@ -19,6 +14,7 @@ use lemmy_db_schema::{ CommunityPersonBan, CommunityPersonBanForm, }, + local_user::LocalUser, moderator::{ModBanFromCommunity, ModBanFromCommunityForm}, }, traits::{Bannable, Crud, Followable}, @@ -49,10 +45,10 @@ pub async fn ban_from_community( ) .await?; - check_is_higher_mod_or_admin( + LocalUser::is_higher_mod_or_admin_check( &mut context.pool(), - &local_user_view, data.community_id, + local_user_view.person.id, vec![data.person_id], ) .await?; diff --git a/crates/api/src/local_user/add_admin.rs b/crates/api/src/local_user/add_admin.rs index 2aeb76d83..44b36fe66 100644 --- a/crates/api/src/local_user/add_admin.rs +++ b/crates/api/src/local_user/add_admin.rs @@ -2,7 +2,7 @@ use actix_web::web::{Data, Json}; use lemmy_api_common::{ context::LemmyContext, person::{AddAdmin, AddAdminResponse}, - utils::{check_is_higher_admin, is_admin}, + utils::is_admin, }; use lemmy_db_schema::{ source::{ @@ -26,7 +26,12 @@ pub async fn add_admin( // If its an admin removal, also check that you're a higher admin if !data.added { - check_is_higher_admin(&mut context.pool(), &local_user_view, vec![data.person_id]).await?; + LocalUser::is_higher_admin_check( + &mut context.pool(), + local_user_view.person.id, + vec![data.person_id], + ) + .await?; } // Make sure that the person_id added is local diff --git a/crates/api/src/local_user/ban_person.rs b/crates/api/src/local_user/ban_person.rs index ddd6eb4b5..58392cefd 100644 --- a/crates/api/src/local_user/ban_person.rs +++ b/crates/api/src/local_user/ban_person.rs @@ -5,10 +5,11 @@ use lemmy_api_common::{ context::LemmyContext, person::{BanPerson, BanPersonResponse}, send_activity::{ActivityChannel, SendActivityData}, - utils::{check_expire_time, check_is_higher_admin, is_admin, remove_user_data}, + utils::{check_expire_time, is_admin, remove_user_data}, }; use lemmy_db_schema::{ source::{ + local_user::LocalUser, login_token::LoginToken, moderator::{ModBan, ModBanForm}, person::{Person, PersonUpdateForm}, @@ -32,7 +33,12 @@ pub async fn ban_from_site( is_admin(&local_user_view)?; // Also make sure you're a higher admin than the target - check_is_higher_admin(&mut context.pool(), &local_user_view, vec![data.person_id]).await?; + LocalUser::is_higher_admin_check( + &mut context.pool(), + local_user_view.person.id, + vec![data.person_id], + ) + .await?; if let Some(reason) = &data.reason { is_valid_body_field(reason, false)?; diff --git a/crates/api/src/site/purge/comment.rs b/crates/api/src/site/purge/comment.rs index e67ea5550..9f90aff99 100644 --- a/crates/api/src/site/purge/comment.rs +++ b/crates/api/src/site/purge/comment.rs @@ -4,12 +4,13 @@ use lemmy_api_common::{ context::LemmyContext, send_activity::{ActivityChannel, SendActivityData}, site::PurgeComment, - utils::{check_is_higher_admin, is_admin}, + utils::is_admin, SuccessResponse, }; use lemmy_db_schema::{ source::{ comment::Comment, + local_user::LocalUser, moderator::{AdminPurgeComment, AdminPurgeCommentForm}, }, traits::Crud, @@ -38,9 +39,9 @@ pub async fn purge_comment( .ok_or(LemmyErrorType::CouldntFindComment)?; // Also check that you're a higher admin - check_is_higher_admin( + LocalUser::is_higher_admin_check( &mut context.pool(), - &local_user_view, + local_user_view.person.id, vec![comment_view.creator.id], ) .await?; diff --git a/crates/api/src/site/purge/community.rs b/crates/api/src/site/purge/community.rs index 4b95f7145..59eded6ad 100644 --- a/crates/api/src/site/purge/community.rs +++ b/crates/api/src/site/purge/community.rs @@ -5,13 +5,14 @@ use lemmy_api_common::{ request::purge_image_from_pictrs, send_activity::{ActivityChannel, SendActivityData}, site::PurgeCommunity, - utils::{check_is_higher_admin, is_admin, purge_image_posts_for_community}, + utils::{is_admin, purge_image_posts_for_community}, SuccessResponse, }; use lemmy_db_schema::{ newtypes::PersonId, source::{ community::Community, + local_user::LocalUser, moderator::{AdminPurgeCommunity, AdminPurgeCommunityForm}, }, traits::Crud, @@ -42,9 +43,9 @@ pub async fn purge_community( .map(|cmv| cmv.moderator.id) .collect::>(); - check_is_higher_admin( + LocalUser::is_higher_admin_check( &mut context.pool(), - &local_user_view, + local_user_view.person.id, community_mod_person_ids, ) .await?; diff --git a/crates/api/src/site/purge/person.rs b/crates/api/src/site/purge/person.rs index d9f56ec82..dc824b163 100644 --- a/crates/api/src/site/purge/person.rs +++ b/crates/api/src/site/purge/person.rs @@ -5,11 +5,12 @@ use lemmy_api_common::{ context::LemmyContext, send_activity::{ActivityChannel, SendActivityData}, site::PurgePerson, - utils::{check_is_higher_admin, is_admin, purge_user_account}, + utils::{is_admin, purge_user_account}, SuccessResponse, }; use lemmy_db_schema::{ source::{ + local_user::LocalUser, moderator::{AdminPurgePerson, AdminPurgePersonForm}, person::{Person, PersonUpdateForm}, }, @@ -28,7 +29,12 @@ pub async fn purge_person( is_admin(&local_user_view)?; // Also check that you're a higher admin - check_is_higher_admin(&mut context.pool(), &local_user_view, vec![data.person_id]).await?; + LocalUser::is_higher_admin_check( + &mut context.pool(), + local_user_view.person.id, + vec![data.person_id], + ) + .await?; let person = Person::read(&mut context.pool(), data.person_id) .await? diff --git a/crates/api/src/site/purge/post.rs b/crates/api/src/site/purge/post.rs index a94e381ac..6e512312f 100644 --- a/crates/api/src/site/purge/post.rs +++ b/crates/api/src/site/purge/post.rs @@ -5,11 +5,12 @@ use lemmy_api_common::{ request::purge_image_from_pictrs, send_activity::{ActivityChannel, SendActivityData}, site::PurgePost, - utils::{check_is_higher_admin, is_admin}, + utils::is_admin, SuccessResponse, }; use lemmy_db_schema::{ source::{ + local_user::LocalUser, moderator::{AdminPurgePost, AdminPurgePostForm}, post::Post, }, @@ -33,7 +34,12 @@ pub async fn purge_post( .ok_or(LemmyErrorType::CouldntFindPost)?; // Also check that you're a higher admin - check_is_higher_admin(&mut context.pool(), &local_user_view, vec![post.creator_id]).await?; + LocalUser::is_higher_admin_check( + &mut context.pool(), + local_user_view.person.id, + vec![post.creator_id], + ) + .await?; // Purge image if let Some(url) = &post.url { diff --git a/crates/api_common/src/utils.rs b/crates/api_common/src/utils.rs index 97916b2a7..97b12cc5b 100644 --- a/crates/api_common/src/utils.rs +++ b/crates/api_common/src/utils.rs @@ -23,7 +23,6 @@ use lemmy_db_schema::{ local_site::LocalSite, local_site_rate_limit::LocalSiteRateLimit, local_site_url_blocklist::LocalSiteUrlBlocklist, - local_user::LocalUser, password_reset_request::PasswordResetRequest, person::{Person, PersonUpdateForm}, person_block::PersonBlock, @@ -145,58 +144,6 @@ pub fn is_top_mod( } } -/// Checks to make sure the acting moderator is higher than the target moderator. -pub async fn check_is_higher_mod( - pool: &mut DbPool<'_>, - local_user_view: &LocalUserView, - community_id: CommunityId, - target_person_ids: Vec, -) -> LemmyResult<()> { - CommunityModerator::is_higher_mod_check( - pool, - community_id, - local_user_view.person.id, - target_person_ids, - ) - .await - .with_lemmy_type(LemmyErrorType::NotHigherMod)?; - - Ok(()) -} - -/// Checks to make sure the acting admin is higher than the target admin. -/// This needs to be done on admin removals, and all purge functions -pub async fn check_is_higher_admin( - pool: &mut DbPool<'_>, - local_user_view: &LocalUserView, - target_person_ids: Vec, -) -> LemmyResult<()> { - LocalUser::is_higher_admin_check(pool, local_user_view.person.id, target_person_ids) - .await - .with_lemmy_type(LemmyErrorType::NotHigherAdmin)?; - - Ok(()) -} - -/// Checks to make sure the acting mod or admin is higher than the target persons. -pub async fn check_is_higher_mod_or_admin( - pool: &mut DbPool<'_>, - local_user_view: &LocalUserView, - community_id: CommunityId, - target_person_ids: Vec, -) -> LemmyResult<()> { - LocalUser::is_higher_mod_or_admin_check( - pool, - community_id, - local_user_view.person.id, - target_person_ids, - ) - .await - .with_lemmy_type(LemmyErrorType::NotHigherMod)?; - - Ok(()) -} - /// Marks a post as read for a given person. #[tracing::instrument(skip_all)] pub async fn mark_post_as_read( diff --git a/crates/api_crud/src/comment/remove.rs b/crates/api_crud/src/comment/remove.rs index 4c236665d..f810c8275 100644 --- a/crates/api_crud/src/comment/remove.rs +++ b/crates/api_crud/src/comment/remove.rs @@ -5,12 +5,13 @@ use lemmy_api_common::{ comment::{CommentResponse, RemoveComment}, context::LemmyContext, send_activity::{ActivityChannel, SendActivityData}, - utils::{check_community_mod_action, check_is_higher_mod_or_admin}, + utils::check_community_mod_action, }; use lemmy_db_schema::{ source::{ comment::{Comment, CommentUpdateForm}, comment_report::CommentReport, + local_user::LocalUser, moderator::{ModRemoveComment, ModRemoveCommentForm}, }, traits::{Crud, Reportable}, @@ -41,10 +42,10 @@ pub async fn remove_comment( ) .await?; - check_is_higher_mod_or_admin( + LocalUser::is_higher_mod_or_admin_check( &mut context.pool(), - &local_user_view, orig_comment.community.id, + local_user_view.person.id, vec![orig_comment.creator.id], ) .await?; diff --git a/crates/api_crud/src/post/remove.rs b/crates/api_crud/src/post/remove.rs index 2fb25dbaf..b4fdba6fb 100644 --- a/crates/api_crud/src/post/remove.rs +++ b/crates/api_crud/src/post/remove.rs @@ -5,10 +5,11 @@ use lemmy_api_common::{ context::LemmyContext, post::{PostResponse, RemovePost}, send_activity::{ActivityChannel, SendActivityData}, - utils::{check_community_mod_action, check_is_higher_mod_or_admin}, + utils::check_community_mod_action, }; use lemmy_db_schema::{ source::{ + local_user::LocalUser, moderator::{ModRemovePost, ModRemovePostForm}, post::{Post, PostUpdateForm}, post_report::PostReport, @@ -37,10 +38,10 @@ pub async fn remove_post( ) .await?; - check_is_higher_mod_or_admin( + LocalUser::is_higher_mod_or_admin_check( &mut context.pool(), - &local_user_view, orig_post.community_id, + local_user_view.person.id, vec![orig_post.creator_id], ) .await?; diff --git a/crates/db_schema/src/impls/community.rs b/crates/db_schema/src/impls/community.rs index 681621184..eaf35a90d 100644 --- a/crates/db_schema/src/impls/community.rs +++ b/crates/db_schema/src/impls/community.rs @@ -49,6 +49,7 @@ use diesel::{ Queryable, }; use diesel_async::RunQueryDsl; +use lemmy_utils::error::{LemmyErrorType, LemmyResult}; #[async_trait] impl Crud for Community { @@ -241,7 +242,7 @@ impl CommunityModerator { for_community_id: CommunityId, mod_person_id: PersonId, target_person_ids: Vec, - ) -> Result<(), Error> { + ) -> LemmyResult<()> { let conn = &mut get_conn(pool).await?; // Build the list of persons @@ -261,7 +262,7 @@ impl CommunityModerator { if res.person_id == mod_person_id { Ok(()) } else { - Err(diesel::result::Error::NotFound) + Err(LemmyErrorType::NotHigherMod)? } } } diff --git a/crates/db_schema/src/impls/local_user.rs b/crates/db_schema/src/impls/local_user.rs index 6e0215ef4..32e8d50b7 100644 --- a/crates/db_schema/src/impls/local_user.rs +++ b/crates/db_schema/src/impls/local_user.rs @@ -25,6 +25,7 @@ use diesel::{ QueryDsl, }; use diesel_async::RunQueryDsl; +use lemmy_utils::error::{LemmyErrorType, LemmyResult}; impl LocalUser { pub async fn create( @@ -223,7 +224,7 @@ impl LocalUser { pool: &mut DbPool<'_>, admin_person_id: PersonId, target_person_ids: Vec, - ) -> Result<(), Error> { + ) -> LemmyResult<()> { let conn = &mut get_conn(pool).await?; // Build the list of persons @@ -243,7 +244,7 @@ impl LocalUser { if res.person_id == admin_person_id { Ok(()) } else { - Err(diesel::result::Error::NotFound) + Err(LemmyErrorType::NotHigherAdmin)? } } @@ -253,7 +254,7 @@ impl LocalUser { for_community_id: CommunityId, admin_person_id: PersonId, target_person_ids: Vec, - ) -> Result<(), Error> { + ) -> LemmyResult<()> { let conn = &mut get_conn(pool).await?; // Build the list of persons @@ -274,16 +275,13 @@ impl LocalUser { .select(community_moderator::person_id); let res = admins.union_all(mods).get_results::(conn).await?; - let first_person = res - .as_slice() - .first() - .ok_or(diesel::result::Error::NotFound)?; + let first_person = res.as_slice().first().ok_or(LemmyErrorType::NotHigherMod)?; // If the first result sorted by published is the acting mod if *first_person == admin_person_id { Ok(()) } else { - Err(diesel::result::Error::NotFound) + Err(LemmyErrorType::NotHigherMod)? } } }