From c86f5472fb384a26d9c13825691211145b90adb1 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Fri, 9 Apr 2021 16:35:51 -0400 Subject: [PATCH] Adding listing_type to search. --- crates/api/src/site.rs | 22 +++++++++++++++++----- crates/api_common/src/site.rs | 1 + crates/api_crud/src/comment/read.rs | 4 ++-- crates/db_views/src/comment_view.rs | 6 +++--- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/crates/api/src/site.rs b/crates/api/src/site.rs index a6f09f59b..481f99687 100644 --- a/crates/api/src/site.rs +++ b/crates/api/src/site.rs @@ -13,7 +13,7 @@ use lemmy_api_common::{ user_show_nsfw, }; use lemmy_apub::fetcher::search::search_by_apub_id; -use lemmy_db_queries::{source::site::Site_, Crud, SearchType, SortType}; +use lemmy_db_queries::{source::site::Site_, Crud, ListingType, SearchType, SortType}; use lemmy_db_schema::source::{moderator::*, site::Site}; use lemmy_db_views::{ comment_view::CommentQueryBuilder, @@ -144,8 +144,6 @@ impl Perform for Search { let person_id = local_user_view.map(|u| u.person.id); - let type_ = SearchType::from_str(&data.type_)?; - let mut posts = Vec::new(); let mut comments = Vec::new(); let mut communities = Vec::new(); @@ -157,9 +155,10 @@ impl Perform for Search { let page = data.page; let limit = data.limit; let sort = SortType::from_str(&data.sort)?; + let type_ = SearchType::from_str(&data.type_)?; + let listing_type = ListingType::from_str(&data.listing_type)?; let community_id = data.community_id; let community_name = data.community_name.to_owned(); - let community_name_2 = data.community_name.to_owned(); let creator_id = data.creator_id; match type_ { SearchType::Posts => { @@ -168,6 +167,8 @@ impl Perform for Search { .sort(&sort) .show_nsfw(show_nsfw) .show_bot_accounts(show_bot_accounts) + .listing_type(&listing_type) + .show_nsfw(true) .community_id(community_id) .community_name(community_name) .creator_id(creator_id) @@ -183,6 +184,7 @@ impl Perform for Search { comments = blocking(context.pool(), move |conn| { CommentQueryBuilder::create(&conn) .sort(&sort) + .listing_type(&listing_type) .search_term(q) .show_bot_accounts(show_bot_accounts) .community_id(community_id) @@ -199,6 +201,7 @@ impl Perform for Search { communities = blocking(context.pool(), move |conn| { CommunityQueryBuilder::create(conn) .sort(&sort) + .listing_type(&listing_type) .search_term(q) .my_person_id(person_id) .page(page) @@ -224,6 +227,8 @@ impl Perform for Search { .sort(&sort) .show_nsfw(show_nsfw) .show_bot_accounts(show_bot_accounts) + .listing_type(&listing_type) + .show_nsfw(true) .community_id(community_id) .community_name(community_name) .creator_id(creator_id) @@ -237,14 +242,17 @@ impl Perform for Search { let q = data.q.to_owned(); let sort = SortType::from_str(&data.sort)?; + let listing_type = ListingType::from_str(&data.listing_type)?; + let community_name = data.community_name.to_owned(); comments = blocking(context.pool(), move |conn| { CommentQueryBuilder::create(conn) .sort(&sort) + .listing_type(&listing_type) .search_term(q) .show_bot_accounts(show_bot_accounts) .community_id(community_id) - .community_name(community_name_2) + .community_name(community_name) .creator_id(creator_id) .my_person_id(person_id) .page(page) @@ -255,10 +263,12 @@ impl Perform for Search { let q = data.q.to_owned(); let sort = SortType::from_str(&data.sort)?; + let listing_type = ListingType::from_str(&data.listing_type)?; communities = blocking(context.pool(), move |conn| { CommunityQueryBuilder::create(conn) .sort(&sort) + .listing_type(&listing_type) .search_term(q) .my_person_id(person_id) .page(page) @@ -286,6 +296,8 @@ impl Perform for Search { .sort(&sort) .show_nsfw(show_nsfw) .show_bot_accounts(show_bot_accounts) + .listing_type(&listing_type) + .show_nsfw(true) .my_person_id(person_id) .community_id(community_id) .community_name(community_name) diff --git a/crates/api_common/src/site.rs b/crates/api_common/src/site.rs index 6d3ebf63e..b244b58a9 100644 --- a/crates/api_common/src/site.rs +++ b/crates/api_common/src/site.rs @@ -27,6 +27,7 @@ pub struct Search { pub community_name: Option, pub creator_id: Option, pub sort: String, + pub listing_type: String, pub page: Option, pub limit: Option, pub auth: Option, diff --git a/crates/api_crud/src/comment/read.rs b/crates/api_crud/src/comment/read.rs index 4d1443820..70fe964c3 100644 --- a/crates/api_crud/src/comment/read.rs +++ b/crates/api_crud/src/comment/read.rs @@ -27,7 +27,7 @@ impl PerformCrud for GetComments { let show_bot_accounts = user_show_bot_accounts(&local_user_view); let person_id = local_user_view.map(|u| u.person.id); - let type_ = ListingType::from_str(&data.type_)?; + let listing_type = ListingType::from_str(&data.type_)?; let sort = SortType::from_str(&data.sort)?; let community_id = data.community_id; @@ -37,7 +37,7 @@ impl PerformCrud for GetComments { let limit = data.limit; let comments = blocking(context.pool(), move |conn| { CommentQueryBuilder::create(conn) - .listing_type(type_) + .listing_type(&listing_type) .sort(&sort) .saved_only(saved_only) .community_id(community_id) diff --git a/crates/db_views/src/comment_view.rs b/crates/db_views/src/comment_view.rs index b85b1c77a..cf12442f6 100644 --- a/crates/db_views/src/comment_view.rs +++ b/crates/db_views/src/comment_view.rs @@ -172,7 +172,7 @@ impl CommentView { pub struct CommentQueryBuilder<'a> { conn: &'a PgConnection, - listing_type: ListingType, + listing_type: &'a ListingType, sort: &'a SortType, community_id: Option, community_name: Option, @@ -192,7 +192,7 @@ impl<'a> CommentQueryBuilder<'a> { pub fn create(conn: &'a PgConnection) -> Self { CommentQueryBuilder { conn, - listing_type: ListingType::All, + listing_type: &ListingType::All, sort: &SortType::New, community_id: None, community_name: None, @@ -209,7 +209,7 @@ impl<'a> CommentQueryBuilder<'a> { } } - pub fn listing_type(mut self, listing_type: ListingType) -> Self { + pub fn listing_type(mut self, listing_type: &'a ListingType) -> Self { self.listing_type = listing_type; self }