Removing return Err(... where feasible.

This commit is contained in:
Dessalines 2023-08-24 14:17:40 -04:00
parent fe26cf7226
commit 3988b0ece7
44 changed files with 109 additions and 103 deletions

View file

@ -29,7 +29,7 @@ pub async fn add_mod_to_community(
is_mod_or_admin(&mut context.pool(), local_user_view.person.id, community_id).await?; is_mod_or_admin(&mut context.pool(), local_user_view.person.id, community_id).await?;
let community = Community::read(&mut context.pool(), community_id).await?; let community = Community::read(&mut context.pool(), community_id).await?;
if local_user_view.local_user.admin && !community.local { if local_user_view.local_user.admin && !community.local {
return Err(LemmyErrorType::NotAModerator)?; Err(LemmyErrorType::NotAModerator)?
} }
// Update in local database // Update in local database

View file

@ -42,7 +42,7 @@ impl Perform for TransferCommunity {
if !(is_top_mod(&local_user_view, &community_mods).is_ok() if !(is_top_mod(&local_user_view, &community_mods).is_ok()
|| is_admin(&local_user_view).is_ok()) || is_admin(&local_user_view).is_ok())
{ {
return Err(LemmyErrorType::NotAnAdmin)?; Err(LemmyErrorType::NotAnAdmin)?
} }
// You have to re-do the community_moderator table, reordering it. // You have to re-do the community_moderator table, reordering it.

View file

@ -47,18 +47,18 @@ pub(crate) fn captcha_as_wav_base64(captcha: &Captcha) -> Result<String, LemmyEr
// Encode the concatenated result as a wav file // Encode the concatenated result as a wav file
let mut output_buffer = Cursor::new(vec![]); let mut output_buffer = Cursor::new(vec![]);
let header = match any_header { if let Some(header) = any_header {
Some(header) => header, wav::write(
None => return Err(LemmyErrorType::CouldntCreateAudioCaptcha)?, header,
}; &wav::BitDepth::Sixteen(concat_samples),
wav::write( &mut output_buffer,
header, )
&wav::BitDepth::Sixteen(concat_samples), .with_lemmy_type(LemmyErrorType::CouldntCreateAudioCaptcha)?;
&mut output_buffer,
)
.with_lemmy_type(LemmyErrorType::CouldntCreateAudioCaptcha)?;
Ok(base64.encode(output_buffer.into_inner())) Ok(base64.encode(output_buffer.into_inner()))
} else {
Err(LemmyErrorType::CouldntCreateAudioCaptcha)?
}
} }
/// Check size of report /// Check size of report

View file

@ -27,7 +27,7 @@ impl Perform for BlockPerson {
// Don't let a person block themselves // Don't let a person block themselves
if target_id == person_id { if target_id == person_id {
return Err(LemmyErrorType::CantBlockYourself)?; Err(LemmyErrorType::CantBlockYourself)?
} }
let person_block_form = PersonBlockForm { let person_block_form = PersonBlockForm {
@ -37,7 +37,7 @@ impl Perform for BlockPerson {
let target_user = LocalUserView::read_person(&mut context.pool(), target_id).await; let target_user = LocalUserView::read_person(&mut context.pool(), target_id).await;
if target_user.map(|t| t.local_user.admin) == Ok(true) { if target_user.map(|t| t.local_user.admin) == Ok(true) {
return Err(LemmyErrorType::CantBlockAdmin)?; Err(LemmyErrorType::CantBlockAdmin)?
} }
if data.block { if data.block {

View file

@ -25,7 +25,7 @@ impl Perform for ChangePassword {
// Make sure passwords match // Make sure passwords match
if data.new_password != data.new_password_verify { if data.new_password != data.new_password_verify {
return Err(LemmyErrorType::PasswordsDoNotMatch)?; Err(LemmyErrorType::PasswordsDoNotMatch)?
} }
// Check the old password // Check the old password
@ -35,7 +35,7 @@ impl Perform for ChangePassword {
) )
.unwrap_or(false); .unwrap_or(false);
if !valid { if !valid {
return Err(LemmyErrorType::IncorrectLogin)?; Err(LemmyErrorType::IncorrectLogin)?
} }
let local_user_id = local_user_view.local_user.id; let local_user_id = local_user_view.local_user.id;

View file

@ -29,7 +29,7 @@ impl Perform for PasswordChangeAfterReset {
// Make sure passwords match // Make sure passwords match
if data.password != data.password_verify { if data.password != data.password_verify {
return Err(LemmyErrorType::PasswordsDoNotMatch)?; Err(LemmyErrorType::PasswordsDoNotMatch)?
} }
// Update the user with the new password // Update the user with the new password

View file

@ -37,7 +37,7 @@ impl Perform for Login {
) )
.unwrap_or(false); .unwrap_or(false);
if !valid { if !valid {
return Err(LemmyErrorType::IncorrectLogin)?; Err(LemmyErrorType::IncorrectLogin)?
} }
check_user_valid( check_user_valid(
local_user_view.person.banned, local_user_view.person.banned,
@ -51,7 +51,7 @@ impl Perform for Login {
&& site_view.local_site.require_email_verification && site_view.local_site.require_email_verification
&& !local_user_view.local_user.email_verified && !local_user_view.local_user.email_verified
{ {
return Err(LemmyErrorType::EmailNotVerified)?; Err(LemmyErrorType::EmailNotVerified)?
} }
check_registration_application(&local_user_view, &site_view.local_site, &mut context.pool()) check_registration_application(&local_user_view, &site_view.local_site, &mut context.pool())

View file

@ -28,7 +28,7 @@ impl Perform for MarkPersonMentionAsRead {
let read_person_mention = PersonMention::read(&mut context.pool(), person_mention_id).await?; let read_person_mention = PersonMention::read(&mut context.pool(), person_mention_id).await?;
if local_user_view.person.id != read_person_mention.recipient_id { if local_user_view.person.id != read_person_mention.recipient_id {
return Err(LemmyErrorType::CouldntUpdateComment)?; Err(LemmyErrorType::CouldntUpdateComment)?
} }
let person_mention_id = read_person_mention.id; let person_mention_id = read_person_mention.id;

View file

@ -22,7 +22,7 @@ pub async fn mark_reply_as_read(
let read_comment_reply = CommentReply::read(&mut context.pool(), comment_reply_id).await?; let read_comment_reply = CommentReply::read(&mut context.pool(), comment_reply_id).await?;
if local_user_view.person.id != read_comment_reply.recipient_id { if local_user_view.person.id != read_comment_reply.recipient_id {
return Err(LemmyErrorType::CouldntUpdateComment)?; Err(LemmyErrorType::CouldntUpdateComment)?
} }
let comment_reply_id = read_comment_reply.id; let comment_reply_id = read_comment_reply.id;

View file

@ -33,7 +33,7 @@ impl Perform for PasswordReset {
) )
.await?; .await?;
if recent_resets_count >= 3 { if recent_resets_count >= 3 {
return Err(LemmyErrorType::PasswordResetLimitReached)?; Err(LemmyErrorType::PasswordResetLimitReached)?
} }
// Email the pure token to the user. // Email the pure token to the user.

View file

@ -65,7 +65,7 @@ impl Perform for SaveUserSettings {
// When the site requires email, make sure email is not Some(None). IE, an overwrite to a None value // When the site requires email, make sure email is not Some(None). IE, an overwrite to a None value
if let Some(email) = &email { if let Some(email) = &email {
if email.is_none() && site_view.local_site.require_email_verification { if email.is_none() && site_view.local_site.require_email_verification {
return Err(LemmyErrorType::EmailRequired)?; Err(LemmyErrorType::EmailRequired)?
} }
} }

View file

@ -29,7 +29,7 @@ impl Perform for MarkPrivateMessageAsRead {
let orig_private_message = let orig_private_message =
PrivateMessage::read(&mut context.pool(), private_message_id).await?; PrivateMessage::read(&mut context.pool(), private_message_id).await?;
if local_user_view.person.id != orig_private_message.recipient_id { if local_user_view.person.id != orig_private_message.recipient_id {
return Err(LemmyErrorType::CouldntUpdatePrivateMessage)?; Err(LemmyErrorType::CouldntUpdatePrivateMessage)?
} }
// Doing the update // Doing the update

View file

@ -36,7 +36,7 @@ impl Perform for LeaveAdmin {
// Make sure there isn't just one admin (so if one leaves, there will still be one left) // Make sure there isn't just one admin (so if one leaves, there will still be one left)
let admins = PersonView::admins(&mut context.pool()).await?; let admins = PersonView::admins(&mut context.pool()).await?;
if admins.len() == 1 { if admins.len() == 1 {
return Err(LemmyErrorType::CannotLeaveAdmin)?; Err(LemmyErrorType::CannotLeaveAdmin)?
} }
LocalUser::update( LocalUser::update(

View file

@ -44,7 +44,7 @@ fn html_to_site_metadata(html_bytes: &[u8], url: &Url) -> Result<SiteMetadata, L
.to_lowercase(); .to_lowercase();
if !first_line.starts_with("<!doctype html>") { if !first_line.starts_with("<!doctype html>") {
Err(LemmyErrorType::SiteMetadataPageIsNotDoctypeHtml)?; Err(LemmyErrorType::SiteMetadataPageIsNotDoctypeHtml)?
} }
let mut page = HTML::from_string(html.to_string(), None)?; let mut page = HTML::from_string(html.to_string(), None)?;

View file

@ -65,7 +65,7 @@ pub async fn create_comment(
// Check if post is locked, no new comments // Check if post is locked, no new comments
if post.locked { if post.locked {
return Err(LemmyErrorType::Locked)?; Err(LemmyErrorType::Locked)?
} }
// Fetch the parent, if it exists // Fetch the parent, if it exists
@ -79,7 +79,7 @@ pub async fn create_comment(
// Strange issue where sometimes the post ID of the parent comment is incorrect // Strange issue where sometimes the post ID of the parent comment is incorrect
if let Some(parent) = parent_opt.as_ref() { if let Some(parent) = parent_opt.as_ref() {
if parent.post_id != post_id { if parent.post_id != post_id {
return Err(LemmyErrorType::CouldntCreateComment)?; Err(LemmyErrorType::CouldntCreateComment)?
} }
check_comment_depth(parent)?; check_comment_depth(parent)?;
} }

View file

@ -29,7 +29,7 @@ pub async fn delete_comment(
// Dont delete it if its already been deleted. // Dont delete it if its already been deleted.
if orig_comment.comment.deleted == data.deleted { if orig_comment.comment.deleted == data.deleted {
return Err(LemmyErrorType::CouldntUpdateComment)?; Err(LemmyErrorType::CouldntUpdateComment)?
} }
check_community_ban( check_community_ban(
@ -41,7 +41,7 @@ pub async fn delete_comment(
// Verify that only the creator can delete // Verify that only the creator can delete
if local_user_view.person.id != orig_comment.creator.id { if local_user_view.person.id != orig_comment.creator.id {
return Err(LemmyErrorType::NoCommentEditAllowed)?; Err(LemmyErrorType::NoCommentEditAllowed)?
} }
// Do the delete // Do the delete

View file

@ -51,7 +51,7 @@ pub async fn update_comment(
// Verify that only the creator can edit // Verify that only the creator can edit
if local_user_view.person.id != orig_comment.creator.id { if local_user_view.person.id != orig_comment.creator.id {
return Err(LemmyErrorType::NoCommentEditAllowed)?; Err(LemmyErrorType::NoCommentEditAllowed)?
} }
let language_id = data.language_id; let language_id = data.language_id;

View file

@ -51,7 +51,7 @@ pub async fn create_community(
let local_site = site_view.local_site; let local_site = site_view.local_site;
if local_site.community_creation_admin_only && is_admin(&local_user_view).is_err() { if local_site.community_creation_admin_only && is_admin(&local_user_view).is_err() {
return Err(LemmyErrorType::OnlyAdminsCanCreateCommunities)?; Err(LemmyErrorType::OnlyAdminsCanCreateCommunities)?
} }
// Check to make sure the icon and banners are urls // Check to make sure the icon and banners are urls
@ -79,7 +79,7 @@ pub async fn create_community(
let community_dupe = let community_dupe =
Community::read_from_apub_id(&mut context.pool(), &community_actor_id).await?; Community::read_from_apub_id(&mut context.pool(), &community_actor_id).await?;
if community_dupe.is_some() { if community_dupe.is_some() {
return Err(LemmyErrorType::CommunityAlreadyExists)?; Err(LemmyErrorType::CommunityAlreadyExists)?
} }
// When you create a community, make sure the user becomes a moderator and a follower // When you create a community, make sure the user becomes a moderator and a follower
@ -135,7 +135,7 @@ pub async fn create_community(
// https://stackoverflow.com/a/64227550 // https://stackoverflow.com/a/64227550
let is_subset = languages.iter().all(|item| site_languages.contains(item)); let is_subset = languages.iter().all(|item| site_languages.contains(item));
if !is_subset { if !is_subset {
return Err(LemmyErrorType::LanguageNotAllowed)?; Err(LemmyErrorType::LanguageNotAllowed)?
} }
CommunityLanguage::update(&mut context.pool(), languages, community_id).await?; CommunityLanguage::update(&mut context.pool(), languages, community_id).await?;
} }

View file

@ -50,7 +50,7 @@ pub async fn update_community(
.await .await
.map(|v| v.into_iter().map(|m| m.moderator.id).collect())?; .map(|v| v.into_iter().map(|m| m.moderator.id).collect())?;
if !mods.contains(&local_user_view.person.id) { if !mods.contains(&local_user_view.person.id) {
return Err(LemmyErrorType::NotAModerator)?; Err(LemmyErrorType::NotAModerator)?
} }
let community_id = data.community_id; let community_id = data.community_id;
@ -60,7 +60,7 @@ pub async fn update_community(
// https://stackoverflow.com/a/64227550 // https://stackoverflow.com/a/64227550
let is_subset = languages.iter().all(|item| site_languages.contains(item)); let is_subset = languages.iter().all(|item| site_languages.contains(item));
if !is_subset { if !is_subset {
return Err(LemmyErrorType::LanguageNotAllowed)?; Err(LemmyErrorType::LanguageNotAllowed)?
} }
CommunityLanguage::update(&mut context.pool(), languages, community_id).await?; CommunityLanguage::update(&mut context.pool(), languages, community_id).await?;
} }

View file

@ -82,7 +82,7 @@ pub async fn create_post(
) )
.await?; .await?;
if !is_mod { if !is_mod {
return Err(LemmyErrorType::OnlyModsCanPostInCommunity)?; Err(LemmyErrorType::OnlyModsCanPostInCommunity)?
} }
} }

View file

@ -25,7 +25,7 @@ pub async fn delete_post(
// Dont delete it if its already been deleted. // Dont delete it if its already been deleted.
if orig_post.deleted == data.deleted { if orig_post.deleted == data.deleted {
return Err(LemmyErrorType::CouldntUpdatePost)?; Err(LemmyErrorType::CouldntUpdatePost)?
} }
check_community_ban( check_community_ban(
@ -38,7 +38,7 @@ pub async fn delete_post(
// Verify that only the creator can delete // Verify that only the creator can delete
if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) { if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
return Err(LemmyErrorType::NoPostEditAllowed)?; Err(LemmyErrorType::NoPostEditAllowed)?
} }
// Update the post // Update the post

View file

@ -68,7 +68,7 @@ pub async fn update_post(
// Verify that only the creator can edit // Verify that only the creator can edit
if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) { if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
return Err(LemmyErrorType::NoPostEditAllowed)?; Err(LemmyErrorType::NoPostEditAllowed)?
} }
// Fetch post links and Pictrs cached image // Fetch post links and Pictrs cached image

View file

@ -24,7 +24,7 @@ pub async fn delete_private_message(
let private_message_id = data.private_message_id; let private_message_id = data.private_message_id;
let orig_private_message = PrivateMessage::read(&mut context.pool(), private_message_id).await?; let orig_private_message = PrivateMessage::read(&mut context.pool(), private_message_id).await?;
if local_user_view.person.id != orig_private_message.creator_id { if local_user_view.person.id != orig_private_message.creator_id {
return Err(LemmyErrorType::EditPrivateMessageNotAllowed)?; Err(LemmyErrorType::EditPrivateMessageNotAllowed)?
} }
// Doing the update // Doing the update

View file

@ -32,7 +32,7 @@ pub async fn update_private_message(
let private_message_id = data.private_message_id; let private_message_id = data.private_message_id;
let orig_private_message = PrivateMessage::read(&mut context.pool(), private_message_id).await?; let orig_private_message = PrivateMessage::read(&mut context.pool(), private_message_id).await?;
if local_user_view.person.id != orig_private_message.creator_id { if local_user_view.person.id != orig_private_message.creator_id {
return Err(LemmyErrorType::EditPrivateMessageNotAllowed)?; Err(LemmyErrorType::EditPrivateMessageNotAllowed)?
} }
// Doing the update // Doing the update

View file

@ -48,23 +48,23 @@ pub async fn register(
local_site.registration_mode == RegistrationMode::RequireApplication; local_site.registration_mode == RegistrationMode::RequireApplication;
if local_site.registration_mode == RegistrationMode::Closed { if local_site.registration_mode == RegistrationMode::Closed {
return Err(LemmyErrorType::RegistrationClosed)?; Err(LemmyErrorType::RegistrationClosed)?
} }
password_length_check(&data.password)?; password_length_check(&data.password)?;
honeypot_check(&data.honeypot)?; honeypot_check(&data.honeypot)?;
if local_site.require_email_verification && data.email.is_none() { if local_site.require_email_verification && data.email.is_none() {
return Err(LemmyErrorType::EmailRequired)?; Err(LemmyErrorType::EmailRequired)?
} }
if local_site.site_setup && require_registration_application && data.answer.is_none() { if local_site.site_setup && require_registration_application && data.answer.is_none() {
return Err(LemmyErrorType::RegistrationApplicationAnswerRequired)?; Err(LemmyErrorType::RegistrationApplicationAnswerRequired)?
} }
// Make sure passwords match // Make sure passwords match
if data.password != data.password_verify { if data.password != data.password_verify {
return Err(LemmyErrorType::PasswordsDoNotMatch)?; Err(LemmyErrorType::PasswordsDoNotMatch)?
} }
if local_site.site_setup && local_site.captcha_enabled { if local_site.site_setup && local_site.captcha_enabled {
@ -79,10 +79,10 @@ pub async fn register(
) )
.await?; .await?;
if !check { if !check {
return Err(LemmyErrorType::CaptchaIncorrect)?; Err(LemmyErrorType::CaptchaIncorrect)?
} }
} else { } else {
return Err(LemmyErrorType::CaptchaIncorrect)?; Err(LemmyErrorType::CaptchaIncorrect)?
} }
} }
@ -101,7 +101,7 @@ pub async fn register(
if let Some(email) = &data.email { if let Some(email) = &data.email {
if LocalUser::is_email_taken(&mut context.pool(), email).await? { if LocalUser::is_email_taken(&mut context.pool(), email).await? {
return Err(LemmyErrorType::EmailAlreadyExists)?; Err(LemmyErrorType::EmailAlreadyExists)?
} }
} }

View file

@ -23,7 +23,7 @@ pub async fn delete_account(
) )
.unwrap_or(false); .unwrap_or(false);
if !valid { if !valid {
return Err(LemmyErrorType::IncorrectLogin)?; Err(LemmyErrorType::IncorrectLogin)?
} }
ActivityChannel::submit_activity( ActivityChannel::submit_activity(

View file

@ -48,7 +48,7 @@ impl ActivityHandler for RawAnnouncableActivities {
let activity: AnnouncableActivities = self.clone().try_into()?; let activity: AnnouncableActivities = self.clone().try_into()?;
// This is only for sending, not receiving so we reject it. // This is only for sending, not receiving so we reject it.
if let AnnouncableActivities::Page(_) = activity { if let AnnouncableActivities::Page(_) = activity {
return Err(LemmyErrorType::CannotReceivePage)?; Err(LemmyErrorType::CannotReceivePage)?
} }
// verify and receive activity // verify and receive activity
@ -146,7 +146,7 @@ impl ActivityHandler for AnnounceActivity {
let object: AnnouncableActivities = self.object.object(context).await?.try_into()?; let object: AnnouncableActivities = self.object.object(context).await?.try_into()?;
// This is only for sending, not receiving so we reject it. // This is only for sending, not receiving so we reject it.
if let AnnouncableActivities::Page(_) = object { if let AnnouncableActivities::Page(_) = object {
return Err(LemmyErrorType::CannotReceivePage)?; Err(LemmyErrorType::CannotReceivePage)?
} }
// verify here in order to avoid fetching the object twice over http // verify here in order to avoid fetching the object twice over http

View file

@ -120,7 +120,7 @@ impl ActivityHandler for CreateOrUpdatePage {
// because then we will definitely receive all create and update activities separately. // because then we will definitely receive all create and update activities separately.
let is_locked = self.object.comments_enabled == Some(false); let is_locked = self.object.comments_enabled == Some(false);
if community.local && is_locked { if community.local && is_locked {
return Err(LemmyErrorType::NewPostCannotBeLocked)?; Err(LemmyErrorType::NewPostCannotBeLocked)?
} }
} }
CreateOrUpdateType::Update => { CreateOrUpdateType::Update => {

View file

@ -112,7 +112,7 @@ pub(in crate::activities) async fn receive_remove_action(
match DeletableObjects::read_from_db(object, context).await? { match DeletableObjects::read_from_db(object, context).await? {
DeletableObjects::Community(community) => { DeletableObjects::Community(community) => {
if community.local { if community.local {
return Err(LemmyErrorType::OnlyLocalAdminCanRemoveCommunity)?; Err(LemmyErrorType::OnlyLocalAdminCanRemoveCommunity)?
} }
let form = ModRemoveCommunityForm { let form = ModRemoveCommunityForm {
mod_person_id: actor.id, mod_person_id: actor.id,

View file

@ -100,7 +100,7 @@ impl UndoDelete {
match DeletableObjects::read_from_db(object, context).await? { match DeletableObjects::read_from_db(object, context).await? {
DeletableObjects::Community(community) => { DeletableObjects::Community(community) => {
if community.local { if community.local {
return Err(LemmyErrorType::OnlyLocalAdminCanRestoreCommunity)?; Err(LemmyErrorType::OnlyLocalAdminCanRestoreCommunity)?
} }
let form = ModRemoveCommunityForm { let form = ModRemoveCommunityForm {
mod_person_id: actor.id, mod_person_id: actor.id,

View file

@ -81,10 +81,11 @@ async fn verify_person(
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let person = person_id.dereference(context).await?; let person = person_id.dereference(context).await?;
if person.banned { if person.banned {
return Err(anyhow!("Person {} is banned", person_id)) Err(anyhow!("Person {} is banned", person_id))
.with_lemmy_type(LemmyErrorType::CouldntUpdateComment); .with_lemmy_type(LemmyErrorType::CouldntUpdateComment)
} else {
Ok(())
} }
Ok(())
} }
/// Fetches the person and community to verify their type, then checks if person is banned from site /// Fetches the person and community to verify their type, then checks if person is banned from site
@ -97,9 +98,9 @@ pub(crate) async fn verify_person_in_community(
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let person = person_id.dereference(context).await?; let person = person_id.dereference(context).await?;
if person.banned { if person.banned {
return Err(LemmyErrorType::PersonIsBannedFromSite( Err(LemmyErrorType::PersonIsBannedFromSite(
person.actor_id.to_string(), person.actor_id.to_string(),
))?; ))?
} }
let person_id = person.id; let person_id = person.id;
let community_id = community.id; let community_id = community.id;
@ -107,10 +108,10 @@ pub(crate) async fn verify_person_in_community(
.await .await
.is_ok(); .is_ok();
if is_banned { if is_banned {
return Err(LemmyErrorType::PersonIsBannedFromCommunity)?; Err(LemmyErrorType::PersonIsBannedFromCommunity)?
} else {
Ok(())
} }
Ok(())
} }
/// Verify that mod action in community was performed by a moderator. /// Verify that mod action in community was performed by a moderator.

View file

@ -64,9 +64,10 @@ impl ActivityHandler for Vote {
.map(|l| l.enable_downvotes) .map(|l| l.enable_downvotes)
.unwrap_or(true); .unwrap_or(true);
if self.kind == VoteType::Dislike && !enable_downvotes { if self.kind == VoteType::Dislike && !enable_downvotes {
return Err(anyhow!("Downvotes disabled").into()); Err(anyhow!("Downvotes disabled").into())
} else {
Ok(())
} }
Ok(())
} }
#[tracing::instrument(skip_all)] #[tracing::instrument(skip_all)]

View file

@ -24,7 +24,7 @@ pub async fn get_community(
let local_site = LocalSite::read(&mut context.pool()).await?; let local_site = LocalSite::read(&mut context.pool()).await?;
if data.name.is_none() && data.id.is_none() { if data.name.is_none() && data.id.is_none() {
return Err(LemmyErrorType::NoIdGiven)?; Err(LemmyErrorType::NoIdGiven)?
} }
check_private_instance(&local_user_view, &local_site)?; check_private_instance(&local_user_view, &local_site)?;

View file

@ -21,7 +21,7 @@ pub async fn read_person(
) -> Result<Json<GetPersonDetailsResponse>, LemmyError> { ) -> Result<Json<GetPersonDetailsResponse>, LemmyError> {
// Check to make sure a person name or an id is given // Check to make sure a person name or an id is given
if data.username.is_none() && data.person_id.is_none() { if data.username.is_none() && data.person_id.is_none() {
return Err(LemmyErrorType::NoIdGiven)?; Err(LemmyErrorType::NoIdGiven)?
} }
let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), &context).await; let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), &context).await;
@ -38,7 +38,7 @@ pub async fn read_person(
.with_lemmy_type(LemmyErrorType::CouldntFindPerson)? .with_lemmy_type(LemmyErrorType::CouldntFindPerson)?
.id .id
} else { } else {
return Err(LemmyErrorType::CouldntFindPerson)?; Err(LemmyErrorType::CouldntFindPerson)?
} }
} }
}; };

View file

@ -71,7 +71,8 @@ async fn convert_response(
}; };
// if the object was deleted from database, dont return it // if the object was deleted from database, dont return it
if removed_or_deleted { if removed_or_deleted {
return Err(NotFound {}.into()); Err(NotFound {}.into())
} else {
Ok(Json(res))
} }
Ok(Json(res))
} }

View file

@ -23,10 +23,8 @@ pub(crate) async fn get_apub_comment(
let id = CommentId(info.comment_id.parse::<i32>()?); let id = CommentId(info.comment_id.parse::<i32>()?);
let comment: ApubComment = Comment::read(&mut context.pool(), id).await?.into(); let comment: ApubComment = Comment::read(&mut context.pool(), id).await?.into();
if !comment.local { if !comment.local {
return Err(err_object_not_local()); Err(err_object_not_local())
} } else if !comment.deleted && !comment.removed {
if !comment.deleted && !comment.removed {
create_apub_response(&comment.into_json(&context).await?) create_apub_response(&comment.into_json(&context).await?)
} else { } else {
create_apub_tombstone_response(comment.ap_id.clone()) create_apub_tombstone_response(comment.ap_id.clone())

View file

@ -81,7 +81,7 @@ pub(crate) async fn get_apub_community_outbox(
.await? .await?
.into(); .into();
if community.deleted || community.removed { if community.deleted || community.removed {
return Err(LemmyErrorType::Deleted)?; Err(LemmyErrorType::Deleted)?
} }
let outbox = ApubCommunityOutbox::read_local(&community, &context).await?; let outbox = ApubCommunityOutbox::read_local(&community, &context).await?;
create_apub_response(&outbox) create_apub_response(&outbox)
@ -97,7 +97,7 @@ pub(crate) async fn get_apub_community_moderators(
.await? .await?
.into(); .into();
if community.deleted || community.removed { if community.deleted || community.removed {
return Err(LemmyErrorType::Deleted)?; Err(LemmyErrorType::Deleted)?
} }
let moderators = ApubCommunityModerators::read_local(&community, &context).await?; let moderators = ApubCommunityModerators::read_local(&community, &context).await?;
create_apub_response(&moderators) create_apub_response(&moderators)
@ -113,7 +113,7 @@ pub(crate) async fn get_apub_community_featured(
.await? .await?
.into(); .into();
if community.deleted || community.removed { if community.deleted || community.removed {
return Err(LemmyErrorType::Deleted)?; Err(LemmyErrorType::Deleted)?
} }
let featured = ApubCommunityFeatured::read_local(&community, &context).await?; let featured = ApubCommunityFeatured::read_local(&community, &context).await?;
create_apub_response(&featured) create_apub_response(&featured)

View file

@ -23,10 +23,8 @@ pub(crate) async fn get_apub_post(
let id = PostId(info.post_id.parse::<i32>()?); let id = PostId(info.post_id.parse::<i32>()?);
let post: ApubPost = Post::read(&mut context.pool(), id).await?.into(); let post: ApubPost = Post::read(&mut context.pool(), id).await?.into();
if !post.local { if !post.local {
return Err(err_object_not_local()); Err(err_object_not_local())
} } else if !post.deleted && !post.removed {
if !post.deleted && !post.removed {
create_apub_response(&post.into_json(&context).await?) create_apub_response(&post.into_json(&context).await?)
} else { } else {
create_apub_tombstone_response(post.ap_id.clone()) create_apub_tombstone_response(post.ap_id.clone())

View file

@ -176,7 +176,7 @@ pub(crate) async fn check_apub_id_valid_with_strictness(
let domain = apub_id.domain().expect("apud id has domain").to_string(); let domain = apub_id.domain().expect("apud id has domain").to_string();
if !allowed_and_local.contains(&domain) { if !allowed_and_local.contains(&domain) {
return Err(LemmyErrorType::FederationDisabledByStrictAllowList)?; Err(LemmyErrorType::FederationDisabledByStrictAllowList)?
} }
} }
Ok(()) Ok(())

View file

@ -143,9 +143,10 @@ impl Object for ApubComment {
verify_person_in_community(&note.attributed_to, &community, context).await?; verify_person_in_community(&note.attributed_to, &community, context).await?;
let (post, _) = note.get_parents(context).await?; let (post, _) = note.get_parents(context).await?;
if post.locked { if post.locked {
return Err(LemmyErrorType::PostIsLocked)?; Err(LemmyErrorType::PostIsLocked)?
} else {
Ok(())
} }
Ok(())
} }
/// Converts a `Note` to `Comment`. /// Converts a `Note` to `Comment`.

View file

@ -107,11 +107,12 @@ impl Object for ApubPrivateMessage {
check_apub_id_valid_with_strictness(note.id.inner(), false, context).await?; check_apub_id_valid_with_strictness(note.id.inner(), false, context).await?;
let person = note.attributed_to.dereference(context).await?; let person = note.attributed_to.dereference(context).await?;
if person.banned { if person.banned {
return Err(LemmyErrorType::PersonIsBannedFromSite( Err(LemmyErrorType::PersonIsBannedFromSite(
person.actor_id.to_string(), person.actor_id.to_string(),
))?; ))?
} else {
Ok(())
} }
Ok(())
} }
#[tracing::instrument(skip_all)] #[tracing::instrument(skip_all)]

View file

@ -208,7 +208,7 @@ impl InCommunity for Page {
break c; break c;
} }
} else { } else {
return Err(LemmyErrorType::NoCommunityFoundInCc)?; Err(LemmyErrorType::NoCommunityFoundInCc)?
} }
} }
} }

View file

@ -38,10 +38,10 @@ impl Settings {
let config = from_str::<Settings>(&Self::read_config_file()?)?; let config = from_str::<Settings>(&Self::read_config_file()?)?;
if config.hostname == "unset" { if config.hostname == "unset" {
return Err(anyhow!("Hostname variable is not set!").into()); Err(anyhow!("Hostname variable is not set!").into())
} else {
Ok(config)
} }
Ok(config)
} }
pub fn get_database_url(&self) -> String { pub fn get_database_url(&self) -> String {

View file

@ -216,10 +216,10 @@ pub fn build_and_check_regex(regex_str_opt: &Option<&str>) -> LemmyResult<Option
// against an innocuous string - a single number - which should help catch a regex // against an innocuous string - a single number - which should help catch a regex
// that accidentally matches against all strings. // that accidentally matches against all strings.
if regex.is_match("1") { if regex.is_match("1") {
return Err(LemmyErrorType::PermissiveRegex.into()); Err(LemmyErrorType::PermissiveRegex.into())
} else {
Ok(Some(regex))
} }
Ok(Some(regex))
}) })
}, },
) )
@ -255,11 +255,13 @@ pub fn check_totp_2fa_valid(
let check_passed = totp.check_current(token)?; let check_passed = totp.check_current(token)?;
if !check_passed { if !check_passed {
return Err(LemmyErrorType::IncorrectTotpToken.into()); Err(LemmyErrorType::IncorrectTotpToken.into())
} else {
Ok(())
} }
} else {
Ok(())
} }
Ok(())
} }
pub fn generate_totp_2fa_secret() -> String { pub fn generate_totp_2fa_secret() -> String {
@ -294,19 +296,22 @@ pub fn check_site_visibility_valid(
let federation_enabled = new_federation_enabled.unwrap_or(current_federation_enabled); let federation_enabled = new_federation_enabled.unwrap_or(current_federation_enabled);
if private_instance && federation_enabled { if private_instance && federation_enabled {
return Err(LemmyErrorType::CantEnablePrivateInstanceAndFederationTogether.into()); Err(LemmyErrorType::CantEnablePrivateInstanceAndFederationTogether.into())
} else {
Ok(())
} }
Ok(())
} }
pub fn check_url_scheme(url: &Option<Url>) -> LemmyResult<()> { pub fn check_url_scheme(url: &Option<Url>) -> LemmyResult<()> {
if let Some(url) = url { if let Some(url) = url {
if url.scheme() != "http" && url.scheme() != "https" { if url.scheme() != "http" && url.scheme() != "https" {
return Err(LemmyErrorType::InvalidUrlScheme.into()); Err(LemmyErrorType::InvalidUrlScheme.into())
} else {
Ok(())
} }
} else {
Ok(())
} }
Ok(())
} }
#[cfg(test)] #[cfg(test)]