mirror of
https://github.com/LemmyNet/lemmy
synced 2024-11-10 06:54:12 +00:00
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
This commit is contained in:
parent
d57d9f6fe1
commit
21e24dc96d
3 changed files with 69 additions and 105 deletions
|
@ -78,6 +78,7 @@ pub struct Search {
|
|||
pub listing_type: Option<ListingType>,
|
||||
pub page: Option<i64>,
|
||||
pub limit: Option<i64>,
|
||||
pub post_title_only: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
|
|
|
@ -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;
|
||||
|
||||
match search_type {
|
||||
SearchType::Posts => {
|
||||
posts = PostQuery {
|
||||
let posts_query = PostQuery {
|
||||
sort: (sort),
|
||||
listing_type: (listing_type),
|
||||
community_id: (community_id),
|
||||
creator_id: (creator_id),
|
||||
local_user,
|
||||
search_term: (Some(q)),
|
||||
search_term: (Some(q.clone())),
|
||||
page: (page),
|
||||
limit: (limit),
|
||||
title_only: (post_title_only),
|
||||
..Default::default()
|
||||
}
|
||||
.list(&local_site.site, &mut context.pool())
|
||||
.await?;
|
||||
}
|
||||
SearchType::Comments => {
|
||||
comments = CommentQuery {
|
||||
};
|
||||
|
||||
let comment_query = CommentQuery {
|
||||
sort: (sort.map(post_to_comment_sort_type)),
|
||||
listing_type: (listing_type),
|
||||
search_term: (Some(q)),
|
||||
search_term: (Some(q.clone())),
|
||||
community_id: (community_id),
|
||||
creator_id: (creator_id),
|
||||
local_user,
|
||||
page: (page),
|
||||
limit: (limit),
|
||||
..Default::default()
|
||||
}
|
||||
.list(&mut context.pool())
|
||||
.await?;
|
||||
}
|
||||
SearchType::Communities => {
|
||||
communities = CommunityQuery {
|
||||
};
|
||||
|
||||
let community_query = CommunityQuery {
|
||||
sort: (sort),
|
||||
listing_type: (listing_type),
|
||||
search_term: (Some(q)),
|
||||
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 = posts_query
|
||||
.list(&local_site.site, &mut context.pool())
|
||||
.await?;
|
||||
}
|
||||
SearchType::Comments => {
|
||||
comments = comment_query.list(&mut context.pool()).await?;
|
||||
}
|
||||
SearchType::Communities => {
|
||||
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 = 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()
|
||||
}
|
||||
posts = posts_query
|
||||
.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()
|
||||
}
|
||||
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 => {
|
||||
|
|
|
@ -387,12 +387,15 @@ 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)));
|
||||
}
|
||||
|
||||
|
@ -617,6 +620,7 @@ pub struct PostQuery<'a> {
|
|||
pub saved_only: Option<bool>,
|
||||
pub liked_only: Option<bool>,
|
||||
pub disliked_only: Option<bool>,
|
||||
pub title_only: Option<bool>,
|
||||
pub page: Option<i64>,
|
||||
pub limit: Option<i64>,
|
||||
pub page_after: Option<PaginationCursorData>,
|
||||
|
|
Loading…
Reference in a new issue