This commit is contained in:
Felix Ableitner 2024-09-20 16:00:15 +02:00
parent 1cc75c4f79
commit c521a92914
17 changed files with 44 additions and 101 deletions

View file

@ -87,8 +87,7 @@ pub async fn build_post_response(
Some(&local_user), Some(&local_user),
is_mod_or_admin, is_mod_or_admin,
) )
.await? .await?;
.ok_or(LemmyErrorType::CouldntFindPost)?;
Ok(Json(PostResponse { post_view })) Ok(Json(PostResponse { post_view }))
} }

View file

@ -106,9 +106,7 @@ pub(crate) async fn get_activity(
info.id info.id
))? ))?
.into(); .into();
let activity = SentActivity::read_from_apub_id(&mut context.pool(), &activity_id) let activity = SentActivity::read_from_apub_id(&mut context.pool(), &activity_id).await?;
.await?
.ok_or(LemmyErrorType::CouldntFindActivity)?;
let sensitive = activity.sensitive; let sensitive = activity.sensitive;
if sensitive { if sensitive {

View file

@ -22,22 +22,15 @@ impl SentActivity {
.await .await
} }
pub async fn read_from_apub_id( pub async fn read_from_apub_id(pool: &mut DbPool<'_>, object_id: &DbUrl) -> Result<Self, Error> {
pool: &mut DbPool<'_>,
object_id: &DbUrl,
) -> Result<Option<Self>, Error> {
use crate::schema::sent_activity::dsl::{ap_id, sent_activity}; use crate::schema::sent_activity::dsl::{ap_id, sent_activity};
let conn = &mut get_conn(pool).await?; let conn = &mut get_conn(pool).await?;
sent_activity sent_activity.filter(ap_id.eq(object_id)).first(conn).await
.filter(ap_id.eq(object_id))
.first(conn)
.await
.optional()
} }
pub async fn read(pool: &mut DbPool<'_>, object_id: ActivityId) -> Result<Option<Self>, Error> { pub async fn read(pool: &mut DbPool<'_>, object_id: ActivityId) -> Result<Self, Error> {
use crate::schema::sent_activity::dsl::sent_activity; use crate::schema::sent_activity::dsl::sent_activity;
let conn = &mut get_conn(pool).await?; 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(); SentActivity::create(pool, form).await.unwrap();
let res = SentActivity::read_from_apub_id(pool, &ap_id) let res = SentActivity::read_from_apub_id(pool, &ap_id).await.unwrap();
.await
.unwrap()
.unwrap();
assert_eq!(res.ap_id, ap_id); assert_eq!(res.ap_id, ap_id);
assert_eq!(res.data, data); assert_eq!(res.data, data);
assert_eq!(res.sensitive, sensitive); assert_eq!(res.sensitive, sensitive);

View file

@ -13,7 +13,6 @@ use diesel::{
}, },
sql_types::{self, Timestamptz}, sql_types::{self, Timestamptz},
IntoSql, IntoSql,
OptionalExtension,
}; };
use diesel_async::{ use diesel_async::{
pg::AsyncPgConnection, pg::AsyncPgConnection,
@ -574,12 +573,12 @@ impl<RF, LF> Queries<RF, LF> {
self, self,
pool: &'a mut DbPool<'_>, pool: &'a mut DbPool<'_>,
args: Args, args: Args,
) -> Result<Option<T>, DieselError> ) -> Result<T, DieselError>
where where
RF: ReadFn<'a, T, Args>, RF: ReadFn<'a, T, Args>,
{ {
let conn = get_conn(pool).await?; 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>( pub async fn list<'a, T, Args>(

View file

@ -192,7 +192,7 @@ impl CommentReportView {
pool: &mut DbPool<'_>, pool: &mut DbPool<'_>,
report_id: CommentReportId, report_id: CommentReportId,
my_person_id: PersonId, my_person_id: PersonId,
) -> Result<Option<Self>, Error> { ) -> Result<Self, Error> {
queries().read(pool, (report_id, my_person_id)).await queries().read(pool, (report_id, my_person_id)).await
} }
@ -388,7 +388,6 @@ mod tests {
let read_jessica_report_view = let read_jessica_report_view =
CommentReportView::read(pool, inserted_jessica_report.id, inserted_timmy.id) CommentReportView::read(pool, inserted_jessica_report.id, inserted_timmy.id)
.await .await
.unwrap()
.unwrap(); .unwrap();
let expected_jessica_report_view = CommentReportView { let expected_jessica_report_view = CommentReportView {
comment_report: inserted_jessica_report.clone(), comment_report: inserted_jessica_report.clone(),
@ -542,7 +541,6 @@ mod tests {
let read_jessica_report_view_after_resolve = let read_jessica_report_view_after_resolve =
CommentReportView::read(pool, inserted_jessica_report.id, inserted_timmy.id) CommentReportView::read(pool, inserted_jessica_report.id, inserted_timmy.id)
.await .await
.unwrap()
.unwrap(); .unwrap();
let mut expected_jessica_report_view_after_resolve = expected_jessica_report_view; let mut expected_jessica_report_view_after_resolve = expected_jessica_report_view;

View file

@ -367,7 +367,7 @@ impl CommentView {
) -> Result<Option<Self>, Error> { ) -> Result<Option<Self>, Error> {
// If a person is given, then my_vote (res.9), if None, should be 0, not null // 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 // 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(); let mut new_view = res.clone();
if my_local_user.is_some() && res.my_vote.is_none() { if my_local_user.is_some() && res.my_vote.is_none() {
new_view.my_vote = Some(0); new_view.my_vote = Some(0);

View file

@ -96,37 +96,28 @@ fn queries<'a>(
} }
impl LocalUserView { impl LocalUserView {
pub async fn read( pub async fn read(pool: &mut DbPool<'_>, local_user_id: LocalUserId) -> Result<Self, Error> {
pool: &mut DbPool<'_>,
local_user_id: LocalUserId,
) -> Result<Option<Self>, Error> {
queries().read(pool, ReadBy::Id(local_user_id)).await queries().read(pool, ReadBy::Id(local_user_id)).await
} }
pub async fn read_person( pub async fn read_person(pool: &mut DbPool<'_>, person_id: PersonId) -> Result<Self, Error> {
pool: &mut DbPool<'_>,
person_id: PersonId,
) -> Result<Option<Self>, Error> {
queries().read(pool, ReadBy::Person(person_id)).await queries().read(pool, ReadBy::Person(person_id)).await
} }
pub async fn read_from_name(pool: &mut DbPool<'_>, name: &str) -> Result<Option<Self>, Error> { pub async fn read_from_name(pool: &mut DbPool<'_>, name: &str) -> Result<Self, Error> {
queries().read(pool, ReadBy::Name(name)).await queries().read(pool, ReadBy::Name(name)).await
} }
pub async fn find_by_email_or_name( pub async fn find_by_email_or_name(
pool: &mut DbPool<'_>, pool: &mut DbPool<'_>,
name_or_email: &str, name_or_email: &str,
) -> Result<Option<Self>, Error> { ) -> Result<Self, Error> {
queries() queries()
.read(pool, ReadBy::NameOrEmail(name_or_email)) .read(pool, ReadBy::NameOrEmail(name_or_email))
.await .await
} }
pub async fn find_by_email( pub async fn find_by_email(pool: &mut DbPool<'_>, from_email: &str) -> Result<Self, Error> {
pool: &mut DbPool<'_>,
from_email: &str,
) -> Result<Option<Self>, Error> {
queries().read(pool, ReadBy::Email(from_email)).await queries().read(pool, ReadBy::Email(from_email)).await
} }
@ -134,7 +125,7 @@ impl LocalUserView {
pool: &mut DbPool<'_>, pool: &mut DbPool<'_>,
oauth_provider_id: OAuthProviderId, oauth_provider_id: OAuthProviderId,
oauth_user_id: &str, oauth_user_id: &str,
) -> Result<Option<Self>, Error> { ) -> Result<Self, Error> {
queries() queries()
.read(pool, ReadBy::OAuthID(oauth_provider_id, oauth_user_id)) .read(pool, ReadBy::OAuthID(oauth_provider_id, oauth_user_id))
.await .await

View file

@ -220,7 +220,7 @@ impl PostReportView {
pool: &mut DbPool<'_>, pool: &mut DbPool<'_>,
report_id: PostReportId, report_id: PostReportId,
my_person_id: PersonId, my_person_id: PersonId,
) -> Result<Option<Self>, Error> { ) -> Result<Self, Error> {
queries().read(pool, (report_id, my_person_id)).await queries().read(pool, (report_id, my_person_id)).await
} }
@ -407,7 +407,6 @@ mod tests {
let read_jessica_report_view = let read_jessica_report_view =
PostReportView::read(pool, inserted_jessica_report.id, inserted_timmy.id) PostReportView::read(pool, inserted_jessica_report.id, inserted_timmy.id)
.await .await
.unwrap()
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
@ -445,7 +444,6 @@ mod tests {
let read_jessica_report_view_after_resolve = let read_jessica_report_view_after_resolve =
PostReportView::read(pool, inserted_jessica_report.id, inserted_timmy.id) PostReportView::read(pool, inserted_jessica_report.id, inserted_timmy.id)
.await .await
.unwrap()
.unwrap(); .unwrap();
assert!(read_jessica_report_view_after_resolve.post_report.resolved); assert!(read_jessica_report_view_after_resolve.post_report.resolved);
assert_eq!( assert_eq!(

View file

@ -568,7 +568,7 @@ impl PostView {
post_id: PostId, post_id: PostId,
my_local_user: Option<&'a LocalUser>, my_local_user: Option<&'a LocalUser>,
is_mod_or_admin: bool, is_mod_or_admin: bool,
) -> Result<Option<Self>, Error> { ) -> Result<Self, Error> {
queries() queries()
.read(pool, (post_id, my_local_user, is_mod_or_admin)) .read(pool, (post_id, my_local_user, is_mod_or_admin))
.await .await
@ -774,7 +774,7 @@ mod tests {
PostSortType, PostSortType,
SubscribedType, SubscribedType,
}; };
use lemmy_utils::error::{LemmyErrorType, LemmyResult}; use lemmy_utils::{error::LemmyResult, LemmyErrorType};
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
use serial_test::serial; use serial_test::serial;
use std::{collections::HashSet, time::Duration}; use std::{collections::HashSet, time::Duration};
@ -955,8 +955,7 @@ mod tests {
Some(&data.local_user_view.local_user), Some(&data.local_user_view.local_user),
false, false,
) )
.await? .await?;
.ok_or(LemmyErrorType::CouldntFindPost)?;
let expected_post_listing_with_user = expected_post_view(&data, pool).await?; let expected_post_listing_with_user = expected_post_view(&data, pool).await?;
@ -1005,9 +1004,7 @@ mod tests {
.await?; .await?;
let read_post_listing_single_no_person = let read_post_listing_single_no_person =
PostView::read(pool, data.inserted_post.id, None, false) PostView::read(pool, data.inserted_post.id, None, false).await?;
.await?
.ok_or(LemmyErrorType::CouldntFindPost)?;
let expected_post_listing_no_person = expected_post_view(&data, pool).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), Some(&data.local_user_view.local_user),
false, false,
) )
.await? .await?;
.ok_or(LemmyErrorType::CouldntFindPost)?;
let mut expected_post_with_upvote = expected_post_view(&data, pool).await?; let mut expected_post_with_upvote = expected_post_view(&data, pool).await?;
expected_post_with_upvote.my_vote = Some(1); 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)); assert_eq!(vec![POST_BY_BOT, POST], names(&post_listings_show_hidden));
// Make sure that hidden field is true. // Make sure that hidden field is true.
assert!( assert!(&post_listings_show_hidden.first().unwrap().hidden);
&post_listings_show_hidden
.first()
.ok_or(LemmyErrorType::CouldntFindPost)?
.hidden
);
cleanup(data, pool).await cleanup(data, pool).await
} }
@ -1687,13 +1678,7 @@ mod tests {
assert_eq!(vec![POST_BY_BOT, POST], names(&post_listings_show_nsfw)); assert_eq!(vec![POST_BY_BOT, POST], names(&post_listings_show_nsfw));
// Make sure that nsfw field is true. // Make sure that nsfw field is true.
assert!( assert!(&post_listings_show_nsfw.first().unwrap().post.nsfw);
&post_listings_show_nsfw
.first()
.ok_or(LemmyErrorType::CouldntFindPost)?
.post
.nsfw
);
cleanup(data, pool).await cleanup(data, pool).await
} }
@ -1862,8 +1847,8 @@ mod tests {
.await?; .await?;
assert_eq!(2, authenticated_query.len()); assert_eq!(2, authenticated_query.len());
let unauthenticated_post = PostView::read(pool, data.inserted_post.id, None, false).await?; let unauthenticated_post = PostView::read(pool, data.inserted_post.id, None, false).await;
assert!(unauthenticated_post.is_none()); assert!(unauthenticated_post.is_err());
let authenticated_post = PostView::read( let authenticated_post = PostView::read(
pool, pool,
@ -1913,8 +1898,7 @@ mod tests {
Some(&inserted_banned_from_comm_local_user), Some(&inserted_banned_from_comm_local_user),
false, false,
) )
.await? .await?;
.ok_or(LemmyErrorType::CouldntFindPost)?;
assert!(post_view.banned_from_community); assert!(post_view.banned_from_community);
@ -1935,8 +1919,7 @@ mod tests {
Some(&data.local_user_view.local_user), Some(&data.local_user_view.local_user),
false, false,
) )
.await? .await?;
.ok_or(LemmyErrorType::CouldntFindPost)?;
assert!(!post_view.banned_from_community); assert!(!post_view.banned_from_community);

View file

@ -78,7 +78,7 @@ impl PrivateMessageReportView {
pub async fn read( pub async fn read(
pool: &mut DbPool<'_>, pool: &mut DbPool<'_>,
report_id: PrivateMessageReportId, report_id: PrivateMessageReportId,
) -> Result<Option<Self>, Error> { ) -> Result<Self, Error> {
queries().read(pool, report_id).await queries().read(pool, report_id).await
} }

View file

@ -113,7 +113,7 @@ impl PrivateMessageView {
pub async fn read( pub async fn read(
pool: &mut DbPool<'_>, pool: &mut DbPool<'_>,
private_message_id: PrivateMessageId, private_message_id: PrivateMessageId,
) -> Result<Option<Self>, Error> { ) -> Result<Self, Error> {
queries().read(pool, private_message_id).await queries().read(pool, private_message_id).await
} }

View file

@ -81,17 +81,11 @@ fn queries<'a>() -> Queries<
} }
impl RegistrationApplicationView { impl RegistrationApplicationView {
pub async fn read( pub async fn read(pool: &mut DbPool<'_>, id: RegistrationApplicationId) -> Result<Self, Error> {
pool: &mut DbPool<'_>,
id: RegistrationApplicationId,
) -> Result<Option<Self>, Error> {
queries().read(pool, ReadBy::Id(id)).await queries().read(pool, ReadBy::Id(id)).await
} }
pub async fn read_by_person( pub async fn read_by_person(pool: &mut DbPool<'_>, person_id: PersonId) -> Result<Self, Error> {
pool: &mut DbPool<'_>,
person_id: PersonId,
) -> Result<Option<Self>, Error> {
queries().read(pool, ReadBy::Person(person_id)).await queries().read(pool, ReadBy::Person(person_id)).await
} }
/// Returns the current unread registration_application count /// Returns the current unread registration_application count
@ -208,7 +202,6 @@ mod tests {
let read_sara_app_view = RegistrationApplicationView::read(pool, sara_app.id) let read_sara_app_view = RegistrationApplicationView::read(pool, sara_app.id)
.await .await
.unwrap()
.unwrap(); .unwrap();
let jess_person_form = PersonInsertForm::test_form(inserted_instance.id, "jess_rav"); 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) let read_jess_app_view = RegistrationApplicationView::read(pool, jess_app.id)
.await .await
.unwrap()
.unwrap(); .unwrap();
let mut expected_sara_app_view = RegistrationApplicationView { 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) let read_sara_app_view_after_approve = RegistrationApplicationView::read(pool, sara_app.id)
.await .await
.unwrap()
.unwrap(); .unwrap();
// Make sure the columns changed // Make sure the columns changed

View file

@ -242,7 +242,7 @@ impl CommentReplyView {
pool: &mut DbPool<'_>, pool: &mut DbPool<'_>,
comment_reply_id: CommentReplyId, comment_reply_id: CommentReplyId,
my_person_id: Option<PersonId>, my_person_id: Option<PersonId>,
) -> Result<Option<Self>, Error> { ) -> Result<Self, Error> {
queries().read(pool, (comment_reply_id, my_person_id)).await queries().read(pool, (comment_reply_id, my_person_id)).await
} }
@ -446,9 +446,7 @@ mod tests {
&recipient_local_user_update_form, &recipient_local_user_update_form,
) )
.await?; .await?;
let recipient_local_user_view = LocalUserView::read(pool, recipient_local_user.id) let recipient_local_user_view = LocalUserView::read(pool, recipient_local_user.id).await?;
.await?
.ok_or(LemmyErrorType::CouldntFindLocalUser)?;
let unread_replies_after_hide_bots = let unread_replies_after_hide_bots =
CommentReplyView::get_unread_replies(pool, &recipient_local_user_view.local_user).await?; CommentReplyView::get_unread_replies(pool, &recipient_local_user_view.local_user).await?;

View file

@ -179,7 +179,7 @@ impl CommunityView {
community_id: CommunityId, community_id: CommunityId,
my_local_user: Option<&'a LocalUser>, my_local_user: Option<&'a LocalUser>,
is_mod_or_admin: bool, is_mod_or_admin: bool,
) -> Result<Option<Self>, Error> { ) -> Result<Self, Error> {
queries() queries()
.read(pool, (community_id, my_local_user, is_mod_or_admin)) .read(pool, (community_id, my_local_user, is_mod_or_admin))
.await .await
@ -194,7 +194,7 @@ impl CommunityView {
CommunityModeratorView::is_community_moderator(pool, community_id, person_id).await?; CommunityModeratorView::is_community_moderator(pool, community_id, person_id).await?;
if is_mod { if is_mod {
Ok(true) 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) Ok(person_view.is_admin)
} else { } else {
Ok(false) Ok(false)
@ -210,7 +210,7 @@ impl CommunityView {
CommunityModeratorView::is_community_moderator_of_any(pool, person_id).await?; CommunityModeratorView::is_community_moderator_of_any(pool, person_id).await?;
if is_mod_of_any { if is_mod_of_any {
Ok(true) 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) Ok(person_view.is_admin)
} else { } else {
Ok(false) Ok(false)
@ -364,10 +364,8 @@ mod tests {
assert_eq!(1, authenticated_query.len()); assert_eq!(1, authenticated_query.len());
let unauthenticated_community = let unauthenticated_community =
CommunityView::read(pool, data.inserted_community.id, None, false) CommunityView::read(pool, data.inserted_community.id, None, false).await;
.await assert!(unauthenticated_community.is_err());
.unwrap();
assert!(unauthenticated_community.is_none());
let authenticated_community = CommunityView::read( let authenticated_community = CommunityView::read(
pool, pool,

View file

@ -241,7 +241,7 @@ impl PersonMentionView {
pool: &mut DbPool<'_>, pool: &mut DbPool<'_>,
person_mention_id: PersonMentionId, person_mention_id: PersonMentionId,
my_person_id: Option<PersonId>, my_person_id: Option<PersonId>,
) -> Result<Option<Self>, Error> { ) -> Result<Self, Error> {
queries() queries()
.read(pool, (person_mention_id, my_person_id)) .read(pool, (person_mention_id, my_person_id))
.await .await

View file

@ -135,7 +135,7 @@ fn queries<'a>(
} }
impl PersonView { impl PersonView {
pub async fn read(pool: &mut DbPool<'_>, person_id: PersonId) -> Result<Option<Self>, Error> { pub async fn read(pool: &mut DbPool<'_>, person_id: PersonId) -> Result<Self, Error> {
queries().read(pool, person_id).await queries().read(pool, person_id).await
} }

View file

@ -77,7 +77,6 @@ pub enum LemmyErrorType {
OnlyModsCanPostInCommunity, OnlyModsCanPostInCommunity,
CouldntUpdatePost, CouldntUpdatePost,
NoPostEditAllowed, NoPostEditAllowed,
CouldntFindPost,
EditPrivateMessageNotAllowed, EditPrivateMessageNotAllowed,
SiteAlreadyExists, SiteAlreadyExists,
ApplicationQuestionRequired, ApplicationQuestionRequired,
@ -188,6 +187,7 @@ pub enum LemmyErrorType {
Unknown(String), Unknown(String),
CantDeleteSite, CantDeleteSite,
UrlLengthOverflow, UrlLengthOverflow,
NotFound,
} }
cfg_if! { cfg_if! {
@ -212,7 +212,7 @@ cfg_if! {
fn from(t: T) -> Self { fn from(t: T) -> Self {
let cause = t.into(); let cause = t.into();
let error_type = match cause.downcast_ref::<diesel::result::Error>() { let error_type = match cause.downcast_ref::<diesel::result::Error>() {
Some(&diesel::NotFound) => LemmyErrorType::CouldntFindPost, Some(&diesel::NotFound) => LemmyErrorType::NotFound,
_ => LemmyErrorType::Unknown(format!("{}", &cause)) _ => LemmyErrorType::Unknown(format!("{}", &cause))
}; };
LemmyError { LemmyError {