From d2995d4d5fea2fe7deea280dd3dec36a879bd33f Mon Sep 17 00:00:00 2001 From: Dessalines Date: Thu, 19 Sep 2024 17:38:35 -0400 Subject: [PATCH] Removing duplicate Url return type for search (was actually post). - This now works like the post_title_only filter. --- crates/api_common/src/site.rs | 1 + crates/api_common/src/utils.rs | 12 +++++++ crates/api_crud/src/post/read.rs | 3 +- crates/apub/src/api/list_posts.rs | 8 ++--- crates/apub/src/api/search.rs | 54 +++++++++++-------------------- crates/db_schema/src/lib.rs | 1 - crates/db_views/src/post_view.rs | 28 ++++++++-------- 7 files changed, 50 insertions(+), 57 deletions(-) diff --git a/crates/api_common/src/site.rs b/crates/api_common/src/site.rs index 78e538e29..fc7ececc9 100644 --- a/crates/api_common/src/site.rs +++ b/crates/api_common/src/site.rs @@ -79,6 +79,7 @@ pub struct Search { pub page: Option, pub limit: Option, pub post_title_only: Option, + pub post_url_only: Option, pub saved_only: Option, pub liked_only: Option, pub disliked_only: Option, diff --git a/crates/api_common/src/utils.rs b/crates/api_common/src/utils.rs index 0e790fe99..c93000152 100644 --- a/crates/api_common/src/utils.rs +++ b/crates/api_common/src/utils.rs @@ -999,6 +999,18 @@ fn limit_expire_time(expires: DateTime) -> LemmyResult } } +#[tracing::instrument(skip_all)] +pub fn check_conflicting_like_filters( + liked_only: Option, + disliked_only: Option, +) -> LemmyResult<()> { + if liked_only.unwrap_or_default() && disliked_only.unwrap_or_default() { + Err(LemmyErrorType::ContradictingFilters)? + } else { + Ok(()) + } +} + pub async fn process_markdown( text: &str, slur_regex: &Option, diff --git a/crates/api_crud/src/post/read.rs b/crates/api_crud/src/post/read.rs index 4ecf24e0c..680f14b36 100644 --- a/crates/api_crud/src/post/read.rs +++ b/crates/api_crud/src/post/read.rs @@ -91,7 +91,8 @@ pub async fn get_post( // Fetch the cross_posts let cross_posts = if let Some(url) = &post_view.post.url { let mut x_posts = PostQuery { - url_search: Some(url.inner().as_str().into()), + url_only: Some(true), + search_term: Some(url.inner().as_str().into()), local_user: local_user.as_ref(), ..Default::default() } diff --git a/crates/apub/src/api/list_posts.rs b/crates/apub/src/api/list_posts.rs index 9b504dbe3..d75a82d3b 100644 --- a/crates/apub/src/api/list_posts.rs +++ b/crates/apub/src/api/list_posts.rs @@ -8,14 +8,14 @@ use actix_web::web::{Json, Query}; use lemmy_api_common::{ context::LemmyContext, post::{GetPosts, GetPostsResponse}, - utils::check_private_instance, + utils::{check_conflicting_like_filters, check_private_instance}, }; use lemmy_db_schema::source::community::Community; use lemmy_db_views::{ post_view::PostQuery, structs::{LocalUserView, PaginationCursor, SiteView}, }; -use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType, LemmyResult}; +use lemmy_utils::error::{LemmyErrorExt, LemmyErrorType, LemmyResult}; #[tracing::instrument(skip(context))] pub async fn list_posts( @@ -45,9 +45,7 @@ pub async fn list_posts( let liked_only = data.liked_only; let disliked_only = data.disliked_only; - if liked_only.unwrap_or_default() && disliked_only.unwrap_or_default() { - return Err(LemmyError::from(LemmyErrorType::ContradictingFilters)); - } + check_conflicting_like_filters(liked_only, disliked_only)?; let local_user = local_user_view.as_ref().map(|u| &u.local_user); let listing_type = Some(listing_type_with_default( diff --git a/crates/apub/src/api/search.rs b/crates/apub/src/api/search.rs index b6d82e21c..b9d32193c 100644 --- a/crates/apub/src/api/search.rs +++ b/crates/apub/src/api/search.rs @@ -4,7 +4,7 @@ use actix_web::web::{Json, Query}; use lemmy_api_common::{ context::LemmyContext, site::{Search, SearchResponse}, - utils::{check_private_instance, is_admin}, + utils::{check_conflicting_like_filters, check_private_instance, is_admin}, }; use lemmy_db_schema::{source::community::Community, utils::post_to_comment_sort_type, SearchType}; use lemmy_db_views::{ @@ -13,10 +13,7 @@ use lemmy_db_views::{ structs::{LocalUserView, SiteView}, }; use lemmy_db_views_actor::{community_view::CommunityQuery, person_view::PersonQuery}; -use lemmy_utils::{ - error::{LemmyError, LemmyResult}, - LemmyErrorType, -}; +use lemmy_utils::error::LemmyResult; #[tracing::instrument(skip(context))] pub async fn search( @@ -58,13 +55,12 @@ 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 post_url_only = data.post_url_only; let saved_only = data.saved_only; let liked_only = data.liked_only; let disliked_only = data.disliked_only; - if liked_only.unwrap_or_default() && disliked_only.unwrap_or_default() { - return Err(LemmyError::from(LemmyErrorType::ContradictingFilters)); - } + check_conflicting_like_filters(liked_only, disliked_only)?; let posts_query = PostQuery { sort, @@ -72,10 +68,11 @@ pub async fn search( community_id, creator_id, local_user, - search_term: (Some(q.clone())), + search_term: Some(q.clone()), page, limit, title_only: post_title_only, + url_only: post_url_only, liked_only, disliked_only, saved_only, @@ -83,9 +80,9 @@ pub async fn search( }; let comment_query = CommentQuery { - sort: (sort.map(post_to_comment_sort_type)), + sort: sort.map(post_to_comment_sort_type), listing_type, - search_term: (Some(q.clone())), + search_term: Some(q.clone()), community_id, creator_id, local_user, @@ -98,22 +95,22 @@ pub async fn search( }; let community_query = CommunityQuery { - sort: (sort), - listing_type: (listing_type), - search_term: (Some(q.clone())), + sort, + listing_type, + search_term: Some(q.clone()), local_user, - is_mod_or_admin: (is_admin), - page: (page), - limit: (limit), + is_mod_or_admin: is_admin, + page, + limit, ..Default::default() }; let person_query = PersonQuery { sort, - search_term: (Some(q.clone())), - listing_type: (listing_type), - page: (page), - limit: (limit), + search_term: Some(q.clone()), + listing_type, + page, + limit, }; match search_type { @@ -158,21 +155,6 @@ pub async fn search( person_query.list(&mut context.pool()).await? }; } - SearchType::Url => { - posts = PostQuery { - sort: (sort), - listing_type: (listing_type), - community_id: (community_id), - creator_id: (creator_id), - url_search: (Some(q)), - local_user, - page: (page), - limit: (limit), - ..Default::default() - } - .list(&local_site.site, &mut context.pool()) - .await?; - } }; // Return the jwt diff --git a/crates/db_schema/src/lib.rs b/crates/db_schema/src/lib.rs index 1e3d96a1d..7c57ddf28 100644 --- a/crates/db_schema/src/lib.rs +++ b/crates/db_schema/src/lib.rs @@ -182,7 +182,6 @@ pub enum SearchType { Posts, Communities, Users, - Url, } #[derive(EnumString, Display, Debug, PartialEq, Eq, Serialize, Deserialize, Clone, Copy, Hash)] diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index 3eab04af5..8227e3854 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -382,22 +382,22 @@ fn queries<'a>() -> Queries< query = query.filter(community::hidden.eq(false)); } - if let Some(url_search) = &options.url_search { - query = query.filter(post::url.eq(url_search)); - } - if let Some(search_term) = &options.search_term { - let searcher = fuzzy_search(search_term); - query = if options.title_only.unwrap_or_default() { - query.filter(post::name.ilike(searcher)) + if options.url_only.unwrap_or_default() { + query = query.filter(post::url.eq(search_term)); } else { - query.filter( - post::name - .ilike(searcher.clone()) - .or(post::body.ilike(searcher)), - ) + let searcher = fuzzy_search(search_term); + 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,7 +617,7 @@ pub struct PostQuery<'a> { pub community_id_just_for_prefetch: bool, pub local_user: Option<&'a LocalUser>, pub search_term: Option, - pub url_search: Option, + pub url_only: Option, pub saved_only: Option, pub liked_only: Option, pub disliked_only: Option,