From 28fad8986c7dc5a2bfad5d6acc8c8f6e2472f92b Mon Sep 17 00:00:00 2001 From: silverpill Date: Fri, 27 May 2022 22:07:03 +0000 Subject: [PATCH] Limit number of profiles in search results --- src/mastodon_api/search/helpers.rs | 17 +++++++++++++++-- src/mastodon_api/search/types.rs | 7 ++++++- src/mastodon_api/search/views.rs | 1 + src/models/profiles/queries.rs | 4 +++- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/mastodon_api/search/helpers.rs b/src/mastodon_api/search/helpers.rs index e43010d..c631a57 100644 --- a/src/mastodon_api/search/helpers.rs +++ b/src/mastodon_api/search/helpers.rs @@ -69,6 +69,7 @@ async fn search_profiles( db_client: &impl GenericClient, username: String, mut instance: Option, + limit: i64, ) -> Result, HttpError> { if let Some(ref actor_host) = instance { if actor_host == &config.instance().host() { @@ -76,7 +77,12 @@ async fn search_profiles( instance = None; }; }; - let mut profiles = search_profile(db_client, &username, instance.as_ref()).await?; + let mut profiles = search_profile( + db_client, + &username, + instance.as_ref(), + limit, + ).await?; if profiles.is_empty() && instance.is_some() { let actor_address = ActorAddress { username: username, @@ -125,12 +131,19 @@ pub async fn search( current_user: &User, db_client: &mut impl GenericClient, search_query: &str, + limit: i64, ) -> Result { let mut profiles = vec![]; let mut posts = vec![]; match parse_search_query(search_query) { SearchQuery::ProfileQuery(username, instance) => { - profiles = search_profiles(config, db_client, username, instance).await?; + profiles = search_profiles( + config, + db_client, + username, + instance, + limit, + ).await?; }, SearchQuery::Url(url) => { let maybe_post = search_post(config, db_client, url).await?; diff --git a/src/mastodon_api/search/types.rs b/src/mastodon_api/search/types.rs index 8edfb22..fec7a3a 100644 --- a/src/mastodon_api/search/types.rs +++ b/src/mastodon_api/search/types.rs @@ -1,12 +1,17 @@ +/// https://docs.joinmastodon.org/methods/search/ use serde::{Deserialize, Serialize}; use crate::mastodon_api::accounts::types::Account; use crate::mastodon_api::statuses::types::Status; -/// https://docs.joinmastodon.org/methods/search/ +fn default_limit() -> i64 { 20 } + #[derive(Deserialize)] pub struct SearchQueryParams { pub q: String, + + #[serde(default = "default_limit")] + pub limit: i64, } #[derive(Serialize)] diff --git a/src/mastodon_api/search/views.rs b/src/mastodon_api/search/views.rs index f9aed4e..3446ab4 100644 --- a/src/mastodon_api/search/views.rs +++ b/src/mastodon_api/search/views.rs @@ -23,6 +23,7 @@ async fn search_view( ¤t_user, db_client, query_params.q.trim(), + query_params.limit, ).await?; Ok(HttpResponse::Ok().json(results)) } diff --git a/src/models/profiles/queries.rs b/src/models/profiles/queries.rs index 2a796c0..ae96981 100644 --- a/src/models/profiles/queries.rs +++ b/src/models/profiles/queries.rs @@ -344,6 +344,7 @@ pub async fn search_profile( db_client: &impl GenericClient, username: &str, instance: Option<&String>, + limit: i64, ) -> Result, DatabaseError> { let db_search_query = match instance { Some(instance) => { @@ -360,8 +361,9 @@ pub async fn search_profile( SELECT actor_profile FROM actor_profile WHERE acct ILIKE $1 + LIMIT $2 ", - &[&db_search_query], + &[&db_search_query, &limit], ).await?; let profiles: Vec = rows.iter() .map(|row| row.try_get("actor_profile"))