From fa192f16bd1af8d2524236854888adfe70e0a213 Mon Sep 17 00:00:00 2001 From: Carlos Cabello Date: Mon, 16 Sep 2024 01:59:09 -0600 Subject: [PATCH] Add option to search exclusively by post title (#5015) * Add option to search exclusively by post title * Address format issues * Remove duplicated 'removed' filter * Replace url_search with search_term * Build generic PostQuery before search match * Create default queries. Move title_only to Search struct. Rename Url to PostURL * Revert PostUrl to Url --- crates/api_common/src/site.rs | 1 + crates/apub/src/api/search.rs | 163 ++++++++++++------------------- crates/db_views/src/post_view.rs | 10 +- 3 files changed, 69 insertions(+), 105 deletions(-) diff --git a/crates/api_common/src/site.rs b/crates/api_common/src/site.rs index 5311eca9c..fa43f2a39 100644 --- a/crates/api_common/src/site.rs +++ b/crates/api_common/src/site.rs @@ -77,6 +77,7 @@ pub struct Search { pub listing_type: Option, pub page: Option, pub limit: Option, + pub post_title_only: Option, } #[derive(Debug, Serialize, Deserialize, Clone)] diff --git a/crates/apub/src/api/search.rs b/crates/apub/src/api/search.rs index a048b64a7..905886913 100644 --- a/crates/apub/src/api/search.rs +++ b/crates/apub/src/api/search.rs @@ -56,133 +56,92 @@ pub async fn search( }; let creator_id = data.creator_id; let local_user = local_user_view.as_ref().map(|l| &l.local_user); + let post_title_only = data.post_title_only; + + let posts_query = PostQuery { + sort: (sort), + listing_type: (listing_type), + community_id: (community_id), + creator_id: (creator_id), + local_user, + search_term: (Some(q.clone())), + page: (page), + limit: (limit), + title_only: (post_title_only), + ..Default::default() + }; + + let comment_query = CommentQuery { + sort: (sort.map(post_to_comment_sort_type)), + listing_type: (listing_type), + search_term: (Some(q.clone())), + community_id: (community_id), + creator_id: (creator_id), + local_user, + page: (page), + limit: (limit), + ..Default::default() + }; + + let community_query = CommunityQuery { + sort: (sort), + listing_type: (listing_type), + search_term: (Some(q.clone())), + local_user, + is_mod_or_admin: (is_admin), + page: (page), + limit: (limit), + ..Default::default() + }; + + let person_query = PersonQuery { + sort, + search_term: (Some(q.clone())), + listing_type: (listing_type), + page: (page), + limit: (limit), + }; match search_type { SearchType::Posts => { - posts = PostQuery { - sort: (sort), - listing_type: (listing_type), - community_id: (community_id), - creator_id: (creator_id), - local_user, - search_term: (Some(q)), - page: (page), - limit: (limit), - ..Default::default() - } - .list(&local_site.site, &mut context.pool()) - .await?; + posts = posts_query + .list(&local_site.site, &mut context.pool()) + .await?; } SearchType::Comments => { - 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, - page: (page), - limit: (limit), - ..Default::default() - } - .list(&mut context.pool()) - .await?; + comments = comment_query.list(&mut context.pool()).await?; } SearchType::Communities => { - communities = CommunityQuery { - sort: (sort), - listing_type: (listing_type), - search_term: (Some(q)), - local_user, - is_mod_or_admin: (is_admin), - page: (page), - limit: (limit), - ..Default::default() - } - .list(&local_site.site, &mut context.pool()) - .await?; + communities = community_query + .list(&local_site.site, &mut context.pool()) + .await?; } SearchType::Users => { - users = PersonQuery { - sort, - search_term: (Some(q)), - listing_type: (listing_type), - page: (page), - limit: (limit), - } - .list(&mut context.pool()) - .await?; + users = person_query.list(&mut context.pool()).await?; } SearchType::All => { // If the community or creator is included, dont search communities or users let community_or_creator_included = data.community_id.is_some() || data.community_name.is_some() || data.creator_id.is_some(); - let q = data.q.clone(); + posts = posts_query + .list(&local_site.site, &mut context.pool()) + .await?; - posts = PostQuery { - sort: (sort), - listing_type: (listing_type), - community_id: (community_id), - creator_id: (creator_id), - local_user, - search_term: (Some(q)), - page: (page), - limit: (limit), - ..Default::default() - } - .list(&local_site.site, &mut context.pool()) - .await?; - - let q = data.q.clone(); - - 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, - page: (page), - limit: (limit), - ..Default::default() - } - .list(&mut context.pool()) - .await?; - - let q = data.q.clone(); + comments = comment_query.list(&mut context.pool()).await?; communities = if community_or_creator_included { vec![] } else { - CommunityQuery { - sort: (sort), - listing_type: (listing_type), - search_term: (Some(q)), - local_user, - is_mod_or_admin: (is_admin), - page: (page), - limit: (limit), - ..Default::default() - } - .list(&local_site.site, &mut context.pool()) - .await? + community_query + .list(&local_site.site, &mut context.pool()) + .await? }; - let q = data.q.clone(); - users = if community_or_creator_included { vec![] } else { - PersonQuery { - sort, - search_term: (Some(q)), - listing_type: (listing_type), - page: (page), - limit: (limit), - } - .list(&mut context.pool()) - .await? + person_query.list(&mut context.pool()).await? }; } SearchType::Url => { diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index 0ec7e0a5d..df6216bf7 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -387,13 +387,16 @@ fn queries<'a>() -> Queries< if let Some(search_term) = &options.search_term { let searcher = fuzzy_search(search_term); - query = query - .filter( + query = if options.title_only.unwrap_or_default() { + query.filter(post::name.ilike(searcher)) + } else { + query.filter( post::name .ilike(searcher.clone()) .or(post::body.ilike(searcher)), ) - .filter(not(post::removed.or(post::deleted))); + } + .filter(not(post::removed.or(post::deleted))); } if !options @@ -617,6 +620,7 @@ pub struct PostQuery<'a> { pub saved_only: Option, pub liked_only: Option, pub disliked_only: Option, + pub title_only: Option, pub page: Option, pub limit: Option, pub page_after: Option,