Remove TypedBuilder from db_views and db_views_actor (#3637)

* change pool fields to parameters for list

* remove my_person_id and admin fields

* Change recipient id to list param

* Remove TypedBuilder from db_views and db_views_actor
pull/3620/head
dullbananas 2023-07-17 03:20:25 -07:00 committed by GitHub
parent 6688a8a5d4
commit 88215bfbc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
29 changed files with 672 additions and 740 deletions

2
Cargo.lock generated
View File

@ -2769,7 +2769,6 @@ dependencies = [
"tokio", "tokio",
"tracing", "tracing",
"ts-rs", "ts-rs",
"typed-builder",
] ]
[[package]] [[package]]
@ -2782,7 +2781,6 @@ dependencies = [
"serde", "serde",
"serde_with", "serde_with",
"ts-rs", "ts-rs",
"typed-builder",
] ]
[[package]] [[package]]

View File

@ -22,24 +22,19 @@ impl Perform for ListCommentReports {
let data: &ListCommentReports = self; let data: &ListCommentReports = self;
let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
let person_id = local_user_view.person.id;
let admin = local_user_view.person.admin;
let community_id = data.community_id; let community_id = data.community_id;
let unresolved_only = data.unresolved_only; let unresolved_only = data.unresolved_only;
let page = data.page; let page = data.page;
let limit = data.limit; let limit = data.limit;
let comment_reports = CommentReportQuery::builder() let comment_reports = CommentReportQuery {
.pool(&mut context.pool()) community_id,
.my_person_id(person_id) unresolved_only,
.admin(admin) page,
.community_id(community_id) limit,
.unresolved_only(unresolved_only) }
.page(page) .list(&mut context.pool(), &local_user_view.person)
.limit(limit) .await?;
.build()
.list()
.await?;
Ok(ListCommentReportsResponse { comment_reports }) Ok(ListCommentReportsResponse { comment_reports })
} }

View File

@ -27,18 +27,17 @@ impl Perform for GetPersonMentions {
let person_id = Some(local_user_view.person.id); let person_id = Some(local_user_view.person.id);
let show_bot_accounts = Some(local_user_view.local_user.show_bot_accounts); let show_bot_accounts = Some(local_user_view.local_user.show_bot_accounts);
let mentions = PersonMentionQuery::builder() let mentions = PersonMentionQuery {
.pool(&mut context.pool()) recipient_id: person_id,
.recipient_id(person_id) my_person_id: person_id,
.my_person_id(person_id) sort,
.sort(sort) unread_only,
.unread_only(unread_only) show_bot_accounts,
.show_bot_accounts(show_bot_accounts) page,
.page(page) limit,
.limit(limit) }
.build() .list(&mut context.pool())
.list() .await?;
.await?;
Ok(GetPersonMentionsResponse { mentions }) Ok(GetPersonMentionsResponse { mentions })
} }

View File

@ -24,18 +24,17 @@ impl Perform for GetReplies {
let person_id = Some(local_user_view.person.id); let person_id = Some(local_user_view.person.id);
let show_bot_accounts = Some(local_user_view.local_user.show_bot_accounts); let show_bot_accounts = Some(local_user_view.local_user.show_bot_accounts);
let replies = CommentReplyQuery::builder() let replies = CommentReplyQuery {
.pool(&mut context.pool()) recipient_id: person_id,
.recipient_id(person_id) my_person_id: person_id,
.my_person_id(person_id) sort,
.sort(sort) unread_only,
.unread_only(unread_only) show_bot_accounts,
.show_bot_accounts(show_bot_accounts) page,
.page(page) limit,
.limit(limit) }
.build() .list(&mut context.pool())
.list() .await?;
.await?;
Ok(GetRepliesResponse { replies }) Ok(GetRepliesResponse { replies })
} }

View File

@ -22,24 +22,19 @@ impl Perform for ListPostReports {
let data: &ListPostReports = self; let data: &ListPostReports = self;
let local_user_view = local_user_view_from_jwt(&data.auth, context).await?; let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
let person_id = local_user_view.person.id;
let admin = local_user_view.person.admin;
let community_id = data.community_id; let community_id = data.community_id;
let unresolved_only = data.unresolved_only; let unresolved_only = data.unresolved_only;
let page = data.page; let page = data.page;
let limit = data.limit; let limit = data.limit;
let post_reports = PostReportQuery::builder() let post_reports = PostReportQuery {
.pool(&mut context.pool()) community_id,
.my_person_id(person_id) unresolved_only,
.admin(admin) page,
.community_id(community_id) limit,
.unresolved_only(unresolved_only) }
.page(page) .list(&mut context.pool(), &local_user_view.person)
.limit(limit) .await?;
.build()
.list()
.await?;
Ok(ListPostReportsResponse { post_reports }) Ok(ListPostReportsResponse { post_reports })
} }

View File

