Get rid of pointless wrapper functions, return lemmyresult directly.

This commit is contained in:
Dessalines 2024-07-15 13:31:19 -04:00
parent 85fe830d5e
commit a373c82595
13 changed files with 64 additions and 94 deletions

View file

@ -4,11 +4,12 @@ use lemmy_api_common::{
community::{AddModToCommunity, AddModToCommunityResponse}, community::{AddModToCommunity, AddModToCommunityResponse},
context::LemmyContext, context::LemmyContext,
send_activity::{ActivityChannel, SendActivityData}, send_activity::{ActivityChannel, SendActivityData},
utils::{check_community_mod_action, check_is_higher_mod_or_admin}, utils::check_community_mod_action,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
community::{Community, CommunityModerator, CommunityModeratorForm}, community::{Community, CommunityModerator, CommunityModeratorForm},
local_user::LocalUser,
moderator::{ModAddCommunity, ModAddCommunityForm}, moderator::{ModAddCommunity, ModAddCommunityForm},
}, },
traits::{Crud, Joinable}, 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 its a mod removal, also check that you're a higher mod.
if !data.added { if !data.added {
check_is_higher_mod_or_admin( LocalUser::is_higher_mod_or_admin_check(
&mut context.pool(), &mut context.pool(),
&local_user_view,
community_id, community_id,
local_user_view.person.id,
vec![data.person_id], vec![data.person_id],
) )
.await?; .await?;

View file

@ -4,12 +4,7 @@ use lemmy_api_common::{
community::{BanFromCommunity, BanFromCommunityResponse}, community::{BanFromCommunity, BanFromCommunityResponse},
context::LemmyContext, context::LemmyContext,
send_activity::{ActivityChannel, SendActivityData}, send_activity::{ActivityChannel, SendActivityData},
utils::{ utils::{check_community_mod_action, check_expire_time, remove_user_data_in_community},
check_community_mod_action,
check_expire_time,
check_is_higher_mod_or_admin,
remove_user_data_in_community,
},
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
@ -19,6 +14,7 @@ use lemmy_db_schema::{
CommunityPersonBan, CommunityPersonBan,
CommunityPersonBanForm, CommunityPersonBanForm,
}, },
local_user::LocalUser,
moderator::{ModBanFromCommunity, ModBanFromCommunityForm}, moderator::{ModBanFromCommunity, ModBanFromCommunityForm},
}, },
traits::{Bannable, Crud, Followable}, traits::{Bannable, Crud, Followable},
@ -49,10 +45,10 @@ pub async fn ban_from_community(
) )
.await?; .await?;
check_is_higher_mod_or_admin( LocalUser::is_higher_mod_or_admin_check(
&mut context.pool(), &mut context.pool(),
&local_user_view,
data.community_id, data.community_id,
local_user_view.person.id,
vec![data.person_id], vec![data.person_id],
) )
.await?; .await?;

View file

