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},
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?;

View file

@ -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?;

View file

@ -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

View file

@ -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)?;

View file

@ -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?;

View file

@ -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::<Vec<PersonId>>();
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?;

View file

@ -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?

View file

@ -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 {

View file

@ -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<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.
#[tracing::instrument(skip_all)]
pub async fn mark_post_as_read(

View file

@ -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?;

View file

@ -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?;

View file

@ -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<PersonId>,
) -> 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)?
}
}
}

View file

@ -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<PersonId>,
) -> 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<PersonId>,
) -> 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::<PersonId>(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)?
}
}
}