diff --git a/api_tests/package.json b/api_tests/package.json index cc58c83e9..cacd476ea 100644 --- a/api_tests/package.json +++ b/api_tests/package.json @@ -27,7 +27,7 @@ "eslint": "^9.8.0", "eslint-plugin-prettier": "^5.1.3", "jest": "^29.5.0", - "lemmy-js-client": "0.20.0-alpha.4", + "lemmy-js-client": "0.20.0-alpha.11", "prettier": "^3.2.5", "ts-jest": "^29.1.0", "typescript": "^5.5.4", diff --git a/api_tests/pnpm-lock.yaml b/api_tests/pnpm-lock.yaml index 0b6a19f81..f72450015 100644 --- a/api_tests/pnpm-lock.yaml +++ b/api_tests/pnpm-lock.yaml @@ -30,8 +30,8 @@ importers: specifier: ^29.5.0 version: 29.7.0(@types/node@22.3.0) lemmy-js-client: - specifier: 0.20.0-alpha.4 - version: 0.20.0-alpha.4 + specifier: 0.20.0-alpha.11 + version: 0.20.0-alpha.11 prettier: specifier: ^3.2.5 version: 3.3.3 @@ -1175,8 +1175,8 @@ packages: resolution: {integrity: sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w==} engines: {node: '>=6'} - lemmy-js-client@0.20.0-alpha.4: - resolution: {integrity: sha512-MuVE8u/IFz59ks2vxOUXMRU8x6SIz3Keu4zRhmhNt+n+gI25JAggsTDrtxVCBtrqBhFEe+a3PdXdwm+fXbs0Dw==} + lemmy-js-client@0.20.0-alpha.11: + resolution: {integrity: sha512-iRSG4xHMjPDIreQqVIoJ5JrMY71uk07G0Zbgyf068xKbib22J3+i1x/XgCTs6tiHlqTnw1Ig/KRq7p7qJoA4uw==} leven@3.1.0: resolution: {integrity: sha512-qsda+H8jTaUaN/x5vzW2rzc+8Rw4TAQ/4KjB46IwK5VH+IlVeeeje/EoZRpiXvIqjFgK84QffqPztGI3VBLG1A==} @@ -3107,7 +3107,7 @@ snapshots: kleur@3.0.3: {} - lemmy-js-client@0.20.0-alpha.4: {} + lemmy-js-client@0.20.0-alpha.11: {} leven@3.1.0: {} diff --git a/api_tests/src/comment.spec.ts b/api_tests/src/comment.spec.ts index 8c3a23ab5..c18469e33 100644 --- a/api_tests/src/comment.spec.ts +++ b/api_tests/src/comment.spec.ts @@ -92,7 +92,7 @@ test("Create a comment", async () => { test("Create a comment in a non-existent post", async () => { await expect(createComment(alpha, -1)).rejects.toStrictEqual( - Error("couldnt_find_post"), + Error("not_found"), ); }); @@ -143,7 +143,7 @@ test("Delete a comment", async () => { await waitUntil( () => resolveComment(gamma, commentRes.comment_view.comment).catch(e => e), - r => r.message !== "couldnt_find_object", + r => r.message !== "not_found", ) ).comment; if (!gammaComment) { @@ -161,13 +161,13 @@ test("Delete a comment", async () => { // Make sure that comment is undefined on beta await waitUntil( () => resolveComment(beta, commentRes.comment_view.comment).catch(e => e), - e => e.message == "couldnt_find_object", + e => e.message == "not_found", ); // Make sure that comment is undefined on gamma after delete await waitUntil( () => resolveComment(gamma, commentRes.comment_view.comment).catch(e => e), - e => e.message === "couldnt_find_object", + e => e.message === "not_found", ); // Test undeleting the comment @@ -182,7 +182,7 @@ test("Delete a comment", async () => { let betaComment2 = ( await waitUntil( () => resolveComment(beta, commentRes.comment_view.comment).catch(e => e), - e => e.message !== "couldnt_find_object", + e => e.message !== "not_found", ) ).comment; expect(betaComment2?.comment.deleted).toBe(false); diff --git a/api_tests/src/community.spec.ts b/api_tests/src/community.spec.ts index d8aa6cad6..77b68e2fc 100644 --- a/api_tests/src/community.spec.ts +++ b/api_tests/src/community.spec.ts @@ -527,12 +527,12 @@ test("Content in local-only community doesn't federate", async () => { // cant resolve the community from another instance await expect( resolveCommunity(beta, communityRes.actor_id), - ).rejects.toStrictEqual(Error("couldnt_find_object")); + ).rejects.toStrictEqual(Error("not_found")); // create a post, also cant resolve it let postRes = await createPost(alpha, communityRes.id); await expect(resolvePost(beta, postRes.post_view.post)).rejects.toStrictEqual( - Error("couldnt_find_object"), + Error("not_found"), ); }); diff --git a/api_tests/src/post.spec.ts b/api_tests/src/post.spec.ts index e059d4585..bb0416057 100644 --- a/api_tests/src/post.spec.ts +++ b/api_tests/src/post.spec.ts @@ -125,12 +125,12 @@ test("Create a post", async () => { // Delta only follows beta, so it should not see an alpha ap_id await expect( resolvePost(delta, postRes.post_view.post), - ).rejects.toStrictEqual(Error("couldnt_find_object")); + ).rejects.toStrictEqual(Error("not_found")); // Epsilon has alpha blocked, it should not see the alpha post await expect( resolvePost(epsilon, postRes.post_view.post), - ).rejects.toStrictEqual(Error("couldnt_find_object")); + ).rejects.toStrictEqual(Error("not_found")); // remove added allow/blocklists editSiteForm.allowed_instances = []; @@ -140,9 +140,7 @@ test("Create a post", async () => { }); test("Create a post in a non-existent community", async () => { - await expect(createPost(alpha, -2)).rejects.toStrictEqual( - Error("couldnt_find_community"), - ); + await expect(createPost(alpha, -2)).rejects.toStrictEqual(Error("not_found")); }); test("Unlike a post", async () => { diff --git a/api_tests/src/shared.ts b/api_tests/src/shared.ts index 84e0551a4..8ec4b29ed 100644 --- a/api_tests/src/shared.ts +++ b/api_tests/src/shared.ts @@ -690,7 +690,7 @@ export async function saveUserSettingsBio( blur_nsfw: false, auto_expand: true, theme: "darkly", - default_sort_type: "Active", + default_post_sort_type: "Active", default_listing_type: "All", interface_language: "en", show_avatars: true, @@ -710,7 +710,7 @@ export async function saveUserSettingsFederated( show_nsfw: false, blur_nsfw: true, auto_expand: false, - default_sort_type: "Hot", + default_post_sort_type: "Hot", default_listing_type: "All", interface_language: "", avatar, diff --git a/crates/api/src/comment/distinguish.rs b/crates/api/src/comment/distinguish.rs index 0683af9a4..a1b25ea44 100644 --- a/crates/api/src/comment/distinguish.rs +++ b/crates/api/src/comment/distinguish.rs @@ -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, @@ -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, diff --git a/crates/api/src/comment/like.rs b/crates/api/src/comment/like.rs index b8a1c6f76..81d5516c1 100644 --- a/crates/api/src/comment/like.rs +++ b/crates/api/src/comment/like.rs @@ -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, @@ -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); } diff --git a/crates/api/src/comment/list_comment_likes.rs b/crates/api/src/comment/list_comment_likes.rs index 4b2e1c8b3..c9721b8a0 100644 --- a/crates/api/src/comment/list_comment_likes.rs +++ b/crates/api/src/comment/list_comment_likes.rs @@ -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))] @@ -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(), diff --git a/crates/api/src/comment/save.rs b/crates/api/src/comment/save.rs index 67c2db331..6efa6296d 100644 --- a/crates/api/src/comment/save.rs +++ b/crates/api/src/comment/save.rs @@ -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, diff --git a/crates/api/src/comment_report/create.rs b/crates/api/src/comment_report/create.rs index a269df07f..a0ff4be77 100644 --- a/crates/api/src/comment_report/create.rs +++ b/crates/api/src/comment_report/create.rs @@ -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, @@ -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 { diff --git a/crates/api/src/comment_report/resolve.rs b/crates/api/src/comment_report/resolve.rs index 40aad9569..a663fdf74 100644 --- a/crates/api/src/comment_report/resolve.rs +++ b/crates/api/src/comment_report/resolve.rs @@ -17,9 +17,7 @@ pub async fn resolve_comment_report( ) -> LemmyResult> { 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( @@ -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, diff --git a/crates/api/src/community/add_mod.rs b/crates/api/src/community/add_mod.rs index 8d8826cd2..09e2c5b61 100644 --- a/crates/api/src/community/add_mod.rs +++ b/crates/api/src/community/add_mod.rs @@ -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 diff --git a/crates/api/src/community/ban.rs b/crates/api/src/community/ban.rs index d00a1b0d0..f7da2154f 100644 --- a/crates/api/src/community/ban.rs +++ b/crates/api/src/community/ban.rs @@ -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 { diff --git a/crates/api/src/community/block.rs b/crates/api/src/community/block.rs index ad31548ea..90931c762 100644 --- a/crates/api/src/community/block.rs +++ b/crates/api/src/community/block.rs @@ -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( diff --git a/crates/api/src/community/follow.rs b/crates/api/src/community/follow.rs index 2236fa5bc..d0f5bbf0d 100644 --- a/crates/api/src/community/follow.rs +++ b/crates/api/src/community/follow.rs @@ -23,9 +23,7 @@ pub async fn follow_community( context: Data, local_user_view: LocalUserView, ) -> LemmyResult> { - 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, @@ -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?; diff --git a/crates/api/src/community/transfer.rs b/crates/api/src/community/transfer.rs index a32c069b1..195adbd8d 100644 --- a/crates/api/src/community/transfer.rs +++ b/crates/api/src/community/transfer.rs @@ -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 { diff --git a/crates/api/src/lib.rs b/crates/api/src/lib.rs index 3cae4737a..44a8d9bbc 100644 --- a/crates/api/src/lib.rs +++ b/crates/api/src/lib.rs @@ -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) diff --git a/crates/api/src/local_user/add_admin.rs b/crates/api/src/local_user/add_admin.rs index 44b36fe66..299c9477a 100644 --- a/crates/api/src/local_user/add_admin.rs +++ b/crates/api/src/local_user/add_admin.rs @@ -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(), diff --git a/crates/api/src/local_user/ban_person.rs b/crates/api/src/local_user/ban_person.rs index bea8247a3..7ab8ad72a 100644 --- a/crates/api/src/local_user/ban_person.rs +++ b/crates/api/src/local_user/ban_person.rs @@ -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?; } @@ -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, diff --git a/crates/api/src/local_user/block.rs b/crates/api/src/local_user/block.rs index 698703a9b..250277be3 100644 --- a/crates/api/src/local_user/block.rs +++ b/crates/api/src/local_user/block.rs @@ -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)? @@ -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, diff --git a/crates/api/src/local_user/change_password_after_reset.rs b/crates/api/src/local_user/change_password_after_reset.rs index f86421796..191815d0f 100644 --- a/crates/api/src/local_user/change_password_after_reset.rs +++ b/crates/api/src/local_user/change_password_after_reset.rs @@ -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)?; diff --git a/crates/api/src/local_user/login.rs b/crates/api/src/local_user/login.rs index a8f65d758..0b2514c5b 100644 --- a/crates/api/src/local_user/login.rs +++ b/crates/api/src/local_user/login.rs @@ -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 diff --git a/crates/api/src/local_user/notifications/mark_mention_read.rs b/crates/api/src/local_user/notifications/mark_mention_read.rs index 90c8efb6e..9a839b2b4 100644 --- a/crates/api/src/local_user/notifications/mark_mention_read.rs +++ b/crates/api/src/local_user/notifications/mark_mention_read.rs @@ -18,9 +18,7 @@ pub async fn mark_person_mention_as_read( local_user_view: LocalUserView, ) -> LemmyResult> { 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)? @@ -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, diff --git a/crates/api/src/local_user/notifications/mark_reply_read.rs b/crates/api/src/local_user/notifications/mark_reply_read.rs index fdcfa5727..5b263145f 100644 --- a/crates/api/src/local_user/notifications/mark_reply_read.rs +++ b/crates/api/src/local_user/notifications/mark_reply_read.rs @@ -18,9 +18,7 @@ pub async fn mark_reply_as_read( local_user_view: LocalUserView, ) -> LemmyResult> { 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)? @@ -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 })) } diff --git a/crates/api/src/local_user/reset_password.rs b/crates/api/src/local_user/reset_password.rs index 4854d1376..5cf06f23a 100644 --- a/crates/api/src/local_user/reset_password.rs +++ b/crates/api/src/local_user/reset_password.rs @@ -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)?; diff --git a/crates/api/src/local_user/verify_email.rs b/crates/api/src/local_user/verify_email.rs index bbd442723..4b6a8c928 100644 --- a/crates/api/src/local_user/verify_email.rs +++ b/crates/api/src/local_user/verify_email.rs @@ -10,7 +10,7 @@ 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, @@ -18,9 +18,7 @@ pub async fn verify_email( ) -> LemmyResult> { 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 @@ -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, diff --git a/crates/api/src/post/feature.rs b/crates/api/src/post/feature.rs index ec99a3345..cb6e6c144 100644 --- a/crates/api/src/post/feature.rs +++ b/crates/api/src/post/feature.rs @@ -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( @@ -25,9 +25,7 @@ pub async fn feature_post( local_user_view: LocalUserView, ) -> LemmyResult> { 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, diff --git a/crates/api/src/post/like.rs b/crates/api/src/post/like.rs index e6903fb3c..967b22a30 100644 --- a/crates/api/src/post/like.rs +++ b/crates/api/src/post/like.rs @@ -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, @@ -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 { diff --git a/crates/api/src/post/list_post_likes.rs b/crates/api/src/post/list_post_likes.rs index b9b2106b7..a9b302f2e 100644 --- a/crates/api/src/post/list_post_likes.rs +++ b/crates/api/src/post/list_post_likes.rs @@ -6,7 +6,7 @@ use lemmy_api_common::{ }; use lemmy_db_schema::{source::post::Post, traits::Crud}; use lemmy_db_views::structs::{LocalUserView, VoteView}; -use lemmy_utils::{error::LemmyResult, LemmyErrorType}; +use lemmy_utils::error::LemmyResult; /// Lists likes for a post #[tracing::instrument(skip(context))] @@ -15,9 +15,7 @@ pub async fn list_post_likes( context: Data, local_user_view: LocalUserView, ) -> LemmyResult> { - let post = Post::read(&mut context.pool(), data.post_id) - .await? - .ok_or(LemmyErrorType::CouldntFindPost)?; + let post = Post::read(&mut context.pool(), data.post_id).await?; is_mod_or_admin( &mut context.pool(), &local_user_view.person, diff --git a/crates/api/src/post/lock.rs b/crates/api/src/post/lock.rs index 36f9c2a33..548947b78 100644 --- a/crates/api/src/post/lock.rs +++ b/crates/api/src/post/lock.rs @@ -15,7 +15,7 @@ use lemmy_db_schema::{ traits::Crud, }; use lemmy_db_views::structs::LocalUserView; -use lemmy_utils::{error::LemmyResult, LemmyErrorType}; +use lemmy_utils::error::LemmyResult; #[tracing::instrument(skip(context))] pub async fn lock_post( @@ -24,9 +24,7 @@ pub async fn lock_post( local_user_view: LocalUserView, ) -> LemmyResult> { 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, diff --git a/crates/api/src/post/save.rs b/crates/api/src/post/save.rs index 85dfc11e3..4549b62b1 100644 --- a/crates/api/src/post/save.rs +++ b/crates/api/src/post/save.rs @@ -40,8 +40,7 @@ pub async fn save_post( Some(&local_user_view.local_user), false, ) - .await? - .ok_or(LemmyErrorType::CouldntFindPost)?; + .await?; mark_post_as_read(person_id, post_id, &mut context.pool()).await?; diff --git a/crates/api/src/post_report/create.rs b/crates/api/src/post_report/create.rs index 72a40f70d..590c9af40 100644 --- a/crates/api/src/post_report/create.rs +++ b/crates/api/src/post_report/create.rs @@ -35,9 +35,7 @@ pub async fn create_post_report( let person_id = local_user_view.person.id; let post_id = data.post_id; - let post_view = PostView::read(&mut context.pool(), post_id, None, false) - .await? - .ok_or(LemmyErrorType::CouldntFindPost)?; + let post_view = PostView::read(&mut context.pool(), post_id, None, false).await?; check_community_user_action( &local_user_view.person, @@ -61,9 +59,7 @@ pub async fn create_post_report( .await .with_lemmy_type(LemmyErrorType::CouldntCreateReport)?; - let post_report_view = PostReportView::read(&mut context.pool(), report.id, person_id) - .await? - .ok_or(LemmyErrorType::CouldntFindPostReport)?; + let post_report_view = PostReportView::read(&mut context.pool(), report.id, person_id).await?; // Email the admins if local_site.reports_email_admins { diff --git a/crates/api/src/post_report/resolve.rs b/crates/api/src/post_report/resolve.rs index 428619674..a3cb85c6c 100644 --- a/crates/api/src/post_report/resolve.rs +++ b/crates/api/src/post_report/resolve.rs @@ -17,9 +17,7 @@ pub async fn resolve_post_report( ) -> LemmyResult> { let report_id = data.report_id; let person_id = local_user_view.person.id; - let report = PostReportView::read(&mut context.pool(), report_id, person_id) - .await? - .ok_or(LemmyErrorType::CouldntFindPostReport)?; + let report = PostReportView::read(&mut context.pool(), report_id, person_id).await?; let person_id = local_user_view.person.id; check_community_mod_action( @@ -40,9 +38,7 @@ pub async fn resolve_post_report( .with_lemmy_type(LemmyErrorType::CouldntResolveReport)?; } - let post_report_view = PostReportView::read(&mut context.pool(), report_id, person_id) - .await? - .ok_or(LemmyErrorType::CouldntFindPostReport)?; + let post_report_view = PostReportView::read(&mut context.pool(), report_id, person_id).await?; Ok(Json(PostReportResponse { post_report_view })) } diff --git a/crates/api/src/private_message/mark_read.rs b/crates/api/src/private_message/mark_read.rs index 07e06fe21..7c213464b 100644 --- a/crates/api/src/private_message/mark_read.rs +++ b/crates/api/src/private_message/mark_read.rs @@ -18,9 +18,7 @@ pub async fn mark_pm_as_read( ) -> LemmyResult> { // Checking permissions let private_message_id = data.private_message_id; - let orig_private_message = PrivateMessage::read(&mut context.pool(), private_message_id) - .await? - .ok_or(LemmyErrorType::CouldntFindPrivateMessage)?; + let orig_private_message = PrivateMessage::read(&mut context.pool(), private_message_id).await?; if local_user_view.person.id != orig_private_message.recipient_id { Err(LemmyErrorType::CouldntUpdatePrivateMessage)? } @@ -39,9 +37,7 @@ pub async fn mark_pm_as_read( .await .with_lemmy_type(LemmyErrorType::CouldntUpdatePrivateMessage)?; - let view = PrivateMessageView::read(&mut context.pool(), private_message_id) - .await? - .ok_or(LemmyErrorType::CouldntFindPrivateMessage)?; + let view = PrivateMessageView::read(&mut context.pool(), private_message_id).await?; Ok(Json(PrivateMessageResponse { private_message_view: view, })) diff --git a/crates/api/src/private_message_report/create.rs b/crates/api/src/private_message_report/create.rs index 41ac592ae..de8ca390f 100644 --- a/crates/api/src/private_message_report/create.rs +++ b/crates/api/src/private_message_report/create.rs @@ -29,9 +29,7 @@ pub async fn create_pm_report( let person_id = local_user_view.person.id; let private_message_id = data.private_message_id; - let private_message = PrivateMessage::read(&mut context.pool(), private_message_id) - .await? - .ok_or(LemmyErrorType::CouldntFindPrivateMessage)?; + let private_message = PrivateMessage::read(&mut context.pool(), private_message_id).await?; // Make sure that only the recipient of the private message can create a report if person_id != private_message.recipient_id { @@ -49,9 +47,8 @@ pub async fn create_pm_report( .await .with_lemmy_type(LemmyErrorType::CouldntCreateReport)?; - let private_message_report_view = PrivateMessageReportView::read(&mut context.pool(), report.id) - .await? - .ok_or(LemmyErrorType::CouldntFindPrivateMessageReport)?; + let private_message_report_view = + PrivateMessageReportView::read(&mut context.pool(), report.id).await?; // Email the admins if local_site.reports_email_admins { diff --git a/crates/api/src/private_message_report/resolve.rs b/crates/api/src/private_message_report/resolve.rs index 27847eeaf..7d821a60c 100644 --- a/crates/api/src/private_message_report/resolve.rs +++ b/crates/api/src/private_message_report/resolve.rs @@ -28,9 +28,8 @@ pub async fn resolve_pm_report( .with_lemmy_type(LemmyErrorType::CouldntResolveReport)?; } - let private_message_report_view = PrivateMessageReportView::read(&mut context.pool(), report_id) - .await? - .ok_or(LemmyErrorType::CouldntFindPrivateMessageReport)?; + let private_message_report_view = + PrivateMessageReportView::read(&mut context.pool(), report_id).await?; Ok(Json(PrivateMessageReportResponse { private_message_report_view, diff --git a/crates/api/src/site/purge/comment.rs b/crates/api/src/site/purge/comment.rs index 9f90aff99..b21ffbc80 100644 --- a/crates/api/src/site/purge/comment.rs +++ b/crates/api/src/site/purge/comment.rs @@ -16,7 +16,7 @@ use lemmy_db_schema::{ traits::Crud, }; use lemmy_db_views::structs::{CommentView, LocalUserView}; -use lemmy_utils::{error::LemmyResult, LemmyErrorType}; +use lemmy_utils::error::LemmyResult; #[tracing::instrument(skip(context))] pub async fn purge_comment( @@ -35,8 +35,7 @@ pub async fn purge_comment( comment_id, Some(&local_user_view.local_user), ) - .await? - .ok_or(LemmyErrorType::CouldntFindComment)?; + .await?; // Also check that you're a higher admin LocalUser::is_higher_admin_check( diff --git a/crates/api/src/site/purge/community.rs b/crates/api/src/site/purge/community.rs index 59eded6ad..bf06bd529 100644 --- a/crates/api/src/site/purge/community.rs +++ b/crates/api/src/site/purge/community.rs @@ -19,7 +19,7 @@ use lemmy_db_schema::{ }; use lemmy_db_views::structs::LocalUserView; use lemmy_db_views_actor::structs::CommunityModeratorView; -use lemmy_utils::{error::LemmyResult, LemmyErrorType}; +use lemmy_utils::error::LemmyResult; #[tracing::instrument(skip(context))] pub async fn purge_community( @@ -31,9 +31,7 @@ pub async fn purge_community( is_admin(&local_user_view)?; // Read the community to get its images - 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?; // Also check that you're a higher admin than all the mods let community_mod_person_ids = diff --git a/crates/api/src/site/purge/person.rs b/crates/api/src/site/purge/person.rs index 1ce8bcd86..7ab573cbc 100644 --- a/crates/api/src/site/purge/person.rs +++ b/crates/api/src/site/purge/person.rs @@ -17,7 +17,7 @@ use lemmy_db_schema::{ traits::Crud, }; use lemmy_db_views::structs::LocalUserView; -use lemmy_utils::{error::LemmyResult, LemmyErrorType}; +use lemmy_utils::error::LemmyResult; #[tracing::instrument(skip(context))] pub async fn purge_person( @@ -36,9 +36,7 @@ pub async fn purge_person( ) .await?; - let person = Person::read(&mut context.pool(), data.person_id) - .await? - .ok_or(LemmyErrorType::CouldntFindPerson)?; + let person = Person::read(&mut context.pool(), data.person_id).await?; ban_nonlocal_user_from_local_communities( &local_user_view, diff --git a/crates/api/src/site/purge/post.rs b/crates/api/src/site/purge/post.rs index 6e512312f..d2cacdae1 100644 --- a/crates/api/src/site/purge/post.rs +++ b/crates/api/src/site/purge/post.rs @@ -17,7 +17,7 @@ use lemmy_db_schema::{ traits::Crud, }; use lemmy_db_views::structs::LocalUserView; -use lemmy_utils::{error::LemmyResult, LemmyErrorType}; +use lemmy_utils::error::LemmyResult; #[tracing::instrument(skip(context))] pub async fn purge_post( @@ -29,9 +29,7 @@ pub async fn purge_post( is_admin(&local_user_view)?; // Read the post to get the community_id - let post = Post::read(&mut context.pool(), data.post_id) - .await? - .ok_or(LemmyErrorType::CouldntFindPost)?; + let post = Post::read(&mut context.pool(), data.post_id).await?; // Also check that you're a higher admin LocalUser::is_higher_admin_check( diff --git a/crates/api/src/site/registration_applications/approve.rs b/crates/api/src/site/registration_applications/approve.rs index dcde78117..b8cd6c0ea 100644 --- a/crates/api/src/site/registration_applications/approve.rs +++ b/crates/api/src/site/registration_applications/approve.rs @@ -14,10 +14,7 @@ use lemmy_db_schema::{ utils::{diesel_string_update, get_conn}, }; use lemmy_db_views::structs::{LocalUserView, RegistrationApplicationView}; -use lemmy_utils::{ - error::{LemmyError, LemmyResult}, - LemmyErrorType, -}; +use lemmy_utils::error::{LemmyError, LemmyResult}; pub async fn approve_registration_application( data: Json, @@ -61,9 +58,8 @@ pub async fn approve_registration_application( .await?; if data.approve { - let approved_local_user_view = LocalUserView::read(&mut context.pool(), approved_user_id) - .await? - .ok_or(LemmyErrorType::CouldntFindLocalUser)?; + let approved_local_user_view = + LocalUserView::read(&mut context.pool(), approved_user_id).await?; if approved_local_user_view.local_user.email.is_some() { // Email sending may fail, but this won't revert the application approval send_application_approved_email(&approved_local_user_view, context.settings()).await?; @@ -71,9 +67,8 @@ pub async fn approve_registration_application( }; // Read the view - let registration_application = RegistrationApplicationView::read(&mut context.pool(), app_id) - .await? - .ok_or(LemmyErrorType::CouldntFindRegistrationApplication)?; + let registration_application = + RegistrationApplicationView::read(&mut context.pool(), app_id).await?; Ok(Json(RegistrationApplicationResponse { registration_application, diff --git a/crates/api/src/site/registration_applications/get.rs b/crates/api/src/site/registration_applications/get.rs index 2d5d6bf5b..23c6fb4d0 100644 --- a/crates/api/src/site/registration_applications/get.rs +++ b/crates/api/src/site/registration_applications/get.rs @@ -5,7 +5,7 @@ use lemmy_api_common::{ utils::is_admin, }; use lemmy_db_views::structs::{LocalUserView, RegistrationApplicationView}; -use lemmy_utils::{error::LemmyResult, LemmyErrorType}; +use lemmy_utils::error::LemmyResult; /// Lists registration applications, filterable by undenied only. pub async fn get_registration_application( @@ -18,9 +18,7 @@ pub async fn get_registration_application( // Read the view let registration_application = - RegistrationApplicationView::read_by_person(&mut context.pool(), data.person_id) - .await? - .ok_or(LemmyErrorType::CouldntFindRegistrationApplication)?; + RegistrationApplicationView::read_by_person(&mut context.pool(), data.person_id).await?; Ok(Json(RegistrationApplicationResponse { registration_application, diff --git a/crates/api/src/site/registration_applications/tests.rs b/crates/api/src/site/registration_applications/tests.rs index a334d8393..30cbdde72 100644 --- a/crates/api/src/site/registration_applications/tests.rs +++ b/crates/api/src/site/registration_applications/tests.rs @@ -54,9 +54,7 @@ async fn create_test_site(context: &Data) -> LemmyResult<(Instance ) .await?; - let admin_local_user_view = LocalUserView::read_person(pool, admin_person.id) - .await? - .unwrap(); + let admin_local_user_view = LocalUserView::read_person(pool, admin_person.id).await?; let site_form = SiteInsertForm::new("test site".to_string(), inserted_instance.id); let site = Site::create(pool, &site_form).await.unwrap(); diff --git a/crates/api_common/src/build_response.rs b/crates/api_common/src/build_response.rs index 8f140f2fe..d40f4c23d 100644 --- a/crates/api_common/src/build_response.rs +++ b/crates/api_common/src/build_response.rs @@ -27,7 +27,6 @@ use lemmy_db_views_actor::structs::CommunityView; use lemmy_utils::{ error::LemmyResult, utils::{markdown::markdown_to_html, mention::MentionData}, - LemmyErrorType, }; pub async fn build_comment_response( @@ -37,9 +36,8 @@ pub async fn build_comment_response( recipient_ids: Vec, ) -> LemmyResult { let local_user = local_user_view.map(|l| l.local_user); - let comment_view = CommentView::read(&mut context.pool(), comment_id, local_user.as_ref()) - .await? - .ok_or(LemmyErrorType::CouldntFindComment)?; + let comment_view = + CommentView::read(&mut context.pool(), comment_id, local_user.as_ref()).await?; Ok(CommentResponse { comment_view, recipient_ids, @@ -61,8 +59,7 @@ pub async fn build_community_response( Some(&local_user), is_mod_or_admin, ) - .await? - .ok_or(LemmyErrorType::CouldntFindCommunity)?; + .await?; let discussion_languages = CommunityLanguage::read(&mut context.pool(), community_id).await?; Ok(Json(CommunityResponse { @@ -87,8 +84,7 @@ pub async fn build_post_response( Some(&local_user), is_mod_or_admin, ) - .await? - .ok_or(LemmyErrorType::CouldntFindPost)?; + .await?; Ok(Json(PostResponse { post_view })) } @@ -112,8 +108,7 @@ pub async fn send_local_notifs( comment_id, local_user_view.map(|view| &view.local_user), ) - .await? - .ok_or(LemmyErrorType::CouldntFindComment)?; + .await?; let comment = comment_view.comment; let post = comment_view.post; let community = comment_view.community; @@ -125,7 +120,7 @@ pub async fn send_local_notifs( { let mention_name = mention.name.clone(); let user_view = LocalUserView::read_from_name(&mut context.pool(), &mention_name).await; - if let Ok(Some(mention_user_view)) = user_view { + if let Ok(mention_user_view) = user_view { // TODO // At some point, make it so you can't tag the parent creator either // Potential duplication of notifications, one for reply and the other for mention, is handled @@ -161,9 +156,7 @@ pub async fn send_local_notifs( // Send comment_reply to the parent commenter / poster if let Some(parent_comment_id) = comment.parent_comment_id() { - let parent_comment = Comment::read(&mut context.pool(), parent_comment_id) - .await? - .ok_or(LemmyErrorType::CouldntFindComment)?; + let parent_comment = Comment::read(&mut context.pool(), parent_comment_id).await?; // Get the parent commenter local_user let parent_creator_id = parent_comment.creator_id; @@ -182,7 +175,7 @@ pub async fn send_local_notifs( // Don't send a notif to yourself if parent_comment.creator_id != person.id && !check_blocks { let user_view = LocalUserView::read_person(&mut context.pool(), parent_creator_id).await; - if let Ok(Some(parent_user_view)) = user_view { + if let Ok(parent_user_view) = user_view { // Don't duplicate notif if already mentioned by checking recipient ids if !recipient_ids.contains(&parent_user_view.local_user.id) { recipient_ids.push(parent_user_view.local_user.id); @@ -229,7 +222,7 @@ pub async fn send_local_notifs( if post.creator_id != person.id && !check_blocks { let creator_id = post.creator_id; let parent_user = LocalUserView::read_person(&mut context.pool(), creator_id).await; - if let Ok(Some(parent_user_view)) = parent_user { + if let Ok(parent_user_view) = parent_user { if !recipient_ids.contains(&parent_user_view.local_user.id) { recipient_ids.push(parent_user_view.local_user.id); diff --git a/crates/api_common/src/utils.rs b/crates/api_common/src/utils.rs index 8ef527dcc..be70c2afe 100644 --- a/crates/api_common/src/utils.rs +++ b/crates/api_common/src/utils.rs @@ -175,9 +175,7 @@ pub async fn update_read_comments( ..PersonPostAggregatesForm::default() }; - PersonPostAggregates::upsert(pool, &person_post_agg_form) - .await - .with_lemmy_type(LemmyErrorType::CouldntFindPost)?; + PersonPostAggregates::upsert(pool, &person_post_agg_form).await?; Ok(()) } @@ -223,9 +221,7 @@ pub async fn check_registration_application( // Fetch the registration application. If no admin id is present its still pending. Otherwise it // was processed (either accepted or denied). let local_user_id = local_user_view.local_user.id; - let registration = RegistrationApplication::find_by_local_user_id(pool, local_user_id) - .await? - .ok_or(LemmyErrorType::CouldntFindRegistrationApplication)?; + let registration = RegistrationApplication::find_by_local_user_id(pool, local_user_id).await?; if registration.admin_id.is_some() { Err(LemmyErrorType::RegistrationDenied(registration.deny_reason))? } else { @@ -254,9 +250,7 @@ async fn check_community_deleted_removed( community_id: CommunityId, pool: &mut DbPool<'_>, ) -> LemmyResult<()> { - let community = Community::read(pool, community_id) - .await? - .ok_or(LemmyErrorType::CouldntFindCommunity)?; + let community = Community::read(pool, community_id).await?; if community.deleted || community.removed { Err(LemmyErrorType::Deleted)? } @@ -704,7 +698,7 @@ pub async fn purge_image_posts_for_person( /// Delete a local_user's images async fn delete_local_user_images(person_id: PersonId, context: &LemmyContext) -> LemmyResult<()> { - if let Ok(Some(local_user)) = LocalUserView::read_person(&mut context.pool(), person_id).await { + if let Ok(local_user) = LocalUserView::read_person(&mut context.pool(), person_id).await { let pictrs_uploads = LocalImageView::get_all_by_local_user_id(&mut context.pool(), local_user.local_user.id) .await?; @@ -749,9 +743,7 @@ pub async fn remove_user_data( ) -> LemmyResult<()> { let pool = &mut context.pool(); // Purge user images - let person = Person::read(pool, banned_person_id) - .await? - .ok_or(LemmyErrorType::CouldntFindPerson)?; + let person = Person::read(pool, banned_person_id).await?; if let Some(avatar) = person.avatar { purge_image_from_pictrs(&avatar, context).await.ok(); } @@ -881,9 +873,7 @@ pub async fn remove_or_restore_user_data_in_community( pub async fn purge_user_account(person_id: PersonId, context: &LemmyContext) -> LemmyResult<()> { let pool = &mut context.pool(); - let person = Person::read(pool, person_id) - .await? - .ok_or(LemmyErrorType::CouldntFindPerson)?; + let person = Person::read(pool, person_id).await?; // Delete their local images, if they're a local user delete_local_user_images(person_id, context).await.ok(); @@ -913,7 +903,7 @@ pub async fn purge_user_account(person_id: PersonId, context: &LemmyContext) -> CommunityModerator::leave_all_communities(pool, person_id).await?; // Delete the oauth accounts linked to the local user - if let Ok(Some(local_user)) = LocalUserView::read_person(pool, person_id).await { + if let Ok(local_user) = LocalUserView::read_person(pool, person_id).await { OAuthAccount::delete_user_accounts(pool, local_user.local_user.id).await?; } diff --git a/crates/api_crud/src/comment/create.rs b/crates/api_crud/src/comment/create.rs index b4e52d3f9..795c27b35 100644 --- a/crates/api_crud/src/comment/create.rs +++ b/crates/api_crud/src/comment/create.rs @@ -57,8 +57,7 @@ pub async fn create_comment( Some(&local_user_view.local_user), true, ) - .await? - .ok_or(LemmyErrorType::CouldntFindPost)?; + .await?; let post = post_view.post; let community_id = post_view.community.id; @@ -79,8 +78,7 @@ pub async fn create_comment( Comment::read(&mut context.pool(), parent_id).await.ok() } else { None - } - .flatten(); + }; // If there's a parent_id, check to make sure that comment is in that post // Strange issue where sometimes the post ID of the parent comment is incorrect diff --git a/crates/api_crud/src/comment/delete.rs b/crates/api_crud/src/comment/delete.rs index 8c81608c8..2b5f35827 100644 --- a/crates/api_crud/src/comment/delete.rs +++ b/crates/api_crud/src/comment/delete.rs @@ -26,8 +26,7 @@ pub async fn delete_comment( comment_id, Some(&local_user_view.local_user), ) - .await? - .ok_or(LemmyErrorType::CouldntFindComment)?; + .await?; // Dont delete it if its already been deleted. if orig_comment.comment.deleted == data.deleted { diff --git a/crates/api_crud/src/comment/remove.rs b/crates/api_crud/src/comment/remove.rs index ec4923e93..3c137a984 100644 --- a/crates/api_crud/src/comment/remove.rs +++ b/crates/api_crud/src/comment/remove.rs @@ -31,8 +31,7 @@ pub async fn remove_comment( comment_id, Some(&local_user_view.local_user), ) - .await? - .ok_or(LemmyErrorType::CouldntFindComment)?; + .await?; check_community_mod_action( &local_user_view.person, diff --git a/crates/api_crud/src/comment/update.rs b/crates/api_crud/src/comment/update.rs index ed9460825..76bdcfbb4 100644 --- a/crates/api_crud/src/comment/update.rs +++ b/crates/api_crud/src/comment/update.rs @@ -41,8 +41,7 @@ pub async fn update_comment( comment_id, Some(&local_user_view.local_user), ) - .await? - .ok_or(LemmyErrorType::CouldntFindComment)?; + .await?; check_community_user_action( &local_user_view.person, diff --git a/crates/api_crud/src/community/update.rs b/crates/api_crud/src/community/update.rs index 6190a0ca7..798f4f4c1 100644 --- a/crates/api_crud/src/community/update.rs +++ b/crates/api_crud/src/community/update.rs @@ -51,9 +51,7 @@ pub async fn update_community( is_valid_body_field(desc, false)?; } - let old_community = Community::read(&mut context.pool(), data.community_id) - .await? - .ok_or(LemmyErrorType::CouldntFindCommunity)?; + let old_community = Community::read(&mut context.pool(), data.community_id).await?; let icon = diesel_url_update(data.icon.as_deref())?; replace_image(&icon, &old_community.icon, &context).await?; diff --git a/crates/api_crud/src/oauth_provider/update.rs b/crates/api_crud/src/oauth_provider/update.rs index 61d5b0adc..b4734bf36 100644 --- a/crates/api_crud/src/oauth_provider/update.rs +++ b/crates/api_crud/src/oauth_provider/update.rs @@ -7,7 +7,7 @@ use lemmy_db_schema::{ utils::{diesel_required_string_update, diesel_required_url_update, naive_now}, }; use lemmy_db_views::structs::LocalUserView; -use lemmy_utils::{error::LemmyError, LemmyErrorType}; +use lemmy_utils::error::LemmyError; #[tracing::instrument(skip(context))] pub async fn update_oauth_provider( @@ -37,8 +37,6 @@ pub async fn update_oauth_provider( let update_result = OAuthProvider::update(&mut context.pool(), data.id, &oauth_provider_form).await?; - let oauth_provider = OAuthProvider::read(&mut context.pool(), update_result.id) - .await? - .ok_or(LemmyErrorType::CouldntFindOauthProvider)?; + let oauth_provider = OAuthProvider::read(&mut context.pool(), update_result.id).await?; Ok(Json(oauth_provider)) } diff --git a/crates/api_crud/src/post/create.rs b/crates/api_crud/src/post/create.rs index e5ecab60c..95b4e4722 100644 --- a/crates/api_crud/src/post/create.rs +++ b/crates/api_crud/src/post/create.rs @@ -92,9 +92,7 @@ pub async fn create_post( .await?; let community_id = data.community_id; - 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 community.posting_restricted_to_mods { let community_id = data.community_id; let is_mod = CommunityModeratorView::is_community_moderator( diff --git a/crates/api_crud/src/post/delete.rs b/crates/api_crud/src/post/delete.rs index 6834030ac..be31759d5 100644 --- a/crates/api_crud/src/post/delete.rs +++ b/crates/api_crud/src/post/delete.rs @@ -21,9 +21,7 @@ pub async fn delete_post( local_user_view: LocalUserView, ) -> LemmyResult> { 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?; // Dont delete it if its already been deleted. if orig_post.deleted == data.deleted { diff --git a/crates/api_crud/src/post/read.rs b/crates/api_crud/src/post/read.rs index 4ecf24e0c..f597eecf4 100644 --- a/crates/api_crud/src/post/read.rs +++ b/crates/api_crud/src/post/read.rs @@ -33,16 +33,14 @@ pub async fn get_post( } else if let Some(comment_id) = data.comment_id { Comment::read(&mut context.pool(), comment_id) .await? - .ok_or(LemmyErrorType::CouldntFindComment)? .post_id } else { - Err(LemmyErrorType::CouldntFindPost)? + Err(LemmyErrorType::NotFound)? }; // Check to see if the person is a mod or admin, to show deleted / removed - let community_id = Post::read(&mut context.pool(), post_id) + let community_id = Post::read_xx(&mut context.pool(), post_id) .await? - .ok_or(LemmyErrorType::CouldntFindPost)? .community_id; let is_mod_or_admin = is_mod_or_admin_opt( @@ -60,8 +58,7 @@ pub async fn get_post( local_user.as_ref(), is_mod_or_admin, ) - .await? - .ok_or(LemmyErrorType::CouldntFindPost)?; + .await?; let post_id = post_view.post.id; if let Some(person_id) = person_id { @@ -83,8 +80,7 @@ pub async fn get_post( local_user.as_ref(), is_mod_or_admin, ) - .await? - .ok_or(LemmyErrorType::CouldntFindCommunity)?; + .await?; let moderators = CommunityModeratorView::for_community(&mut context.pool(), community_id).await?; diff --git a/crates/api_crud/src/post/remove.rs b/crates/api_crud/src/post/remove.rs index b4fdba6fb..c53a4552c 100644 --- a/crates/api_crud/src/post/remove.rs +++ b/crates/api_crud/src/post/remove.rs @@ -17,7 +17,7 @@ use lemmy_db_schema::{ traits::{Crud, Reportable}, }; use lemmy_db_views::structs::LocalUserView; -use lemmy_utils::{error::LemmyResult, LemmyErrorType}; +use lemmy_utils::error::LemmyResult; #[tracing::instrument(skip(context))] pub async fn remove_post( @@ -26,9 +26,7 @@ pub async fn remove_post( local_user_view: LocalUserView, ) -> LemmyResult> { 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, diff --git a/crates/api_crud/src/post/update.rs b/crates/api_crud/src/post/update.rs index d5053515c..48b2ccd42 100644 --- a/crates/api_crud/src/post/update.rs +++ b/crates/api_crud/src/post/update.rs @@ -85,9 +85,7 @@ pub async fn update_post( } 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_user_action( &local_user_view.person, diff --git a/crates/api_crud/src/private_message/create.rs b/crates/api_crud/src/private_message/create.rs index d8fe1bbfc..e79241022 100644 --- a/crates/api_crud/src/private_message/create.rs +++ b/crates/api_crud/src/private_message/create.rs @@ -56,16 +56,12 @@ pub async fn create_private_message( .await .with_lemmy_type(LemmyErrorType::CouldntCreatePrivateMessage)?; - let view = PrivateMessageView::read(&mut context.pool(), inserted_private_message.id) - .await? - .ok_or(LemmyErrorType::CouldntFindPrivateMessage)?; + let view = PrivateMessageView::read(&mut context.pool(), inserted_private_message.id).await?; // Send email to the local recipient, if one exists if view.recipient.local { let recipient_id = data.recipient_id; - let local_recipient = LocalUserView::read_person(&mut context.pool(), recipient_id) - .await? - .ok_or(LemmyErrorType::CouldntFindPerson)?; + let local_recipient = LocalUserView::read_person(&mut context.pool(), recipient_id).await?; let lang = get_interface_language(&local_recipient); let inbox_link = format!("{}/inbox", context.settings().get_protocol_and_hostname()); let sender_name = &local_user_view.person.name; diff --git a/crates/api_crud/src/private_message/delete.rs b/crates/api_crud/src/private_message/delete.rs index dc028ff41..936ff57b8 100644 --- a/crates/api_crud/src/private_message/delete.rs +++ b/crates/api_crud/src/private_message/delete.rs @@ -20,9 +20,7 @@ pub async fn delete_private_message( ) -> LemmyResult> { // Checking permissions let private_message_id = data.private_message_id; - let orig_private_message = PrivateMessage::read(&mut context.pool(), private_message_id) - .await? - .ok_or(LemmyErrorType::CouldntFindPrivateMessage)?; + let orig_private_message = PrivateMessage::read(&mut context.pool(), private_message_id).await?; if local_user_view.person.id != orig_private_message.creator_id { Err(LemmyErrorType::EditPrivateMessageNotAllowed)? } @@ -47,9 +45,7 @@ pub async fn delete_private_message( ) .await?; - let view = PrivateMessageView::read(&mut context.pool(), private_message_id) - .await? - .ok_or(LemmyErrorType::CouldntFindPrivateMessage)?; + let view = PrivateMessageView::read(&mut context.pool(), private_message_id).await?; Ok(Json(PrivateMessageResponse { private_message_view: view, })) diff --git a/crates/api_crud/src/private_message/update.rs b/crates/api_crud/src/private_message/update.rs index 364d5c2e3..20eaadb36 100644 --- a/crates/api_crud/src/private_message/update.rs +++ b/crates/api_crud/src/private_message/update.rs @@ -30,9 +30,7 @@ pub async fn update_private_message( // Checking permissions let private_message_id = data.private_message_id; - let orig_private_message = PrivateMessage::read(&mut context.pool(), private_message_id) - .await? - .ok_or(LemmyErrorType::CouldntFindPrivateMessage)?; + let orig_private_message = PrivateMessage::read(&mut context.pool(), private_message_id).await?; if local_user_view.person.id != orig_private_message.creator_id { Err(LemmyErrorType::EditPrivateMessageNotAllowed)? } @@ -56,9 +54,7 @@ pub async fn update_private_message( .await .with_lemmy_type(LemmyErrorType::CouldntUpdatePrivateMessage)?; - let view = PrivateMessageView::read(&mut context.pool(), private_message_id) - .await? - .ok_or(LemmyErrorType::CouldntFindPrivateMessage)?; + let view = PrivateMessageView::read(&mut context.pool(), private_message_id).await?; ActivityChannel::submit_activity( SendActivityData::UpdatePrivateMessage(view.clone()), diff --git a/crates/api_crud/src/user/create.rs b/crates/api_crud/src/user/create.rs index 1fb14a6e2..174b02930 100644 --- a/crates/api_crud/src/user/create.rs +++ b/crates/api_crud/src/user/create.rs @@ -234,7 +234,6 @@ pub async fn authenticate_with_oauth( let oauth_provider = OAuthProvider::read(&mut context.pool(), oauth_provider_id) .await .ok() - .flatten() .ok_or(LemmyErrorType::OauthAuthorizationInvalid)?; if !oauth_provider.enabled { @@ -262,10 +261,10 @@ pub async fn authenticate_with_oauth( // Lookup user by oauth_user_id let mut local_user_view = - LocalUserView::find_by_oauth_id(&mut context.pool(), oauth_provider.id, &oauth_user_id).await?; + LocalUserView::find_by_oauth_id(&mut context.pool(), oauth_provider.id, &oauth_user_id).await; let local_user: LocalUser; - if let Some(user_view) = local_user_view { + if let Ok(user_view) = local_user_view { // user found by oauth_user_id => Login user local_user = user_view.clone().local_user; @@ -292,10 +291,10 @@ pub async fn authenticate_with_oauth( local_site.registration_mode == RegistrationMode::RequireApplication; // Lookup user by OAUTH email and link accounts - local_user_view = LocalUserView::find_by_email(&mut context.pool(), &email).await?; + local_user_view = LocalUserView::find_by_email(&mut context.pool(), &email).await; let person; - if let Some(user_view) = local_user_view { + if let Ok(user_view) = local_user_view { // user found by email => link and login if linking is allowed // we only allow linking by email when email_verification is required otherwise emails cannot diff --git a/crates/apub/src/activities/block/mod.rs b/crates/apub/src/activities/block/mod.rs index 4641dd1d8..c8323fcb4 100644 --- a/crates/apub/src/activities/block/mod.rs +++ b/crates/apub/src/activities/block/mod.rs @@ -22,10 +22,7 @@ use lemmy_db_schema::{ traits::Crud, utils::DbPool, }; -use lemmy_utils::{ - error::{LemmyError, LemmyResult}, - LemmyErrorType, -}; +use lemmy_utils::error::{LemmyError, LemmyResult}; use serde::Deserialize; use url::Url; @@ -182,7 +179,6 @@ pub(crate) async fn send_ban_from_community( ) -> LemmyResult<()> { let community: ApubCommunity = Community::read(&mut context.pool(), community_id) .await? - .ok_or(LemmyErrorType::CouldntFindCommunity)? .into(); let expires = check_expire_time(data.expires)?; diff --git a/crates/apub/src/activities/community/announce.rs b/crates/apub/src/activities/community/announce.rs index aebf28217..69dbbdd3d 100644 --- a/crates/apub/src/activities/community/announce.rs +++ b/crates/apub/src/activities/community/announce.rs @@ -220,7 +220,7 @@ async fn can_accept_activity_in_community( } // Local only community can't federate if community.visibility != CommunityVisibility::Public { - return Err(LemmyErrorType::CouldntFindCommunity.into()); + return Err(LemmyErrorType::NotFound.into()); } } Ok(()) diff --git a/crates/apub/src/activities/community/collection_add.rs b/crates/apub/src/activities/community/collection_add.rs index 4048a1469..5ab754d35 100644 --- a/crates/apub/src/activities/community/collection_add.rs +++ b/crates/apub/src/activities/community/collection_add.rs @@ -36,10 +36,7 @@ use lemmy_db_schema::{ }, traits::{Crud, Joinable}, }; -use lemmy_utils::{ - error::{LemmyError, LemmyResult}, - LemmyErrorType, -}; +use lemmy_utils::error::{LemmyError, LemmyResult}; use url::Url; impl CollectionAdd { @@ -129,9 +126,7 @@ impl ActivityHandler for CollectionAdd { async fn receive(self, context: &Data) -> LemmyResult<()> { insert_received_activity(&self.id, context).await?; let (community, collection_type) = - Community::get_by_collection_url(&mut context.pool(), &self.target.into()) - .await? - .ok_or(LemmyErrorType::CouldntFindCommunity)?; + Community::get_by_collection_url(&mut context.pool(), &self.target.into()).await?; match collection_type { CollectionType::Moderators => { let new_mod = ObjectId::::from(self.object) @@ -188,11 +183,9 @@ pub(crate) async fn send_add_mod_to_community( let actor: ApubPerson = actor.into(); let community: ApubCommunity = Community::read(&mut context.pool(), community_id) .await? - .ok_or(LemmyErrorType::CouldntFindCommunity)? .into(); let updated_mod: ApubPerson = Person::read(&mut context.pool(), updated_mod_id) .await? - .ok_or(LemmyErrorType::CouldntFindPerson)? .into(); if added { CollectionAdd::send_add_mod(&community, &updated_mod, &actor, &context).await @@ -211,7 +204,6 @@ pub(crate) async fn send_feature_post( let post: ApubPost = post.into(); let community = Community::read(&mut context.pool(), post.community_id) .await? - .ok_or(LemmyErrorType::CouldntFindCommunity)? .into(); if featured { CollectionAdd::send_add_featured_post(&community, &post, &actor, &context).await diff --git a/crates/apub/src/activities/community/collection_remove.rs b/crates/apub/src/activities/community/collection_remove.rs index 634ca526c..90df1fd14 100644 --- a/crates/apub/src/activities/community/collection_remove.rs +++ b/crates/apub/src/activities/community/collection_remove.rs @@ -31,10 +31,7 @@ use lemmy_db_schema::{ }, traits::{Crud, Joinable}, }; -use lemmy_utils::{ - error::{LemmyError, LemmyResult}, - LemmyErrorType, -}; +use lemmy_utils::error::{LemmyError, LemmyResult}; use url::Url; impl CollectionRemove { @@ -124,9 +121,7 @@ impl ActivityHandler for CollectionRemove { async fn receive(self, context: &Data) -> LemmyResult<()> { insert_received_activity(&self.id, context).await?; let (community, collection_type) = - Community::get_by_collection_url(&mut context.pool(), &self.target.into()) - .await? - .ok_or(LemmyErrorType::CouldntFindCommunity)?; + Community::get_by_collection_url(&mut context.pool(), &self.target.into()).await?; match collection_type { CollectionType::Moderators => { let remove_mod = ObjectId::::from(self.object) diff --git a/crates/apub/src/activities/community/lock_page.rs b/crates/apub/src/activities/community/lock_page.rs index 322cd88c2..0d90b5bb0 100644 --- a/crates/apub/src/activities/community/lock_page.rs +++ b/crates/apub/src/activities/community/lock_page.rs @@ -32,10 +32,7 @@ use lemmy_db_schema::{ }, traits::Crud, }; -use lemmy_utils::{ - error::{LemmyError, LemmyResult}, - LemmyErrorType, -}; +use lemmy_utils::error::{LemmyError, LemmyResult}; use url::Url; #[async_trait::async_trait] @@ -132,7 +129,6 @@ pub(crate) async fn send_lock_post( ) -> LemmyResult<()> { let community: ApubCommunity = Community::read(&mut context.pool(), post.community_id) .await? - .ok_or(LemmyErrorType::CouldntFindCommunity)? .into(); let id = generate_activity_id( LockType::Lock, diff --git a/crates/apub/src/activities/community/report.rs b/crates/apub/src/activities/community/report.rs index d1bec0b75..4966add34 100644 --- a/crates/apub/src/activities/community/report.rs +++ b/crates/apub/src/activities/community/report.rs @@ -29,10 +29,7 @@ use lemmy_db_schema::{ }, traits::{Crud, Reportable}, }; -use lemmy_utils::{ - error::{LemmyError, LemmyResult}, - LemmyErrorType, -}; +use lemmy_utils::error::{LemmyError, LemmyResult}; use url::Url; impl Report { @@ -70,9 +67,7 @@ impl Report { PostOrComment::Post(p) => p.creator_id, PostOrComment::Comment(c) => c.creator_id, }; - let object_creator = Person::read(&mut context.pool(), object_creator_id) - .await? - .ok_or(LemmyErrorType::CouldntFindPerson)?; + let object_creator = Person::read(&mut context.pool(), object_creator_id).await?; let object_creator_site: Option = Site::read_from_instance_id(&mut context.pool(), object_creator.instance_id) .await? diff --git a/crates/apub/src/activities/create_or_update/comment.rs b/crates/apub/src/activities/create_or_update/comment.rs index 89be8d49e..b2c436049 100644 --- a/crates/apub/src/activities/create_or_update/comment.rs +++ b/crates/apub/src/activities/create_or_update/comment.rs @@ -42,7 +42,6 @@ use lemmy_db_schema::{ use lemmy_utils::{ error::{LemmyError, LemmyResult}, utils::mention::scrape_text_for_mentions, - LemmyErrorType, }; use url::Url; @@ -56,17 +55,11 @@ impl CreateOrUpdateNote { ) -> LemmyResult<()> { // TODO: might be helpful to add a comment method to retrieve community directly let post_id = comment.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?; let community_id = post.community_id; - let person: ApubPerson = Person::read(&mut context.pool(), person_id) - .await? - .ok_or(LemmyErrorType::CouldntFindPerson)? - .into(); + let person: ApubPerson = Person::read(&mut context.pool(), person_id).await?.into(); let community: ApubCommunity = Community::read(&mut context.pool(), community_id) .await? - .ok_or(LemmyErrorType::CouldntFindCommunity)? .into(); let id = generate_activity_id( diff --git a/crates/apub/src/activities/create_or_update/post.rs b/crates/apub/src/activities/create_or_update/post.rs index e8bfc36e6..fb53100f6 100644 --- a/crates/apub/src/activities/create_or_update/post.rs +++ b/crates/apub/src/activities/create_or_update/post.rs @@ -32,7 +32,7 @@ use lemmy_db_schema::{ }, traits::{Crud, Likeable}, }; -use lemmy_utils::error::{LemmyError, LemmyErrorType, LemmyResult}; +use lemmy_utils::error::{LemmyError, LemmyResult}; use url::Url; impl CreateOrUpdatePage { @@ -66,13 +66,9 @@ impl CreateOrUpdatePage { context: Data, ) -> LemmyResult<()> { let community_id = post.community_id; - let person: ApubPerson = Person::read(&mut context.pool(), person_id) - .await? - .ok_or(LemmyErrorType::CouldntFindPerson)? - .into(); + let person: ApubPerson = Person::read(&mut context.pool(), person_id).await?.into(); let community: ApubCommunity = Community::read(&mut context.pool(), community_id) .await? - .ok_or(LemmyErrorType::CouldntFindCommunity)? .into(); let create_or_update = diff --git a/crates/apub/src/activities/deletion/delete.rs b/crates/apub/src/activities/deletion/delete.rs index d203aacf2..ec2a0de95 100644 --- a/crates/apub/src/activities/deletion/delete.rs +++ b/crates/apub/src/activities/deletion/delete.rs @@ -176,8 +176,8 @@ pub(in crate::activities) async fn receive_remove_action( .await?; } // TODO these need to be implemented yet, for now, return errors - DeletableObjects::PrivateMessage(_) => Err(LemmyErrorType::CouldntFindPrivateMessage)?, - DeletableObjects::Person(_) => Err(LemmyErrorType::CouldntFindPerson)?, + DeletableObjects::PrivateMessage(_) => Err(LemmyErrorType::NotFound)?, + DeletableObjects::Person(_) => Err(LemmyErrorType::NotFound)?, } Ok(()) } diff --git a/crates/apub/src/activities/deletion/mod.rs b/crates/apub/src/activities/deletion/mod.rs index c9d268e74..b12532087 100644 --- a/crates/apub/src/activities/deletion/mod.rs +++ b/crates/apub/src/activities/deletion/mod.rs @@ -39,7 +39,7 @@ use lemmy_db_schema::{ }, traits::Crud, }; -use lemmy_utils::{error::LemmyResult, LemmyErrorType}; +use lemmy_utils::error::LemmyResult; use std::ops::Deref; use url::Url; @@ -87,7 +87,6 @@ pub(crate) async fn send_apub_delete_private_message( let recipient_id = pm.recipient_id; let recipient: ApubPerson = Person::read(&mut context.pool(), recipient_id) .await? - .ok_or(LemmyErrorType::CouldntFindPerson)? .into(); let deletable = DeletableObjects::PrivateMessage(pm.into()); diff --git a/crates/apub/src/activities/deletion/undo_delete.rs b/crates/apub/src/activities/deletion/undo_delete.rs index b50580852..102c72678 100644 --- a/crates/apub/src/activities/deletion/undo_delete.rs +++ b/crates/apub/src/activities/deletion/undo_delete.rs @@ -156,8 +156,8 @@ impl UndoDelete { .await?; } // TODO these need to be implemented yet, for now, return errors - DeletableObjects::PrivateMessage(_) => Err(LemmyErrorType::CouldntFindPrivateMessage)?, - DeletableObjects::Person(_) => Err(LemmyErrorType::CouldntFindPerson)?, + DeletableObjects::PrivateMessage(_) => Err(LemmyErrorType::NotFound)?, + DeletableObjects::Person(_) => Err(LemmyErrorType::NotFound)?, } Ok(()) } diff --git a/crates/apub/src/activities/following/follow.rs b/crates/apub/src/activities/following/follow.rs index 97227835a..02f29a1a9 100644 --- a/crates/apub/src/activities/following/follow.rs +++ b/crates/apub/src/activities/following/follow.rs @@ -106,7 +106,7 @@ impl ActivityHandler for Follow { UserOrCommunity::Community(c) => { // Dont allow following local-only community via federation. if c.visibility != CommunityVisibility::Public { - return Err(LemmyErrorType::CouldntFindCommunity.into()); + return Err(LemmyErrorType::NotFound.into()); } let form = CommunityFollowerForm { community_id: c.id, diff --git a/crates/apub/src/activities/mod.rs b/crates/apub/src/activities/mod.rs index b256a91f7..5389e5fdd 100644 --- a/crates/apub/src/activities/mod.rs +++ b/crates/apub/src/activities/mod.rs @@ -245,9 +245,7 @@ pub async fn match_outgoing_activities( CreateOrUpdatePage::send(post, creator_id, CreateOrUpdateType::Update, context).await } DeletePost(post, person, data) => { - 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?; send_apub_delete_in_community( person, community, @@ -264,9 +262,7 @@ pub async fn match_outgoing_activities( reason, removed, } => { - 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?; send_apub_delete_in_community( moderator, community, diff --git a/crates/apub/src/activity_lists.rs b/crates/apub/src/activity_lists.rs index 2d1fac449..e895ad1cc 100644 --- a/crates/apub/src/activity_lists.rs +++ b/crates/apub/src/activity_lists.rs @@ -117,7 +117,7 @@ impl InCommunity for AnnouncableActivities { CollectionRemove(a) => a.community(context).await, LockPost(a) => a.community(context).await, UndoLockPost(a) => a.community(context).await, - Page(_) => Err(LemmyErrorType::CouldntFindPost.into()), + Page(_) => Err(LemmyErrorType::NotFound.into()), } } } diff --git a/crates/apub/src/api/list_comments.rs b/crates/apub/src/api/list_comments.rs index 3223921bc..c97e051d0 100644 --- a/crates/apub/src/api/list_comments.rs +++ b/crates/apub/src/api/list_comments.rs @@ -64,12 +64,7 @@ pub async fn list_comments( // If a parent_id is given, fetch the comment to get the path let parent_path = if let Some(parent_id) = parent_id { - Some( - Comment::read(&mut context.pool(), parent_id) - .await? - .ok_or(LemmyErrorType::CouldntFindComment)? - .path, - ) + Some(Comment::read(&mut context.pool(), parent_id).await?.path) } else { None }; diff --git a/crates/apub/src/api/read_community.rs b/crates/apub/src/api/read_community.rs index 62fd6ec0b..f94769158 100644 --- a/crates/apub/src/api/read_community.rs +++ b/crates/apub/src/api/read_community.rs @@ -13,7 +13,7 @@ use lemmy_db_schema::source::{ }; use lemmy_db_views::structs::LocalUserView; use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView}; -use lemmy_utils::error::{LemmyErrorExt, LemmyErrorExt2, LemmyErrorType, LemmyResult}; +use lemmy_utils::error::{LemmyErrorType, LemmyResult}; #[tracing::instrument(skip(context))] pub async fn get_community( @@ -36,8 +36,7 @@ pub async fn get_community( None => { let name = data.name.clone().unwrap_or_else(|| "main".to_string()); resolve_actor_identifier::(&name, &context, &local_user_view, true) - .await - .with_lemmy_type(LemmyErrorType::CouldntFindCommunity)? + .await? .id } }; @@ -56,12 +55,9 @@ pub async fn get_community( local_user, is_mod_or_admin, ) - .await? - .ok_or(LemmyErrorType::CouldntFindCommunity)?; + .await?; - 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?; let site = read_site_for_actor(community_view.community.actor_id.clone(), &context).await?; diff --git a/crates/apub/src/api/read_person.rs b/crates/apub/src/api/read_person.rs index 0a2a76465..58e68f4b9 100644 --- a/crates/apub/src/api/read_person.rs +++ b/crates/apub/src/api/read_person.rs @@ -13,7 +13,7 @@ use lemmy_db_views::{ structs::{LocalUserView, SiteView}, }; use lemmy_db_views_actor::structs::{CommunityModeratorView, PersonView}; -use lemmy_utils::error::{LemmyErrorExt2, LemmyErrorType, LemmyResult}; +use lemmy_utils::error::{LemmyErrorType, LemmyResult}; #[tracing::instrument(skip(context))] pub async fn read_person( @@ -35,20 +35,17 @@ pub async fn read_person( None => { if let Some(username) = &data.username { resolve_actor_identifier::(username, &context, &local_user_view, true) - .await - .with_lemmy_type(LemmyErrorType::CouldntFindPerson)? + .await? .id } else { - Err(LemmyErrorType::CouldntFindPerson)? + Err(LemmyErrorType::NotFound)? } } }; // You don't need to return settings for the user, since this comes back with GetSite // `my_user` - let person_view = PersonView::read(&mut context.pool(), person_details_id) - .await? - .ok_or(LemmyErrorType::CouldntFindPerson)?; + let person_view = PersonView::read(&mut context.pool(), person_details_id).await?; let sort = data.sort; let page = data.page; diff --git a/crates/apub/src/api/resolve_object.rs b/crates/apub/src/api/resolve_object.rs index 4c592aeeb..b3061d1ce 100644 --- a/crates/apub/src/api/resolve_object.rs +++ b/crates/apub/src/api/resolve_object.rs @@ -34,11 +34,11 @@ pub async fn resolve_object( // user isn't authenticated only allow a local search. search_query_to_object_id_local(&data.q, &context).await } - .with_lemmy_type(LemmyErrorType::CouldntFindObject)?; + .with_lemmy_type(LemmyErrorType::NotFound)?; convert_response(res, local_user_view, &mut context.pool()) .await - .with_lemmy_type(LemmyErrorType::CouldntFindObject) + .with_lemmy_type(LemmyErrorType::NotFound) } async fn convert_response( @@ -54,36 +54,20 @@ async fn convert_response( match object { Post(p) => { removed_or_deleted = p.deleted || p.removed; - res.post = Some( - PostView::read(pool, p.id, local_user.as_ref(), false) - .await? - .ok_or(LemmyErrorType::CouldntFindPost)?, - ) + res.post = Some(PostView::read(pool, p.id, local_user.as_ref(), false).await?) } Comment(c) => { removed_or_deleted = c.deleted || c.removed; - res.comment = Some( - CommentView::read(pool, c.id, local_user.as_ref()) - .await? - .ok_or(LemmyErrorType::CouldntFindComment)?, - ) + res.comment = Some(CommentView::read(pool, c.id, local_user.as_ref()).await?) } PersonOrCommunity(p) => match *p { UserOrCommunity::User(u) => { removed_or_deleted = u.deleted; - res.person = Some( - PersonView::read(pool, u.id) - .await? - .ok_or(LemmyErrorType::CouldntFindPerson)?, - ) + res.person = Some(PersonView::read(pool, u.id).await?) } UserOrCommunity::Community(c) => { removed_or_deleted = c.deleted || c.removed; - res.community = Some( - CommunityView::read(pool, c.id, local_user.as_ref(), false) - .await? - .ok_or(LemmyErrorType::CouldntFindCommunity)?, - ) + res.community = Some(CommunityView::read(pool, c.id, local_user.as_ref(), false).await?) } }, }; diff --git a/crates/apub/src/api/user_settings_backup.rs b/crates/apub/src/api/user_settings_backup.rs index 3eb9c16e5..6764f62cb 100644 --- a/crates/apub/src/api/user_settings_backup.rs +++ b/crates/apub/src/api/user_settings_backup.rs @@ -348,11 +348,7 @@ mod tests { let user_form = LocalUserInsertForm::test_form(person.id); let local_user = LocalUser::create(&mut context.pool(), &user_form, vec![]).await?; - Ok( - LocalUserView::read(&mut context.pool(), local_user.id) - .await? - .ok_or(LemmyErrorType::CouldntFindLocalUser)?, - ) + Ok(LocalUserView::read(&mut context.pool(), local_user.id).await?) } #[tokio::test] @@ -386,9 +382,8 @@ mod tests { // wait for background task to finish sleep(Duration::from_millis(1000)).await; - let import_user_updated = LocalUserView::read(&mut context.pool(), import_user.local_user.id) - .await? - .ok_or(LemmyErrorType::CouldntFindLocalUser)?; + let import_user_updated = + LocalUserView::read(&mut context.pool(), import_user.local_user.id).await?; assert_eq!( export_user.person.display_name, diff --git a/crates/apub/src/fetcher/post_or_comment.rs b/crates/apub/src/fetcher/post_or_comment.rs index e352e1257..8502ffb1a 100644 --- a/crates/apub/src/fetcher/post_or_comment.rs +++ b/crates/apub/src/fetcher/post_or_comment.rs @@ -12,10 +12,7 @@ use lemmy_db_schema::{ source::{community::Community, post::Post}, traits::Crud, }; -use lemmy_utils::{ - error::{LemmyError, LemmyResult}, - LemmyErrorType, -}; +use lemmy_utils::error::{LemmyError, LemmyResult}; use serde::Deserialize; use url::Url; @@ -97,15 +94,9 @@ impl InCommunity for PostOrComment { PostOrComment::Comment(c) => { Post::read(&mut context.pool(), c.post_id) .await? - .ok_or(LemmyErrorType::CouldntFindPost)? .community_id } }; - Ok( - Community::read(&mut context.pool(), cid) - .await? - .ok_or(LemmyErrorType::CouldntFindCommunity)? - .into(), - ) + Ok(Community::read(&mut context.pool(), cid).await?.into()) } } diff --git a/crates/apub/src/http/comment.rs b/crates/apub/src/http/comment.rs index 17711817e..d6b3c818d 100644 --- a/crates/apub/src/http/comment.rs +++ b/crates/apub/src/http/comment.rs @@ -15,7 +15,7 @@ use lemmy_db_schema::{ source::{comment::Comment, community::Community, post::Post}, traits::Crud, }; -use lemmy_utils::{error::LemmyResult, LemmyErrorType}; +use lemmy_utils::error::LemmyResult; use serde::Deserialize; #[derive(Deserialize)] @@ -31,16 +31,9 @@ pub(crate) async fn get_apub_comment( ) -> LemmyResult { let id = CommentId(info.comment_id.parse::()?); // Can't use CommentView here because it excludes deleted/removed/local-only items - let comment: ApubComment = Comment::read(&mut context.pool(), id) - .await? - .ok_or(LemmyErrorType::CouldntFindComment)? - .into(); - let post = Post::read(&mut context.pool(), comment.post_id) - .await? - .ok_or(LemmyErrorType::CouldntFindPost)?; - let community = Community::read(&mut context.pool(), post.community_id) - .await? - .ok_or(LemmyErrorType::CouldntFindCommunity)?; + let comment: ApubComment = Comment::read(&mut context.pool(), id).await?.into(); + let post = Post::read(&mut context.pool(), comment.post_id).await?; + let community = Community::read(&mut context.pool(), post.community_id).await?; check_community_public(&community)?; if !comment.local { diff --git a/crates/apub/src/http/community.rs b/crates/apub/src/http/community.rs index da72319d5..53d25be62 100644 --- a/crates/apub/src/http/community.rs +++ b/crates/apub/src/http/community.rs @@ -35,7 +35,7 @@ pub(crate) async fn get_apub_community_http( let community: ApubCommunity = Community::read_from_name(&mut context.pool(), &info.community_name, true) .await? - .ok_or(LemmyErrorType::CouldntFindCommunity)? + .ok_or(LemmyErrorType::NotFound)? .into(); if community.deleted || community.removed { @@ -67,7 +67,7 @@ pub(crate) async fn get_apub_community_followers( ) -> LemmyResult { let community = Community::read_from_name(&mut context.pool(), &info.community_name, false) .await? - .ok_or(LemmyErrorType::CouldntFindCommunity)?; + .ok_or(LemmyErrorType::NotFound)?; check_community_public(&community)?; let followers = ApubCommunityFollower::read_local(&community.into(), &context).await?; create_apub_response(&followers) @@ -82,7 +82,7 @@ pub(crate) async fn get_apub_community_outbox( let community: ApubCommunity = Community::read_from_name(&mut context.pool(), &info.community_name, false) .await? - .ok_or(LemmyErrorType::CouldntFindCommunity)? + .ok_or(LemmyErrorType::NotFound)? .into(); check_community_public(&community)?; let outbox = ApubCommunityOutbox::read_local(&community, &context).await?; @@ -97,7 +97,7 @@ pub(crate) async fn get_apub_community_moderators( let community: ApubCommunity = Community::read_from_name(&mut context.pool(), &info.community_name, false) .await? - .ok_or(LemmyErrorType::CouldntFindCommunity)? + .ok_or(LemmyErrorType::NotFound)? .into(); check_community_public(&community)?; let moderators = ApubCommunityModerators::read_local(&community, &context).await?; @@ -112,7 +112,7 @@ pub(crate) async fn get_apub_community_featured( let community: ApubCommunity = Community::read_from_name(&mut context.pool(), &info.community_name, false) .await? - .ok_or(LemmyErrorType::CouldntFindCommunity)? + .ok_or(LemmyErrorType::NotFound)? .into(); check_community_public(&community)?; let featured = ApubCommunityFeatured::read_local(&community, &context).await?; diff --git a/crates/apub/src/http/mod.rs b/crates/apub/src/http/mod.rs index a01afe676..dad39881f 100644 --- a/crates/apub/src/http/mod.rs +++ b/crates/apub/src/http/mod.rs @@ -106,9 +106,7 @@ pub(crate) async fn get_activity( info.id ))? .into(); - let activity = SentActivity::read_from_apub_id(&mut context.pool(), &activity_id) - .await? - .ok_or(LemmyErrorType::CouldntFindActivity)?; + let activity = SentActivity::read_from_apub_id(&mut context.pool(), &activity_id).await?; let sensitive = activity.sensitive; if sensitive { @@ -124,7 +122,7 @@ fn check_community_public(community: &Community) -> LemmyResult<()> { Err(LemmyErrorType::Deleted)? } if community.visibility != CommunityVisibility::Public { - return Err(LemmyErrorType::CouldntFindCommunity.into()); + return Err(LemmyErrorType::NotFound.into()); } Ok(()) } diff --git a/crates/apub/src/http/person.rs b/crates/apub/src/http/person.rs index ba2372fe8..e8e072a97 100644 --- a/crates/apub/src/http/person.rs +++ b/crates/apub/src/http/person.rs @@ -32,7 +32,7 @@ pub(crate) async fn get_apub_person_http( // TODO: this needs to be able to read deleted persons, so that it can send tombstones let person: ApubPerson = Person::read_from_name(&mut context.pool(), &user_name, true) .await? - .ok_or(LemmyErrorType::CouldntFindPerson)? + .ok_or(LemmyErrorType::NotFound)? .into(); if !person.deleted { @@ -63,7 +63,7 @@ pub(crate) async fn get_apub_person_outbox( ) -> LemmyResult { let person = Person::read_from_name(&mut context.pool(), &info.user_name, false) .await? - .ok_or(LemmyErrorType::CouldntFindPerson)?; + .ok_or(LemmyErrorType::NotFound)?; let outbox_id = generate_outbox_url(&person.actor_id)?.into(); let outbox = EmptyOutbox::new(outbox_id)?; create_apub_response(&outbox) diff --git a/crates/apub/src/http/post.rs b/crates/apub/src/http/post.rs index 513cba7ea..ce6612826 100644 --- a/crates/apub/src/http/post.rs +++ b/crates/apub/src/http/post.rs @@ -15,7 +15,7 @@ use lemmy_db_schema::{ source::{community::Community, post::Post}, traits::Crud, }; -use lemmy_utils::{error::LemmyResult, LemmyErrorType}; +use lemmy_utils::error::LemmyResult; use serde::Deserialize; #[derive(Deserialize)] @@ -31,13 +31,8 @@ pub(crate) async fn get_apub_post( ) -> LemmyResult { let id = PostId(info.post_id.parse::()?); // Can't use PostView here because it excludes deleted/removed/local-only items - let post: ApubPost = Post::read(&mut context.pool(), id) - .await? - .ok_or(LemmyErrorType::CouldntFindPost)? - .into(); - let community = Community::read(&mut context.pool(), post.community_id) - .await? - .ok_or(LemmyErrorType::CouldntFindCommunity)?; + let post: ApubPost = Post::read(&mut context.pool(), id).await?.into(); + let community = Community::read(&mut context.pool(), post.community_id).await?; check_community_public(&community)?; if !post.local { diff --git a/crates/apub/src/mentions.rs b/crates/apub/src/mentions.rs index de472bd8a..6aa1d97fb 100644 --- a/crates/apub/src/mentions.rs +++ b/crates/apub/src/mentions.rs @@ -99,21 +99,12 @@ async fn get_comment_parent_creator( comment: &Comment, ) -> LemmyResult { let parent_creator_id = if let Some(parent_comment_id) = comment.parent_comment_id() { - let parent_comment = Comment::read(pool, parent_comment_id) - .await? - .ok_or(LemmyErrorType::CouldntFindComment)?; + let parent_comment = Comment::read(pool, parent_comment_id).await?; parent_comment.creator_id } else { let parent_post_id = comment.post_id; - let parent_post = Post::read(pool, parent_post_id) - .await? - .ok_or(LemmyErrorType::CouldntFindPost)?; + let parent_post = Post::read(pool, parent_post_id).await?; parent_post.creator_id }; - Ok( - Person::read(pool, parent_creator_id) - .await? - .ok_or(LemmyErrorType::CouldntFindPerson)? - .into(), - ) + Ok(Person::read(pool, parent_creator_id).await?.into()) } diff --git a/crates/apub/src/objects/comment.rs b/crates/apub/src/objects/comment.rs index 466094b7f..ad7d84a6f 100644 --- a/crates/apub/src/objects/comment.rs +++ b/crates/apub/src/objects/comment.rs @@ -91,23 +91,15 @@ impl Object for ApubComment { #[tracing::instrument(skip_all)] async fn into_json(self, context: &Data) -> LemmyResult { let creator_id = self.creator_id; - let creator = Person::read(&mut context.pool(), creator_id) - .await? - .ok_or(LemmyErrorType::CouldntFindPerson)?; + let creator = Person::read(&mut context.pool(), creator_id).await?; let post_id = self.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?; let community_id = post.community_id; - let community = Community::read(&mut context.pool(), community_id) - .await? - .ok_or(LemmyErrorType::CouldntFindCommunity)?; + let community = Community::read(&mut context.pool(), community_id).await?; let in_reply_to = if let Some(comment_id) = self.parent_comment_id() { - let parent_comment = Comment::read(&mut context.pool(), comment_id) - .await? - .ok_or(LemmyErrorType::CouldntFindComment)?; + let parent_comment = Comment::read(&mut context.pool(), comment_id).await?; parent_comment.ap_id.into() } else { post.ap_id.into() diff --git a/crates/apub/src/objects/post.rs b/crates/apub/src/objects/post.rs index f86bed802..dd8b247d7 100644 --- a/crates/apub/src/objects/post.rs +++ b/crates/apub/src/objects/post.rs @@ -107,13 +107,9 @@ impl Object for ApubPost { #[tracing::instrument(skip_all)] async fn into_json(self, context: &Data) -> LemmyResult { let creator_id = self.creator_id; - let creator = Person::read(&mut context.pool(), creator_id) - .await? - .ok_or(LemmyErrorType::CouldntFindPerson)?; + let creator = Person::read(&mut context.pool(), creator_id).await?; let community_id = self.community_id; - let community = Community::read(&mut context.pool(), community_id) - .await? - .ok_or(LemmyErrorType::CouldntFindCommunity)?; + let community = Community::read(&mut context.pool(), community_id).await?; let language = LanguageTag::new_single(self.language_id, &mut context.pool()).await?; let attachment = self diff --git a/crates/apub/src/objects/private_message.rs b/crates/apub/src/objects/private_message.rs index fc9697391..ccebcf110 100644 --- a/crates/apub/src/objects/private_message.rs +++ b/crates/apub/src/objects/private_message.rs @@ -73,20 +73,16 @@ impl Object for ApubPrivateMessage { async fn delete(self, _context: &Data) -> LemmyResult<()> { // do nothing, because pm can't be fetched over http - Err(LemmyErrorType::CouldntFindPrivateMessage.into()) + Err(LemmyErrorType::NotFound.into()) } #[tracing::instrument(skip_all)] async fn into_json(self, context: &Data) -> LemmyResult { let creator_id = self.creator_id; - let creator = Person::read(&mut context.pool(), creator_id) - .await? - .ok_or(LemmyErrorType::CouldntFindPerson)?; + let creator = Person::read(&mut context.pool(), creator_id).await?; let recipient_id = self.recipient_id; - let recipient = Person::read(&mut context.pool(), recipient_id) - .await? - .ok_or(LemmyErrorType::CouldntFindPerson)?; + let recipient = Person::read(&mut context.pool(), recipient_id).await?; let note = ChatMessage { r#type: ChatMessageType::ChatMessage, diff --git a/crates/apub/src/protocol/activities/community/collection_add.rs b/crates/apub/src/protocol/activities/community/collection_add.rs index 0e2ab75a6..777ad8b62 100644 --- a/crates/apub/src/protocol/activities/community/collection_add.rs +++ b/crates/apub/src/protocol/activities/community/collection_add.rs @@ -11,7 +11,7 @@ use activitypub_federation::{ }; use lemmy_api_common::context::LemmyContext; use lemmy_db_schema::source::community::Community; -use lemmy_utils::{error::LemmyResult, LemmyErrorType}; +use lemmy_utils::error::LemmyResult; use serde::{Deserialize, Serialize}; use url::Url; @@ -35,9 +35,7 @@ pub struct CollectionAdd { impl InCommunity for CollectionAdd { async fn community(&self, context: &Data) -> LemmyResult { let (community, _) = - Community::get_by_collection_url(&mut context.pool(), &self.clone().target.into()) - .await? - .ok_or(LemmyErrorType::CouldntFindCommunity)?; + Community::get_by_collection_url(&mut context.pool(), &self.clone().target.into()).await?; if let Some(audience) = &self.audience { verify_community_matches(audience, community.actor_id.clone())?; } diff --git a/crates/apub/src/protocol/activities/community/collection_remove.rs b/crates/apub/src/protocol/activities/community/collection_remove.rs index 51c4761ba..afc0c24a0 100644 --- a/crates/apub/src/protocol/activities/community/collection_remove.rs +++ b/crates/apub/src/protocol/activities/community/collection_remove.rs @@ -11,7 +11,7 @@ use activitypub_federation::{ }; use lemmy_api_common::context::LemmyContext; use lemmy_db_schema::source::community::Community; -use lemmy_utils::{error::LemmyResult, LemmyErrorType}; +use lemmy_utils::error::LemmyResult; use serde::{Deserialize, Serialize}; use url::Url; @@ -35,9 +35,7 @@ pub struct CollectionRemove { impl InCommunity for CollectionRemove { async fn community(&self, context: &Data) -> LemmyResult { let (community, _) = - Community::get_by_collection_url(&mut context.pool(), &self.clone().target.into()) - .await? - .ok_or(LemmyErrorType::CouldntFindCommunity)?; + Community::get_by_collection_url(&mut context.pool(), &self.clone().target.into()).await?; if let Some(audience) = &self.audience { verify_community_matches(audience, community.actor_id.clone())?; } diff --git a/crates/apub/src/protocol/activities/community/lock_page.rs b/crates/apub/src/protocol/activities/community/lock_page.rs index a08b3c5a2..5c8ecfca9 100644 --- a/crates/apub/src/protocol/activities/community/lock_page.rs +++ b/crates/apub/src/protocol/activities/community/lock_page.rs @@ -11,7 +11,7 @@ use activitypub_federation::{ }; use lemmy_api_common::context::LemmyContext; use lemmy_db_schema::{source::community::Community, traits::Crud}; -use lemmy_utils::{error::LemmyResult, LemmyErrorType}; +use lemmy_utils::error::LemmyResult; use serde::{Deserialize, Serialize}; use strum::Display; use url::Url; @@ -55,9 +55,7 @@ pub struct UndoLockPage { impl InCommunity for LockPage { async fn community(&self, context: &Data) -> LemmyResult { let post = self.object.dereference(context).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?; if let Some(audience) = &self.audience { verify_community_matches(audience, community.actor_id.clone())?; } diff --git a/crates/apub/src/protocol/activities/community/report.rs b/crates/apub/src/protocol/activities/community/report.rs index 7698cde50..dd0f72f43 100644 --- a/crates/apub/src/protocol/activities/community/report.rs +++ b/crates/apub/src/protocol/activities/community/report.rs @@ -38,7 +38,7 @@ impl Report { .summary .clone() .or(self.content.clone()) - .ok_or(LemmyErrorType::CouldntFindObject.into()) + .ok_or(LemmyErrorType::NotFound.into()) } } @@ -63,7 +63,7 @@ impl ReportObject { return deref; } } - Err(LemmyErrorType::CouldntFindObject.into()) + Err(LemmyErrorType::NotFound.into()) } } } diff --git a/crates/apub/src/protocol/activities/create_or_update/note.rs b/crates/apub/src/protocol/activities/create_or_update/note.rs index 43ffeb291..ff0728174 100644 --- a/crates/apub/src/protocol/activities/create_or_update/note.rs +++ b/crates/apub/src/protocol/activities/create_or_update/note.rs @@ -11,7 +11,7 @@ use activitypub_federation::{ }; use lemmy_api_common::context::LemmyContext; use lemmy_db_schema::{source::community::Community, traits::Crud}; -use lemmy_utils::{error::LemmyResult, LemmyErrorType}; +use lemmy_utils::error::LemmyResult; use serde::{Deserialize, Serialize}; use url::Url; @@ -36,9 +36,7 @@ pub struct CreateOrUpdateNote { impl InCommunity for CreateOrUpdateNote { async fn community(&self, context: &Data) -> LemmyResult { let post = self.object.get_parents(context).await?.0; - 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?; if let Some(audience) = &self.audience { verify_community_matches(audience, community.actor_id.clone())?; } diff --git a/crates/apub/src/protocol/activities/deletion/delete.rs b/crates/apub/src/protocol/activities/deletion/delete.rs index 3b9aad079..3a29da069 100644 --- a/crates/apub/src/protocol/activities/deletion/delete.rs +++ b/crates/apub/src/protocol/activities/deletion/delete.rs @@ -15,7 +15,7 @@ use lemmy_db_schema::{ source::{community::Community, post::Post}, traits::Crud, }; -use lemmy_utils::{error::LemmyResult, LemmyErrorType}; +use lemmy_utils::error::LemmyResult; use serde::{Deserialize, Serialize}; use serde_with::skip_serializing_none; use url::Url; @@ -51,9 +51,7 @@ impl InCommunity for Delete { let community_id = match DeletableObjects::read_from_db(self.object.id(), context).await? { DeletableObjects::Community(c) => c.id, DeletableObjects::Comment(c) => { - let post = Post::read(&mut context.pool(), c.post_id) - .await? - .ok_or(LemmyErrorType::CouldntFindPost)?; + let post = Post::read(&mut context.pool(), c.post_id).await?; post.community_id } DeletableObjects::Post(p) => p.community_id, @@ -62,9 +60,7 @@ impl InCommunity for Delete { return Err(anyhow!("Private message is not part of community").into()) } }; - 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 let Some(audience) = &self.audience { verify_community_matches(audience, community.actor_id.clone())?; } diff --git a/crates/apub/src/protocol/objects/note.rs b/crates/apub/src/protocol/objects/note.rs index b0ae00037..a092cec9f 100644 --- a/crates/apub/src/protocol/objects/note.rs +++ b/crates/apub/src/protocol/objects/note.rs @@ -20,7 +20,7 @@ use lemmy_db_schema::{ source::{community::Community, post::Post}, traits::Crud, }; -use lemmy_utils::{error::LemmyResult, LemmyErrorType}; +use lemmy_utils::error::LemmyResult; use serde::{Deserialize, Serialize}; use serde_with::skip_serializing_none; use std::ops::Deref; @@ -64,9 +64,7 @@ impl Note { PostOrComment::Post(p) => Ok((p.clone(), None)), PostOrComment::Comment(c) => { let post_id = c.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?; Ok((post.into(), Some(c.clone()))) } } @@ -77,9 +75,7 @@ impl Note { impl InCommunity for Note { async fn community(&self, context: &Data) -> LemmyResult { let (post, _) = self.get_parents(context).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?; if let Some(audience) = &self.audience { verify_community_matches(audience, community.actor_id.clone())?; } diff --git a/crates/apub/src/protocol/objects/page.rs b/crates/apub/src/protocol/objects/page.rs index 9c37c88c3..9a9866cfb 100644 --- a/crates/apub/src/protocol/objects/page.rs +++ b/crates/apub/src/protocol/objects/page.rs @@ -226,7 +226,7 @@ impl InCommunity for Page { break c; } } else { - Err(LemmyErrorType::CouldntFindCommunity)?; + Err(LemmyErrorType::NotFound)?; } } } @@ -234,7 +234,7 @@ impl InCommunity for Page { p.iter() .find(|a| a.kind == PersonOrGroupType::Group) .map(|a| ObjectId::::from(a.id.clone().into_inner())) - .ok_or(LemmyErrorType::CouldntFindCommunity)? + .ok_or(LemmyErrorType::NotFound)? .dereference(context) .await? } diff --git a/crates/db_schema/src/aggregates/comment_aggregates.rs b/crates/db_schema/src/aggregates/comment_aggregates.rs index a928f2684..150899763 100644 --- a/crates/db_schema/src/aggregates/comment_aggregates.rs +++ b/crates/db_schema/src/aggregates/comment_aggregates.rs @@ -1,6 +1,5 @@ use crate::{ aggregates::structs::CommentAggregates, - diesel::OptionalExtension, newtypes::CommentId, schema::comment_aggregates, utils::{functions::hot_rank, get_conn, DbPool}, @@ -9,13 +8,9 @@ use diesel::{result::Error, ExpressionMethods, QueryDsl}; use diesel_async::RunQueryDsl; impl CommentAggregates { - pub async fn read(pool: &mut DbPool<'_>, comment_id: CommentId) -> Result, Error> { + pub async fn read(pool: &mut DbPool<'_>, comment_id: CommentId) -> Result { let conn = &mut get_conn(pool).await?; - comment_aggregates::table - .find(comment_id) - .first(conn) - .await - .optional() + comment_aggregates::table.find(comment_id).first(conn).await } pub async fn update_hot_rank( @@ -115,7 +110,6 @@ mod tests { let comment_aggs_before_delete = CommentAggregates::read(pool, inserted_comment.id) .await - .unwrap() .unwrap(); assert_eq!(1, comment_aggs_before_delete.score); @@ -134,7 +128,6 @@ mod tests { let comment_aggs_after_dislike = CommentAggregates::read(pool, inserted_comment.id) .await - .unwrap() .unwrap(); assert_eq!(0, comment_aggs_after_dislike.score); @@ -147,7 +140,6 @@ mod tests { .unwrap(); let after_like_remove = CommentAggregates::read(pool, inserted_comment.id) .await - .unwrap() .unwrap(); assert_eq!(-1, after_like_remove.score); assert_eq!(0, after_like_remove.upvotes); @@ -157,10 +149,8 @@ mod tests { Post::delete(pool, inserted_post.id).await.unwrap(); // Should be none found, since the post was deleted - let after_delete = CommentAggregates::read(pool, inserted_comment.id) - .await - .unwrap(); - assert!(after_delete.is_none()); + let after_delete = CommentAggregates::read(pool, inserted_comment.id).await; + assert!(after_delete.is_err()); // This should delete all the associated rows, and fire triggers Person::delete(pool, another_inserted_person.id) diff --git a/crates/db_schema/src/aggregates/post_aggregates.rs b/crates/db_schema/src/aggregates/post_aggregates.rs index fe6ced04b..f15567d8f 100644 --- a/crates/db_schema/src/aggregates/post_aggregates.rs +++ b/crates/db_schema/src/aggregates/post_aggregates.rs @@ -1,6 +1,5 @@ use crate::{ aggregates::structs::PostAggregates, - diesel::OptionalExtension, newtypes::PostId, schema::{community_aggregates, post, post_aggregates}, utils::{ @@ -13,13 +12,9 @@ use diesel::{result::Error, ExpressionMethods, JoinOnDsl, QueryDsl}; use diesel_async::RunQueryDsl; impl PostAggregates { - pub async fn read(pool: &mut DbPool<'_>, post_id: PostId) -> Result, Error> { + pub async fn read(pool: &mut DbPool<'_>, post_id: PostId) -> Result { let conn = &mut get_conn(pool).await?; - post_aggregates::table - .find(post_id) - .first(conn) - .await - .optional() + post_aggregates::table.find(post_id).first(conn).await } pub async fn update_ranks(pool: &mut DbPool<'_>, post_id: PostId) -> Result { @@ -131,10 +126,7 @@ mod tests { PostLike::like(pool, &post_like).await.unwrap(); - let post_aggs_before_delete = PostAggregates::read(pool, inserted_post.id) - .await - .unwrap() - .unwrap(); + let post_aggs_before_delete = PostAggregates::read(pool, inserted_post.id).await.unwrap(); assert_eq!(2, post_aggs_before_delete.comments); assert_eq!(1, post_aggs_before_delete.score); @@ -150,10 +142,7 @@ mod tests { PostLike::like(pool, &post_dislike).await.unwrap(); - let post_aggs_after_dislike = PostAggregates::read(pool, inserted_post.id) - .await - .unwrap() - .unwrap(); + let post_aggs_after_dislike = PostAggregates::read(pool, inserted_post.id).await.unwrap(); assert_eq!(2, post_aggs_after_dislike.comments); assert_eq!(0, post_aggs_after_dislike.score); @@ -165,10 +154,7 @@ mod tests { Comment::delete(pool, inserted_child_comment.id) .await .unwrap(); - let after_comment_delete = PostAggregates::read(pool, inserted_post.id) - .await - .unwrap() - .unwrap(); + let after_comment_delete = PostAggregates::read(pool, inserted_post.id).await.unwrap(); assert_eq!(0, after_comment_delete.comments); assert_eq!(0, after_comment_delete.score); assert_eq!(1, after_comment_delete.upvotes); @@ -178,10 +164,7 @@ mod tests { PostLike::remove(pool, inserted_person.id, inserted_post.id) .await .unwrap(); - let after_like_remove = PostAggregates::read(pool, inserted_post.id) - .await - .unwrap() - .unwrap(); + let after_like_remove = PostAggregates::read(pool, inserted_post.id).await.unwrap(); assert_eq!(0, after_like_remove.comments); assert_eq!(-1, after_like_remove.score); assert_eq!(0, after_like_remove.upvotes); @@ -201,8 +184,8 @@ mod tests { assert_eq!(1, community_num_deleted); // Should be none found, since the creator was deleted - let after_delete = PostAggregates::read(pool, inserted_post.id).await.unwrap(); - assert!(after_delete.is_none()); + let after_delete = PostAggregates::read(pool, inserted_post.id).await; + assert!(after_delete.is_err()); Instance::delete(pool, inserted_instance.id).await.unwrap(); } @@ -244,10 +227,7 @@ mod tests { let inserted_comment = Comment::create(pool, &comment_form, None).await.unwrap(); - let post_aggregates_before = PostAggregates::read(pool, inserted_post.id) - .await - .unwrap() - .unwrap(); + let post_aggregates_before = PostAggregates::read(pool, inserted_post.id).await.unwrap(); assert_eq!(1, post_aggregates_before.comments); Comment::update( @@ -261,10 +241,7 @@ mod tests { .await .unwrap(); - let post_aggregates_after_remove = PostAggregates::read(pool, inserted_post.id) - .await - .unwrap() - .unwrap(); + let post_aggregates_after_remove = PostAggregates::read(pool, inserted_post.id).await.unwrap(); assert_eq!(0, post_aggregates_after_remove.comments); Comment::update( @@ -289,10 +266,7 @@ mod tests { .await .unwrap(); - let post_aggregates_after_delete = PostAggregates::read(pool, inserted_post.id) - .await - .unwrap() - .unwrap(); + let post_aggregates_after_delete = PostAggregates::read(pool, inserted_post.id).await.unwrap(); assert_eq!(0, post_aggregates_after_delete.comments); Comment::update( @@ -306,10 +280,8 @@ mod tests { .await .unwrap(); - let post_aggregates_after_delete_remove = PostAggregates::read(pool, inserted_post.id) - .await - .unwrap() - .unwrap(); + let post_aggregates_after_delete_remove = + PostAggregates::read(pool, inserted_post.id).await.unwrap(); assert_eq!(0, post_aggregates_after_delete_remove.comments); Comment::delete(pool, inserted_comment.id).await.unwrap(); diff --git a/crates/db_schema/src/impls/activity.rs b/crates/db_schema/src/impls/activity.rs index 9391d55bc..6b057d254 100644 --- a/crates/db_schema/src/impls/activity.rs +++ b/crates/db_schema/src/impls/activity.rs @@ -22,22 +22,15 @@ impl SentActivity { .await } - pub async fn read_from_apub_id( - pool: &mut DbPool<'_>, - object_id: &DbUrl, - ) -> Result, Error> { + pub async fn read_from_apub_id(pool: &mut DbPool<'_>, object_id: &DbUrl) -> Result { use crate::schema::sent_activity::dsl::{ap_id, sent_activity}; let conn = &mut get_conn(pool).await?; - sent_activity - .filter(ap_id.eq(object_id)) - .first(conn) - .await - .optional() + sent_activity.filter(ap_id.eq(object_id)).first(conn).await } - pub async fn read(pool: &mut DbPool<'_>, object_id: ActivityId) -> Result, Error> { + pub async fn read(pool: &mut DbPool<'_>, object_id: ActivityId) -> Result { use crate::schema::sent_activity::dsl::sent_activity; let conn = &mut get_conn(pool).await?; - sent_activity.find(object_id).first(conn).await.optional() + sent_activity.find(object_id).first(conn).await } } @@ -119,10 +112,7 @@ mod tests { SentActivity::create(pool, form).await.unwrap(); - let res = SentActivity::read_from_apub_id(pool, &ap_id) - .await - .unwrap() - .unwrap(); + let res = SentActivity::read_from_apub_id(pool, &ap_id).await.unwrap(); assert_eq!(res.ap_id, ap_id); assert_eq!(res.data, data); assert_eq!(res.sensitive, sensitive); diff --git a/crates/db_schema/src/impls/comment.rs b/crates/db_schema/src/impls/comment.rs index 44151cfbb..0a4d7dd0e 100644 --- a/crates/db_schema/src/impls/comment.rs +++ b/crates/db_schema/src/impls/comment.rs @@ -333,10 +333,7 @@ mod tests { .await .unwrap(); - let read_comment = Comment::read(pool, inserted_comment.id) - .await - .unwrap() - .unwrap(); + let read_comment = Comment::read(pool, inserted_comment.id).await.unwrap(); let like_removed = CommentLike::remove(pool, inserted_person.id, inserted_comment.id) .await .unwrap(); diff --git a/crates/db_schema/src/impls/community.rs b/crates/db_schema/src/impls/community.rs index a79a23f47..a26fdc233 100644 --- a/crates/db_schema/src/impls/community.rs +++ b/crates/db_schema/src/impls/community.rs @@ -152,26 +152,24 @@ impl Community { pub async fn get_by_collection_url( pool: &mut DbPool<'_>, url: &DbUrl, - ) -> Result, Error> { + ) -> LemmyResult<(Community, CollectionType)> { let conn = &mut get_conn(pool).await?; let res = community::table .filter(community::moderators_url.eq(url)) .first(conn) - .await - .optional()?; + .await; - if let Some(c) = res { - Ok(Some((c, CollectionType::Moderators))) + if let Ok(c) = res { + Ok((c, CollectionType::Moderators)) } else { let res = community::table .filter(community::featured_url.eq(url)) .first(conn) - .await - .optional()?; - if let Some(c) = res { - Ok(Some((c, CollectionType::Featured))) + .await; + if let Ok(c) = res { + Ok((c, CollectionType::Featured)) } else { - Ok(None) + Err(LemmyErrorType::NotFound.into()) } } } @@ -455,7 +453,7 @@ mod tests { utils::build_db_pool_for_tests, CommunityVisibility, }; - use lemmy_utils::{error::LemmyResult, LemmyErrorType}; + use lemmy_utils::error::LemmyResult; use pretty_assertions::assert_eq; use serial_test::serial; @@ -594,9 +592,7 @@ mod tests { expires: None, }; - let read_community = Community::read(pool, inserted_community.id) - .await? - .ok_or(LemmyErrorType::CouldntFindCommunity)?; + let read_community = Community::read(pool, inserted_community.id).await?; let update_community_form = CommunityUpdateForm { title: Some("nada".to_owned()), diff --git a/crates/db_schema/src/impls/email_verification.rs b/crates/db_schema/src/impls/email_verification.rs index b4951cf73..39c7fe0bc 100644 --- a/crates/db_schema/src/impls/email_verification.rs +++ b/crates/db_schema/src/impls/email_verification.rs @@ -16,7 +16,6 @@ use diesel::{ sql_types::Timestamptz, ExpressionMethods, IntoSql, - OptionalExtension, QueryDsl, }; use diesel_async::RunQueryDsl; @@ -30,14 +29,13 @@ impl EmailVerification { .await } - pub async fn read_for_token(pool: &mut DbPool<'_>, token: &str) -> Result, Error> { + pub async fn read_for_token(pool: &mut DbPool<'_>, token: &str) -> Result { let conn = &mut get_conn(pool).await?; email_verification .filter(verification_token.eq(token)) .filter(published.gt(now.into_sql::() - 7.days())) .first(conn) .await - .optional() } pub async fn delete_old_tokens_for_local_user( pool: &mut DbPool<'_>, diff --git a/crates/db_schema/src/impls/moderator.rs b/crates/db_schema/src/impls/moderator.rs index fdee8537b..14964aa00 100644 --- a/crates/db_schema/src/impls/moderator.rs +++ b/crates/db_schema/src/impls/moderator.rs @@ -558,7 +558,6 @@ mod tests { .unwrap(); let read_mod_remove_post = ModRemovePost::read(pool, inserted_mod_remove_post.id) .await - .unwrap() .unwrap(); let expected_mod_remove_post = ModRemovePost { id: inserted_mod_remove_post.id, @@ -581,7 +580,6 @@ mod tests { .unwrap(); let read_mod_lock_post = ModLockPost::read(pool, inserted_mod_lock_post.id) .await - .unwrap() .unwrap(); let expected_mod_lock_post = ModLockPost { id: inserted_mod_lock_post.id, @@ -604,7 +602,6 @@ mod tests { .unwrap(); let read_mod_feature_post = ModFeaturePost::read(pool, inserted_mod_feature_post.id) .await - .unwrap() .unwrap(); let expected_mod_feature_post = ModFeaturePost { id: inserted_mod_feature_post.id, @@ -628,7 +625,6 @@ mod tests { .unwrap(); let read_mod_remove_comment = ModRemoveComment::read(pool, inserted_mod_remove_comment.id) .await - .unwrap() .unwrap(); let expected_mod_remove_comment = ModRemoveComment { id: inserted_mod_remove_comment.id, @@ -654,7 +650,6 @@ mod tests { let read_mod_remove_community = ModRemoveCommunity::read(pool, inserted_mod_remove_community.id) .await - .unwrap() .unwrap(); let expected_mod_remove_community = ModRemoveCommunity { id: inserted_mod_remove_community.id, @@ -682,7 +677,6 @@ mod tests { let read_mod_ban_from_community = ModBanFromCommunity::read(pool, inserted_mod_ban_from_community.id) .await - .unwrap() .unwrap(); let expected_mod_ban_from_community = ModBanFromCommunity { id: inserted_mod_ban_from_community.id, @@ -705,10 +699,7 @@ mod tests { expires: None, }; let inserted_mod_ban = ModBan::create(pool, &mod_ban_form).await.unwrap(); - let read_mod_ban = ModBan::read(pool, inserted_mod_ban.id) - .await - .unwrap() - .unwrap(); + let read_mod_ban = ModBan::read(pool, inserted_mod_ban.id).await.unwrap(); let expected_mod_ban = ModBan { id: inserted_mod_ban.id, mod_person_id: inserted_mod.id, @@ -732,7 +723,6 @@ mod tests { .unwrap(); let read_mod_add_community = ModAddCommunity::read(pool, inserted_mod_add_community.id) .await - .unwrap() .unwrap(); let expected_mod_add_community = ModAddCommunity { id: inserted_mod_add_community.id, @@ -751,10 +741,7 @@ mod tests { removed: None, }; let inserted_mod_add = ModAdd::create(pool, &mod_add_form).await.unwrap(); - let read_mod_add = ModAdd::read(pool, inserted_mod_add.id) - .await - .unwrap() - .unwrap(); + let read_mod_add = ModAdd::read(pool, inserted_mod_add.id).await.unwrap(); let expected_mod_add = ModAdd { id: inserted_mod_add.id, mod_person_id: inserted_mod.id, diff --git a/crates/db_schema/src/impls/password_reset_request.rs b/crates/db_schema/src/impls/password_reset_request.rs index be05ed8ac..8ce7640a5 100644 --- a/crates/db_schema/src/impls/password_reset_request.rs +++ b/crates/db_schema/src/impls/password_reset_request.rs @@ -1,5 +1,4 @@ use crate::{ - diesel::OptionalExtension, newtypes::LocalUserId, schema::password_reset_request::dsl::{password_reset_request, published, token}, source::password_reset_request::{PasswordResetRequest, PasswordResetRequestForm}, @@ -32,14 +31,13 @@ impl PasswordResetRequest { .await } - pub async fn read_and_delete(pool: &mut DbPool<'_>, token_: &str) -> Result, Error> { + pub async fn read_and_delete(pool: &mut DbPool<'_>, token_: &str) -> Result { let conn = &mut get_conn(pool).await?; delete(password_reset_request) .filter(token.eq(token_)) .filter(published.gt(now.into_sql::() - 1.days())) .get_result(conn) .await - .optional() } } @@ -81,9 +79,7 @@ mod tests { PasswordResetRequest::create(pool, inserted_local_user.id, token.to_string()).await?; // Read it and verify - let read_password_reset_request = PasswordResetRequest::read_and_delete(pool, token) - .await? - .unwrap(); + let read_password_reset_request = PasswordResetRequest::read_and_delete(pool, token).await?; assert_eq!( inserted_password_reset_request.id, read_password_reset_request.id @@ -102,8 +98,8 @@ mod tests { ); // Cannot reuse same token again - let read_password_reset_request = PasswordResetRequest::read_and_delete(pool, token).await?; - assert!(read_password_reset_request.is_none()); + let read_password_reset_request = PasswordResetRequest::read_and_delete(pool, token).await; + assert!(read_password_reset_request.is_err()); // Cleanup let num_deleted = Person::delete(pool, inserted_person.id).await?; diff --git a/crates/db_schema/src/impls/person.rs b/crates/db_schema/src/impls/person.rs index 312bbcf21..0a0a2b7a2 100644 --- a/crates/db_schema/src/impls/person.rs +++ b/crates/db_schema/src/impls/person.rs @@ -29,14 +29,13 @@ impl Crud for Person { type IdType = PersonId; // Override this, so that you don't get back deleted - async fn read(pool: &mut DbPool<'_>, person_id: PersonId) -> Result, Error> { + async fn read(pool: &mut DbPool<'_>, person_id: PersonId) -> Result { let conn = &mut get_conn(pool).await?; person::table .filter(person::deleted.eq(false)) .find(person_id) .first(conn) .await - .optional() } async fn create(pool: &mut DbPool<'_>, form: &PersonInsertForm) -> Result { @@ -244,7 +243,7 @@ mod tests { traits::{Crud, Followable}, utils::build_db_pool_for_tests, }; - use lemmy_utils::{error::LemmyResult, LemmyErrorType}; + use lemmy_utils::error::LemmyResult; use pretty_assertions::assert_eq; use serial_test::serial; @@ -284,9 +283,7 @@ mod tests { instance_id: inserted_instance.id, }; - let read_person = Person::read(pool, inserted_person.id) - .await? - .ok_or(LemmyErrorType::CouldntFindPerson)?; + let read_person = Person::read(pool, inserted_person.id).await?; let update_person_form = PersonUpdateForm { actor_id: Some(inserted_person.actor_id.clone()), diff --git a/crates/db_schema/src/impls/post.rs b/crates/db_schema/src/impls/post.rs index 0fd567c53..43ccbc0a7 100644 --- a/crates/db_schema/src/impls/post.rs +++ b/crates/db_schema/src/impls/post.rs @@ -68,6 +68,10 @@ impl Crud for Post { } impl Post { + pub async fn read_xx(pool: &mut DbPool<'_>, id: PostId) -> Result { + let conn = &mut *get_conn(pool).await?; + post::table.find(id).first(conn).await + } pub async fn insert_apub( pool: &mut DbPool<'_>, timestamp: DateTime, @@ -497,7 +501,7 @@ mod tests { .unwrap(); assert_eq!(2, marked_as_read); - let read_post = Post::read(pool, inserted_post.id).await.unwrap().unwrap(); + let read_post = Post::read(pool, inserted_post.id).await.unwrap(); let new_post_update = PostUpdateForm { name: Some("A test post".into()), diff --git a/crates/db_schema/src/impls/private_message.rs b/crates/db_schema/src/impls/private_message.rs index c11882b14..d0ca7378e 100644 --- a/crates/db_schema/src/impls/private_message.rs +++ b/crates/db_schema/src/impls/private_message.rs @@ -150,7 +150,6 @@ mod tests { let read_private_message = PrivateMessage::read(pool, inserted_private_message.id) .await - .unwrap() .unwrap(); let private_message_update_form = PrivateMessageUpdateForm { diff --git a/crates/db_schema/src/impls/registration_application.rs b/crates/db_schema/src/impls/registration_application.rs index 055ffb51f..d9777919d 100644 --- a/crates/db_schema/src/impls/registration_application.rs +++ b/crates/db_schema/src/impls/registration_application.rs @@ -1,5 +1,4 @@ use crate::{ - diesel::OptionalExtension, newtypes::{LocalUserId, RegistrationApplicationId}, schema::registration_application::dsl::{local_user_id, registration_application}, source::registration_application::{ @@ -44,12 +43,11 @@ impl RegistrationApplication { pub async fn find_by_local_user_id( pool: &mut DbPool<'_>, local_user_id_: LocalUserId, - ) -> Result, Error> { + ) -> Result { let conn = &mut get_conn(pool).await?; registration_application .filter(local_user_id.eq(local_user_id_)) .first(conn) .await - .optional() } } diff --git a/crates/db_schema/src/impls/site.rs b/crates/db_schema/src/impls/site.rs index f2b634354..8f57647a3 100644 --- a/crates/db_schema/src/impls/site.rs +++ b/crates/db_schema/src/impls/site.rs @@ -20,7 +20,7 @@ impl Crud for Site { type IdType = SiteId; /// Use SiteView::read_local, or Site::read_from_apub_id instead - async fn read(_pool: &mut DbPool<'_>, _site_id: SiteId) -> Result, Error> { + async fn read(_pool: &mut DbPool<'_>, _site_id: SiteId) -> Result { Err(Error::NotFound) } diff --git a/crates/db_schema/src/traits.rs b/crates/db_schema/src/traits.rs index 2b0da6c7f..74f5ea009 100644 --- a/crates/db_schema/src/traits.rs +++ b/crates/db_schema/src/traits.rs @@ -1,5 +1,4 @@ use crate::{ - diesel::OptionalExtension, newtypes::{CommunityId, DbUrl, PersonId}, utils::{get_conn, DbPool}, }; @@ -43,10 +42,10 @@ where async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result; - async fn read(pool: &mut DbPool<'_>, id: Self::IdType) -> Result, Error> { + async fn read(pool: &mut DbPool<'_>, id: Self::IdType) -> Result { let query: Find = Self::table().find(id); let conn = &mut *get_conn(pool).await?; - query.first(conn).await.optional() + query.first(conn).await } /// when you want to null out a column, you have to send Some(None)), since sending None means you diff --git a/crates/db_schema/src/utils.rs b/crates/db_schema/src/utils.rs index a61a230fa..fb9c542df 100644 --- a/crates/db_schema/src/utils.rs +++ b/crates/db_schema/src/utils.rs @@ -13,7 +13,6 @@ use diesel::{ }, sql_types::{self, Timestamptz}, IntoSql, - OptionalExtension, }; use diesel_async::{ pg::AsyncPgConnection, @@ -574,12 +573,12 @@ impl Queries { self, pool: &'a mut DbPool<'_>, args: Args, - ) -> Result, DieselError> + ) -> Result where RF: ReadFn<'a, T, Args>, { let conn = get_conn(pool).await?; - (self.read_fn)(conn, args).await.optional() + (self.read_fn)(conn, args).await } pub async fn list<'a, T, Args>( diff --git a/crates/db_views/src/comment_report_view.rs b/crates/db_views/src/comment_report_view.rs index c140196b0..42b5c3238 100644 --- a/crates/db_views/src/comment_report_view.rs +++ b/crates/db_views/src/comment_report_view.rs @@ -192,7 +192,7 @@ impl CommentReportView { pool: &mut DbPool<'_>, report_id: CommentReportId, my_person_id: PersonId, - ) -> Result, Error> { + ) -> Result { queries().read(pool, (report_id, my_person_id)).await } @@ -380,13 +380,11 @@ mod tests { let agg = CommentAggregates::read(pool, inserted_comment.id) .await - .unwrap() .unwrap(); let read_jessica_report_view = CommentReportView::read(pool, inserted_jessica_report.id, inserted_timmy.id) .await - .unwrap() .unwrap(); let expected_jessica_report_view = CommentReportView { comment_report: inserted_jessica_report.clone(), @@ -540,7 +538,6 @@ mod tests { let read_jessica_report_view_after_resolve = CommentReportView::read(pool, inserted_jessica_report.id, inserted_timmy.id) .await - .unwrap() .unwrap(); let mut expected_jessica_report_view_after_resolve = expected_jessica_report_view; diff --git a/crates/db_views/src/comment_view.rs b/crates/db_views/src/comment_view.rs index 5757ab298..4f6135479 100644 --- a/crates/db_views/src/comment_view.rs +++ b/crates/db_views/src/comment_view.rs @@ -364,21 +364,18 @@ impl CommentView { pool: &mut DbPool<'_>, comment_id: CommentId, my_local_user: Option<&'a LocalUser>, - ) -> Result, Error> { + ) -> Result { // If a person is given, then my_vote (res.9), if None, should be 0, not null // Necessary to differentiate between other person's votes - if let Ok(Some(res)) = queries().read(pool, (comment_id, my_local_user)).await { - let mut new_view = res.clone(); - if my_local_user.is_some() && res.my_vote.is_none() { - new_view.my_vote = Some(0); - } - if res.comment.deleted || res.comment.removed { - new_view.comment.content = String::new(); - } - Ok(Some(new_view)) - } else { - Ok(None) + let res = queries().read(pool, (comment_id, my_local_user)).await?; + let mut new_view = res.clone(); + if my_local_user.is_some() && res.my_vote.is_none() { + new_view.my_vote = Some(0); } + if res.comment.deleted || res.comment.removed { + new_view.comment.content = String::new(); + } + Ok(new_view) } } @@ -420,6 +417,7 @@ impl<'a> CommentQuery<'a> { #[cfg(test)] #[allow(clippy::indexing_slicing)] +#[allow(clippy::unwrap_used)] mod tests { use crate::{ @@ -464,7 +462,7 @@ mod tests { CommunityVisibility, SubscribedType, }; - use lemmy_utils::{error::LemmyResult, LemmyErrorType}; + use lemmy_utils::error::LemmyResult; use pretty_assertions::assert_eq; use serial_test::serial; @@ -563,7 +561,7 @@ mod tests { let polish_id = Language::read_id_from_code(pool, Some("pl")) .await? - .ok_or(LemmyErrorType::LanguageNotAllowed)?; + .unwrap(); let comment_form_4 = CommentInsertForm { language_id: Some(polish_id), ..CommentInsertForm::new( @@ -647,9 +645,7 @@ mod tests { assert_eq!( &expected_comment_view_no_person, - read_comment_views_no_person - .first() - .ok_or(LemmyErrorType::CouldntFindComment)? + read_comment_views_no_person.first().unwrap() ); let read_comment_views_with_person = CommentQuery { @@ -674,8 +670,7 @@ mod tests { data.inserted_comment_1.id, Some(&data.timmy_local_user_view.local_user), ) - .await? - .ok_or(LemmyErrorType::CouldntFindComment)?; + .await?; // Make sure block set the creator blocked assert!(read_comment_from_blocked_person.creator_blocked); @@ -828,7 +823,7 @@ mod tests { // change user lang to finnish, should only show one post in finnish and one undetermined let finnish_id = Language::read_id_from_code(pool, Some("fi")) .await? - .ok_or(LemmyErrorType::LanguageNotAllowed)?; + .unwrap(); LocalUserLanguage::update( pool, vec![finnish_id], @@ -848,10 +843,7 @@ mod tests { assert!(finnish_comment.is_some()); assert_eq!( data.inserted_comment_2.content, - finnish_comment - .ok_or(LemmyErrorType::CouldntFindComment)? - .comment - .content + finnish_comment.unwrap().comment.content ); // now show all comments with undetermined language (which is the default value) @@ -1014,9 +1006,7 @@ mod tests { } async fn expected_comment_view(data: &Data, pool: &mut DbPool<'_>) -> LemmyResult { - let agg = CommentAggregates::read(pool, data.inserted_comment_0.id) - .await? - .ok_or(LemmyErrorType::CouldntFindComment)?; + let agg = CommentAggregates::read(pool, data.inserted_comment_0.id).await?; Ok(CommentView { creator_banned_from_community: false, banned_from_community: false, @@ -1161,8 +1151,8 @@ mod tests { .await?; assert_eq!(5, authenticated_query.len()); - let unauthenticated_comment = CommentView::read(pool, data.inserted_comment_0.id, None).await?; - assert!(unauthenticated_comment.is_none()); + let unauthenticated_comment = CommentView::read(pool, data.inserted_comment_0.id, None).await; + assert!(unauthenticated_comment.is_err()); let authenticated_comment = CommentView::read( pool, @@ -1209,8 +1199,7 @@ mod tests { data.inserted_comment_0.id, Some(&inserted_banned_from_comm_local_user), ) - .await? - .ok_or(LemmyErrorType::CouldntFindComment)?; + .await?; assert!(comment_view.banned_from_community); @@ -1230,8 +1219,7 @@ mod tests { data.inserted_comment_0.id, Some(&data.timmy_local_user_view.local_user), ) - .await? - .ok_or(LemmyErrorType::CouldntFindComment)?; + .await?; assert!(!comment_view.banned_from_community); diff --git a/crates/db_views/src/local_user_view.rs b/crates/db_views/src/local_user_view.rs index b20dfe235..b8ae14d5e 100644 --- a/crates/db_views/src/local_user_view.rs +++ b/crates/db_views/src/local_user_view.rs @@ -96,37 +96,28 @@ fn queries<'a>( } impl LocalUserView { - pub async fn read( - pool: &mut DbPool<'_>, - local_user_id: LocalUserId, - ) -> Result, Error> { + pub async fn read(pool: &mut DbPool<'_>, local_user_id: LocalUserId) -> Result { queries().read(pool, ReadBy::Id(local_user_id)).await } - pub async fn read_person( - pool: &mut DbPool<'_>, - person_id: PersonId, - ) -> Result, Error> { + pub async fn read_person(pool: &mut DbPool<'_>, person_id: PersonId) -> Result { queries().read(pool, ReadBy::Person(person_id)).await } - pub async fn read_from_name(pool: &mut DbPool<'_>, name: &str) -> Result, Error> { + pub async fn read_from_name(pool: &mut DbPool<'_>, name: &str) -> Result { queries().read(pool, ReadBy::Name(name)).await } pub async fn find_by_email_or_name( pool: &mut DbPool<'_>, name_or_email: &str, - ) -> Result, Error> { + ) -> Result { queries() .read(pool, ReadBy::NameOrEmail(name_or_email)) .await } - pub async fn find_by_email( - pool: &mut DbPool<'_>, - from_email: &str, - ) -> Result, Error> { + pub async fn find_by_email(pool: &mut DbPool<'_>, from_email: &str) -> Result { queries().read(pool, ReadBy::Email(from_email)).await } @@ -134,7 +125,7 @@ impl LocalUserView { pool: &mut DbPool<'_>, oauth_provider_id: OAuthProviderId, oauth_user_id: &str, - ) -> Result, Error> { + ) -> Result { queries() .read(pool, ReadBy::OAuthID(oauth_provider_id, oauth_user_id)) .await diff --git a/crates/db_views/src/post_report_view.rs b/crates/db_views/src/post_report_view.rs index ed72db2c1..527ebdafa 100644 --- a/crates/db_views/src/post_report_view.rs +++ b/crates/db_views/src/post_report_view.rs @@ -220,7 +220,7 @@ impl PostReportView { pool: &mut DbPool<'_>, report_id: PostReportId, my_person_id: PersonId, - ) -> Result, Error> { + ) -> Result { queries().read(pool, (report_id, my_person_id)).await } @@ -404,7 +404,6 @@ mod tests { let read_jessica_report_view = PostReportView::read(pool, inserted_jessica_report.id, inserted_timmy.id) .await - .unwrap() .unwrap(); assert_eq!( @@ -442,7 +441,6 @@ mod tests { let read_jessica_report_view_after_resolve = PostReportView::read(pool, inserted_jessica_report.id, inserted_timmy.id) .await - .unwrap() .unwrap(); assert!(read_jessica_report_view_after_resolve.post_report.resolved); assert_eq!( diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index 425fc0f2f..74202abda 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -568,7 +568,7 @@ impl PostView { post_id: PostId, my_local_user: Option<&'a LocalUser>, is_mod_or_admin: bool, - ) -> Result, Error> { + ) -> Result { queries() .read(pool, (post_id, my_local_user, is_mod_or_admin)) .await @@ -593,8 +593,7 @@ impl PaginationCursor { .ok_or_else(err_msg)?, ), ) - .await? - .ok_or_else(err_msg)?; + .await?; Ok(PaginationCursorData(token)) } @@ -735,6 +734,7 @@ impl<'a> PostQuery<'a> { } #[cfg(test)] +#[allow(clippy::unwrap_used)] mod tests { use crate::{ post_view::{PaginationCursorData, PostQuery, PostView}, @@ -774,7 +774,7 @@ mod tests { PostSortType, SubscribedType, }; - use lemmy_utils::error::{LemmyErrorType, LemmyResult}; + use lemmy_utils::error::LemmyResult; use pretty_assertions::assert_eq; use serial_test::serial; use std::{collections::HashSet, time::Duration}; @@ -952,8 +952,7 @@ mod tests { Some(&data.local_user_view.local_user), false, ) - .await? - .ok_or(LemmyErrorType::CouldntFindPost)?; + .await?; let expected_post_listing_with_user = expected_post_view(&data, pool).await?; @@ -1002,9 +1001,7 @@ mod tests { .await?; let read_post_listing_single_no_person = - PostView::read(pool, data.inserted_post.id, None, false) - .await? - .ok_or(LemmyErrorType::CouldntFindPost)?; + PostView::read(pool, data.inserted_post.id, None, false).await?; let expected_post_listing_no_person = expected_post_view(&data, pool).await?; @@ -1140,8 +1137,7 @@ mod tests { Some(&data.local_user_view.local_user), false, ) - .await? - .ok_or(LemmyErrorType::CouldntFindPost)?; + .await?; let mut expected_post_with_upvote = expected_post_view(&data, pool).await?; expected_post_with_upvote.my_vote = Some(1); @@ -1649,12 +1645,7 @@ mod tests { assert_eq!(vec![POST_BY_BOT, POST], names(&post_listings_show_hidden)); // Make sure that hidden field is true. - assert!( - &post_listings_show_hidden - .first() - .ok_or(LemmyErrorType::CouldntFindPost)? - .hidden - ); + assert!(&post_listings_show_hidden.first().unwrap().hidden); cleanup(data, pool).await } @@ -1690,13 +1681,7 @@ mod tests { assert_eq!(vec![POST_BY_BOT, POST], names(&post_listings_show_nsfw)); // Make sure that nsfw field is true. - assert!( - &post_listings_show_nsfw - .first() - .ok_or(LemmyErrorType::CouldntFindPost)? - .post - .nsfw - ); + assert!(&post_listings_show_nsfw.first().unwrap().post.nsfw); cleanup(data, pool).await } @@ -1719,9 +1704,7 @@ mod tests { &data.inserted_community, &data.inserted_post, ); - let agg = PostAggregates::read(pool, inserted_post.id) - .await? - .ok_or(LemmyErrorType::CouldntFindPost)?; + let agg = PostAggregates::read(pool, inserted_post.id).await?; Ok(PostView { post: Post { @@ -1865,8 +1848,8 @@ mod tests { .await?; assert_eq!(2, authenticated_query.len()); - let unauthenticated_post = PostView::read(pool, data.inserted_post.id, None, false).await?; - assert!(unauthenticated_post.is_none()); + let unauthenticated_post = PostView::read(pool, data.inserted_post.id, None, false).await; + assert!(unauthenticated_post.is_err()); let authenticated_post = PostView::read( pool, @@ -1916,8 +1899,7 @@ mod tests { Some(&inserted_banned_from_comm_local_user), false, ) - .await? - .ok_or(LemmyErrorType::CouldntFindPost)?; + .await?; assert!(post_view.banned_from_community); @@ -1938,8 +1920,7 @@ mod tests { Some(&data.local_user_view.local_user), false, ) - .await? - .ok_or(LemmyErrorType::CouldntFindPost)?; + .await?; assert!(!post_view.banned_from_community); diff --git a/crates/db_views/src/private_message_report_view.rs b/crates/db_views/src/private_message_report_view.rs index 5a8c8a8fd..49b8e415b 100644 --- a/crates/db_views/src/private_message_report_view.rs +++ b/crates/db_views/src/private_message_report_view.rs @@ -78,7 +78,7 @@ impl PrivateMessageReportView { pub async fn read( pool: &mut DbPool<'_>, report_id: PrivateMessageReportId, - ) -> Result, Error> { + ) -> Result { queries().read(pool, report_id).await } diff --git a/crates/db_views/src/private_message_view.rs b/crates/db_views/src/private_message_view.rs index 9d5f8aba1..cd475897f 100644 --- a/crates/db_views/src/private_message_view.rs +++ b/crates/db_views/src/private_message_view.rs @@ -113,7 +113,7 @@ impl PrivateMessageView { pub async fn read( pool: &mut DbPool<'_>, private_message_id: PrivateMessageId, - ) -> Result, Error> { + ) -> Result { queries().read(pool, private_message_id).await } diff --git a/crates/db_views/src/registration_application_view.rs b/crates/db_views/src/registration_application_view.rs index 51e2ff1a6..ba44e927e 100644 --- a/crates/db_views/src/registration_application_view.rs +++ b/crates/db_views/src/registration_application_view.rs @@ -81,17 +81,11 @@ fn queries<'a>() -> Queries< } impl RegistrationApplicationView { - pub async fn read( - pool: &mut DbPool<'_>, - id: RegistrationApplicationId, - ) -> Result, Error> { + pub async fn read(pool: &mut DbPool<'_>, id: RegistrationApplicationId) -> Result { queries().read(pool, ReadBy::Id(id)).await } - pub async fn read_by_person( - pool: &mut DbPool<'_>, - person_id: PersonId, - ) -> Result, Error> { + pub async fn read_by_person(pool: &mut DbPool<'_>, person_id: PersonId) -> Result { queries().read(pool, ReadBy::Person(person_id)).await } /// Returns the current unread registration_application count @@ -208,7 +202,6 @@ mod tests { let read_sara_app_view = RegistrationApplicationView::read(pool, sara_app.id) .await - .unwrap() .unwrap(); let jess_person_form = PersonInsertForm::test_form(inserted_instance.id, "jess_rav"); @@ -233,7 +226,6 @@ mod tests { let read_jess_app_view = RegistrationApplicationView::read(pool, jess_app.id) .await - .unwrap() .unwrap(); let mut expected_sara_app_view = RegistrationApplicationView { @@ -337,7 +329,6 @@ mod tests { let read_sara_app_view_after_approve = RegistrationApplicationView::read(pool, sara_app.id) .await - .unwrap() .unwrap(); // Make sure the columns changed diff --git a/crates/db_views_actor/src/comment_reply_view.rs b/crates/db_views_actor/src/comment_reply_view.rs index 3c5fea4a7..6f8810986 100644 --- a/crates/db_views_actor/src/comment_reply_view.rs +++ b/crates/db_views_actor/src/comment_reply_view.rs @@ -242,7 +242,7 @@ impl CommentReplyView { pool: &mut DbPool<'_>, comment_reply_id: CommentReplyId, my_person_id: Option, - ) -> Result, Error> { + ) -> Result { queries().read(pool, (comment_reply_id, my_person_id)).await } @@ -322,7 +322,7 @@ mod tests { utils::build_db_pool_for_tests, }; use lemmy_db_views::structs::LocalUserView; - use lemmy_utils::{error::LemmyResult, LemmyErrorType}; + use lemmy_utils::error::LemmyResult; use pretty_assertions::assert_eq; use serial_test::serial; @@ -383,9 +383,7 @@ mod tests { published: inserted_reply.published, }; - let read_reply = CommentReply::read(pool, inserted_reply.id) - .await? - .ok_or(LemmyErrorType::CouldntFindComment)?; + let read_reply = CommentReply::read(pool, inserted_reply.id).await?; let comment_reply_update_form = CommentReplyUpdateForm { read: Some(false) }; let updated_reply = @@ -440,9 +438,7 @@ mod tests { &recipient_local_user_update_form, ) .await?; - let recipient_local_user_view = LocalUserView::read(pool, recipient_local_user.id) - .await? - .ok_or(LemmyErrorType::CouldntFindLocalUser)?; + let recipient_local_user_view = LocalUserView::read(pool, recipient_local_user.id).await?; let unread_replies_after_hide_bots = CommentReplyView::get_unread_replies(pool, &recipient_local_user_view.local_user).await?; diff --git a/crates/db_views_actor/src/community_view.rs b/crates/db_views_actor/src/community_view.rs index d55a82c22..6282bef31 100644 --- a/crates/db_views_actor/src/community_view.rs +++ b/crates/db_views_actor/src/community_view.rs @@ -179,7 +179,7 @@ impl CommunityView { community_id: CommunityId, my_local_user: Option<&'a LocalUser>, is_mod_or_admin: bool, - ) -> Result, Error> { + ) -> Result { queries() .read(pool, (community_id, my_local_user, is_mod_or_admin)) .await @@ -194,7 +194,7 @@ impl CommunityView { CommunityModeratorView::is_community_moderator(pool, community_id, person_id).await?; if is_mod { Ok(true) - } else if let Ok(Some(person_view)) = PersonView::read(pool, person_id).await { + } else if let Ok(person_view) = PersonView::read(pool, person_id).await { Ok(person_view.is_admin) } else { Ok(false) @@ -210,7 +210,7 @@ impl CommunityView { CommunityModeratorView::is_community_moderator_of_any(pool, person_id).await?; if is_mod_of_any { Ok(true) - } else if let Ok(Some(person_view)) = PersonView::read(pool, person_id).await { + } else if let Ok(person_view) = PersonView::read(pool, person_id).await { Ok(person_view.is_admin) } else { Ok(false) @@ -363,10 +363,8 @@ mod tests { assert_eq!(1, authenticated_query.len()); let unauthenticated_community = - CommunityView::read(pool, data.inserted_community.id, None, false) - .await - .unwrap(); - assert!(unauthenticated_community.is_none()); + CommunityView::read(pool, data.inserted_community.id, None, false).await; + assert!(unauthenticated_community.is_err()); let authenticated_community = CommunityView::read( pool, diff --git a/crates/db_views_actor/src/person_mention_view.rs b/crates/db_views_actor/src/person_mention_view.rs index b31610ce4..102f95c87 100644 --- a/crates/db_views_actor/src/person_mention_view.rs +++ b/crates/db_views_actor/src/person_mention_view.rs @@ -241,7 +241,7 @@ impl PersonMentionView { pool: &mut DbPool<'_>, person_mention_id: PersonMentionId, my_person_id: Option, - ) -> Result, Error> { + ) -> Result { queries() .read(pool, (person_mention_id, my_person_id)) .await @@ -322,7 +322,7 @@ mod tests { utils::build_db_pool_for_tests, }; use lemmy_db_views::structs::LocalUserView; - use lemmy_utils::{error::LemmyResult, LemmyErrorType}; + use lemmy_utils::error::LemmyResult; use pretty_assertions::assert_eq; use serial_test::serial; @@ -384,9 +384,7 @@ mod tests { published: inserted_mention.published, }; - let read_mention = PersonMention::read(pool, inserted_mention.id) - .await? - .ok_or(LemmyErrorType::CouldntFindComment)?; + let read_mention = PersonMention::read(pool, inserted_mention.id).await?; let person_mention_update_form = PersonMentionUpdateForm { read: Some(false) }; let updated_mention = @@ -442,9 +440,7 @@ mod tests { &recipient_local_user_update_form, ) .await?; - let recipient_local_user_view = LocalUserView::read(pool, recipient_local_user.id) - .await? - .ok_or(LemmyErrorType::CouldntFindLocalUser)?; + let recipient_local_user_view = LocalUserView::read(pool, recipient_local_user.id).await?; let unread_mentions_after_hide_bots = PersonMentionView::get_unread_mentions(pool, &recipient_local_user_view.local_user).await?; diff --git a/crates/db_views_actor/src/person_view.rs b/crates/db_views_actor/src/person_view.rs index fe9877d01..ff4a595ff 100644 --- a/crates/db_views_actor/src/person_view.rs +++ b/crates/db_views_actor/src/person_view.rs @@ -135,7 +135,7 @@ fn queries<'a>( } impl PersonView { - pub async fn read(pool: &mut DbPool<'_>, person_id: PersonId) -> Result, Error> { + pub async fn read(pool: &mut DbPool<'_>, person_id: PersonId) -> Result { queries().read(pool, person_id).await } @@ -178,7 +178,7 @@ mod tests { traits::Crud, utils::build_db_pool_for_tests, }; - use lemmy_utils::{error::LemmyResult, LemmyErrorType}; + use lemmy_utils::error::LemmyResult; use pretty_assertions::assert_eq; use serial_test::serial; @@ -243,8 +243,8 @@ mod tests { ) .await?; - let read = PersonView::read(pool, data.alice.id).await?; - assert!(read.is_none()); + let read = PersonView::read(pool, data.alice.id).await; + assert!(read.is_err()); let list = PersonQuery { sort: Some(PostSortType::New), @@ -303,16 +303,10 @@ mod tests { assert_length!(1, list); assert_eq!(list[0].person.id, data.alice.id); - let is_admin = PersonView::read(pool, data.alice.id) - .await? - .ok_or(LemmyErrorType::CouldntFindPerson)? - .is_admin; + let is_admin = PersonView::read(pool, data.alice.id).await?.is_admin; assert!(is_admin); - let is_admin = PersonView::read(pool, data.bob.id) - .await? - .ok_or(LemmyErrorType::CouldntFindPerson)? - .is_admin; + let is_admin = PersonView::read(pool, data.bob.id).await?.is_admin; assert!(!is_admin); cleanup(data, pool).await diff --git a/crates/federate/src/util.rs b/crates/federate/src/util.rs index e10a01c30..9473aafa3 100644 --- a/crates/federate/src/util.rs +++ b/crates/federate/src/util.rs @@ -183,8 +183,8 @@ pub(crate) async fn get_activity_cached( .try_get_with(activity_id, async { let row = SentActivity::read(pool, activity_id) .await - .context("could not read activity")?; - let Some(mut row) = row else { + .context("could not read activity"); + let Ok(mut row) = row else { return anyhow::Result::<_, anyhow::Error>::Ok(None); }; // swap to avoid cloning diff --git a/crates/routes/src/feeds.rs b/crates/routes/src/feeds.rs index 2db3f9661..00518032d 100644 --- a/crates/routes/src/feeds.rs +++ b/crates/routes/src/feeds.rs @@ -260,7 +260,7 @@ async fn get_feed_user( let site_view = SiteView::read_local(&mut context.pool()).await?; let person = Person::read_from_name(&mut context.pool(), user_name, false) .await? - .ok_or(LemmyErrorType::CouldntFindPerson)?; + .ok_or(LemmyErrorType::NotFound)?; check_private_instance(&None, &site_view.local_site)?; @@ -298,9 +298,9 @@ async fn get_feed_community( let site_view = SiteView::read_local(&mut context.pool()).await?; let community = Community::read_from_name(&mut context.pool(), community_name, false) .await? - .ok_or(LemmyErrorType::CouldntFindCommunity)?; + .ok_or(LemmyErrorType::NotFound)?; if community.visibility != CommunityVisibility::Public { - return Err(LemmyErrorType::CouldntFindCommunity.into()); + return Err(LemmyErrorType::NotFound.into()); } check_private_instance(&None, &site_view.local_site)?; diff --git a/crates/routes/src/lib.rs b/crates/routes/src/lib.rs index 4f8d60246..a88225622 100644 --- a/crates/routes/src/lib.rs +++ b/crates/routes/src/lib.rs @@ -1,6 +1,6 @@ use lemmy_api_common::{claims::Claims, context::LemmyContext, utils::check_user_valid}; use lemmy_db_views::structs::LocalUserView; -use lemmy_utils::{error::LemmyResult, LemmyErrorType}; +use lemmy_utils::error::LemmyResult; pub mod feeds; pub mod images; @@ -10,9 +10,7 @@ pub mod webfinger; #[tracing::instrument(skip_all)] async fn local_user_view_from_jwt(jwt: &str, context: &LemmyContext) -> LemmyResult { let local_user_id = Claims::validate(jwt, context).await?; - 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) diff --git a/crates/utils/src/error.rs b/crates/utils/src/error.rs index 1935e4132..6d5c40b04 100644 --- a/crates/utils/src/error.rs +++ b/crates/utils/src/error.rs @@ -43,19 +43,6 @@ pub enum LemmyErrorType { SiteBan, Deleted, BannedFromCommunity, - CouldntFindCommunity, - CouldntFindPerson, - CouldntFindComment, - CouldntFindCommentReport, - CouldntFindPostReport, - CouldntFindPrivateMessageReport, - CouldntFindLocalUser, - CouldntFindPersonMention, - CouldntFindRegistrationApplication, - CouldntFindCommentReply, - CouldntFindPrivateMessage, - CouldntFindActivity, - CouldntFindOauthProvider, PersonIsBlocked, CommunityIsBlocked, InstanceIsBlocked, @@ -77,7 +64,6 @@ pub enum LemmyErrorType { OnlyModsCanPostInCommunity, CouldntUpdatePost, NoPostEditAllowed, - CouldntFindPost, EditPrivateMessageNotAllowed, SiteAlreadyExists, ApplicationQuestionRequired, @@ -133,7 +119,6 @@ pub enum LemmyErrorType { CouldntUpdateCommunityHiddenStatus, PersonBlockAlreadyExists, UserAlreadyExists, - TokenNotFound, CouldntLikePost, CouldntSavePost, CouldntMarkPostAsRead, @@ -155,7 +140,6 @@ pub enum LemmyErrorType { InvalidUrl, EmailSendFailed, Slurs, - CouldntFindObject, RegistrationDenied(Option), FederationDisabled, DomainBlocked(String), @@ -188,6 +172,7 @@ pub enum LemmyErrorType { Unknown(String), CantDeleteSite, UrlLengthOverflow, + NotFound, } cfg_if! { @@ -211,8 +196,12 @@ cfg_if! { { fn from(t: T) -> Self { let cause = t.into(); + let error_type = match cause.downcast_ref::() { + Some(&diesel::NotFound) => LemmyErrorType::NotFound, + _ => LemmyErrorType::Unknown(format!("{}", &cause)) + }; LemmyError { - error_type: LemmyErrorType::Unknown(format!("{}", &cause)), + error_type, inner: cause, context: Backtrace::capture(), } @@ -323,6 +312,17 @@ cfg_if! { ) } + #[test] + fn test_convert_diesel_errors() { + let not_found_error = LemmyError::from(diesel::NotFound); + assert_eq!(LemmyErrorType::NotFound, not_found_error.error_type); + assert_eq!(404, not_found_error.status_code()); + + let other_error = LemmyError::from(diesel::result::Error::NotInTransaction); + assert!(matches!(other_error.error_type, LemmyErrorType::Unknown{..})); + assert_eq!(400, other_error.status_code()); + } + /// Check if errors match translations. Disabled because many are not translated at all. #[test] #[ignore] diff --git a/src/scheduled_tasks.rs b/src/scheduled_tasks.rs index dd189ddd1..dc93aecb4 100644 --- a/src/scheduled_tasks.rs +++ b/src/scheduled_tasks.rs @@ -563,11 +563,8 @@ mod tests { let client = ClientBuilder::new(client_builder(&Settings::default()).build()?).build(); let form = build_update_instance_form("lemmy.ml", &client) .await - .ok_or(LemmyErrorType::CouldntFindObject)?; - assert_eq!( - form.software.ok_or(LemmyErrorType::CouldntFindObject)?, - "lemmy" - ); + .ok_or(LemmyErrorType::NotFound)?; + assert_eq!(form.software.ok_or(LemmyErrorType::NotFound)?, "lemmy"); Ok(()) } @@ -577,11 +574,8 @@ mod tests { let client = ClientBuilder::new(client_builder(&Settings::default()).build()?).build(); let form = build_update_instance_form("mastodon.social", &client) .await - .ok_or(LemmyErrorType::CouldntFindObject)?; - assert_eq!( - form.software.ok_or(LemmyErrorType::CouldntFindObject)?, - "mastodon" - ); + .ok_or(LemmyErrorType::NotFound)?; + assert_eq!(form.software.ok_or(LemmyErrorType::NotFound)?, "mastodon"); Ok(()) } }