mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-10-01 01:36:12 -04: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
dea6ee462c
commit
fa192f16bd
@ -77,6 +77,7 @@ pub struct Search {
|
|||||||
pub listing_type: Option<ListingType>,
|
pub listing_type: Option<ListingType>,
|
||||||
pub page: Option<i64>,
|
pub page: Option<i64>,
|
||||||
pub limit: Option<i64>,
|
pub limit: Option<i64>,
|
||||||
|
pub post_title_only: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
|
@ -56,133 +56,92 @@ pub async fn search(
|
|||||||
};
|
};
|
||||||
let creator_id = data.creator_id;
|
let creator_id = data.creator_id;
|
||||||
let local_user = local_user_view.as_ref().map(|l| &l.local_user);
|
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 {
|
match search_type {
|
||||||
SearchType::Posts => {
|
SearchType::Posts => {
|
||||||
posts = PostQuery {
|
posts = posts_query
|
||||||
sort: (sort),
|
.list(&local_site.site, &mut context.pool())
|
||||||
listing_type: (listing_type),
|
.await?;
|
||||||
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?;
|
|
||||||
}
|
}
|
||||||
SearchType::Comments => {
|
SearchType::Comments => {
|
||||||
comments = CommentQuery {
|
comments = comment_query.list(&mut context.pool()).await?;
|
||||||
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?;
|
|
||||||
}
|
}
|
||||||
SearchType::Communities => {
|
SearchType::Communities => {
|
||||||
communities = CommunityQuery {
|
communities = community_query
|
||||||
sort: (sort),
|
.list(&local_site.site, &mut context.pool())
|
||||||
listing_type: (listing_type),
|
.await?;
|
||||||
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?;
|
|
||||||
}
|
}
|
||||||
SearchType::Users => {
|
SearchType::Users => {
|
||||||
users = PersonQuery {
|
users = person_query.list(&mut context.pool()).await?;
|
||||||
sort,
|
|
||||||
search_term: (Some(q)),
|
|
||||||
listing_type: (listing_type),
|
|
||||||
page: (page),
|
|
||||||
limit: (limit),
|
|
||||||
}
|
|
||||||
.list(&mut context.pool())
|
|
||||||
.await?;
|
|
||||||
}
|
}
|
||||||
SearchType::All => {
|
SearchType::All => {
|
||||||
// If the community or creator is included, dont search communities or users
|
// If the community or creator is included, dont search communities or users
|
||||||
let community_or_creator_included =
|
let community_or_creator_included =
|
||||||
data.community_id.is_some() || data.community_name.is_some() || data.creator_id.is_some();
|
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 {
|
comments = comment_query.list(&mut context.pool()).await?;
|
||||||
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();
|
|
||||||
|
|
||||||
communities = if community_or_creator_included {
|
communities = if community_or_creator_included {
|
||||||
vec![]
|
vec![]
|
||||||
} else {
|
} else {
|
||||||
CommunityQuery {
|
community_query
|
||||||
sort: (sort),
|
.list(&local_site.site, &mut context.pool())
|
||||||
listing_type: (listing_type),
|
.await?
|
||||||
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?
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let q = data.q.clone();
|
|
||||||
|
|
||||||
users = if community_or_creator_included {
|
users = if community_or_creator_included {
|
||||||
vec![]
|
vec![]
|
||||||
} else {
|
} else {
|
||||||
PersonQuery {
|
person_query.list(&mut context.pool()).await?
|
||||||
sort,
|
|
||||||
search_term: (Some(q)),
|
|
||||||
listing_type: (listing_type),
|
|
||||||
page: (page),
|
|
||||||
limit: (limit),
|
|
||||||
}
|
|
||||||
.list(&mut context.pool())
|
|
||||||
.await?
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
SearchType::Url => {
|
SearchType::Url => {
|
||||||
|
@ -387,13 +387,16 @@ fn queries<'a>() -> Queries<
|
|||||||
|
|
||||||
if let Some(search_term) = &options.search_term {
|
if let Some(search_term) = &options.search_term {
|
||||||
let searcher = fuzzy_search(search_term);
|
let searcher = fuzzy_search(search_term);
|
||||||
query = query
|
query = if options.title_only.unwrap_or_default() {
|
||||||
.filter(
|
query.filter(post::name.ilike(searcher))
|
||||||
|
} else {
|
||||||
|
query.filter(
|
||||||
post::name
|
post::name
|
||||||
.ilike(searcher.clone())
|
.ilike(searcher.clone())
|
||||||
.or(post::body.ilike(searcher)),
|
.or(post::body.ilike(searcher)),
|
||||||
)
|
)
|
||||||
.filter(not(post::removed.or(post::deleted)));
|
}
|
||||||
|
.filter(not(post::removed.or(post::deleted)));
|
||||||
}
|
}
|
||||||
|
|
||||||
if !options
|
if !options
|
||||||
@ -617,6 +620,7 @@ pub struct PostQuery<'a> {
|
|||||||
pub saved_only: Option<bool>,
|
pub saved_only: Option<bool>,
|
||||||
pub liked_only: Option<bool>,
|
pub liked_only: Option<bool>,
|
||||||
pub disliked_only: Option<bool>,
|
pub disliked_only: Option<bool>,
|
||||||
|
pub title_only: Option<bool>,
|
||||||
pub page: Option<i64>,
|
pub page: Option<i64>,
|
||||||
pub limit: Option<i64>,
|
pub limit: Option<i64>,
|
||||||
pub page_after: Option<PaginationCursorData>,
|
pub page_after: Option<PaginationCursorData>,
|
||||||
|
Loading…
Reference in New Issue
Block a user