@ -2,7 +2,7 @@ use actix_web::web::{Data, Json};
use lemmy_api_common::{ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
person::{AddAdmin, AddAdminResponse}, person::{AddAdmin, AddAdminResponse},
utils::{check_is_higher_admin, is_admin}, utils::is_admin,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
@ -26,7 +26,12 @@ pub async fn add_admin(
// If its an admin removal, also check that you're a higher admin // If its an admin removal, also check that you're a higher admin
if !data.added { 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 // Make sure that the person_id added is local

View file

@ -5,10 +5,11 @@ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
person::{BanPerson, BanPersonResponse}, person::{BanPerson, BanPersonResponse},
send_activity::{ActivityChannel, SendActivityData}, 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::{ use lemmy_db_schema::{
source::{ source::{
local_user::LocalUser,
login_token::LoginToken, login_token::LoginToken,
moderator::{ModBan, ModBanForm}, moderator::{ModBan, ModBanForm},
person::{Person, PersonUpdateForm}, person::{Person, PersonUpdateForm},
@ -32,7 +33,12 @@ pub async fn ban_from_site(
is_admin(&local_user_view)?; is_admin(&local_user_view)?;
// Also make sure you're a higher admin than the target // 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 { if let Some(reason) = &data.reason {
is_valid_body_field(reason, false)?; is_valid_body_field(reason, false)?;

View file

@ -4,12 +4,13 @@ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
send_activity::{ActivityChannel, SendActivityData}, send_activity::{ActivityChannel, SendActivityData},
site::PurgeComment, site::PurgeComment,
utils::{check_is_higher_admin, is_admin}, utils::is_admin,
SuccessResponse, SuccessResponse,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
comment::Comment, comment::Comment,
local_user::LocalUser,
moderator::{AdminPurgeComment, AdminPurgeCommentForm}, moderator::{AdminPurgeComment, AdminPurgeCommentForm},
}, },
traits::Crud, traits::Crud,
@ -38,9 +39,9 @@ pub async fn purge_comment(
.ok_or(LemmyErrorType::CouldntFindComment)?; .ok_or(LemmyErrorType::CouldntFindComment)?;
// Also check that you're a higher admin // Also check that you're a higher admin
check_is_higher_admin( LocalUser::is_higher_admin_check(
&mut context.pool(), &mut context.pool(),
&local_user_view, local_user_view.person.id,
vec![comment_view.creator.id], vec![comment_view.creator.id],
) )
.await?; .await?;

View file

@ -5,13 +5,14 @@ use lemmy_api_common::{
request::purge_image_from_pictrs, request::purge_image_from_pictrs,
send_activity::{ActivityChannel, SendActivityData}, send_activity::{ActivityChannel, SendActivityData},
site::PurgeCommunity, site::PurgeCommunity,
utils::{check_is_higher_admin, is_admin, purge_image_posts_for_community}, utils::{is_admin, purge_image_posts_for_community},
SuccessResponse, SuccessResponse,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::PersonId, newtypes::PersonId,
source::{ source::{
community::Community, community::Community,
local_user::LocalUser,
moderator::{AdminPurgeCommunity, AdminPurgeCommunityForm}, moderator::{AdminPurgeCommunity, AdminPurgeCommunityForm},
}, },
traits::Crud, traits::Crud,
@ -42,9 +43,9 @@ pub async fn purge_community(
.map(|cmv| cmv.moderator.id) .map(|cmv| cmv.moderator.id)
.collect::<Vec<PersonId>>(); .collect::<Vec<PersonId>>();
check_is_higher_admin( LocalUser::is_higher_admin_check(
&mut context.pool(), &mut context.pool(),
&local_user_view, local_user_view.person.id,
community_mod_person_ids, community_mod_person_ids,
) )
.await?; .await?;

View file

@ -5,11 +5,12 @@ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
send_activity::{ActivityChannel, SendActivityData}, send_activity::{ActivityChannel, SendActivityData},
site::PurgePerson, site::PurgePerson,
utils::{check_is_higher_admin, is_admin, purge_user_account}, utils::{is_admin, purge_user_account},
SuccessResponse, SuccessResponse,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
local_user::LocalUser,
moderator::{AdminPurgePerson, AdminPurgePersonForm}, moderator::{AdminPurgePerson, AdminPurgePersonForm},
person::{Person, PersonUpdateForm}, person::{Person, PersonUpdateForm},
}, },
@ -28,7 +29,12 @@ pub async fn purge_person(
is_admin(&local_user_view)?; is_admin(&local_user_view)?;
// Also check that you're a higher admin // 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) let person = Person::read(&mut context.pool(), data.person_id)
.await? .await?

View file

@ -5,11 +5,12 @@ use lemmy_api_common::{
request::purge_image_from_pictrs, request::purge_image_from_pictrs,
send_activity::{ActivityChannel, SendActivityData}, send_activity::{ActivityChannel, SendActivityData},
site::PurgePost, site::PurgePost,
utils::{check_is_higher_admin, is_admin}, utils::is_admin,
SuccessResponse, SuccessResponse,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
local_user::LocalUser,
moderator::{AdminPurgePost, AdminPurgePostForm}, moderator::{AdminPurgePost, AdminPurgePostForm},
post::Post, post::Post,
}, },
@ -33,7 +34,12 @@ pub async fn purge_post(
.ok_or(LemmyErrorType::CouldntFindPost)?; .ok_or(LemmyErrorType::CouldntFindPost)?;
// Also check that you're a higher admin // 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 // Purge image
if let Some(url) = &post.url { if let Some(url) = &post.url {

View file

@ -23,7 +23,6 @@ use lemmy_db_schema::{
local_site::LocalSite, local_site::LocalSite,
local_site_rate_limit::LocalSiteRateLimit, local_site_rate_limit::LocalSiteRateLimit,
local_site_url_blocklist::LocalSiteUrlBlocklist, local_site_url_blocklist::LocalSiteUrlBlocklist,
local_user::LocalUser,
password_reset_request::PasswordResetRequest, password_reset_request::PasswordResetRequest,
person::{Person, PersonUpdateForm}, person::{Person, PersonUpdateForm},
person_block::PersonBlock, 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<PersonId>,
) -> 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<PersonId>,
) -> 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<PersonId>,
) -> 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. /// Marks a post as read for a given person.
#[tracing::instrument(skip_all)] #[tracing::instrument(skip_all)]
pub async fn mark_post_as_read( pub async fn mark_post_as_read(

View file

@ -5,12 +5,13 @@ use lemmy_api_common::{
comment::{CommentResponse, RemoveComment}, comment::{CommentResponse, RemoveComment},
context::LemmyContext, context::LemmyContext,
send_activity::{ActivityChannel, SendActivityData}, send_activity::{ActivityChannel, SendActivityData},
utils::{check_community_mod_action, check_is_higher_mod_or_admin}, utils::check_community_mod_action,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
comment::{Comment, CommentUpdateForm}, comment::{Comment, CommentUpdateForm},
comment_report::CommentReport, comment_report::CommentReport,
local_user::LocalUser,
moderator::{ModRemoveComment, ModRemoveCommentForm}, moderator::{ModRemoveComment, ModRemoveCommentForm},
}, },
traits::{Crud, Reportable}, traits::{Crud, Reportable},
@ -41,10 +42,10 @@ pub async fn remove_comment(
) )
.await?; .await?;
check_is_higher_mod_or_admin( LocalUser::is_higher_mod_or_admin_check(
&mut context.pool(), &mut context.pool(),
&local_user_view,
orig_comment.community.id, orig_comment.community.id,
local_user_view.person.id,
vec![orig_comment.creator.id], vec![orig_comment.creator.id],
) )
.await?; .await?;

View file

@ -5,10 +5,11 @@ use lemmy_api_common::{
context::LemmyContext, context::LemmyContext,
post::{PostResponse, RemovePost}, post::{PostResponse, RemovePost},
send_activity::{ActivityChannel, SendActivityData}, send_activity::{ActivityChannel, SendActivityData},
utils::{check_community_mod_action, check_is_higher_mod_or_admin}, utils::check_community_mod_action,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
local_user::LocalUser,
moderator::{ModRemovePost, ModRemovePostForm}, moderator::{ModRemovePost, ModRemovePostForm},
post::{Post, PostUpdateForm}, post::{Post, PostUpdateForm},
post_report::PostReport, post_report::PostReport,
@ -37,10 +38,10 @@ pub async fn remove_post(
) )
.await?; .await?;
check_is_higher_mod_or_admin( LocalUser::is_higher_mod_or_admin_check(
&mut context.pool(), &mut context.pool(),
&local_user_view,
orig_post.community_id, orig_post.community_id,
local_user_view.person.id,
vec![orig_post.creator_id], vec![orig_post.creator_id],
) )
.await?; .await?;

View file

@ -49,6 +49,7 @@ use diesel::{
Queryable, Queryable,
}; };
use diesel_async::RunQueryDsl; use diesel_async::RunQueryDsl;
use lemmy_utils::error::{LemmyErrorType, LemmyResult};
#[async_trait] #[async_trait]
impl Crud for Community { impl Crud for Community {
@ -241,7 +242,7 @@ impl CommunityModerator {
for_community_id: CommunityId, for_community_id: CommunityId,
mod_person_id: PersonId, mod_person_id: PersonId,
target_person_ids: Vec<PersonId>, target_person_ids: Vec<PersonId>,
) -> Result<(), Error> { ) -> LemmyResult<()> {
let conn = &mut get_conn(pool).await?; let conn = &mut get_conn(pool).await?;
// Build the list of persons // Build the list of persons
@ -261,7 +262,7 @@ impl CommunityModerator {
if res.person_id == mod_person_id { if res.person_id == mod_person_id {
Ok(()) Ok(())
} else { } else {
Err(diesel::result::Error::NotFound) Err(LemmyErrorType::NotHigherMod)?
} }
} }
} }

View file

@ -25,6 +25,7 @@ use diesel::{
QueryDsl, QueryDsl,
}; };
use diesel_async::RunQueryDsl; use diesel_async::RunQueryDsl;
use lemmy_utils::error::{LemmyErrorType, LemmyResult};
impl LocalUser { impl LocalUser {
pub async fn create( pub async fn create(
@ -223,7 +224,7 @@ impl LocalUser {
pool: &mut DbPool<'_>, pool: &mut DbPool<'_>,
admin_person_id: PersonId, admin_person_id: PersonId,
target_person_ids: Vec<PersonId>, target_person_ids: Vec<PersonId>,
) -> Result<(), Error> { ) -> LemmyResult<()> {
let conn = &mut get_conn(pool).await?; let conn = &mut get_conn(pool).await?;
// Build the list of persons // Build the list of persons
@ -243,7 +244,7 @@ impl LocalUser {
if res.person_id == admin_person_id { if res.person_id == admin_person_id {
Ok(()) Ok(())
} else { } else {
Err(diesel::result::Error::NotFound) Err(LemmyErrorType::NotHigherAdmin)?
} }
} }
@ -253,7 +254,7 @@ impl LocalUser {
for_community_id: CommunityId, for_community_id: CommunityId,
admin_person_id: PersonId, admin_person_id: PersonId,
target_person_ids: Vec<PersonId>, target_person_ids: Vec<PersonId>,
) -> Result<(), Error> { ) -> LemmyResult<()> {
let conn = &mut get_conn(pool).await?; let conn = &mut get_conn(pool).await?;
// Build the list of persons // Build the list of persons
@ -274,16 +275,13 @@ impl LocalUser {
.select(community_moderator::person_id); .select(community_moderator::person_id);
let res = admins.union_all(mods).get_results::<PersonId>(conn).await?; let res = admins.union_all(mods).get_results::<PersonId>(conn).await?;
let first_person = res let first_person = res.as_slice().first().ok_or(LemmyErrorType::NotHigherMod)?;
.as_slice()
.first()
.ok_or(diesel::result::Error::NotFound)?;
// If the first result sorted by published is the acting mod // If the first result sorted by published is the acting mod
if *first_person == admin_person_id { if *first_person == admin_person_id {
Ok(()) Ok(())
} else { } else {
Err(diesel::result::Error::NotFound) Err(LemmyErrorType::NotHigherMod)?
} }
} }
} }