Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify handling of NotFound SQL errors (fixes #4633) #5031

Merged
merged 9 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions crates/api/src/comment/distinguish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ pub async fn distinguish_comment(
data.comment_id,
Some(&local_user_view.local_user),
)
.await?
.ok_or(LemmyErrorType::CouldntFindComment)?;
.await?;

check_community_user_action(
&local_user_view.person,
Expand Down Expand Up @@ -60,8 +59,7 @@ pub async fn distinguish_comment(
data.comment_id,
Some(&local_user_view.local_user),
)
.await?
.ok_or(LemmyErrorType::CouldntFindComment)?;
.await?;

Ok(Json(CommentResponse {
comment_view,
Expand Down
6 changes: 2 additions & 4 deletions crates/api/src/comment/like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ pub async fn like_comment(
comment_id,
Some(&local_user_view.local_user),
)
.await?
.ok_or(LemmyErrorType::CouldntFindComment)?;
.await?;

check_community_user_action(
&local_user_view.person,
Expand All @@ -54,8 +53,7 @@ pub async fn like_comment(
let comment_reply = CommentReply::read_by_comment(&mut context.pool(), comment_id).await;
if let Ok(Some(reply)) = comment_reply {
let recipient_id = reply.recipient_id;
if let Ok(Some(local_recipient)) =
LocalUserView::read_person(&mut context.pool(), recipient_id).await
if let Ok(local_recipient) = LocalUserView::read_person(&mut context.pool(), recipient_id).await
{
recipient_ids.push(local_recipient.local_user.id);
}
Expand Down
5 changes: 2 additions & 3 deletions crates/api/src/comment/list_comment_likes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use lemmy_api_common::{
utils::is_mod_or_admin,
};
use lemmy_db_views::structs::{CommentView, LocalUserView, VoteView};
use lemmy_utils::{error::LemmyResult, LemmyErrorType};
use lemmy_utils::error::LemmyResult;

/// Lists likes for a comment
#[tracing::instrument(skip(context))]
Expand All @@ -19,8 +19,7 @@ pub async fn list_comment_likes(
data.comment_id,
Some(&local_user_view.local_user),
)
.await?
.ok_or(LemmyErrorType::CouldntFindComment)?;
.await?;

is_mod_or_admin(
&mut context.pool(),
Expand Down
3 changes: 1 addition & 2 deletions crates/api/src/comment/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ pub async fn save_comment(
comment_id,
Some(&local_user_view.local_user),
)
.await?
.ok_or(LemmyErrorType::CouldntFindComment)?;
.await?;

Ok(Json(CommentResponse {
comment_view,
Expand Down
8 changes: 3 additions & 5 deletions crates/api/src/comment_report/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ pub async fn create_comment_report(
comment_id,
Some(&local_user_view.local_user),
)
.await?
.ok_or(LemmyErrorType::CouldntFindComment)?;
.await?;

check_community_user_action(
&local_user_view.person,
Expand All @@ -64,9 +63,8 @@ pub async fn create_comment_report(
.await
.with_lemmy_type(LemmyErrorType::CouldntCreateReport)?;

let comment_report_view = CommentReportView::read(&mut context.pool(), report.id, person_id)
.await?
.ok_or(LemmyErrorType::CouldntFindCommentReport)?;
let comment_report_view =
CommentReportView::read(&mut context.pool(), report.id, person_id).await?;

// Email the admins
if local_site.reports_email_admins {
Expand Down
9 changes: 3 additions & 6 deletions crates/api/src/comment_report/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ pub async fn resolve_comment_report(
) -> LemmyResult<Json<CommentReportResponse>> {
let report_id = data.report_id;
let person_id = local_user_view.person.id;
let report = CommentReportView::read(&mut context.pool(), report_id, person_id)
.await?
.ok_or(LemmyErrorType::CouldntFindCommentReport)?;
let report = CommentReportView::read(&mut context.pool(), report_id, person_id).await?;

let person_id = local_user_view.person.id;
check_community_mod_action(
Expand All @@ -41,9 +39,8 @@ pub async fn resolve_comment_report(
}

let report_id = data.report_id;
let comment_report_view = CommentReportView::read(&mut context.pool(), report_id, person_id)
.await?
.ok_or(LemmyErrorType::CouldntFindCommentReport)?;
let comment_report_view =
CommentReportView::read(&mut context.pool(), report_id, person_id).await?;

Ok(Json(CommentReportResponse {
comment_report_view,
Expand Down
4 changes: 1 addition & 3 deletions crates/api/src/community/add_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ pub async fn add_mod_to_community(
.await?;
}

let community = Community::read(&mut context.pool(), community_id)
.await?
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
let community = Community::read(&mut context.pool(), community_id).await?;

// If user is admin and community is remote, explicitly check that he is a
// moderator. This is necessary because otherwise the action would be rejected
Expand Down
4 changes: 1 addition & 3 deletions crates/api/src/community/ban.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ pub async fn ban_from_community(

ModBanFromCommunity::create(&mut context.pool(), &form).await?;

let person_view = PersonView::read(&mut context.pool(), data.person_id)
.await?
.ok_or(LemmyErrorType::CouldntFindPerson)?;
let person_view = PersonView::read(&mut context.pool(), data.person_id).await?;

ActivityChannel::submit_activity(
SendActivityData::BanFromCommunity {
Expand Down
3 changes: 1 addition & 2 deletions crates/api/src/community/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ pub async fn block_community(
Some(&local_user_view.local_user),
false,
)
.await?
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
.await?;

ActivityChannel::submit_activity(
SendActivityData::FollowCommunity(
Expand Down
7 changes: 2 additions & 5 deletions crates/api/src/community/follow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ pub async fn follow_community(
context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> LemmyResult<Json<CommunityResponse>> {
let community = Community::read(&mut context.pool(), data.community_id)
.await?
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
let community = Community::read(&mut context.pool(), data.community_id).await?;
let mut community_follower_form = CommunityFollowerForm {
community_id: community.id,
person_id: local_user_view.person.id,
Expand Down Expand Up @@ -68,8 +66,7 @@ pub async fn follow_community(
Some(&local_user_view.local_user),
false,
)
.await?
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
.await?;

let discussion_languages = CommunityLanguage::read(&mut context.pool(), community_id).await?;

Expand Down
7 changes: 2 additions & 5 deletions crates/api/src/community/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,10 @@ pub async fn transfer_community(
Some(&local_user_view.local_user),
false,
)
.await?
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
.await?;

let community_id = data.community_id;
let moderators = CommunityModeratorView::for_community(&mut context.pool(), community_id)
.await
.with_lemmy_type(LemmyErrorType::CouldntFindCommunity)?;
let moderators = CommunityModeratorView::for_community(&mut context.pool(), community_id).await?;

// Return the jwt
Ok(Json(GetCommunityResponse {
Expand Down
4 changes: 1 addition & 3 deletions crates/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,7 @@ pub async fn local_user_view_from_jwt(
let local_user_id = Claims::validate(jwt, context)
.await
.with_lemmy_type(LemmyErrorType::NotLoggedIn)?;
let local_user_view = LocalUserView::read(&mut context.pool(), local_user_id)
.await?
.ok_or(LemmyErrorType::CouldntFindLocalUser)?;
let local_user_view = LocalUserView::read(&mut context.pool(), local_user_id).await?;
check_user_valid(&local_user_view.person)?;

Ok(local_user_view)
Expand Down
4 changes: 2 additions & 2 deletions crates/api/src/local_user/add_admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ pub async fn add_admin(

// Make sure that the person_id added is local
let added_local_user = LocalUserView::read_person(&mut context.pool(), data.person_id)
.await?
.ok_or(LemmyErrorType::ObjectNotLocal)?;
.await
.map_err(|_| LemmyErrorType::ObjectNotLocal)?;

LocalUser::update(
&mut context.pool(),
Expand Down
6 changes: 2 additions & 4 deletions crates/api/src/local_user/ban_person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub async fn ban_from_site(

// if its a local user, invalidate logins
let local_user = LocalUserView::read_person(&mut context.pool(), person.id).await;
if let Ok(Some(local_user)) = local_user {
if let Ok(local_user) = local_user {
LoginToken::invalidate_all(&mut context.pool(), local_user.local_user.id).await?;
}

Expand All @@ -84,9 +84,7 @@ pub async fn ban_from_site(

ModBan::create(&mut context.pool(), &form).await?;

let person_view = PersonView::read(&mut context.pool(), person.id)
.await?
.ok_or(LemmyErrorType::CouldntFindPerson)?;
let person_view = PersonView::read(&mut context.pool(), person.id).await?;

ban_nonlocal_user_from_local_communities(
&local_user_view,
Expand Down
7 changes: 2 additions & 5 deletions crates/api/src/local_user/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ pub async fn block_person(

let target_user = LocalUserView::read_person(&mut context.pool(), target_id)
.await
.ok()
.flatten();
.ok();

if target_user.is_some_and(|t| t.local_user.admin) {
Err(LemmyErrorType::CantBlockAdmin)?
Expand All @@ -49,9 +48,7 @@ pub async fn block_person(
.with_lemmy_type(LemmyErrorType::PersonBlockAlreadyExists)?;
}

let person_view = PersonView::read(&mut context.pool(), target_id)
.await?
.ok_or(LemmyErrorType::CouldntFindPerson)?;
let person_view = PersonView::read(&mut context.pool(), target_id).await?;
Ok(Json(BlockPersonResponse {
person_view,
blocked: data.block,
Expand Down
1 change: 0 additions & 1 deletion crates/api/src/local_user/change_password_after_reset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ pub async fn change_password_after_reset(
let token = data.token.clone();
let local_user_id = PasswordResetRequest::read_and_delete(&mut context.pool(), &token)
.await?
.ok_or(LemmyErrorType::TokenNotFound)?
.local_user_id;

password_length_check(&data.password)?;
Expand Down
4 changes: 1 addition & 3 deletions crates/api/src/local_user/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ pub async fn login(
// Fetch that username / email
let username_or_email = data.username_or_email.clone();
let local_user_view =
LocalUserView::find_by_email_or_name(&mut context.pool(), &username_or_email)
.await?
.ok_or(LemmyErrorType::IncorrectLogin)?;
LocalUserView::find_by_email_or_name(&mut context.pool(), &username_or_email).await?;

// Verify the password
let valid: bool = local_user_view
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ pub async fn mark_person_mention_as_read(
local_user_view: LocalUserView,
) -> LemmyResult<Json<PersonMentionResponse>> {
let person_mention_id = data.person_mention_id;
let read_person_mention = PersonMention::read(&mut context.pool(), person_mention_id)
.await?
.ok_or(LemmyErrorType::CouldntFindPersonMention)?;
let read_person_mention = PersonMention::read(&mut context.pool(), person_mention_id).await?;

if local_user_view.person.id != read_person_mention.recipient_id {
Err(LemmyErrorType::CouldntUpdateComment)?
Expand All @@ -39,9 +37,7 @@ pub async fn mark_person_mention_as_read(
let person_mention_id = read_person_mention.id;
let person_id = local_user_view.person.id;
let person_mention_view =
PersonMentionView::read(&mut context.pool(), person_mention_id, Some(person_id))
.await?
.ok_or(LemmyErrorType::CouldntFindPersonMention)?;
PersonMentionView::read(&mut context.pool(), person_mention_id, Some(person_id)).await?;

Ok(Json(PersonMentionResponse {
person_mention_view,
Expand Down
8 changes: 2 additions & 6 deletions crates/api/src/local_user/notifications/mark_reply_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ pub async fn mark_reply_as_read(
local_user_view: LocalUserView,
) -> LemmyResult<Json<CommentReplyResponse>> {
let comment_reply_id = data.comment_reply_id;
let read_comment_reply = CommentReply::read(&mut context.pool(), comment_reply_id)
.await?
.ok_or(LemmyErrorType::CouldntFindCommentReply)?;
let read_comment_reply = CommentReply::read(&mut context.pool(), comment_reply_id).await?;

if local_user_view.person.id != read_comment_reply.recipient_id {
Err(LemmyErrorType::CouldntUpdateComment)?
Expand All @@ -40,9 +38,7 @@ pub async fn mark_reply_as_read(
let comment_reply_id = read_comment_reply.id;
let person_id = local_user_view.person.id;
let comment_reply_view =
CommentReplyView::read(&mut context.pool(), comment_reply_id, Some(person_id))
.await?
.ok_or(LemmyErrorType::CouldntFindCommentReply)?;
CommentReplyView::read(&mut context.pool(), comment_reply_id, Some(person_id)).await?;

Ok(Json(CommentReplyResponse { comment_reply_view }))
}
4 changes: 2 additions & 2 deletions crates/api/src/local_user/reset_password.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ pub async fn reset_password(
// Fetch that email
let email = data.email.to_lowercase();
let local_user_view = LocalUserView::find_by_email(&mut context.pool(), &email)
.await?
.ok_or(LemmyErrorType::IncorrectLogin)?;
.await
.map_err(|_| LemmyErrorType::IncorrectLogin)?;

let site_view = SiteView::read_local(&mut context.pool()).await?;
check_email_verified(&local_user_view, &site_view)?;
Expand Down
10 changes: 3 additions & 7 deletions crates/api/src/local_user/verify_email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@ use lemmy_db_schema::source::{
local_user::{LocalUser, LocalUserUpdateForm},
};
use lemmy_db_views::structs::{LocalUserView, SiteView};
use lemmy_utils::error::{LemmyErrorType, LemmyResult};
use lemmy_utils::error::LemmyResult;

pub async fn verify_email(
data: Json<VerifyEmail>,
context: Data<LemmyContext>,
) -> LemmyResult<Json<SuccessResponse>> {
let site_view = SiteView::read_local(&mut context.pool()).await?;
let token = data.token.clone();
let verification = EmailVerification::read_for_token(&mut context.pool(), &token)
.await?
.ok_or(LemmyErrorType::TokenNotFound)?;
let verification = EmailVerification::read_for_token(&mut context.pool(), &token).await?;

let form = LocalUserUpdateForm {
// necessary in case this is a new signup
Expand All @@ -37,9 +35,7 @@ pub async fn verify_email(

// send out notification about registration application to admins if enabled
if site_view.local_site.application_email_admins {
let local_user = LocalUserView::read(&mut context.pool(), local_user_id)
.await?
.ok_or(LemmyErrorType::CouldntFindPerson)?;
let local_user = LocalUserView::read(&mut context.pool(), local_user_id).await?;

send_new_applicant_email_to_admins(
&local_user.person.name,
Expand Down
6 changes: 2 additions & 4 deletions crates/api/src/post/feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use lemmy_db_schema::{
PostFeatureType,
};
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::{error::LemmyResult, LemmyErrorType};
use lemmy_utils::error::LemmyResult;

#[tracing::instrument(skip(context))]
pub async fn feature_post(
Expand All @@ -25,9 +25,7 @@ pub async fn feature_post(
local_user_view: LocalUserView,
) -> LemmyResult<Json<PostResponse>> {
let post_id = data.post_id;
let orig_post = Post::read(&mut context.pool(), post_id)
.await?
.ok_or(LemmyErrorType::CouldntFindPost)?;
let orig_post = Post::read(&mut context.pool(), post_id).await?;

check_community_mod_action(
&local_user_view.person,
Expand Down
8 changes: 2 additions & 6 deletions crates/api/src/post/like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ pub async fn like_post(

// Check for a community ban
let post_id = data.post_id;
let post = Post::read(&mut context.pool(), post_id)
.await?
.ok_or(LemmyErrorType::CouldntFindPost)?;
let post = Post::read(&mut context.pool(), post_id).await?;

check_community_user_action(
&local_user_view.person,
Expand Down Expand Up @@ -70,9 +68,7 @@ pub async fn like_post(

mark_post_as_read(person_id, post_id, &mut context.pool()).await?;

let community = Community::read(&mut context.pool(), post.community_id)
.await?
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
let community = Community::read(&mut context.pool(), post.community_id).await?;

ActivityChannel::submit_activity(
SendActivityData::LikePostOrComment {
Expand Down
Loading