From 0fab5bed24b4b654452a5cdc2d84cf178b15c4db Mon Sep 17 00:00:00 2001 From: Dessalines Date: Tue, 24 Sep 2024 13:24:28 -0400 Subject: [PATCH] Add ability to search for Community by its description (or title only). (#5044) - This changes the post_title_only for Search to title_only, since its also used in the community query now. - Fixes #4785 --- crates/api_common/src/site.rs | 2 +- crates/apub/src/api/search.rs | 5 +++-- crates/db_views/src/post_view.rs | 10 ++++------ crates/db_views_actor/src/community_view.rs | 12 +++++++++--- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/crates/api_common/src/site.rs b/crates/api_common/src/site.rs index fc7ececc9..1ffabb75a 100644 --- a/crates/api_common/src/site.rs +++ b/crates/api_common/src/site.rs @@ -78,7 +78,7 @@ pub struct Search { pub listing_type: Option, pub page: Option, pub limit: Option, - pub post_title_only: Option, + pub title_only: Option, pub post_url_only: Option, pub saved_only: Option, pub liked_only: Option, diff --git a/crates/apub/src/api/search.rs b/crates/apub/src/api/search.rs index 642c32efa..d9ae20ede 100644 --- a/crates/apub/src/api/search.rs +++ b/crates/apub/src/api/search.rs @@ -47,7 +47,7 @@ pub async fn search( listing_type, page, limit, - post_title_only, + title_only, post_url_only, saved_only, liked_only, @@ -78,7 +78,7 @@ pub async fn search( search_term: Some(q.clone()), page, limit, - title_only: post_title_only, + title_only, url_only: post_url_only, liked_only, disliked_only, @@ -105,6 +105,7 @@ pub async fn search( sort, listing_type, search_term: Some(q.clone()), + title_only, local_user, is_mod_or_admin: is_admin, page, diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index a8d908c7d..1510e9a62 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -394,14 +394,12 @@ fn queries<'a>() -> Queries< query = query.filter(post::url.eq(search_term)); } else { let searcher = fuzzy_search(search_term); + let name_filter = post::name.ilike(searcher.clone()); + let body_filter = post::body.ilike(searcher.clone()); query = if options.title_only.unwrap_or_default() { - query.filter(post::name.ilike(searcher)) + query.filter(name_filter) } else { - query.filter( - post::name - .ilike(searcher.clone()) - .or(post::body.ilike(searcher)), - ) + query.filter(name_filter.or(body_filter)) } .filter(not(post::removed.or(post::deleted))); } diff --git a/crates/db_views_actor/src/community_view.rs b/crates/db_views_actor/src/community_view.rs index a1b387bea..89724b04d 100644 --- a/crates/db_views_actor/src/community_view.rs +++ b/crates/db_views_actor/src/community_view.rs @@ -112,9 +112,14 @@ fn queries<'a>() -> Queries< if let Some(search_term) = options.search_term { let searcher = fuzzy_search(&search_term); - query = query - .filter(community::name.ilike(searcher.clone())) - .or_filter(community::title.ilike(searcher)) + let name_filter = community::name.ilike(searcher.clone()); + let title_filter = community::title.ilike(searcher.clone()); + let description_filter = community::description.ilike(searcher.clone()); + query = if options.title_only.unwrap_or_default() { + query.filter(name_filter.or(title_filter)) + } else { + query.filter(name_filter.or(title_filter.or(description_filter))) + } } // Hide deleted and removed for non-admins or mods @@ -229,6 +234,7 @@ pub struct CommunityQuery<'a> { pub sort: Option, pub local_user: Option<&'a LocalUser>, pub search_term: Option, + pub title_only: Option, pub is_mod_or_admin: bool, pub show_nsfw: bool, pub page: Option,