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),
is_mod_or_admin,
)
.await?
.ok_or(LemmyErrorType::CouldntFindPost)?;
.await?;
Ok(Json(PostResponse { post_view }))
}

View file

@ -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 {

View file

@ -22,22 +22,15 @@ impl SentActivity {
.await
}
pub async fn read_from_apub_id(
pool: &mut DbPool<'_>,
object_id: &DbUrl,
) -> Result<Option<Self>, Error> {
pub async fn read_from_apub_id(pool: &mut DbPool<'_>, object_id: &DbUrl) -> Result<Self, Error> {
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<Option<Self>, Error> {
pub async fn read(pool: &mut DbPool<'_>, object_id: ActivityId) -> Result<Self, Error> {
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);

View file

@ -13,7 +13,6 @@ use diesel::{
},
sql_types::{self, Timestamptz},
IntoSql,
OptionalExtension,
};
use diesel_async::{
pg::AsyncPgConnection,
@ -574,12 +573,12 @@ impl<RF, LF> Queries<RF, LF> {
self,
pool: &'a mut DbPool<'_>,
args: Args,
) -> Result<Option<T>, DieselError>
) -> Result<T, DieselError>
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>(

View file

@ -192,7 +192,7 @@ impl CommentReportView {
pool: &mut DbPool<'_>,
report_id: CommentReportId,
my_person_id: PersonId,
) -> Result<Option<Self>, Error> {
) -> Result<Self, Error> {
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;

View file

@ -367,7 +367,7 @@ impl CommentView {
) -> Result<Option<Self>, 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);

View file

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

View file

@ -220,7 +220,7 @@ impl PostReportView {
pool: &mut DbPool<'_>,
report_id: PostReportId,
my_person_id: PersonId,
) -> Result<Option<Self>, Error> {
) -> Result<Self, Error> {
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!(

View file

@ -568,7 +568,7 @@ impl PostView {
post_id: PostId,
my_local_user: Option<&'a LocalUser>,
is_mod_or_admin: bool,
) -> Result<Option<Self>, Error> {
) -> Result<Self, Error> {
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);

View file

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

View file

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

View file

@ -81,17 +81,11 @@ fn queries<'a>() -> Queries<
}
impl RegistrationApplicationView {
pub async fn read(
pool: &mut DbPool<'_>,
id: RegistrationApplicationId,
) -> Result<Option<Self>, Error> {
pub async fn read(pool: &mut DbPool<'_>, id: RegistrationApplicationId) -> Result<Self, Error> {
queries().read(pool, ReadBy::Id(id)).await
}
pub async fn read_by_person(
pool: &mut DbPool<'_>,
person_id: PersonId,
) -> Result<Option<Self>, Error> {
pub async fn read_by_person(pool: &mut DbPool<'_>, person_id: PersonId) -> Result<Self, Error> {
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

View file

@ -242,7 +242,7 @@ impl CommentReplyView {
pool: &mut DbPool<'_>,
comment_reply_id: CommentReplyId,
my_person_id: Option<PersonId>,
) -> Result<Option<Self>, Error> {
) -> Result<Self, Error> {
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?;

View file

@ -179,7 +179,7 @@ impl CommunityView {
community_id: CommunityId,
my_local_user: Option<&'a LocalUser>,
is_mod_or_admin: bool,
) -> Result<Option<Self>, Error> {
) -> Result<Self, Error> {
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,

View file

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

View file

@ -135,7 +135,7 @@ fn queries<'a>(
}
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
}

View file

@ -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::<diesel::result::Error>() {
Some(&diesel::NotFound) => LemmyErrorType::CouldntFindPost,
Some(&diesel::NotFound) => LemmyErrorType::NotFound,
_ => LemmyErrorType::Unknown(format!("{}", &cause))
};
LemmyError {