From c521a92914f7534521e95709ffa0e1b74eceed71 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 20 Sep 2024 16:00:15 +0200 Subject: [PATCH] wip --- crates/api_common/src/build_response.rs | 3 +- crates/apub/src/http/mod.rs | 4 +- crates/db_schema/src/impls/activity.rs | 20 +++------- crates/db_schema/src/utils.rs | 5 +-- crates/db_views/src/comment_report_view.rs | 4 +- crates/db_views/src/comment_view.rs | 2 +- crates/db_views/src/local_user_view.rs | 21 +++------- crates/db_views/src/post_report_view.rs | 4 +- crates/db_views/src/post_view.rs | 39 ++++++------------- .../src/private_message_report_view.rs | 2 +- crates/db_views/src/private_message_view.rs | 2 +- .../src/registration_application_view.rs | 13 +------ .../db_views_actor/src/comment_reply_view.rs | 6 +-- crates/db_views_actor/src/community_view.rs | 12 +++--- .../db_views_actor/src/person_mention_view.rs | 2 +- crates/db_views_actor/src/person_view.rs | 2 +- crates/utils/src/error.rs | 4 +- 17 files changed, 44 insertions(+), 101 deletions(-) diff --git a/crates/api_common/src/build_response.rs b/crates/api_common/src/build_response.rs index 8f140f2fe..90aa08942 100644 --- a/crates/api_common/src/build_response.rs +++ b/crates/api_common/src/build_response.rs @@ -87,8 +87,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 })) } diff --git a/crates/apub/src/http/mod.rs b/crates/apub/src/http/mod.rs index a01afe676..dc3314a10 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 { 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/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 1866aaee9..1f8450561 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 } @@ -388,7 +388,6 @@ mod tests { 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(), @@ -542,7 +541,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 e2752a0c7..179ccd1c6 100644 --- a/crates/db_views/src/comment_view.rs +++ b/crates/db_views/src/comment_view.rs @@ -367,7 +367,7 @@ impl CommentView { ) -> Result, Error> { // 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 { + if let Ok(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); 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 0cd06dd4e..f0e07d0ff 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 } @@ -407,7 +407,6 @@ mod tests { let read_jessica_report_view = PostReportView::read(pool, inserted_jessica_report.id, inserted_timmy.id) .await - .unwrap() .unwrap(); assert_eq!( @@ -445,7 +444,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 3eab04af5..d5659ed95 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 @@ -774,7 +774,7 @@ mod tests { PostSortType, SubscribedType, }; - use lemmy_utils::error::{LemmyErrorType, LemmyResult}; + use lemmy_utils::{error::LemmyResult, LemmyErrorType}; use pretty_assertions::assert_eq; use serial_test::serial; use std::{collections::HashSet, time::Duration}; @@ -955,8 +955,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?; @@ -1005,9 +1004,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?; @@ -1141,8 +1138,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); @@ -1646,12 +1642,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 } @@ -1687,13 +1678,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 } @@ -1862,8 +1847,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, @@ -1913,8 +1898,7 @@ mod tests { Some(&inserted_banned_from_comm_local_user), false, ) - .await? - .ok_or(LemmyErrorType::CouldntFindPost)?; + .await?; assert!(post_view.banned_from_community); @@ -1935,8 +1919,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 f5e70fb3e..db3861b59 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 79224d86f..ef62d61ff 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 b1d95e719..f2c67a44d 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 } @@ -446,9 +446,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 4e09c4c43..2bec07393 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) @@ -364,10 +364,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 d6fd7363d..66b075a37 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 diff --git a/crates/db_views_actor/src/person_view.rs b/crates/db_views_actor/src/person_view.rs index fe9877d01..332fd6b21 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 } diff --git a/crates/utils/src/error.rs b/crates/utils/src/error.rs index 528342709..3c3e89a76 100644 --- a/crates/utils/src/error.rs +++ b/crates/utils/src/error.rs @@ -77,7 +77,6 @@ pub enum LemmyErrorType { OnlyModsCanPostInCommunity, CouldntUpdatePost, NoPostEditAllowed, - CouldntFindPost, EditPrivateMessageNotAllowed, SiteAlreadyExists, ApplicationQuestionRequired, @@ -188,6 +187,7 @@ pub enum LemmyErrorType { Unknown(String), CantDeleteSite, UrlLengthOverflow, + NotFound, } cfg_if! { @@ -212,7 +212,7 @@ cfg_if! { fn from(t: T) -> Self { let cause = t.into(); let error_type = match cause.downcast_ref::() { - Some(&diesel::NotFound) => LemmyErrorType::CouldntFindPost, + Some(&diesel::NotFound) => LemmyErrorType::NotFound, _ => LemmyErrorType::Unknown(format!("{}", &cause)) }; LemmyError {