From: dullbananas Date: Mon, 17 Jul 2023 10:20:25 +0000 (-0700) Subject: Remove TypedBuilder from db_views and db_views_actor (#3637) X-Git-Url: http://these/git/readmes/static/git-logo.png?a=commitdiff_plain;h=88215bfbc98b70595035426c06cfe67f4a9ab0ab;p=lemmy.git 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 --- diff --git a/Cargo.lock b/Cargo.lock index 3396e1e5..407cb9aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2769,7 +2769,6 @@ dependencies = [ "tokio", "tracing", "ts-rs", - "typed-builder", ] [[package]] @@ -2782,7 +2781,6 @@ dependencies = [ "serde", "serde_with", "ts-rs", - "typed-builder", ] [[package]] diff --git a/crates/api/src/comment_report/list.rs b/crates/api/src/comment_report/list.rs index baa1bf45..b67ec333 100644 --- a/crates/api/src/comment_report/list.rs +++ b/crates/api/src/comment_report/list.rs @@ -22,24 +22,19 @@ impl Perform for ListCommentReports { let data: &ListCommentReports = self; 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 unresolved_only = data.unresolved_only; let page = data.page; let limit = data.limit; - let comment_reports = CommentReportQuery::builder() - .pool(&mut context.pool()) - .my_person_id(person_id) - .admin(admin) - .community_id(community_id) - .unresolved_only(unresolved_only) - .page(page) - .limit(limit) - .build() - .list() - .await?; + let comment_reports = CommentReportQuery { + community_id, + unresolved_only, + page, + limit, + } + .list(&mut context.pool(), &local_user_view.person) + .await?; Ok(ListCommentReportsResponse { comment_reports }) } diff --git a/crates/api/src/local_user/notifications/list_mentions.rs b/crates/api/src/local_user/notifications/list_mentions.rs index 1b2d3c7b..10d24ff8 100644 --- a/crates/api/src/local_user/notifications/list_mentions.rs +++ b/crates/api/src/local_user/notifications/list_mentions.rs @@ -27,18 +27,17 @@ impl Perform for GetPersonMentions { let person_id = Some(local_user_view.person.id); let show_bot_accounts = Some(local_user_view.local_user.show_bot_accounts); - let mentions = PersonMentionQuery::builder() - .pool(&mut context.pool()) - .recipient_id(person_id) - .my_person_id(person_id) - .sort(sort) - .unread_only(unread_only) - .show_bot_accounts(show_bot_accounts) - .page(page) - .limit(limit) - .build() - .list() - .await?; + let mentions = PersonMentionQuery { + recipient_id: person_id, + my_person_id: person_id, + sort, + unread_only, + show_bot_accounts, + page, + limit, + } + .list(&mut context.pool()) + .await?; Ok(GetPersonMentionsResponse { mentions }) } diff --git a/crates/api/src/local_user/notifications/list_replies.rs b/crates/api/src/local_user/notifications/list_replies.rs index 79b0fe22..8c7f3059 100644 --- a/crates/api/src/local_user/notifications/list_replies.rs +++ b/crates/api/src/local_user/notifications/list_replies.rs @@ -24,18 +24,17 @@ impl Perform for GetReplies { let person_id = Some(local_user_view.person.id); let show_bot_accounts = Some(local_user_view.local_user.show_bot_accounts); - let replies = CommentReplyQuery::builder() - .pool(&mut context.pool()) - .recipient_id(person_id) - .my_person_id(person_id) - .sort(sort) - .unread_only(unread_only) - .show_bot_accounts(show_bot_accounts) - .page(page) - .limit(limit) - .build() - .list() - .await?; + let replies = CommentReplyQuery { + recipient_id: person_id, + my_person_id: person_id, + sort, + unread_only, + show_bot_accounts, + page, + limit, + } + .list(&mut context.pool()) + .await?; Ok(GetRepliesResponse { replies }) } diff --git a/crates/api/src/post_report/list.rs b/crates/api/src/post_report/list.rs index f7496e1a..a0d90968 100644 --- a/crates/api/src/post_report/list.rs +++ b/crates/api/src/post_report/list.rs @@ -22,24 +22,19 @@ impl Perform for ListPostReports { let data: &ListPostReports = self; 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 unresolved_only = data.unresolved_only; let page = data.page; let limit = data.limit; - let post_reports = PostReportQuery::builder() - .pool(&mut context.pool()) - .my_person_id(person_id) - .admin(admin) - .community_id(community_id) - .unresolved_only(unresolved_only) - .page(page) - .limit(limit) - .build() - .list() - .await?; + let post_reports = PostReportQuery { + community_id, + unresolved_only, + page, + limit, + } + .list(&mut context.pool(), &local_user_view.person) + .await?; Ok(ListPostReportsResponse { post_reports }) } diff --git a/crates/api/src/private_message_report/list.rs b/crates/api/src/private_message_report/list.rs index 72d182d4..8b4e5076 100644 --- a/crates/api/src/private_message_report/list.rs +++ b/crates/api/src/private_message_report/list.rs @@ -21,14 +21,13 @@ impl Perform for ListPrivateMessageReports { let unresolved_only = self.unresolved_only; let page = self.page; let limit = self.limit; - let private_message_reports = PrivateMessageReportQuery::builder() - .pool(&mut context.pool()) - .unresolved_only(unresolved_only) - .page(page) - .limit(limit) - .build() - .list() - .await?; + let private_message_reports = PrivateMessageReportQuery { + unresolved_only, + page, + limit, + } + .list(&mut context.pool()) + .await?; Ok(ListPrivateMessageReportsResponse { private_message_reports, diff --git a/crates/api/src/site/registration_applications/list.rs b/crates/api/src/site/registration_applications/list.rs index 2389ad40..433cee42 100644 --- a/crates/api/src/site/registration_applications/list.rs +++ b/crates/api/src/site/registration_applications/list.rs @@ -23,19 +23,18 @@ impl Perform for ListRegistrationApplications { is_admin(&local_user_view)?; 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 limit = data.limit; - let registration_applications = RegistrationApplicationQuery::builder() - .pool(&mut context.pool()) - .unread_only(unread_only) - .verified_email_only(Some(verified_email_only)) - .page(page) - .limit(limit) - .build() - .list() - .await?; + let registration_applications = RegistrationApplicationQuery { + unread_only, + verified_email_only, + page, + limit, + } + .list(&mut context.pool()) + .await?; Ok(Self::Response { registration_applications, diff --git a/crates/api_common/src/utils.rs b/crates/api_common/src/utils.rs index bd194c0f..97afeaf0 100644 --- a/crates/api_common/src/utils.rs +++ b/crates/api_common/src/utils.rs @@ -667,13 +667,13 @@ pub async fn remove_user_data_in_community( // Comments // TODO Diesel doesn't allow updates with joins, so this has to be a loop - let comments = CommentQuery::builder() - .pool(pool) - .creator_id(Some(banned_person_id)) - .community_id(Some(community_id)) - .build() - .list() - .await?; + let comments = CommentQuery { + creator_id: Some(banned_person_id), + community_id: Some(community_id), + ..Default::default() + } + .list(pool) + .await?; for comment_view in &comments { let comment_id = comment_view.comment.id; diff --git a/crates/api_crud/src/community/list.rs b/crates/api_crud/src/community/list.rs index 80ad6129..bd818995 100644 --- a/crates/api_crud/src/community/list.rs +++ b/crates/api_crud/src/community/list.rs @@ -31,18 +31,18 @@ impl PerformCrud for ListCommunities { let page = data.page; let limit = data.limit; let local_user = local_user_view.map(|l| l.local_user); - let communities = CommunityQuery::builder() - .pool(&mut context.pool()) - .listing_type(listing_type) - .show_nsfw(show_nsfw) - .sort(sort) - .local_user(local_user.as_ref()) - .page(page) - .limit(limit) - .is_mod_or_admin(is_admin) - .build() - .list() - .await?; + let communities = CommunityQuery { + listing_type, + show_nsfw, + sort, + local_user: local_user.as_ref(), + page, + limit, + is_mod_or_admin: is_admin, + ..Default::default() + } + .list(&mut context.pool()) + .await?; // Return the jwt Ok(ListCommunitiesResponse { communities }) diff --git a/crates/api_crud/src/post/read.rs b/crates/api_crud/src/post/read.rs index af19402e..e668517d 100644 --- a/crates/api_crud/src/post/read.rs +++ b/crates/api_crud/src/post/read.rs @@ -100,12 +100,12 @@ impl PerformCrud for GetPost { // Fetch the cross_posts let cross_posts = if let Some(url) = &post_view.post.url { - let mut x_posts = PostQuery::builder() - .pool(&mut context.pool()) - .url_search(Some(url.inner().as_str().into())) - .build() - .list() - .await?; + let mut x_posts = PostQuery { + url_search: Some(url.inner().as_str().into()), + ..Default::default() + } + .list(&mut context.pool()) + .await?; // Don't return this post as one of the cross_posts x_posts.retain(|x| x.post.id != post_id); diff --git a/crates/api_crud/src/private_message/read.rs b/crates/api_crud/src/private_message/read.rs index 73ce034e..87d8ee66 100644 --- a/crates/api_crud/src/private_message/read.rs +++ b/crates/api_crud/src/private_message/read.rs @@ -24,15 +24,13 @@ impl PerformCrud for GetPrivateMessages { let page = data.page; let limit = data.limit; let unread_only = data.unread_only; - let mut messages = PrivateMessageQuery::builder() - .pool(&mut context.pool()) - .recipient_id(person_id) - .page(page) - .limit(limit) - .unread_only(unread_only) - .build() - .list() - .await?; + let mut messages = PrivateMessageQuery { + page, + limit, + unread_only, + } + .list(&mut context.pool(), person_id) + .await?; // 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. diff --git a/crates/apub/src/api/list_comments.rs b/crates/apub/src/api/list_comments.rs index e3b7d065..b6487122 100644 --- a/crates/apub/src/api/list_comments.rs +++ b/crates/apub/src/api/list_comments.rs @@ -39,7 +39,11 @@ pub async fn list_comments( let limit = data.limit; 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 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 post_id = data.post_id; let local_user = local_user_view.map(|l| l.local_user); - let comments = CommentQuery::builder() - .pool(&mut context.pool()) - .listing_type(Some(listing_type)) - .sort(sort) - .max_depth(max_depth) - .saved_only(saved_only) - .community_id(community_id) - .parent_path(parent_path_cloned) - .post_id(post_id) - .local_user(local_user.as_ref()) - .page(page) - .limit(limit) - .build() - .list() - .await - .with_lemmy_type(LemmyErrorType::CouldntGetComments)?; + let comments = CommentQuery { + listing_type, + sort, + max_depth, + saved_only, + community_id, + parent_path: parent_path_cloned, + post_id, + local_user: local_user.as_ref(), + page, + limit, + ..Default::default() + } + .list(&mut context.pool()) + .await + .with_lemmy_type(LemmyErrorType::CouldntGetComments)?; Ok(Json(GetCommentsResponse { comments })) } diff --git a/crates/apub/src/api/list_posts.rs b/crates/apub/src/api/list_posts.rs index 63d6800e..c60abc96 100644 --- a/crates/apub/src/api/list_posts.rs +++ b/crates/apub/src/api/list_posts.rs @@ -36,27 +36,32 @@ pub async fn list_posts( }; 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) .await - .is_ok(); - - let posts = PostQuery::builder() - .pool(&mut context.pool()) - .local_user(local_user_view.map(|l| l.local_user).as_ref()) - .listing_type(Some(listing_type)) - .sort(sort) - .community_id(community_id) - .saved_only(saved_only) - .page(page) - .limit(limit) - .is_mod_or_admin(Some(is_mod_or_admin)) - .build() - .list() - .await - .with_lemmy_type(LemmyErrorType::CouldntGetPosts)?; + .is_ok(), + ); + + let posts = PostQuery { + local_user: local_user_view.map(|l| l.local_user).as_ref(), + listing_type, + sort, + community_id, + saved_only, + page, + limit, + is_mod_or_admin, + ..Default::default() + } + .list(&mut context.pool()) + .await + .with_lemmy_type(LemmyErrorType::CouldntGetPosts)?; Ok(Json(GetPostsResponse { posts })) } diff --git a/crates/apub/src/api/read_person.rs b/crates/apub/src/api/read_person.rs index 71f4abd5..60cea766 100644 --- a/crates/apub/src/api/read_person.rs +++ b/crates/apub/src/api/read_person.rs @@ -56,49 +56,49 @@ pub async fn read_person( let local_user = local_user_view.map(|l| l.local_user); let local_user_clone = local_user.clone(); - let posts = PostQuery::builder() - .pool(&mut context.pool()) - .sort(sort) - .saved_only(saved_only) - .local_user(local_user.as_ref()) - .community_id(community_id) - .is_mod_or_admin(is_admin) - .page(page) - .limit(limit) - .creator_id( + let posts = PostQuery { + sort, + saved_only, + local_user:local_user.as_ref(), + community_id, + is_mod_or_admin: is_admin, + page, + limit, + creator_id: // 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 if !saved_only.unwrap_or(false) { Some(person_details_id) } else { None - }, - ) - .build() - .list() - .await?; + } + , + ..Default::default() + } + .list(&mut context.pool()) + .await?; - let comments = CommentQuery::builder() - .pool(&mut context.pool()) - .local_user(local_user_clone.as_ref()) - .sort(sort.map(post_to_comment_sort_type)) - .saved_only(saved_only) - .show_deleted_and_removed(Some(false)) - .community_id(community_id) - .page(page) - .limit(limit) - .creator_id( + let comments = CommentQuery { + local_user: (local_user_clone.as_ref()), + sort: (sort.map(post_to_comment_sort_type)), + saved_only: (saved_only), + show_deleted_and_removed: (Some(false)), + community_id: (community_id), + page: (page), + limit: (limit), + creator_id: ( // 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 if !saved_only.unwrap_or(false) { Some(person_details_id) } else { None - }, - ) - .build() - .list() - .await?; + } + ), + ..Default::default() + } + .list(&mut context.pool()) + .await?; let moderates = CommunityModeratorView::for_person(&mut context.pool(), person_details_id).await?; diff --git a/crates/apub/src/api/search.rs b/crates/apub/src/api/search.rs index b7723a6f..2c65f302 100644 --- a/crates/apub/src/api/search.rs +++ b/crates/apub/src/api/search.rs @@ -53,60 +53,59 @@ pub async fn search( let local_user = local_user_view.map(|l| l.local_user); match search_type { SearchType::Posts => { - posts = PostQuery::builder() - .pool(&mut context.pool()) - .sort(sort) - .listing_type(listing_type) - .community_id(community_id) - .creator_id(creator_id) - .local_user(local_user.as_ref()) - .search_term(Some(q)) - .is_mod_or_admin(is_admin) - .page(page) - .limit(limit) - .build() - .list() - .await?; + posts = PostQuery { + sort: (sort), + listing_type: (listing_type), + community_id: (community_id), + creator_id: (creator_id), + local_user: (local_user.as_ref()), + search_term: (Some(q)), + is_mod_or_admin: (is_admin), + page: (page), + limit: (limit), + ..Default::default() + } + .list(&mut context.pool()) + .await?; } SearchType::Comments => { - comments = CommentQuery::builder() - .pool(&mut context.pool()) - .sort(sort.map(post_to_comment_sort_type)) - .listing_type(listing_type) - .search_term(Some(q)) - .community_id(community_id) - .creator_id(creator_id) - .local_user(local_user.as_ref()) - .page(page) - .limit(limit) - .build() - .list() - .await?; + comments = CommentQuery { + sort: (sort.map(post_to_comment_sort_type)), + listing_type: (listing_type), + search_term: (Some(q)), + community_id: (community_id), + creator_id: (creator_id), + local_user: (local_user.as_ref()), + page: (page), + limit: (limit), + ..Default::default() + } + .list(&mut context.pool()) + .await?; } SearchType::Communities => { - communities = CommunityQuery::builder() - .pool(&mut context.pool()) - .sort(sort) - .listing_type(listing_type) - .search_term(Some(q)) - .local_user(local_user.as_ref()) - .is_mod_or_admin(is_admin) - .page(page) - .limit(limit) - .build() - .list() - .await?; + communities = CommunityQuery { + sort: (sort), + listing_type: (listing_type), + search_term: (Some(q)), + local_user: (local_user.as_ref()), + is_mod_or_admin: (is_admin), + page: (page), + limit: (limit), + ..Default::default() + } + .list(&mut context.pool()) + .await?; } SearchType::Users => { - users = PersonQuery::builder() - .pool(&mut context.pool()) - .sort(sort) - .search_term(Some(q)) - .page(page) - .limit(limit) - .build() - .list() - .await?; + users = PersonQuery { + sort: (sort), + search_term: (Some(q)), + page: (page), + limit: (limit), + } + .list(&mut context.pool()) + .await?; } SearchType::All => { // 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(); let local_user_ = local_user.clone(); - posts = PostQuery::builder() - .pool(&mut context.pool()) - .sort(sort) - .listing_type(listing_type) - .community_id(community_id) - .creator_id(creator_id) - .local_user(local_user_.as_ref()) - .search_term(Some(q)) - .is_mod_or_admin(is_admin) - .page(page) - .limit(limit) - .build() - .list() - .await?; + posts = PostQuery { + sort: (sort), + listing_type: (listing_type), + community_id: (community_id), + creator_id: (creator_id), + local_user: (local_user_.as_ref()), + search_term: (Some(q)), + is_mod_or_admin: (is_admin), + page: (page), + limit: (limit), + ..Default::default() + } + .list(&mut context.pool()) + .await?; let q = data.q.clone(); let local_user_ = local_user.clone(); - comments = CommentQuery::builder() - .pool(&mut context.pool()) - .sort(sort.map(post_to_comment_sort_type)) - .listing_type(listing_type) - .search_term(Some(q)) - .community_id(community_id) - .creator_id(creator_id) - .local_user(local_user_.as_ref()) - .page(page) - .limit(limit) - .build() - .list() - .await?; + comments = CommentQuery { + sort: (sort.map(post_to_comment_sort_type)), + listing_type: (listing_type), + search_term: (Some(q)), + community_id: (community_id), + creator_id: (creator_id), + local_user: (local_user_.as_ref()), + page: (page), + limit: (limit), + ..Default::default() + } + .list(&mut context.pool()) + .await?; let q = data.q.clone(); communities = if community_or_creator_included { vec![] } else { - CommunityQuery::builder() - .pool(&mut context.pool()) - .sort(sort) - .listing_type(listing_type) - .search_term(Some(q)) - .local_user(local_user.as_ref()) - .is_mod_or_admin(is_admin) - .page(page) - .limit(limit) - .build() - .list() - .await? + CommunityQuery { + sort: (sort), + listing_type: (listing_type), + search_term: (Some(q)), + local_user: (local_user.as_ref()), + is_mod_or_admin: (is_admin), + page: (page), + limit: (limit), + ..Default::default() + } + .list(&mut context.pool()) + .await? }; let q = data.q.clone(); @@ -170,31 +169,30 @@ pub async fn search( users = if community_or_creator_included { vec![] } else { - PersonQuery::builder() - .pool(&mut context.pool()) - .sort(sort) - .search_term(Some(q)) - .page(page) - .limit(limit) - .build() - .list() - .await? + PersonQuery { + sort: (sort), + search_term: (Some(q)), + page: (page), + limit: (limit), + } + .list(&mut context.pool()) + .await? }; } SearchType::Url => { - posts = PostQuery::builder() - .pool(&mut context.pool()) - .sort(sort) - .listing_type(listing_type) - .community_id(community_id) - .creator_id(creator_id) - .url_search(Some(q)) - .is_mod_or_admin(is_admin) - .page(page) - .limit(limit) - .build() - .list() - .await?; + posts = PostQuery { + sort: (sort), + listing_type: (listing_type), + community_id: (community_id), + creator_id: (creator_id), + url_search: (Some(q)), + is_mod_or_admin: (is_admin), + page: (page), + limit: (limit), + ..Default::default() + } + .list(&mut context.pool()) + .await?; } }; diff --git a/crates/db_views/Cargo.toml b/crates/db_views/Cargo.toml index 69dc1533..54a3aab6 100644 --- a/crates/db_views/Cargo.toml +++ b/crates/db_views/Cargo.toml @@ -29,7 +29,6 @@ diesel_ltree = { workspace = true, optional = true } serde = { workspace = true } serde_with = { workspace = true } tracing = { workspace = true, optional = true } -typed-builder = { workspace = true } ts-rs = { workspace = true, optional = true } [dev-dependencies] diff --git a/crates/db_views/src/comment_report_view.rs b/crates/db_views/src/comment_report_view.rs index 9c50eb67..c28c7d67 100644 --- a/crates/db_views/src/comment_report_view.rs +++ b/crates/db_views/src/comment_report_view.rs @@ -33,7 +33,6 @@ use lemmy_db_schema::{ traits::JoinView, utils::{get_conn, limit_and_offset, DbPool}, }; -use typed_builder::TypedBuilder; impl CommentReportView { /// returns the CommentReportView for the provided report_id @@ -137,24 +136,21 @@ impl CommentReportView { } } -#[derive(TypedBuilder)] -#[builder(field_defaults(default))] -pub struct CommentReportQuery<'a, 'b: 'a> { - #[builder(!default)] - pool: &'a mut DbPool<'b>, - #[builder(!default)] - my_person_id: PersonId, - #[builder(!default)] - admin: bool, - community_id: Option, - page: Option, - limit: Option, - unresolved_only: Option, +#[derive(Default)] +pub struct CommentReportQuery { + pub community_id: Option, + pub page: Option, + pub limit: Option, + pub unresolved_only: Option, } -impl<'a, 'b: 'a> CommentReportQuery<'a, 'b> { - pub async fn list(self) -> Result, Error> { - let conn = &mut get_conn(self.pool).await?; +impl CommentReportQuery { + pub async fn list( + self, + pool: &mut DbPool<'_>, + my_person: &Person, + ) -> Result, Error> { + let conn = &mut get_conn(pool).await?; 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::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( @@ -220,13 +216,13 @@ impl<'a, 'b: 'a> CommentReportQuery<'a, 'b> { .offset(offset); // If its not an admin, get only the ones you mod - let res = if !self.admin { + let res = if !my_person.admin { query .inner_join( community_moderator::table.on( community_moderator::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::<::JoinTuple>(conn) @@ -514,12 +510,8 @@ mod tests { }; // Do a batch read of timmys reports - let reports = CommentReportQuery::builder() - .pool(pool) - .my_person_id(inserted_timmy.id) - .admin(false) - .build() - .list() + let reports = CommentReportQuery::default() + .list(pool, &inserted_timmy) .await .unwrap(); @@ -590,15 +582,13 @@ mod tests { // Do a batch read of timmys reports // It should only show saras, which is unresolved - let reports_after_resolve = CommentReportQuery::builder() - .pool(pool) - .my_person_id(inserted_timmy.id) - .admin(false) - .unresolved_only(Some(true)) - .build() - .list() - .await - .unwrap(); + let reports_after_resolve = CommentReportQuery { + unresolved_only: (Some(true)), + ..Default::default() + } + .list(pool, &inserted_timmy) + .await + .unwrap(); assert_eq!(reports_after_resolve[0], expected_sara_report_view); assert_eq!(reports_after_resolve.len(), 1); diff --git a/crates/db_views/src/comment_view.rs b/crates/db_views/src/comment_view.rs index 3556f850..435a3d82 100644 --- a/crates/db_views/src/comment_view.rs +++ b/crates/db_views/src/comment_view.rs @@ -40,7 +40,6 @@ use lemmy_db_schema::{ CommentSortType, ListingType, }; -use typed_builder::TypedBuilder; type CommentViewTuple = ( Comment, @@ -156,29 +155,26 @@ impl CommentView { } } -#[derive(TypedBuilder)] -#[builder(field_defaults(default))] -pub struct CommentQuery<'a, 'b: 'a> { - #[builder(!default)] - pool: &'a mut DbPool<'b>, - listing_type: Option, - sort: Option, - community_id: Option, - post_id: Option, - parent_path: Option, - creator_id: Option, - local_user: Option<&'a LocalUser>, - search_term: Option, - saved_only: Option, - show_deleted_and_removed: Option, - page: Option, - limit: Option, - max_depth: Option, +#[derive(Default)] +pub struct CommentQuery<'a> { + pub listing_type: Option, + pub sort: Option, + pub community_id: Option, + pub post_id: Option, + pub parent_path: Option, + pub creator_id: Option, + pub local_user: Option<&'a LocalUser>, + pub search_term: Option, + pub saved_only: Option, + pub show_deleted_and_removed: Option, + pub page: Option, + pub limit: Option, + pub max_depth: Option, } -impl<'a, 'b: 'a> CommentQuery<'a, 'b> { - pub async fn list(self) -> Result, Error> { - let conn = &mut get_conn(self.pool).await?; +impl<'a> CommentQuery<'a> { + pub async fn list(self, pool: &mut DbPool<'_>) -> Result, Error> { + let conn = &mut get_conn(pool).await?; // 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)); @@ -602,29 +598,29 @@ mod tests { let mut expected_comment_view_with_person = expected_comment_view_no_person.clone(); expected_comment_view_with_person.my_vote = Some(1); - let read_comment_views_no_person = CommentQuery::builder() - .pool(pool) - .sort(Some(CommentSortType::Old)) - .post_id(Some(data.inserted_post.id)) - .build() - .list() - .await - .unwrap(); + let read_comment_views_no_person = CommentQuery { + sort: (Some(CommentSortType::Old)), + post_id: (Some(data.inserted_post.id)), + ..Default::default() + } + .list(pool) + .await + .unwrap(); assert_eq!( expected_comment_view_no_person, read_comment_views_no_person[0] ); - let read_comment_views_with_person = CommentQuery::builder() - .pool(pool) - .sort(Some(CommentSortType::Old)) - .post_id(Some(data.inserted_post.id)) - .local_user(Some(&data.inserted_local_user)) - .build() - .list() - .await - .unwrap(); + let read_comment_views_with_person = CommentQuery { + sort: (Some(CommentSortType::Old)), + post_id: (Some(data.inserted_post.id)), + local_user: (Some(&data.inserted_local_user)), + ..Default::default() + } + .list(pool) + .await + .unwrap(); assert_eq!( expected_comment_view_with_person, @@ -656,24 +652,24 @@ mod tests { let data = init_data(pool).await; let top_path = data.inserted_comment_0.path.clone(); - let read_comment_views_top_path = CommentQuery::builder() - .pool(pool) - .post_id(Some(data.inserted_post.id)) - .parent_path(Some(top_path)) - .build() - .list() - .await - .unwrap(); + let read_comment_views_top_path = CommentQuery { + post_id: (Some(data.inserted_post.id)), + parent_path: (Some(top_path)), + ..Default::default() + } + .list(pool) + .await + .unwrap(); let child_path = data.inserted_comment_1.path.clone(); - let read_comment_views_child_path = CommentQuery::builder() - .pool(pool) - .post_id(Some(data.inserted_post.id)) - .parent_path(Some(child_path)) - .build() - .list() - .await - .unwrap(); + let read_comment_views_child_path = CommentQuery { + post_id: (Some(data.inserted_post.id)), + parent_path: (Some(child_path)), + ..Default::default() + } + .list(pool) + .await + .unwrap(); // Make sure the comment parent-limited fetch is correct 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_2)); - let read_comment_views_top_max_depth = CommentQuery::builder() - .pool(pool) - .post_id(Some(data.inserted_post.id)) - .max_depth(Some(1)) - .build() - .list() - .await - .unwrap(); + let read_comment_views_top_max_depth = CommentQuery { + post_id: (Some(data.inserted_post.id)), + max_depth: (Some(1)), + ..Default::default() + } + .list(pool) + .await + .unwrap(); // Make sure a depth limited one only has the top comment assert_eq!( @@ -704,16 +700,16 @@ mod tests { assert_eq!(1, read_comment_views_top_max_depth.len()); let child_path = data.inserted_comment_1.path.clone(); - let read_comment_views_parent_max_depth = CommentQuery::builder() - .pool(pool) - .post_id(Some(data.inserted_post.id)) - .parent_path(Some(child_path)) - .max_depth(Some(1)) - .sort(Some(CommentSortType::New)) - .build() - .list() - .await - .unwrap(); + let read_comment_views_parent_max_depth = CommentQuery { + post_id: (Some(data.inserted_post.id)), + parent_path: (Some(child_path)), + max_depth: (Some(1)), + sort: (Some(CommentSortType::New)), + ..Default::default() + } + .list(pool) + .await + .unwrap(); // Make sure a depth limited one, and given child comment 1, has 3 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 // (except from blocked user) - let all_languages = CommentQuery::builder() - .pool(pool) - .local_user(Some(&data.inserted_local_user)) - .build() - .list() - .await - .unwrap(); + let all_languages = CommentQuery { + local_user: (Some(&data.inserted_local_user)), + ..Default::default() + } + .list(pool) + .await + .unwrap(); assert_eq!(5, all_languages.len()); // 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) .await .unwrap(); - let finnish_comments = CommentQuery::builder() - .pool(pool) - .local_user(Some(&data.inserted_local_user)) - .build() - .list() - .await - .unwrap(); + let finnish_comments = CommentQuery { + local_user: (Some(&data.inserted_local_user)), + ..Default::default() + } + .list(pool) + .await + .unwrap(); assert_eq!(2, finnish_comments.len()); let finnish_comment = finnish_comments .iter() @@ -772,13 +768,13 @@ mod tests { LocalUserLanguage::update(pool, vec![UNDETERMINED_ID], data.inserted_local_user.id) .await .unwrap(); - let undetermined_comment = CommentQuery::builder() - .pool(pool) - .local_user(Some(&data.inserted_local_user)) - .build() - .list() - .await - .unwrap(); + let undetermined_comment = CommentQuery { + local_user: (Some(&data.inserted_local_user)), + ..Default::default() + } + .list(pool) + .await + .unwrap(); assert_eq!(1, undetermined_comment.len()); cleanup(data, pool).await; diff --git a/crates/db_views/src/post_report_view.rs b/crates/db_views/src/post_report_view.rs index 1219e0db..fff2a2bc 100644 --- a/crates/db_views/src/post_report_view.rs +++ b/crates/db_views/src/post_report_view.rs @@ -30,7 +30,6 @@ use lemmy_db_schema::{ traits::JoinView, utils::{get_conn, limit_and_offset, DbPool}, }; -use typed_builder::TypedBuilder; type PostReportViewTuple = ( PostReport, @@ -159,24 +158,21 @@ impl PostReportView { } } -#[derive(TypedBuilder)] -#[builder(field_defaults(default))] -pub struct PostReportQuery<'a, 'b: 'a> { - #[builder(!default)] - pool: &'a mut DbPool<'b>, - #[builder(!default)] - my_person_id: PersonId, - #[builder(!default)] - admin: bool, - community_id: Option, - page: Option, - limit: Option, - unresolved_only: Option, +#[derive(Default)] +pub struct PostReportQuery { + pub community_id: Option, + pub page: Option, + pub limit: Option, + pub unresolved_only: Option, } -impl<'a, 'b: 'a> PostReportQuery<'a, 'b> { - pub async fn list(self) -> Result, Error> { - let conn = &mut get_conn(self.pool).await?; +impl PostReportQuery { + pub async fn list( + self, + pool: &mut DbPool<'_>, + my_person: &Person, + ) -> Result, Error> { + let conn = &mut get_conn(pool).await?; let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); let mut query = post_report::table @@ -195,7 +191,7 @@ impl<'a, 'b: 'a> PostReportQuery<'a, 'b> { post_like::table.on( 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))) @@ -231,13 +227,13 @@ impl<'a, 'b: 'a> PostReportQuery<'a, 'b> { .offset(offset); // If its not an admin, get only the ones you mod - let res = if !self.admin { + let res = if !my_person.admin { query .inner_join( community_moderator::table.on( community_moderator::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::(conn) @@ -506,12 +502,8 @@ mod tests { }; // Do a batch read of timmys reports - let reports = PostReportQuery::builder() - .pool(pool) - .my_person_id(inserted_timmy.id) - .admin(false) - .build() - .list() + let reports = PostReportQuery::default() + .list(pool, &inserted_timmy) .await .unwrap(); @@ -580,15 +572,13 @@ mod tests { // Do a batch read of timmys reports // It should only show saras, which is unresolved - let reports_after_resolve = PostReportQuery::builder() - .pool(pool) - .my_person_id(inserted_timmy.id) - .admin(false) - .unresolved_only(Some(true)) - .build() - .list() - .await - .unwrap(); + let reports_after_resolve = PostReportQuery { + unresolved_only: (Some(true)), + ..Default::default() + } + .list(pool, &inserted_timmy) + .await + .unwrap(); assert_eq!(reports_after_resolve[0], expected_sara_report_view); // Make sure the counts are correct diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index 07433667..466713e1 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -45,7 +45,6 @@ use lemmy_db_schema::{ SortType, }; use tracing::debug; -use typed_builder::TypedBuilder; type PostViewTuple = ( Post, @@ -193,28 +192,25 @@ impl PostView { } } -#[derive(TypedBuilder)] -#[builder(field_defaults(default))] -pub struct PostQuery<'a, 'b: 'a> { - #[builder(!default)] - pool: &'a mut DbPool<'b>, - listing_type: Option, - sort: Option, - creator_id: Option, - community_id: Option, - local_user: Option<&'a LocalUser>, - search_term: Option, - url_search: Option, - saved_only: Option, +#[derive(Default)] +pub struct PostQuery<'a> { + pub listing_type: Option, + pub sort: Option, + pub creator_id: Option, + pub community_id: Option, + pub local_user: Option<&'a LocalUser>, + pub search_term: Option, + pub url_search: Option, + pub saved_only: Option, /// Used to show deleted or removed posts for admins - is_mod_or_admin: Option, - page: Option, - limit: Option, + pub is_mod_or_admin: Option, + pub page: Option, + pub limit: Option, } -impl<'a, 'b: 'a> PostQuery<'a, 'b> { - pub async fn list(self) -> Result, Error> { - let conn = &mut get_conn(self.pool).await?; +impl<'a> PostQuery<'a> { + pub async fn list(self, pool: &mut DbPool<'_>) -> Result, Error> { + let conn = &mut get_conn(pool).await?; // 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)); @@ -619,15 +615,15 @@ mod tests { .await .unwrap(); - let read_post_listing = PostQuery::builder() - .pool(pool) - .sort(Some(SortType::New)) - .community_id(Some(data.inserted_community.id)) - .local_user(Some(&inserted_local_user)) - .build() - .list() - .await - .unwrap(); + let read_post_listing = PostQuery { + sort: (Some(SortType::New)), + community_id: (Some(data.inserted_community.id)), + local_user: (Some(&inserted_local_user)), + ..Default::default() + } + .list(pool) + .await + .unwrap(); let post_listing_single_with_person = PostView::read( pool, @@ -658,15 +654,15 @@ mod tests { .await .unwrap(); - let post_listings_with_bots = PostQuery::builder() - .pool(pool) - .sort(Some(SortType::New)) - .community_id(Some(data.inserted_community.id)) - .local_user(Some(&inserted_local_user)) - .build() - .list() - .await - .unwrap(); + let post_listings_with_bots = PostQuery { + sort: (Some(SortType::New)), + community_id: (Some(data.inserted_community.id)), + local_user: (Some(&inserted_local_user)), + ..Default::default() + } + .list(pool) + .await + .unwrap(); // should include bot post which has "undetermined" language assert_eq!(2, post_listings_with_bots.len()); @@ -680,14 +676,14 @@ mod tests { let pool = &mut pool.into(); let data = init_data(pool).await; - let read_post_listing_multiple_no_person = PostQuery::builder() - .pool(pool) - .sort(Some(SortType::New)) - .community_id(Some(data.inserted_community.id)) - .build() - .list() - .await - .unwrap(); + let read_post_listing_multiple_no_person = PostQuery { + sort: (Some(SortType::New)), + community_id: (Some(data.inserted_community.id)), + ..Default::default() + } + .list(pool) + .await + .unwrap(); let read_post_listing_single_no_person = PostView::read(pool, data.inserted_post.id, None, None) @@ -724,15 +720,15 @@ mod tests { }; CommunityBlock::block(pool, &community_block).await.unwrap(); - let read_post_listings_with_person_after_block = PostQuery::builder() - .pool(pool) - .sort(Some(SortType::New)) - .community_id(Some(data.inserted_community.id)) - .local_user(Some(&data.inserted_local_user)) - .build() - .list() - .await - .unwrap(); + let read_post_listings_with_person_after_block = PostQuery { + sort: (Some(SortType::New)), + community_id: (Some(data.inserted_community.id)), + local_user: (Some(&data.inserted_local_user)), + ..Default::default() + } + .list(pool) + .await + .unwrap(); // Should be 0 posts after the community block assert_eq!(0, read_post_listings_with_person_after_block.len()); @@ -789,15 +785,15 @@ mod tests { .await .unwrap(); - let read_post_listing = PostQuery::builder() - .pool(pool) - .sort(Some(SortType::New)) - .community_id(Some(data.inserted_community.id)) - .local_user(Some(&inserted_local_user)) - .build() - .list() - .await - .unwrap(); + let read_post_listing = PostQuery { + sort: (Some(SortType::New)), + community_id: (Some(data.inserted_community.id)), + local_user: (Some(&inserted_local_user)), + ..Default::default() + } + .list(pool) + .await + .unwrap(); assert_eq!(1, read_post_listing.len()); assert_eq!(expected_post_with_upvote, read_post_listing[0]); @@ -829,14 +825,14 @@ mod tests { Post::create(pool, &post_spanish).await.unwrap(); - let post_listings_all = PostQuery::builder() - .pool(pool) - .sort(Some(SortType::New)) - .local_user(Some(&data.inserted_local_user)) - .build() - .list() - .await - .unwrap(); + let post_listings_all = PostQuery { + sort: (Some(SortType::New)), + local_user: (Some(&data.inserted_local_user)), + ..Default::default() + } + .list(pool) + .await + .unwrap(); // no language filters specified, all posts should be returned assert_eq!(3, post_listings_all.len()); @@ -849,14 +845,14 @@ mod tests { .await .unwrap(); - let post_listing_french = PostQuery::builder() - .pool(pool) - .sort(Some(SortType::New)) - .local_user(Some(&data.inserted_local_user)) - .build() - .list() - .await - .unwrap(); + let post_listing_french = PostQuery { + sort: (Some(SortType::New)), + local_user: (Some(&data.inserted_local_user)), + ..Default::default() + } + .list(pool) + .await + .unwrap(); // only one post in french and one undetermined should be returned assert_eq!(2, post_listing_french.len()); @@ -871,14 +867,14 @@ mod tests { ) .await .unwrap(); - let post_listings_french_und = PostQuery::builder() - .pool(pool) - .sort(Some(SortType::New)) - .local_user(Some(&data.inserted_local_user)) - .build() - .list() - .await - .unwrap(); + let post_listings_french_und = PostQuery { + sort: (Some(SortType::New)), + local_user: (Some(&data.inserted_local_user)), + ..Default::default() + } + .list(pool) + .await + .unwrap(); // french post and undetermined language post should be returned assert_eq!(2, post_listings_french_und.len()); @@ -908,28 +904,28 @@ mod tests { .unwrap(); // Make sure you don't see the deleted post in the results - let post_listings_no_admin = PostQuery::builder() - .pool(pool) - .sort(Some(SortType::New)) - .local_user(Some(&data.inserted_local_user)) - .is_mod_or_admin(Some(false)) - .build() - .list() - .await - .unwrap(); + let post_listings_no_admin = PostQuery { + sort: (Some(SortType::New)), + local_user: (Some(&data.inserted_local_user)), + is_mod_or_admin: (Some(false)), + ..Default::default() + } + .list(pool) + .await + .unwrap(); assert_eq!(1, post_listings_no_admin.len()); // Make sure they see both - let post_listings_is_admin = PostQuery::builder() - .pool(pool) - .sort(Some(SortType::New)) - .local_user(Some(&data.inserted_local_user)) - .is_mod_or_admin(Some(true)) - .build() - .list() - .await - .unwrap(); + let post_listings_is_admin = PostQuery { + sort: (Some(SortType::New)), + local_user: (Some(&data.inserted_local_user)), + is_mod_or_admin: (Some(true)), + ..Default::default() + } + .list(pool) + .await + .unwrap(); assert_eq!(2, post_listings_is_admin.len()); diff --git a/crates/db_views/src/private_message_report_view.rs b/crates/db_views/src/private_message_report_view.rs index 74209f33..bdffa42e 100644 --- a/crates/db_views/src/private_message_report_view.rs +++ b/crates/db_views/src/private_message_report_view.rs @@ -12,7 +12,6 @@ use lemmy_db_schema::{ traits::JoinView, utils::{get_conn, limit_and_offset, DbPool}, }; -use typed_builder::TypedBuilder; type PrivateMessageReportViewTuple = ( PrivateMessageReport, @@ -81,19 +80,16 @@ impl PrivateMessageReportView { } } -#[derive(TypedBuilder)] -#[builder(field_defaults(default))] -pub struct PrivateMessageReportQuery<'a, 'b: 'a> { - #[builder(!default)] - pool: &'a mut DbPool<'b>, - page: Option, - limit: Option, - unresolved_only: Option, +#[derive(Default)] +pub struct PrivateMessageReportQuery { + pub page: Option, + pub limit: Option, + pub unresolved_only: Option, } -impl<'a, 'b: 'a> PrivateMessageReportQuery<'a, 'b> { - pub async fn list(self) -> Result, Error> { - let conn = &mut get_conn(self.pool).await?; +impl PrivateMessageReportQuery { + pub async fn list(self, pool: &mut DbPool<'_>) -> Result, Error> { + let conn = &mut get_conn(pool).await?; let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); let mut query = private_message_report::table @@ -208,10 +204,8 @@ mod tests { .await .unwrap(); - let reports = PrivateMessageReportQuery::builder() - .pool(pool) - .build() - .list() + let reports = PrivateMessageReportQuery::default() + .list(pool) .await .unwrap(); assert_eq!(1, reports.len()); @@ -233,13 +227,13 @@ mod tests { .await .unwrap(); - let reports = PrivateMessageReportQuery::builder() - .pool(pool) - .unresolved_only(Some(false)) - .build() - .list() - .await - .unwrap(); + let reports = PrivateMessageReportQuery { + unresolved_only: (Some(false)), + ..Default::default() + } + .list(pool) + .await + .unwrap(); assert_eq!(1, reports.len()); assert!(reports[0].private_message_report.resolved); assert!(reports[0].resolver.is_some()); diff --git a/crates/db_views/src/private_message_view.rs b/crates/db_views/src/private_message_view.rs index 3d8fd42d..863db812 100644 --- a/crates/db_views/src/private_message_view.rs +++ b/crates/db_views/src/private_message_view.rs @@ -17,7 +17,6 @@ use lemmy_db_schema::{ utils::{get_conn, limit_and_offset, DbPool}, }; use tracing::debug; -use typed_builder::TypedBuilder; type PrivateMessageViewTuple = (PrivateMessage, Person, Person); @@ -68,21 +67,20 @@ impl PrivateMessageView { } } -#[derive(TypedBuilder)] -#[builder(field_defaults(default))] -pub struct PrivateMessageQuery<'a, 'b: 'a> { - #[builder(!default)] - pool: &'a mut DbPool<'b>, - #[builder(!default)] - recipient_id: PersonId, - unread_only: Option, - page: Option, - limit: Option, +#[derive(Default)] +pub struct PrivateMessageQuery { + pub unread_only: Option, + pub page: Option, + pub limit: Option, } -impl<'a, 'b: 'a> PrivateMessageQuery<'a, 'b> { - pub async fn list(self) -> Result, Error> { - let conn = &mut get_conn(self.pool).await?; +impl PrivateMessageQuery { + pub async fn list( + self, + pool: &mut DbPool<'_>, + recipient_id: PersonId, + ) -> Result, Error> { + let conn = &mut get_conn(pool).await?; let person_alias_1 = diesel::alias!(person as person1); let mut query = private_message::table @@ -101,14 +99,14 @@ impl<'a, 'b: 'a> PrivateMessageQuery<'a, 'b> { if self.unread_only.unwrap_or(false) { query = query .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 else { query = query.filter( private_message::recipient_id - .eq(self.recipient_id) - .or(private_message::creator_id.eq(self.recipient_id)), + .eq(recipient_id) + .or(private_message::creator_id.eq(recipient_id)), ) } diff --git a/crates/db_views/src/registration_application_view.rs b/crates/db_views/src/registration_application_view.rs index a2fdcba6..1ee82044 100644 --- a/crates/db_views/src/registration_application_view.rs +++ b/crates/db_views/src/registration_application_view.rs @@ -18,7 +18,6 @@ use lemmy_db_schema::{ traits::JoinView, utils::{get_conn, limit_and_offset, DbPool}, }; -use typed_builder::TypedBuilder; type RegistrationApplicationViewTuple = (RegistrationApplication, LocalUser, Person, Option); @@ -89,20 +88,20 @@ impl RegistrationApplicationView { } } -#[derive(TypedBuilder)] -#[builder(field_defaults(default))] -pub struct RegistrationApplicationQuery<'a, 'b: 'a> { - #[builder(!default)] - pool: &'a mut DbPool<'b>, - unread_only: Option, - verified_email_only: Option, - page: Option, - limit: Option, +#[derive(Default)] +pub struct RegistrationApplicationQuery { + pub unread_only: Option, + pub verified_email_only: Option, + pub page: Option, + pub limit: Option, } -impl<'a, 'b: 'a> RegistrationApplicationQuery<'a, 'b> { - pub async fn list(self) -> Result, Error> { - let conn = &mut get_conn(self.pool).await?; +impl RegistrationApplicationQuery { + pub async fn list( + self, + pool: &mut DbPool<'_>, + ) -> Result, Error> { + let conn = &mut get_conn(pool).await?; let person_alias_1 = diesel::alias!(person as person1); let mut query = registration_application::table @@ -327,13 +326,13 @@ mod tests { assert_eq!(read_sara_app_view, expected_sara_app_view); // Do a batch read of the applications - let apps = RegistrationApplicationQuery::builder() - .pool(pool) - .unread_only(Some(true)) - .build() - .list() - .await - .unwrap(); + let apps = RegistrationApplicationQuery { + unread_only: (Some(true)), + ..Default::default() + } + .list(pool) + .await + .unwrap(); assert_eq!( apps, @@ -403,13 +402,13 @@ mod tests { // Do a batch read of apps again // It should show only jessicas which is unresolved - let apps_after_resolve = RegistrationApplicationQuery::builder() - .pool(pool) - .unread_only(Some(true)) - .build() - .list() - .await - .unwrap(); + let apps_after_resolve = RegistrationApplicationQuery { + unread_only: (Some(true)), + ..Default::default() + } + .list(pool) + .await + .unwrap(); assert_eq!(apps_after_resolve, vec![read_jess_app_view]); // Make sure the counts are correct @@ -419,10 +418,8 @@ mod tests { assert_eq!(unread_count_after_approve, 1); // Make sure the not undenied_only has all the apps - let all_apps = RegistrationApplicationQuery::builder() - .pool(pool) - .build() - .list() + let all_apps = RegistrationApplicationQuery::default() + .list(pool) .await .unwrap(); assert_eq!(all_apps.len(), 2); diff --git a/crates/db_views_actor/Cargo.toml b/crates/db_views_actor/Cargo.toml index da844124..069013d7 100644 --- a/crates/db_views_actor/Cargo.toml +++ b/crates/db_views_actor/Cargo.toml @@ -27,5 +27,4 @@ diesel-async = { workspace = true, features = [ ], optional = true } serde = { workspace = true } serde_with = { workspace = true } -typed-builder = { workspace = true } ts-rs = { workspace = true, optional = true } diff --git a/crates/db_views_actor/src/comment_reply_view.rs b/crates/db_views_actor/src/comment_reply_view.rs index 08cc5a45..4d7a8eac 100644 --- a/crates/db_views_actor/src/comment_reply_view.rs +++ b/crates/db_views_actor/src/comment_reply_view.rs @@ -36,7 +36,6 @@ use lemmy_db_schema::{ utils::{get_conn, limit_and_offset, DbPool}, CommentSortType, }; -use typed_builder::TypedBuilder; type CommentReplyViewTuple = ( CommentReply, @@ -175,23 +174,20 @@ impl CommentReplyView { } } -#[derive(TypedBuilder)] -#[builder(field_defaults(default))] -pub struct CommentReplyQuery<'a, 'b: 'a> { - #[builder(!default)] - pool: &'a mut DbPool<'b>, - my_person_id: Option, - recipient_id: Option, - sort: Option, - unread_only: Option, - show_bot_accounts: Option, - page: Option, - limit: Option, +#[derive(Default)] +pub struct CommentReplyQuery { + pub my_person_id: Option, + pub recipient_id: Option, + pub sort: Option, + pub unread_only: Option, + pub show_bot_accounts: Option, + pub page: Option, + pub limit: Option, } -impl<'a, 'b: 'a> CommentReplyQuery<'a, 'b> { - pub async fn list(self) -> Result, Error> { - let conn = &mut get_conn(self.pool).await?; +impl CommentReplyQuery { + pub async fn list(self, pool: &mut DbPool<'_>) -> Result, Error> { + let conn = &mut get_conn(pool).await?; let person_alias_1 = diesel::alias!(person as person1); diff --git a/crates/db_views_actor/src/community_view.rs b/crates/db_views_actor/src/community_view.rs index 74aba52f..64dc0909 100644 --- a/crates/db_views_actor/src/community_view.rs +++ b/crates/db_views_actor/src/community_view.rs @@ -23,7 +23,6 @@ use lemmy_db_schema::{ ListingType, SortType, }; -use typed_builder::TypedBuilder; type CommunityViewTuple = ( Community, @@ -100,26 +99,23 @@ impl CommunityView { } } -#[derive(TypedBuilder)] -#[builder(field_defaults(default))] -pub struct CommunityQuery<'a, 'b: 'a> { - #[builder(!default)] - pool: &'a mut DbPool<'b>, - listing_type: Option, - sort: Option, - local_user: Option<&'a LocalUser>, - search_term: Option, - is_mod_or_admin: Option, - show_nsfw: Option, - page: Option, - limit: Option, +#[derive(Default)] +pub struct CommunityQuery<'a> { + pub listing_type: Option, + pub sort: Option, + pub local_user: Option<&'a LocalUser>, + pub search_term: Option, + pub is_mod_or_admin: Option, + pub show_nsfw: Option, + pub page: Option, + pub limit: Option, } -impl<'a, 'b: 'a> CommunityQuery<'a, 'b> { - pub async fn list(self) -> Result, Error> { +impl<'a> CommunityQuery<'a> { + pub async fn list(self, pool: &mut DbPool<'_>) -> Result, Error> { 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 let person_id_join = self.local_user.map(|l| l.person_id).unwrap_or(PersonId(-1)); diff --git a/crates/db_views_actor/src/person_mention_view.rs b/crates/db_views_actor/src/person_mention_view.rs index caa91160..3e142254 100644 --- a/crates/db_views_actor/src/person_mention_view.rs +++ b/crates/db_views_actor/src/person_mention_view.rs @@ -37,7 +37,6 @@ use lemmy_db_schema::{ utils::{get_conn, limit_and_offset, DbPool}, CommentSortType, }; -use typed_builder::TypedBuilder; type PersonMentionViewTuple = ( PersonMention, @@ -175,23 +174,20 @@ impl PersonMentionView { } } -#[derive(TypedBuilder)] -#[builder(field_defaults(default))] -pub struct PersonMentionQuery<'a, 'b: 'a> { - #[builder(!default)] - pool: &'a mut DbPool<'b>, - my_person_id: Option, - recipient_id: Option, - sort: Option, - unread_only: Option, - show_bot_accounts: Option, - page: Option, - limit: Option, +#[derive(Default)] +pub struct PersonMentionQuery { + pub my_person_id: Option, + pub recipient_id: Option, + pub sort: Option, + pub unread_only: Option, + pub show_bot_accounts: Option, + pub page: Option, + pub limit: Option, } -impl<'a, 'b: 'a> PersonMentionQuery<'a, 'b> { - pub async fn list(self) -> Result, Error> { - let conn = &mut get_conn(self.pool).await?; +impl PersonMentionQuery { + pub async fn list(self, pool: &mut DbPool<'_>) -> Result, Error> { + let conn = &mut get_conn(pool).await?; let person_alias_1 = diesel::alias!(person as person1); diff --git a/crates/db_views_actor/src/person_view.rs b/crates/db_views_actor/src/person_view.rs index 3aee145c..e6baa1fc 100644 --- a/crates/db_views_actor/src/person_view.rs +++ b/crates/db_views_actor/src/person_view.rs @@ -19,7 +19,6 @@ use lemmy_db_schema::{ SortType, }; use std::iter::Iterator; -use typed_builder::TypedBuilder; type PersonViewTuple = (Person, PersonAggregates); @@ -79,20 +78,17 @@ impl PersonView { } } -#[derive(TypedBuilder)] -#[builder(field_defaults(default))] -pub struct PersonQuery<'a, 'b: 'a> { - #[builder(!default)] - pool: &'a mut DbPool<'b>, - sort: Option, - search_term: Option, - page: Option, - limit: Option, +#[derive(Default)] +pub struct PersonQuery { + pub sort: Option, + pub search_term: Option, + pub page: Option, + pub limit: Option, } -impl<'a, 'b: 'a> PersonQuery<'a, 'b> { - pub async fn list(self) -> Result, Error> { - let conn = &mut get_conn(self.pool).await?; +impl PersonQuery { + pub async fn list(self, pool: &mut DbPool<'_>) -> Result, Error> { + let conn = &mut get_conn(pool).await?; let mut query = person::table .inner_join(person_aggregates::table) .select((person::all_columns, person_aggregates::all_columns)) diff --git a/crates/routes/src/feeds.rs b/crates/routes/src/feeds.rs index 8429f860..a71871ab 100644 --- a/crates/routes/src/feeds.rs +++ b/crates/routes/src/feeds.rs @@ -124,15 +124,15 @@ async fn get_feed_data( ) -> Result { let site_view = SiteView::read_local(&mut context.pool()).await?; - let posts = PostQuery::builder() - .pool(&mut context.pool()) - .listing_type(Some(listing_type)) - .sort(Some(sort_type)) - .limit(Some(limit)) - .page(Some(page)) - .build() - .list() - .await?; + let posts = PostQuery { + listing_type: (Some(listing_type)), + sort: (Some(sort_type)), + limit: (Some(limit)), + page: (Some(page)), + ..Default::default() + } + .list(&mut context.pool()) + .await?; 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 person = Person::read_from_name(pool, user_name, false).await?; - let posts = PostQuery::builder() - .pool(pool) - .listing_type(Some(ListingType::All)) - .sort(Some(*sort_type)) - .creator_id(Some(person.id)) - .limit(Some(*limit)) - .page(Some(*page)) - .build() - .list() - .await?; + let posts = PostQuery { + listing_type: (Some(ListingType::All)), + sort: (Some(*sort_type)), + creator_id: (Some(person.id)), + limit: (Some(*limit)), + page: (Some(*page)), + ..Default::default() + } + .list(pool) + .await?; 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 community = Community::read_from_name(pool, community_name, false).await?; - let posts = PostQuery::builder() - .pool(pool) - .sort(Some(*sort_type)) - .community_id(Some(community.id)) - .limit(Some(*limit)) - .page(Some(*page)) - .build() - .list() - .await?; + let posts = PostQuery { + sort: (Some(*sort_type)), + community_id: (Some(community.id)), + limit: (Some(*limit)), + page: (Some(*page)), + ..Default::default() + } + .list(pool) + .await?; 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 = LocalUser::read(pool, local_user_id).await?; - let posts = PostQuery::builder() - .pool(pool) - .listing_type(Some(ListingType::Subscribed)) - .local_user(Some(&local_user)) - .sort(Some(*sort_type)) - .limit(Some(*limit)) - .page(Some(*page)) - .build() - .list() - .await?; + let posts = PostQuery { + listing_type: (Some(ListingType::Subscribed)), + local_user: (Some(&local_user)), + sort: (Some(*sort_type)), + limit: (Some(*limit)), + page: (Some(*page)), + ..Default::default() + } + .list(pool) + .await?; let items = create_post_items(posts, protocol_and_hostname)?; @@ -360,27 +360,27 @@ async fn get_feed_inbox( let sort = CommentSortType::New; - let replies = CommentReplyQuery::builder() - .pool(pool) - .recipient_id(Some(person_id)) - .my_person_id(Some(person_id)) - .show_bot_accounts(Some(show_bot_accounts)) - .sort(Some(sort)) - .limit(Some(RSS_FETCH_LIMIT)) - .build() - .list() - .await?; - - let mentions = PersonMentionQuery::builder() - .pool(pool) - .recipient_id(Some(person_id)) - .my_person_id(Some(person_id)) - .show_bot_accounts(Some(show_bot_accounts)) - .sort(Some(sort)) - .limit(Some(RSS_FETCH_LIMIT)) - .build() - .list() - .await?; + let replies = CommentReplyQuery { + recipient_id: (Some(person_id)), + my_person_id: (Some(person_id)), + show_bot_accounts: (Some(show_bot_accounts)), + sort: (Some(sort)), + limit: (Some(RSS_FETCH_LIMIT)), + ..Default::default() + } + .list(pool) + .await?; + + let mentions = PersonMentionQuery { + recipient_id: (Some(person_id)), + my_person_id: (Some(person_id)), + show_bot_accounts: (Some(show_bot_accounts)), + sort: (Some(sort)), + limit: (Some(RSS_FETCH_LIMIT)), + ..Default::default() + } + .list(pool) + .await?; let items = create_reply_and_mention_items(replies, mentions, protocol_and_hostname)?;