@ -21,14 +21,13 @@ impl Perform for ListPrivateMessageReports {
let unresolved_only = self.unresolved_only; let unresolved_only = self.unresolved_only;
let page = self.page; let page = self.page;
let limit = self.limit; let limit = self.limit;
let private_message_reports = PrivateMessageReportQuery::builder() let private_message_reports = PrivateMessageReportQuery {
.pool(&mut context.pool()) unresolved_only,
.unresolved_only(unresolved_only) page,
.page(page) limit,
.limit(limit) }
.build() .list(&mut context.pool())
.list() .await?;
.await?;
Ok(ListPrivateMessageReportsResponse { Ok(ListPrivateMessageReportsResponse {
private_message_reports, private_message_reports,

View File

@ -23,19 +23,18 @@ impl Perform for ListRegistrationApplications {
is_admin(&local_user_view)?; is_admin(&local_user_view)?;
let unread_only = data.unread_only; let unread_only = data.unread_only;
let verified_email_only = local_site.require_email_verification; let verified_email_only = Some(local_site.require_email_verification);
let page = data.page; let page = data.page;
let limit = data.limit; let limit = data.limit;
let registration_applications = RegistrationApplicationQuery::builder() let registration_applications = RegistrationApplicationQuery {
.pool(&mut context.pool()) unread_only,
.unread_only(unread_only) verified_email_only,
.verified_email_only(Some(verified_email_only)) page,
.page(page) limit,
.limit(limit) }
.build() .list(&mut context.pool())
.list() .await?;
.await?;
Ok(Self::Response { Ok(Self::Response {
registration_applications, registration_applications,

View File

@ -667,13 +667,13 @@ pub async fn remove_user_data_in_community(
// Comments // Comments
// TODO Diesel doesn't allow updates with joins, so this has to be a loop // TODO Diesel doesn't allow updates with joins, so this has to be a loop
let comments = CommentQuery::builder() let comments = CommentQuery {
.pool(pool) creator_id: Some(banned_person_id),
.creator_id(Some(banned_person_id)) community_id: Some(community_id),
.community_id(Some(community_id)) ..Default::default()
.build() }
.list() .list(pool)
.await?; .await?;
for comment_view in &comments { for comment_view in &comments {
let comment_id = comment_view.comment.id; let comment_id = comment_view.comment.id;

View File

@ -31,18 +31,18 @@ impl PerformCrud for ListCommunities {
let page = data.page; let page = data.page;
let limit = data.limit; let limit = data.limit;
let local_user = local_user_view.map(|l| l.local_user); let local_user = local_user_view.map(|l| l.local_user);
let communities = CommunityQuery::builder() let communities = CommunityQuery {
.pool(&mut context.pool()) listing_type,
.listing_type(listing_type) show_nsfw,
.show_nsfw(show_nsfw) sort,
.sort(sort) local_user: local_user.as_ref(),
.local_user(local_user.as_ref()) page,
.page(page) limit,
.limit(limit) is_mod_or_admin: is_admin,
.is_mod_or_admin(is_admin) ..Default::default()
.build() }
.list() .list(&mut context.pool())
.await?; .await?;
// Return the jwt // Return the jwt
Ok(ListCommunitiesResponse { communities }) Ok(ListCommunitiesResponse { communities })

View File

@ -100,12 +100,12 @@ impl PerformCrud for GetPost {
// Fetch the cross_posts // Fetch the cross_posts
let cross_posts = if let Some(url) = &post_view.post.url { let cross_posts = if let Some(url) = &post_view.post.url {
let mut x_posts = PostQuery::builder() let mut x_posts = PostQuery {
.pool(&mut context.pool()) url_search: Some(url.inner().as_str().into()),
.url_search(Some(url.inner().as_str().into())) ..Default::default()
.build() }
.list() .list(&mut context.pool())
.await?; .await?;
// Don't return this post as one of the cross_posts // Don't return this post as one of the cross_posts
x_posts.retain(|x| x.post.id != post_id); x_posts.retain(|x| x.post.id != post_id);

View File

@ -24,15 +24,13 @@ impl PerformCrud for GetPrivateMessages {
let page = data.page; let page = data.page;
let limit = data.limit; let limit = data.limit;
let unread_only = data.unread_only; let unread_only = data.unread_only;
let mut messages = PrivateMessageQuery::builder() let mut messages = PrivateMessageQuery {
.pool(&mut context.pool()) page,
.recipient_id(person_id) limit,
.page(page) unread_only,
.limit(limit) }
.unread_only(unread_only) .list(&mut context.pool(), person_id)
.build() .await?;
.list()
.await?;
// Messages sent by ourselves should be marked as read. The `read` column in database is only // Messages sent by ourselves should be marked as read. The `read` column in database is only
// for the recipient, and shouldnt be exposed to sender. // for the recipient, and shouldnt be exposed to sender.

View File

@ -39,7 +39,11 @@ pub async fn list_comments(
let limit = data.limit; let limit = data.limit;
let parent_id = data.parent_id; let parent_id = data.parent_id;
let listing_type = listing_type_with_default(data.type_, &local_site, community_id)?; let listing_type = Some(listing_type_with_default(
data.type_,
&local_site,
community_id,
)?);
// If a parent_id is given, fetch the comment to get the path // If a parent_id is given, fetch the comment to get the path
let parent_path = if let Some(parent_id) = parent_id { let parent_path = if let Some(parent_id) = parent_id {
@ -51,22 +55,22 @@ pub async fn list_comments(
let parent_path_cloned = parent_path.clone(); let parent_path_cloned = parent_path.clone();
let post_id = data.post_id; let post_id = data.post_id;
let local_user = local_user_view.map(|l| l.local_user); let local_user = local_user_view.map(|l| l.local_user);
let comments = CommentQuery::builder() let comments = CommentQuery {
.pool(&mut context.pool()) listing_type,
.listing_type(Some(listing_type)) sort,
.sort(sort) max_depth,
.max_depth(max_depth) saved_only,
.saved_only(saved_only) community_id,
.community_id(community_id) parent_path: parent_path_cloned,
.parent_path(parent_path_cloned) post_id,
.post_id(post_id) local_user: local_user.as_ref(),
.local_user(local_user.as_ref()) page,
.page(page) limit,
.limit(limit) ..Default::default()
.build() }
.list() .list(&mut context.pool())
.await .await
.with_lemmy_type(LemmyErrorType::CouldntGetComments)?; .with_lemmy_type(LemmyErrorType::CouldntGetComments)?;
Ok(Json(GetCommentsResponse { comments })) Ok(Json(GetCommentsResponse { comments }))
} }

View File

@ -36,27 +36,32 @@ pub async fn list_posts(
}; };
let saved_only = data.saved_only; let saved_only = data.saved_only;
let listing_type = listing_type_with_default(data.type_, &local_site, community_id)?; let listing_type = Some(listing_type_with_default(
data.type_,
&local_site,
community_id,
)?);
let is_mod_or_admin = let is_mod_or_admin = Some(
is_mod_or_admin_opt(&mut context.pool(), local_user_view.as_ref(), community_id) is_mod_or_admin_opt(&mut context.pool(), local_user_view.as_ref(), community_id)
.await .await
.is_ok(); .is_ok(),
);
let posts = PostQuery::builder() let posts = PostQuery {
.pool(&mut context.pool()) local_user: local_user_view.map(|l| l.local_user).as_ref(),
.local_user(local_user_view.map(|l| l.local_user).as_ref()) listing_type,
.listing_type(Some(listing_type)) sort,
.sort(sort) community_id,
.community_id(community_id) saved_only,
.saved_only(saved_only) page,
.page(page) limit,
.limit(limit) is_mod_or_admin,
.is_mod_or_admin(Some(is_mod_or_admin)) ..Default::default()
.build() }
.list() .list(&mut context.pool())
.await .await
.with_lemmy_type(LemmyErrorType::CouldntGetPosts)?; .with_lemmy_type(LemmyErrorType::CouldntGetPosts)?;
Ok(Json(GetPostsResponse { posts })) Ok(Json(GetPostsResponse { posts }))
} }

View File

@ -56,49 +56,49 @@ pub async fn read_person(
let local_user = local_user_view.map(|l| l.local_user); let local_user = local_user_view.map(|l| l.local_user);
let local_user_clone = local_user.clone(); let local_user_clone = local_user.clone();
let posts = PostQuery::builder() let posts = PostQuery {
.pool(&mut context.pool()) sort,
.sort(sort) saved_only,
.saved_only(saved_only) local_user:local_user.as_ref(),
.local_user(local_user.as_ref()) community_id,
.community_id(community_id) is_mod_or_admin: is_admin,
.is_mod_or_admin(is_admin) page,
.page(page) limit,
.limit(limit) creator_id:
.creator_id(
// If its saved only, you don't care what creator it was // If its saved only, you don't care what creator it was
// Or, if its not saved, then you only want it for that specific creator // Or, if its not saved, then you only want it for that specific creator
if !saved_only.unwrap_or(false) { if !saved_only.unwrap_or(false) {
Some(person_details_id) Some(person_details_id)
} else { } else {
None None
}, }
) ,
.build() ..Default::default()
.list() }
.await?; .list(&mut context.pool())
.await?;
let comments = CommentQuery::builder() let comments = CommentQuery {
.pool(&mut context.pool()) local_user: (local_user_clone.as_ref()),
.local_user(local_user_clone.as_ref()) sort: (sort.map(post_to_comment_sort_type)),
.sort(sort.map(post_to_comment_sort_type)) saved_only: (saved_only),
.saved_only(saved_only) show_deleted_and_removed: (Some(false)),
.show_deleted_and_removed(Some(false)) community_id: (community_id),
.community_id(community_id) page: (page),
.page(page) limit: (limit),
.limit(limit) creator_id: (
.creator_id(
// If its saved only, you don't care what creator it was // If its saved only, you don't care what creator it was
// Or, if its not saved, then you only want it for that specific creator // Or, if its not saved, then you only want it for that specific creator
if !saved_only.unwrap_or(false) { if !saved_only.unwrap_or(false) {
Some(person_details_id) Some(person_details_id)
} else { } else {
None None
}, }
) ),
.build() ..Default::default()
.list() }
.await?; .list(&mut context.pool())
.await?;
let moderates = let moderates =
CommunityModeratorView::for_person(&mut context.pool(), person_details_id).await?; CommunityModeratorView::for_person(&mut context.pool(), person_details_id).await?;

View File

@ -53,60 +53,59 @@ pub async fn search(
let local_user = local_user_view.map(|l| l.local_user); let local_user = local_user_view.map(|l| l.local_user);
match search_type { match search_type {
SearchType::Posts => { SearchType::Posts => {
posts = PostQuery::builder() posts = PostQuery {
.pool(&mut context.pool()) sort: (sort),
.sort(sort) listing_type: (listing_type),
.listing_type(listing_type) community_id: (community_id),
.community_id(community_id) creator_id: (creator_id),
.creator_id(creator_id) local_user: (local_user.as_ref()),
.local_user(local_user.as_ref()) search_term: (Some(q)),
.search_term(Some(q)) is_mod_or_admin: (is_admin),
.is_mod_or_admin(is_admin) page: (page),
.page(page) limit: (limit),
.limit(limit) ..Default::default()
.build() }
.list() .list(&mut context.pool())
.await?; .await?;
} }
SearchType::Comments => { SearchType::Comments => {
comments = CommentQuery::builder() comments = CommentQuery {
.pool(&mut context.pool()) sort: (sort.map(post_to_comment_sort_type)),
.sort(sort.map(post_to_comment_sort_type)) listing_type: (listing_type),
.listing_type(listing_type) search_term: (Some(q)),
.search_term(Some(q)) community_id: (community_id),
.community_id(community_id) creator_id: (creator_id),
.creator_id(creator_id) local_user: (local_user.as_ref()),
.local_user(local_user.as_ref()) page: (page),
.page(page) limit: (limit),
.limit(limit) ..Default::default()
.build() }
.list() .list(&mut context.pool())
.await?; .await?;
} }
SearchType::Communities => { SearchType::Communities => {
communities = CommunityQuery::builder() communities = CommunityQuery {
.pool(&mut context.pool()) sort: (sort),
.sort(sort) listing_type: (listing_type),
.listing_type(listing_type) search_term: (Some(q)),
.search_term(Some(q)) local_user: (local_user.as_ref()),
.local_user(local_user.as_ref()) is_mod_or_admin: (is_admin),
.is_mod_or_admin(is_admin) page: (page),
.page(page) limit: (limit),
.limit(limit) ..Default::default()
.build() }
.list() .list(&mut context.pool())
.await?; .await?;
} }
SearchType::Users => { SearchType::Users => {
users = PersonQuery::builder() users = PersonQuery {
.pool(&mut context.pool()) sort: (sort),
.sort(sort) search_term: (Some(q)),
.search_term(Some(q)) page: (page),
.page(page) limit: (limit),
.limit(limit) }
.build() .list(&mut context.pool())
.list() .await?;
.await?;
} }
SearchType::All => { SearchType::All => {
// If the community or creator is included, dont search communities or users // If the community or creator is included, dont search communities or users
@ -114,55 +113,55 @@ pub async fn search(
data.community_id.is_some() || data.community_name.is_some() || data.creator_id.is_some(); data.community_id.is_some() || data.community_name.is_some() || data.creator_id.is_some();
let local_user_ = local_user.clone(); let local_user_ = local_user.clone();
posts = PostQuery::builder() posts = PostQuery {
.pool(&mut context.pool()) sort: (sort),
.sort(sort) listing_type: (listing_type),
.listing_type(listing_type) community_id: (community_id),
.community_id(community_id) creator_id: (creator_id),
.creator_id(creator_id) local_user: (local_user_.as_ref()),
.local_user(local_user_.as_ref()) search_term: (Some(q)),
.search_term(Some(q)) is_mod_or_admin: (is_admin),
.is_mod_or_admin(is_admin) page: (page),
.page(page) limit: (limit),
.limit(limit) ..Default::default()
.build() }
.list() .list(&mut context.pool())
.await?; .await?;
let q = data.q.clone(); let q = data.q.clone();
let local_user_ = local_user.clone(); let local_user_ = local_user.clone();
comments = CommentQuery::builder() comments = CommentQuery {
.pool(&mut context.pool()) sort: (sort.map(post_to_comment_sort_type)),
.sort(sort.map(post_to_comment_sort_type)) listing_type: (listing_type),
.listing_type(listing_type) search_term: (Some(q)),
.search_term(Some(q)) community_id: (community_id),
.community_id(community_id) creator_id: (creator_id),
.creator_id(creator_id) local_user: (local_user_.as_ref()),
.local_user(local_user_.as_ref()) page: (page),
.page(page) limit: (limit),
.limit(limit) ..Default::default()
.build() }
.list() .list(&mut context.pool())
.await?; .await?;
let q = data.q.clone(); let q = data.q.clone();
communities = if community_or_creator_included { communities = if community_or_creator_included {
vec![] vec![]
} else { } else {
CommunityQuery::builder() CommunityQuery {
.pool(&mut context.pool()) sort: (sort),
.sort(sort) listing_type: (listing_type),
.listing_type(listing_type) search_term: (Some(q)),
.search_term(Some(q)) local_user: (local_user.as_ref()),
.local_user(local_user.as_ref()) is_mod_or_admin: (is_admin),
.is_mod_or_admin(is_admin) page: (page),
.page(page) limit: (limit),
.limit(limit) ..Default::default()
.build() }
.list() .list(&mut context.pool())
.await? .await?
}; };
let q = data.q.clone(); let q = data.q.clone();
@ -170,31 +169,30 @@ pub async fn search(
users = if community_or_creator_included { users = if community_or_creator_included {
vec![] vec![]
} else { } else {
PersonQuery::builder() PersonQuery {
.pool(&mut context.pool()) sort: (sort),
.sort(sort) search_term: (Some(q)),
.search_term(Some(q)) page: (page),
.page(page) limit: (limit),
.limit(limit) }
.build() .list(&mut context.pool())
.list() .await?
.await?
}; };
} }
SearchType::Url => { SearchType::Url => {
posts = PostQuery::builder() posts = PostQuery {
.pool(&mut context.pool()) sort: (sort),
.sort(sort) listing_type: (listing_type),
.listing_type(listing_type) community_id: (community_id),
.community_id(community_id) creator_id: (creator_id),
.creator_id(creator_id) url_search: (Some(q)),
.url_search(Some(q)) is_mod_or_admin: (is_admin),
.is_mod_or_admin(is_admin) page: (page),
.page(page) limit: (limit),
.limit(limit) ..Default::default()
.build() }
.list() .list(&mut context.pool())
.await?; .await?;
} }
}; };

View File

@ -29,7 +29,6 @@ diesel_ltree = { workspace = true, optional = true }
serde = { workspace = true } serde = { workspace = true }
serde_with = { workspace = true } serde_with = { workspace = true }
tracing = { workspace = true, optional = true } tracing = { workspace = true, optional = true }
typed-builder = { workspace = true }
ts-rs = { workspace = true, optional = true } ts-rs = { workspace = true, optional = true }
[dev-dependencies] [dev-dependencies]

View File

@ -33,7 +33,6 @@ use lemmy_db_schema::{
traits::JoinView, traits::JoinView,
utils::{get_conn, limit_and_offset, DbPool}, utils::{get_conn, limit_and_offset, DbPool},
}; };
use typed_builder::TypedBuilder;
impl CommentReportView { impl CommentReportView {
/// returns the CommentReportView for the provided report_id /// returns the CommentReportView for the provided report_id
@ -137,24 +136,21 @@ impl CommentReportView {
} }
} }
#[derive(TypedBuilder)] #[derive(Default)]
#[builder(field_defaults(default))] pub struct CommentReportQuery {
pub struct CommentReportQuery<'a, 'b: 'a> { pub community_id: Option<CommunityId>,
#[builder(!default)] pub page: Option<i64>,
pool: &'a mut DbPool<'b>, pub limit: Option<i64>,
#[builder(!default)] pub unresolved_only: Option<bool>,
my_person_id: PersonId,
#[builder(!default)]
admin: bool,
community_id: Option<CommunityId>,
page: Option<i64>,
limit: Option<i64>,
unresolved_only: Option<bool>,
} }
impl<'a, 'b: 'a> CommentReportQuery<'a, 'b> { impl CommentReportQuery {
pub async fn list(self) -> Result<Vec<CommentReportView>, Error> { pub async fn list(
let conn = &mut get_conn(self.pool).await?; self,
pool: &mut DbPool<'_>,
my_person: &Person,
) -> Result<Vec<CommentReportView>, Error> {
let conn = &mut get_conn(pool).await?;
let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2);
@ -183,7 +179,7 @@ impl<'a, 'b: 'a> CommentReportQuery<'a, 'b> {
comment_like::table.on( comment_like::table.on(
comment::id comment::id
.eq(comment_like::comment_id) .eq(comment_like::comment_id)
.and(comment_like::person_id.eq(self.my_person_id)), .and(comment_like::person_id.eq(my_person.id)),
), ),
) )
.left_join( .left_join(
@ -220,13 +216,13 @@ impl<'a, 'b: 'a> CommentReportQuery<'a, 'b> {
.offset(offset); .offset(offset);
// If its not an admin, get only the ones you mod // If its not an admin, get only the ones you mod
let res = if !self.admin { let res = if !my_person.admin {
query query
.inner_join( .inner_join(
community_moderator::table.on( community_moderator::table.on(
community_moderator::community_id community_moderator::community_id
.eq(post::community_id) .eq(post::community_id)
.and(community_moderator::person_id.eq(self.my_person_id)), .and(community_moderator::person_id.eq(my_person.id)),
), ),
) )
.load::<<CommentReportView as JoinView>::JoinTuple>(conn) .load::<<CommentReportView as JoinView>::JoinTuple>(conn)
@ -514,12 +510,8 @@ mod tests {
}; };
// Do a batch read of timmys reports // Do a batch read of timmys reports
let reports = CommentReportQuery::builder() let reports = CommentReportQuery::default()
.pool(pool) .list(pool, &inserted_timmy)
.my_person_id(inserted_timmy.id)
.admin(false)
.build()
.list()
.await .await
.unwrap(); .unwrap();
@ -590,15 +582,13 @@ mod tests {
// Do a batch read of timmys reports // Do a batch read of timmys reports
// It should only show saras, which is unresolved // It should only show saras, which is unresolved
let reports_after_resolve = CommentReportQuery::builder() let reports_after_resolve = CommentReportQuery {
.pool(pool) unresolved_only: (Some(true)),
.my_person_id(inserted_timmy.id) ..Default::default()
.admin(false) }
.unresolved_only(Some(true)) .list(pool, &inserted_timmy)
.build() .await
.list() .unwrap();
.await
.unwrap();
assert_eq!(reports_after_resolve[0], expected_sara_report_view); assert_eq!(reports_after_resolve[0], expected_sara_report_view);
assert_eq!(reports_after_resolve.len(), 1); assert_eq!(reports_after_resolve.len(), 1);

View File

@ -40,7 +40,6 @@ use lemmy_db_schema::{
CommentSortType, CommentSortType,
ListingType, ListingType,
}; };
use typed_builder::TypedBuilder;
type CommentViewTuple = ( type CommentViewTuple = (
Comment, Comment,
@ -156,29 +155,26 @@ impl CommentView {
} }
} }
#[derive(TypedBuilder)] #[derive(Default)]
#[builder(field_defaults(default))] pub struct CommentQuery<'a> {
pub struct CommentQuery<'a, 'b: 'a> { pub listing_type: Option<ListingType>,
#[builder(!default)] pub sort: Option<CommentSortType>,
pool: &'a mut DbPool<'b>, pub community_id: Option<CommunityId>,
listing_type: Option<ListingType>, pub post_id: Option<PostId>,
sort: Option<CommentSortType>, pub parent_path: Option<Ltree>,
community_id: Option<CommunityId>, pub creator_id: Option<PersonId>,
post_id: Option<PostId>, pub local_user: Option<&'a LocalUser>,
parent_path: Option<Ltree>, pub search_term: Option<String>,
creator_id: Option<PersonId>, pub saved_only: Option<bool>,
local_user: Option<&'a LocalUser>, pub show_deleted_and_removed: Option<bool>,
search_term: Option<String>, pub page: Option<i64>,
saved_only: Option<bool>, pub limit: Option<i64>,
show_deleted_and_removed: Option<bool>, pub max_depth: Option<i32>,
page: Option<i64>,
limit: Option<i64>,
max_depth: Option<i32>,
} }
impl<'a, 'b: 'a> CommentQuery<'a, 'b> { impl<'a> CommentQuery<'a> {
pub async fn list(self) -> Result<Vec<CommentView>, Error> { pub async fn list(self, pool: &mut DbPool<'_>) -> Result<Vec<CommentView>, Error> {
let conn = &mut get_conn(self.pool).await?; let conn = &mut get_conn(pool).await?;
// The left join below will return None in this case // The left join below will return None in this case
let person_id_join = self.local_user.map(|l| l.person_id).unwrap_or(PersonId(-1)); let person_id_join = self.local_user.map(|l| l.person_id).unwrap_or(PersonId(-1));
@ -602,29 +598,29 @@ mod tests {
let mut expected_comment_view_with_person = expected_comment_view_no_person.clone(); let mut expected_comment_view_with_person = expected_comment_view_no_person.clone();
expected_comment_view_with_person.my_vote = Some(1); expected_comment_view_with_person.my_vote = Some(1);
let read_comment_views_no_person = CommentQuery::builder() let read_comment_views_no_person = CommentQuery {
.pool(pool) sort: (Some(CommentSortType::Old)),
.sort(Some(CommentSortType::Old)) post_id: (Some(data.inserted_post.id)),
.post_id(Some(data.inserted_post.id)) ..Default::default()
.build() }
.list() .list(pool)
.await .await
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
expected_comment_view_no_person, expected_comment_view_no_person,
read_comment_views_no_person[0] read_comment_views_no_person[0]
); );
let read_comment_views_with_person = CommentQuery::builder() let read_comment_views_with_person = CommentQuery {
.pool(pool) sort: (Some(CommentSortType::Old)),
.sort(Some(CommentSortType::Old)) post_id: (Some(data.inserted_post.id)),
.post_id(Some(data.inserted_post.id)) local_user: (Some(&data.inserted_local_user)),
.local_user(Some(&data.inserted_local_user)) ..Default::default()
.build() }
.list() .list(pool)
.await .await
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
expected_comment_view_with_person, expected_comment_view_with_person,
@ -656,24 +652,24 @@ mod tests {
let data = init_data(pool).await; let data = init_data(pool).await;
let top_path = data.inserted_comment_0.path.clone(); let top_path = data.inserted_comment_0.path.clone();
let read_comment_views_top_path = CommentQuery::builder() let read_comment_views_top_path = CommentQuery {
.pool(pool) post_id: (Some(data.inserted_post.id)),
.post_id(Some(data.inserted_post.id)) parent_path: (Some(top_path)),
.parent_path(Some(top_path)) ..Default::default()
.build() }
.list() .list(pool)
.await .await
.unwrap(); .unwrap();
let child_path = data.inserted_comment_1.path.clone(); let child_path = data.inserted_comment_1.path.clone();
let read_comment_views_child_path = CommentQuery::builder() let read_comment_views_child_path = CommentQuery {
.pool(pool) post_id: (Some(data.inserted_post.id)),
.post_id(Some(data.inserted_post.id)) parent_path: (Some(child_path)),
.parent_path(Some(child_path)) ..Default::default()
.build() }
.list() .list(pool)
.await .await
.unwrap(); .unwrap();
// Make sure the comment parent-limited fetch is correct // Make sure the comment parent-limited fetch is correct
assert_eq!(6, read_comment_views_top_path.len()); assert_eq!(6, read_comment_views_top_path.len());
@ -687,14 +683,14 @@ mod tests {
assert!(child_comments.contains(&data.inserted_comment_1)); assert!(child_comments.contains(&data.inserted_comment_1));
assert!(!child_comments.contains(&data.inserted_comment_2)); assert!(!child_comments.contains(&data.inserted_comment_2));
let read_comment_views_top_max_depth = CommentQuery::builder() let read_comment_views_top_max_depth = CommentQuery {
.pool(pool) post_id: (Some(data.inserted_post.id)),
.post_id(Some(data.inserted_post.id)) max_depth: (Some(1)),
.max_depth(Some(1)) ..Default::default()
.build() }
.list() .list(pool)
.await .await
.unwrap(); .unwrap();
// Make sure a depth limited one only has the top comment // Make sure a depth limited one only has the top comment
assert_eq!( assert_eq!(
@ -704,16 +700,16 @@ mod tests {
assert_eq!(1, read_comment_views_top_max_depth.len()); assert_eq!(1, read_comment_views_top_max_depth.len());
let child_path = data.inserted_comment_1.path.clone(); let child_path = data.inserted_comment_1.path.clone();
let read_comment_views_parent_max_depth = CommentQuery::builder() let read_comment_views_parent_max_depth = CommentQuery {
.pool(pool) post_id: (Some(data.inserted_post.id)),
.post_id(Some(data.inserted_post.id)) parent_path: (Some(child_path)),
.parent_path(Some(child_path)) max_depth: (Some(1)),
.max_depth(Some(1)) sort: (Some(CommentSortType::New)),
.sort(Some(CommentSortType::New)) ..Default::default()
.build() }
.list() .list(pool)
.await .await
.unwrap(); .unwrap();
// Make sure a depth limited one, and given child comment 1, has 3 // Make sure a depth limited one, and given child comment 1, has 3
assert!(read_comment_views_parent_max_depth[2] assert!(read_comment_views_parent_max_depth[2]
@ -734,13 +730,13 @@ mod tests {
// by default, user has all languages enabled and should see all comments // by default, user has all languages enabled and should see all comments
// (except from blocked user) // (except from blocked user)
let all_languages = CommentQuery::builder() let all_languages = CommentQuery {
.pool(pool) local_user: (Some(&data.inserted_local_user)),
.local_user(Some(&data.inserted_local_user)) ..Default::default()
.build() }
.list() .list(pool)
.await .await
.unwrap(); .unwrap();
assert_eq!(5, all_languages.len()); assert_eq!(5, all_languages.len());
// change user lang to finnish, should only show one post in finnish and one undetermined // change user lang to finnish, should only show one post in finnish and one undetermined
@ -751,13 +747,13 @@ mod tests {
LocalUserLanguage::update(pool, vec![finnish_id], data.inserted_local_user.id) LocalUserLanguage::update(pool, vec![finnish_id], data.inserted_local_user.id)
.await .await
.unwrap(); .unwrap();
let finnish_comments = CommentQuery::builder() let finnish_comments = CommentQuery {
.pool(pool) local_user: (Some(&data.inserted_local_user)),
.local_user(Some(&data.inserted_local_user)) ..Default::default()
.build() }
.list() .list(pool)
.await .await
.unwrap(); .unwrap();
assert_eq!(2, finnish_comments.len()); assert_eq!(2, finnish_comments.len());
let finnish_comment = finnish_comments let finnish_comment = finnish_comments
.iter() .iter()
@ -772,13 +768,13 @@ mod tests {
LocalUserLanguage::update(pool, vec![UNDETERMINED_ID], data.inserted_local_user.id) LocalUserLanguage::update(pool, vec![UNDETERMINED_ID], data.inserted_local_user.id)
.await .await
.unwrap(); .unwrap();
let undetermined_comment = CommentQuery::builder() let undetermined_comment = CommentQuery {
.pool(pool) local_user: (Some(&data.inserted_local_user)),
.local_user(Some(&data.inserted_local_user)) ..Default::default()
.build() }
.list() .list(pool)
.await .await
.unwrap(); .unwrap();
assert_eq!(1, undetermined_comment.len()); assert_eq!(1, undetermined_comment.len());
cleanup(data, pool).await; cleanup(data, pool).await;

View File

@ -30,7 +30,6 @@ use lemmy_db_schema::{
traits::JoinView, traits::JoinView,
utils::{get_conn, limit_and_offset, DbPool}, utils::{get_conn, limit_and_offset, DbPool},
}; };
use typed_builder::TypedBuilder;
type PostReportViewTuple = ( type PostReportViewTuple = (
PostReport, PostReport,
@ -159,24 +158,21 @@ impl PostReportView {
} }
} }
#[derive(TypedBuilder)] #[derive(Default)]
#[builder(field_defaults(default))] pub struct PostReportQuery {
pub struct PostReportQuery<'a, 'b: 'a> { pub community_id: Option<CommunityId>,
#[builder(!default)] pub page: Option<i64>,
pool: &'a mut DbPool<'b>, pub limit: Option<i64>,
#[builder(!default)] pub unresolved_only: Option<bool>,
my_person_id: PersonId,
#[builder(!default)]
admin: bool,
community_id: Option<CommunityId>,
page: Option<i64>,
limit: Option<i64>,
unresolved_only: Option<bool>,
} }
impl<'a, 'b: 'a> PostReportQuery<'a, 'b> { impl PostReportQuery {
pub async fn list(self) -> Result<Vec<PostReportView>, Error> { pub async fn list(
let conn = &mut get_conn(self.pool).await?; self,
pool: &mut DbPool<'_>,
my_person: &Person,
) -> Result<Vec<PostReportView>, Error> {
let conn = &mut get_conn(pool).await?;
let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2);
let mut query = post_report::table let mut query = post_report::table
@ -195,7 +191,7 @@ impl<'a, 'b: 'a> PostReportQuery<'a, 'b> {
post_like::table.on( post_like::table.on(
post::id post::id
.eq(post_like::post_id) .eq(post_like::post_id)
.and(post_like::person_id.eq(self.my_person_id)), .and(post_like::person_id.eq(my_person.id)),
), ),
) )
.inner_join(post_aggregates::table.on(post_report::post_id.eq(post_aggregates::post_id))) .inner_join(post_aggregates::table.on(post_report::post_id.eq(post_aggregates::post_id)))
@ -231,13 +227,13 @@ impl<'a, 'b: 'a> PostReportQuery<'a, 'b> {
.offset(offset); .offset(offset);
// If its not an admin, get only the ones you mod // If its not an admin, get only the ones you mod
let res = if !self.admin { let res = if !my_person.admin {
query query
.inner_join( .inner_join(
community_moderator::table.on( community_moderator::table.on(
community_moderator::community_id community_moderator::community_id
.eq(post::community_id) .eq(post::community_id)
.and(community_moderator::person_id.eq(self.my_person_id)), .and(community_moderator::person_id.eq(my_person.id)),
), ),
) )
.load::<PostReportViewTuple>(conn) .load::<PostReportViewTuple>(conn)
@ -506,12 +502,8 @@ mod tests {
}; };
// Do a batch read of timmys reports // Do a batch read of timmys reports
let reports = PostReportQuery::builder() let reports = PostReportQuery::default()
.pool(pool) .list(pool, &inserted_timmy)
.my_person_id(inserted_timmy.id)
.admin(false)
.build()
.list()
.await .await
.unwrap(); .unwrap();
@ -580,15 +572,13 @@ mod tests {
// Do a batch read of timmys reports // Do a batch read of timmys reports
// It should only show saras, which is unresolved // It should only show saras, which is unresolved
let reports_after_resolve = PostReportQuery::builder() let reports_after_resolve = PostReportQuery {
.pool(pool) unresolved_only: (Some(true)),
.my_person_id(inserted_timmy.id) ..Default::default()
.admin(false) }
.unresolved_only(Some(true)) .list(pool, &inserted_timmy)
.build() .await
.list() .unwrap();
.await
.unwrap();
assert_eq!(reports_after_resolve[0], expected_sara_report_view); assert_eq!(reports_after_resolve[0], expected_sara_report_view);
// Make sure the counts are correct // Make sure the counts are correct

View File

@ -45,7 +45,6 @@ use lemmy_db_schema::{
SortType, SortType,
}; };
use tracing::debug; use tracing::debug;
use typed_builder::TypedBuilder;
type PostViewTuple = ( type PostViewTuple = (
Post, Post,
@ -193,28 +192,25 @@ impl PostView {
} }
} }
#[derive(TypedBuilder)] #[derive(Default)]
#[builder(field_defaults(default))] pub struct PostQuery<'a> {
pub struct PostQuery<'a, 'b: 'a> { pub listing_type: Option<ListingType>,
#[builder(!default)] pub sort: Option<SortType>,
pool: &'a mut DbPool<'b>, pub creator_id: Option<PersonId>,
listing_type: Option<ListingType>, pub community_id: Option<CommunityId>,
sort: Option<SortType>, pub local_user: Option<&'a LocalUser>,
creator_id: Option<PersonId>, pub search_term: Option<String>,
community_id: Option<CommunityId>, pub url_search: Option<String>,
local_user: Option<&'a LocalUser>, pub saved_only: Option<bool>,
search_term: Option<String>,
url_search: Option<String>,
saved_only: Option<bool>,
/// Used to show deleted or removed posts for admins /// Used to show deleted or removed posts for admins
is_mod_or_admin: Option<bool>, pub is_mod_or_admin: Option<bool>,
page: Option<i64>, pub page: Option<i64>,
limit: Option<i64>, pub limit: Option<i64>,
} }
impl<'a, 'b: 'a> PostQuery<'a, 'b> { impl<'a> PostQuery<'a> {
pub async fn list(self) -> Result<Vec<PostView>, Error> { pub async fn list(self, pool: &mut DbPool<'_>) -> Result<Vec<PostView>, Error> {
let conn = &mut get_conn(self.pool).await?; let conn = &mut get_conn(pool).await?;
// The left join below will return None in this case // The left join below will return None in this case
let person_id_join = self.local_user.map(|l| l.person_id).unwrap_or(PersonId(-1)); let person_id_join = self.local_user.map(|l| l.person_id).unwrap_or(PersonId(-1));
@ -619,15 +615,15 @@ mod tests {
.await .await
.unwrap(); .unwrap();
let read_post_listing = PostQuery::builder() let read_post_listing = PostQuery {
.pool(pool) sort: (Some(SortType::New)),
.sort(Some(SortType::New)) community_id: (Some(data.inserted_community.id)),
.community_id(Some(data.inserted_community.id)) local_user: (Some(&inserted_local_user)),
.local_user(Some(&inserted_local_user)) ..Default::default()
.build() }
.list() .list(pool)
.await .await
.unwrap(); .unwrap();
let post_listing_single_with_person = PostView::read( let post_listing_single_with_person = PostView::read(
pool, pool,
@ -658,15 +654,15 @@ mod tests {
.await .await
.unwrap(); .unwrap();
let post_listings_with_bots = PostQuery::builder() let post_listings_with_bots = PostQuery {
.pool(pool) sort: (Some(SortType::New)),
.sort(Some(SortType::New)) community_id: (Some(data.inserted_community.id)),
.community_id(Some(data.inserted_community.id)) local_user: (Some(&inserted_local_user)),
.local_user(Some(&inserted_local_user)) ..Default::default()
.build() }
.list() .list(pool)
.await .await
.unwrap(); .unwrap();
// should include bot post which has "undetermined" language // should include bot post which has "undetermined" language
assert_eq!(2, post_listings_with_bots.len()); assert_eq!(2, post_listings_with_bots.len());
@ -680,14 +676,14 @@ mod tests {
let pool = &mut pool.into(); let pool = &mut pool.into();
let data = init_data(pool).await; let data = init_data(pool).await;
let read_post_listing_multiple_no_person = PostQuery::builder() let read_post_listing_multiple_no_person = PostQuery {
.pool(pool) sort: (Some(SortType::New)),
.sort(Some(SortType::New)) community_id: (Some(data.inserted_community.id)),
.community_id(Some(data.inserted_community.id)) ..Default::default()
.build() }
.list() .list(pool)
.await .await
.unwrap(); .unwrap();
let read_post_listing_single_no_person = let read_post_listing_single_no_person =
PostView::read(pool, data.inserted_post.id, None, None) PostView::read(pool, data.inserted_post.id, None, None)
@ -724,15 +720,15 @@ mod tests {
}; };
CommunityBlock::block(pool, &community_block).await.unwrap(); CommunityBlock::block(pool, &community_block).await.unwrap();
let read_post_listings_with_person_after_block = PostQuery::builder() let read_post_listings_with_person_after_block = PostQuery {
.pool(pool) sort: (Some(SortType::New)),
.sort(Some(SortType::New)) community_id: (Some(data.inserted_community.id)),
.community_id(Some(data.inserted_community.id)) local_user: (Some(&data.inserted_local_user)),
.local_user(Some(&data.inserted_local_user)) ..Default::default()
.build() }
.list() .list(pool)
.await .await
.unwrap(); .unwrap();
// Should be 0 posts after the community block // Should be 0 posts after the community block
assert_eq!(0, read_post_listings_with_person_after_block.len()); assert_eq!(0, read_post_listings_with_person_after_block.len());
@ -789,15 +785,15 @@ mod tests {
.await .await
.unwrap(); .unwrap();
let read_post_listing = PostQuery::builder() let read_post_listing = PostQuery {
.pool(pool) sort: (Some(SortType::New)),
.sort(Some(SortType::New)) community_id: (Some(data.inserted_community.id)),
.community_id(Some(data.inserted_community.id)) local_user: (Some(&inserted_local_user)),
.local_user(Some(&inserted_local_user)) ..Default::default()
.build() }
.list() .list(pool)
.await .await
.unwrap(); .unwrap();
assert_eq!(1, read_post_listing.len()); assert_eq!(1, read_post_listing.len());
assert_eq!(expected_post_with_upvote, read_post_listing[0]); assert_eq!(expected_post_with_upvote, read_post_listing[0]);
@ -829,14 +825,14 @@ mod tests {
Post::create(pool, &post_spanish).await.unwrap(); Post::create(pool, &post_spanish).await.unwrap();
let post_listings_all = PostQuery::builder() let post_listings_all = PostQuery {
.pool(pool) sort: (Some(SortType::New)),
.sort(Some(SortType::New)) local_user: (Some(&data.inserted_local_user)),
.local_user(Some(&data.inserted_local_user)) ..Default::default()
.build() }
.list() .list(pool)
.await .await
.unwrap(); .unwrap();
// no language filters specified, all posts should be returned // no language filters specified, all posts should be returned
assert_eq!(3, post_listings_all.len()); assert_eq!(3, post_listings_all.len());
@ -849,14 +845,14 @@ mod tests {
.await .await
.unwrap(); .unwrap();
let post_listing_french = PostQuery::builder() let post_listing_french = PostQuery {
.pool(pool) sort: (Some(SortType::New)),
.sort(Some(SortType::New)) local_user: (Some(&data.inserted_local_user)),
.local_user(Some(&data.inserted_local_user)) ..Default::default()
.build() }
.list() .list(pool)
.await .await
.unwrap(); .unwrap();
// only one post in french and one undetermined should be returned // only one post in french and one undetermined should be returned
assert_eq!(2, post_listing_french.len()); assert_eq!(2, post_listing_french.len());
@ -871,14 +867,14 @@ mod tests {
) )
.await .await
.unwrap(); .unwrap();
let post_listings_french_und = PostQuery::builder() let post_listings_french_und = PostQuery {
.pool(pool) sort: (Some(SortType::New)),
.sort(Some(SortType::New)) local_user: (Some(&data.inserted_local_user)),
.local_user(Some(&data.inserted_local_user)) ..Default::default()
.build() }
.list() .list(pool)
.await .await
.unwrap(); .unwrap();
// french post and undetermined language post should be returned // french post and undetermined language post should be returned
assert_eq!(2, post_listings_french_und.len()); assert_eq!(2, post_listings_french_und.len());
@ -908,28 +904,28 @@ mod tests {
.unwrap(); .unwrap();
// Make sure you don't see the deleted post in the results // Make sure you don't see the deleted post in the results
let post_listings_no_admin = PostQuery::builder() let post_listings_no_admin = PostQuery {
.pool(pool) sort: (Some(SortType::New)),
.sort(Some(SortType::New)) local_user: (Some(&data.inserted_local_user)),
.local_user(Some(&data.inserted_local_user)) is_mod_or_admin: (Some(false)),
.is_mod_or_admin(Some(false)) ..Default::default()
.build() }
.list() .list(pool)
.await .await
.unwrap(); .unwrap();
assert_eq!(1, post_listings_no_admin.len()); assert_eq!(1, post_listings_no_admin.len());
// Make sure they see both // Make sure they see both
let post_listings_is_admin = PostQuery::builder() let post_listings_is_admin = PostQuery {
.pool(pool) sort: (Some(SortType::New)),
.sort(Some(SortType::New)) local_user: (Some(&data.inserted_local_user)),
.local_user(Some(&data.inserted_local_user)) is_mod_or_admin: (Some(true)),
.is_mod_or_admin(Some(true)) ..Default::default()
.build() }
.list() .list(pool)
.await .await
.unwrap(); .unwrap();
assert_eq!(2, post_listings_is_admin.len()); assert_eq!(2, post_listings_is_admin.len());

View File

@ -12,7 +12,6 @@ use lemmy_db_schema::{
traits::JoinView, traits::JoinView,
utils::{get_conn, limit_and_offset, DbPool}, utils::{get_conn, limit_and_offset, DbPool},
}; };
use typed_builder::TypedBuilder;
type PrivateMessageReportViewTuple = ( type PrivateMessageReportViewTuple = (
PrivateMessageReport, PrivateMessageReport,
@ -81,19 +80,16 @@ impl PrivateMessageReportView {
} }
} }
#[derive(TypedBuilder)] #[derive(Default)]
#[builder(field_defaults(default))] pub struct PrivateMessageReportQuery {
pub struct PrivateMessageReportQuery<'a, 'b: 'a> { pub page: Option<i64>,
#[builder(!default)] pub limit: Option<i64>,
pool: &'a mut DbPool<'b>, pub unresolved_only: Option<bool>,
page: Option<i64>,
limit: Option<i64>,
unresolved_only: Option<bool>,
} }
impl<'a, 'b: 'a> PrivateMessageReportQuery<'a, 'b> { impl PrivateMessageReportQuery {
pub async fn list(self) -> Result<Vec<PrivateMessageReportView>, Error> { pub async fn list(self, pool: &mut DbPool<'_>) -> Result<Vec<PrivateMessageReportView>, Error> {
let conn = &mut get_conn(self.pool).await?; let conn = &mut get_conn(pool).await?;
let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2);
let mut query = private_message_report::table let mut query = private_message_report::table
@ -208,10 +204,8 @@ mod tests {
.await .await
.unwrap(); .unwrap();
let reports = PrivateMessageReportQuery::builder() let reports = PrivateMessageReportQuery::default()
.pool(pool) .list(pool)
.build()
.list()
.await .await
.unwrap(); .unwrap();
assert_eq!(1, reports.len()); assert_eq!(1, reports.len());
@ -233,13 +227,13 @@ mod tests {
.await .await
.unwrap(); .unwrap();
let reports = PrivateMessageReportQuery::builder() let reports = PrivateMessageReportQuery {
.pool(pool) unresolved_only: (Some(false)),
.unresolved_only(Some(false)) ..Default::default()
.build() }
.list() .list(pool)
.await .await
.unwrap(); .unwrap();
assert_eq!(1, reports.len()); assert_eq!(1, reports.len());
assert!(reports[0].private_message_report.resolved); assert!(reports[0].private_message_report.resolved);
assert!(reports[0].resolver.is_some()); assert!(reports[0].resolver.is_some());

View File

@ -17,7 +17,6 @@ use lemmy_db_schema::{
utils::{get_conn, limit_and_offset, DbPool}, utils::{get_conn, limit_and_offset, DbPool},
}; };
use tracing::debug; use tracing::debug;
use typed_builder::TypedBuilder;
type PrivateMessageViewTuple = (PrivateMessage, Person, Person); type PrivateMessageViewTuple = (PrivateMessage, Person, Person);
@ -68,21 +67,20 @@ impl PrivateMessageView {
} }
} }
#[derive(TypedBuilder)] #[derive(Default)]
#[builder(field_defaults(default))] pub struct PrivateMessageQuery {
pub struct PrivateMessageQuery<'a, 'b: 'a> { pub unread_only: Option<bool>,
#[builder(!default)] pub page: Option<i64>,
pool: &'a mut DbPool<'b>, pub limit: Option<i64>,
#[builder(!default)]
recipient_id: PersonId,
unread_only: Option<bool>,
page: Option<i64>,
limit: Option<i64>,
} }
impl<'a, 'b: 'a> PrivateMessageQuery<'a, 'b> { impl PrivateMessageQuery {
pub async fn list(self) -> Result<Vec<PrivateMessageView>, Error> { pub async fn list(
let conn = &mut get_conn(self.pool).await?; self,
pool: &mut DbPool<'_>,
recipient_id: PersonId,
) -> Result<Vec<PrivateMessageView>, Error> {
let conn = &mut get_conn(pool).await?;
let person_alias_1 = diesel::alias!(person as person1); let person_alias_1 = diesel::alias!(person as person1);
let mut query = private_message::table let mut query = private_message::table
@ -101,14 +99,14 @@ impl<'a, 'b: 'a> PrivateMessageQuery<'a, 'b> {
if self.unread_only.unwrap_or(false) { if self.unread_only.unwrap_or(false) {
query = query query = query
.filter(private_message::read.eq(false)) .filter(private_message::read.eq(false))
.filter(private_message::recipient_id.eq(self.recipient_id)); .filter(private_message::recipient_id.eq(recipient_id));
} }
// Otherwise, I want the ALL view to show both sent and received // Otherwise, I want the ALL view to show both sent and received
else { else {
query = query.filter( query = query.filter(
private_message::recipient_id private_message::recipient_id
.eq(self.recipient_id) .eq(recipient_id)
.or(private_message::creator_id.eq(self.recipient_id)), .or(private_message::creator_id.eq(recipient_id)),
) )
} }

View File

@ -18,7 +18,6 @@ use lemmy_db_schema::{
traits::JoinView, traits::JoinView,
utils::{get_conn, limit_and_offset, DbPool}, utils::{get_conn, limit_and_offset, DbPool},
}; };
use typed_builder::TypedBuilder;
type RegistrationApplicationViewTuple = type RegistrationApplicationViewTuple =
(RegistrationApplication, LocalUser, Person, Option<Person>); (RegistrationApplication, LocalUser, Person, Option<Person>);
@ -89,20 +88,20 @@ impl RegistrationApplicationView {
} }
} }
#[derive(TypedBuilder)] #[derive(Default)]
#[builder(field_defaults(default))] pub struct RegistrationApplicationQuery {
pub struct RegistrationApplicationQuery<'a, 'b: 'a> { pub unread_only: Option<bool>,
#[builder(!default)] pub verified_email_only: Option<bool>,
pool: &'a mut DbPool<'b>, pub page: Option<i64>,
unread_only: Option<bool>, pub limit: Option<i64>,
verified_email_only: Option<bool>,
page: Option<i64>,
limit: Option<i64>,
} }
impl<'a, 'b: 'a> RegistrationApplicationQuery<'a, 'b> { impl RegistrationApplicationQuery {
pub async fn list(self) -> Result<Vec<RegistrationApplicationView>, Error> { pub async fn list(
let conn = &mut get_conn(self.pool).await?; self,
pool: &mut DbPool<'_>,
) -> Result<Vec<RegistrationApplicationView>, Error> {
let conn = &mut get_conn(pool).await?;
let person_alias_1 = diesel::alias!(person as person1); let person_alias_1 = diesel::alias!(person as person1);
let mut query = registration_application::table let mut query = registration_application::table
@ -327,13 +326,13 @@ mod tests {
assert_eq!(read_sara_app_view, expected_sara_app_view); assert_eq!(read_sara_app_view, expected_sara_app_view);
// Do a batch read of the applications // Do a batch read of the applications
let apps = RegistrationApplicationQuery::builder() let apps = RegistrationApplicationQuery {
.pool(pool) unread_only: (Some(true)),
.unread_only(Some(true)) ..Default::default()
.build() }
.list() .list(pool)
.await .await
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
apps, apps,
@ -403,13 +402,13 @@ mod tests {
// Do a batch read of apps again // Do a batch read of apps again
// It should show only jessicas which is unresolved // It should show only jessicas which is unresolved
let apps_after_resolve = RegistrationApplicationQuery::builder() let apps_after_resolve = RegistrationApplicationQuery {
.pool(pool) unread_only: (Some(true)),
.unread_only(Some(true)) ..Default::default()
.build() }
.list() .list(pool)
.await .await
.unwrap(); .unwrap();
assert_eq!(apps_after_resolve, vec![read_jess_app_view]); assert_eq!(apps_after_resolve, vec![read_jess_app_view]);
// Make sure the counts are correct // Make sure the counts are correct
@ -419,10 +418,8 @@ mod tests {
assert_eq!(unread_count_after_approve, 1); assert_eq!(unread_count_after_approve, 1);
// Make sure the not undenied_only has all the apps // Make sure the not undenied_only has all the apps
let all_apps = RegistrationApplicationQuery::builder() let all_apps = RegistrationApplicationQuery::default()
.pool(pool) .list(pool)
.build()
.list()
.await .await
.unwrap(); .unwrap();
assert_eq!(all_apps.len(), 2); assert_eq!(all_apps.len(), 2);

View File

@ -27,5 +27,4 @@ diesel-async = { workspace = true, features = [
], optional = true } ], optional = true }
serde = { workspace = true } serde = { workspace = true }
serde_with = { workspace = true } serde_with = { workspace = true }
typed-builder = { workspace = true }
ts-rs = { workspace = true, optional = true } ts-rs = { workspace = true, optional = true }

View File

@ -36,7 +36,6 @@ use lemmy_db_schema::{
utils::{get_conn, limit_and_offset, DbPool}, utils::{get_conn, limit_and_offset, DbPool},
CommentSortType, CommentSortType,
}; };
use typed_builder::TypedBuilder;
type CommentReplyViewTuple = ( type CommentReplyViewTuple = (
CommentReply, CommentReply,
@ -175,23 +174,20 @@ impl CommentReplyView {
} }
} }
#[derive(TypedBuilder)] #[derive(Default)]
#[builder(field_defaults(default))] pub struct CommentReplyQuery {
pub struct CommentReplyQuery<'a, 'b: 'a> { pub my_person_id: Option<PersonId>,
#[builder(!default)] pub recipient_id: Option<PersonId>,
pool: &'a mut DbPool<'b>, pub sort: Option<CommentSortType>,
my_person_id: Option<PersonId>, pub unread_only: Option<bool>,
recipient_id: Option<PersonId>, pub show_bot_accounts: Option<bool>,
sort: Option<CommentSortType>, pub page: Option<i64>,
unread_only: Option<bool>, pub limit: Option<i64>,
show_bot_accounts: Option<bool>,
page: Option<i64>,
limit: Option<i64>,
} }
impl<'a, 'b: 'a> CommentReplyQuery<'a, 'b> { impl CommentReplyQuery {
pub async fn list(self) -> Result<Vec<CommentReplyView>, Error> { pub async fn list(self, pool: &mut DbPool<'_>) -> Result<Vec<CommentReplyView>, Error> {
let conn = &mut get_conn(self.pool).await?; let conn = &mut get_conn(pool).await?;
let person_alias_1 = diesel::alias!(person as person1); let person_alias_1 = diesel::alias!(person as person1);

View File

@ -23,7 +23,6 @@ use lemmy_db_schema::{
ListingType, ListingType,
SortType, SortType,
}; };
use typed_builder::TypedBuilder;
type CommunityViewTuple = ( type CommunityViewTuple = (
Community, Community,
@ -100,26 +99,23 @@ impl CommunityView {
} }
} }
#[derive(TypedBuilder)] #[derive(Default)]
#[builder(field_defaults(default))] pub struct CommunityQuery<'a> {
pub struct CommunityQuery<'a, 'b: 'a> { pub listing_type: Option<ListingType>,
#[builder(!default)] pub sort: Option<SortType>,
pool: &'a mut DbPool<'b>, pub local_user: Option<&'a LocalUser>,
listing_type: Option<ListingType>, pub search_term: Option<String>,
sort: Option<SortType>, pub is_mod_or_admin: Option<bool>,
local_user: Option<&'a LocalUser>, pub show_nsfw: Option<bool>,
search_term: Option<String>, pub page: Option<i64>,
is_mod_or_admin: Option<bool>, pub limit: Option<i64>,
show_nsfw: Option<bool>,
page: Option<i64>,
limit: Option<i64>,
} }
impl<'a, 'b: 'a> CommunityQuery<'a, 'b> { impl<'a> CommunityQuery<'a> {
pub async fn list(self) -> Result<Vec<CommunityView>, Error> { pub async fn list(self, pool: &mut DbPool<'_>) -> Result<Vec<CommunityView>, Error> {
use SortType::*; use SortType::*;
let conn = &mut get_conn(self.pool).await?; let conn = &mut get_conn(pool).await?;
// The left join below will return None in this case // The left join below will return None in this case
let person_id_join = self.local_user.map(|l| l.person_id).unwrap_or(PersonId(-1)); let person_id_join = self.local_user.map(|l| l.person_id).unwrap_or(PersonId(-1));

View File

@ -37,7 +37,6 @@ use lemmy_db_schema::{
utils::{get_conn, limit_and_offset, DbPool}, utils::{get_conn, limit_and_offset, DbPool},
CommentSortType, CommentSortType,
}; };
use typed_builder::TypedBuilder;
type PersonMentionViewTuple = ( type PersonMentionViewTuple = (
PersonMention, PersonMention,
@ -175,23 +174,20 @@ impl PersonMentionView {
} }
} }
#[derive(TypedBuilder)] #[derive(Default)]
#[builder(field_defaults(default))] pub struct PersonMentionQuery {
pub struct PersonMentionQuery<'a, 'b: 'a> { pub my_person_id: Option<PersonId>,
#[builder(!default)] pub recipient_id: Option<PersonId>,
pool: &'a mut DbPool<'b>, pub sort: Option<CommentSortType>,
my_person_id: Option<PersonId>, pub unread_only: Option<bool>,
recipient_id: Option<PersonId>, pub show_bot_accounts: Option<bool>,
sort: Option<CommentSortType>, pub page: Option<i64>,
unread_only: Option<bool>, pub limit: Option<i64>,
show_bot_accounts: Option<bool>,
page: Option<i64>,
limit: Option<i64>,
} }
impl<'a, 'b: 'a> PersonMentionQuery<'a, 'b> { impl PersonMentionQuery {
pub async fn list(self) -> Result<Vec<PersonMentionView>, Error> { pub async fn list(self, pool: &mut DbPool<'_>) -> Result<Vec<PersonMentionView>, Error> {
let conn = &mut get_conn(self.pool).await?; let conn = &mut get_conn(pool).await?;
let person_alias_1 = diesel::alias!(person as person1); let person_alias_1 = diesel::alias!(person as person1);

View File

@ -19,7 +19,6 @@ use lemmy_db_schema::{
SortType, SortType,
}; };
use std::iter::Iterator; use std::iter::Iterator;
use typed_builder::TypedBuilder;
type PersonViewTuple = (Person, PersonAggregates); type PersonViewTuple = (Person, PersonAggregates);
@ -79,20 +78,17 @@ impl PersonView {
} }
} }
#[derive(TypedBuilder)] #[derive(Default)]
#[builder(field_defaults(default))] pub struct PersonQuery {
pub struct PersonQuery<'a, 'b: 'a> { pub sort: Option<SortType>,
#[builder(!default)] pub search_term: Option<String>,
pool: &'a mut DbPool<'b>, pub page: Option<i64>,
sort: Option<SortType>, pub limit: Option<i64>,
search_term: Option<String>,
page: Option<i64>,
limit: Option<i64>,
} }
impl<'a, 'b: 'a> PersonQuery<'a, 'b> { impl PersonQuery {
pub async fn list(self) -> Result<Vec<PersonView>, Error> { pub async fn list(self, pool: &mut DbPool<'_>) -> Result<Vec<PersonView>, Error> {
let conn = &mut get_conn(self.pool).await?; let conn = &mut get_conn(pool).await?;
let mut query = person::table let mut query = person::table
.inner_join(person_aggregates::table) .inner_join(person_aggregates::table)
.select((person::all_columns, person_aggregates::all_columns)) .select((person::all_columns, person_aggregates::all_columns))

View File

@ -124,15 +124,15 @@ async fn get_feed_data(
) -> Result<HttpResponse, LemmyError> { ) -> Result<HttpResponse, LemmyError> {
let site_view = SiteView::read_local(&mut context.pool()).await?; let site_view = SiteView::read_local(&mut context.pool()).await?;
let posts = PostQuery::builder() let posts = PostQuery {
.pool(&mut context.pool()) listing_type: (Some(listing_type)),
.listing_type(Some(listing_type)) sort: (Some(sort_type)),
.sort(Some(sort_type)) limit: (Some(limit)),
.limit(Some(limit)) page: (Some(page)),
.page(Some(page)) ..Default::default()
.build() }
.list() .list(&mut context.pool())
.await?; .await?;
let items = create_post_items(posts, &context.settings().get_protocol_and_hostname())?; let items = create_post_items(posts, &context.settings().get_protocol_and_hostname())?;
@ -243,16 +243,16 @@ async fn get_feed_user(
let site_view = SiteView::read_local(pool).await?; let site_view = SiteView::read_local(pool).await?;
let person = Person::read_from_name(pool, user_name, false).await?; let person = Person::read_from_name(pool, user_name, false).await?;
let posts = PostQuery::builder() let posts = PostQuery {
.pool(pool) listing_type: (Some(ListingType::All)),
.listing_type(Some(ListingType::All)) sort: (Some(*sort_type)),
.sort(Some(*sort_type)) creator_id: (Some(person.id)),
.creator_id(Some(person.id)) limit: (Some(*limit)),
.limit(Some(*limit)) page: (Some(*page)),
.page(Some(*page)) ..Default::default()
.build() }
.list() .list(pool)
.await?; .await?;
let items = create_post_items(posts, protocol_and_hostname)?; let items = create_post_items(posts, protocol_and_hostname)?;
@ -278,15 +278,15 @@ async fn get_feed_community(
let site_view = SiteView::read_local(pool).await?; let site_view = SiteView::read_local(pool).await?;
let community = Community::read_from_name(pool, community_name, false).await?; let community = Community::read_from_name(pool, community_name, false).await?;
let posts = PostQuery::builder() let posts = PostQuery {
.pool(pool) sort: (Some(*sort_type)),
.sort(Some(*sort_type)) community_id: (Some(community.id)),
.community_id(Some(community.id)) limit: (Some(*limit)),
.limit(Some(*limit)) page: (Some(*page)),
.page(Some(*page)) ..Default::default()
.build() }
.list() .list(pool)
.await?; .await?;
let items = create_post_items(posts, protocol_and_hostname)?; let items = create_post_items(posts, protocol_and_hostname)?;
@ -318,16 +318,16 @@ async fn get_feed_front(
let local_user_id = LocalUserId(Claims::decode(jwt, jwt_secret)?.claims.sub); let local_user_id = LocalUserId(Claims::decode(jwt, jwt_secret)?.claims.sub);
let local_user = LocalUser::read(pool, local_user_id).await?; let local_user = LocalUser::read(pool, local_user_id).await?;
let posts = PostQuery::builder() let posts = PostQuery {
.pool(pool) listing_type: (Some(ListingType::Subscribed)),
.listing_type(Some(ListingType::Subscribed)) local_user: (Some(&local_user)),
.local_user(Some(&local_user)) sort: (Some(*sort_type)),
.sort(Some(*sort_type)) limit: (Some(*limit)),
.limit(Some(*limit)) page: (Some(*page)),
.page(Some(*page)) ..Default::default()
.build() }
.list() .list(pool)
.await?; .await?;
let items = create_post_items(posts, protocol_and_hostname)?; let items = create_post_items(posts, protocol_and_hostname)?;
@ -360,27 +360,27 @@ async fn get_feed_inbox(
let sort = CommentSortType::New; let sort = CommentSortType::New;
let replies = CommentReplyQuery::builder() let replies = CommentReplyQuery {
.pool(pool) recipient_id: (Some(person_id)),
.recipient_id(Some(person_id)) my_person_id: (Some(person_id)),
.my_person_id(Some(person_id)) show_bot_accounts: (Some(show_bot_accounts)),
.show_bot_accounts(Some(show_bot_accounts)) sort: (Some(sort)),
.sort(Some(sort)) limit: (Some(RSS_FETCH_LIMIT)),
.limit(Some(RSS_FETCH_LIMIT)) ..Default::default()
.build() }
.list() .list(pool)
.await?; .await?;
let mentions = PersonMentionQuery::builder() let mentions = PersonMentionQuery {
.pool(pool) recipient_id: (Some(person_id)),
.recipient_id(Some(person_id)) my_person_id: (Some(person_id)),
.my_person_id(Some(person_id)) show_bot_accounts: (Some(show_bot_accounts)),
.show_bot_accounts(Some(show_bot_accounts)) sort: (Some(sort)),
.sort(Some(sort)) limit: (Some(RSS_FETCH_LIMIT)),
.limit(Some(RSS_FETCH_LIMIT)) ..Default::default()
.build() }
.list() .list(pool)
.await?; .await?;
let items = create_reply_and_mention_items(replies, mentions, protocol_and_hostname)?; let items = create_reply_and_mention_items(replies, mentions, protocol_and_hostname)?;