From 59e5f12016e2a29d4cc4ccfbd25768dfd8f67899 Mon Sep 17 00:00:00 2001 From: silverpill Date: Sun, 2 Apr 2023 22:12:45 +0000 Subject: [PATCH] Support calling /api/v1/accounts/search with "resolve" parameter --- CHANGELOG.md | 4 ++++ docs/openapi.yaml | 6 ++++++ src/mastodon_api/accounts/types.rs | 3 +++ src/mastodon_api/accounts/views.rs | 16 +++++++++++++++- src/mastodon_api/search/helpers.rs | 16 +++++++++++----- 5 files changed, 39 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c3f37a6..1737117 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## [Unreleased] +### Added + +- Support calling `/api/v1/accounts/search` with `resolve` parameter. + ### Changed - Increase maximum number of custom emojis per post to 50. diff --git a/docs/openapi.yaml b/docs/openapi.yaml index 1f1443a..813a35e 100644 --- a/docs/openapi.yaml +++ b/docs/openapi.yaml @@ -352,6 +352,12 @@ paths: required: true schema: type: string + - name: resolve + in: query + description: Attempt WebFinger lookup. + required: false + schema: + type: boolean - name: limit in: query description: Maximum number of results. Defaults to 40. diff --git a/src/mastodon_api/accounts/types.rs b/src/mastodon_api/accounts/types.rs index 93a1572..681d356 100644 --- a/src/mastodon_api/accounts/types.rs +++ b/src/mastodon_api/accounts/types.rs @@ -470,6 +470,9 @@ fn default_search_page_size() -> PageSize { PageSize::new(40) } pub struct SearchAcctQueryParams { pub q: String, + #[serde(default)] + pub resolve: bool, + #[serde(default = "default_search_page_size")] pub limit: PageSize, } diff --git a/src/mastodon_api/accounts/views.rs b/src/mastodon_api/accounts/views.rs index f97d6ae..4baf3e1 100644 --- a/src/mastodon_api/accounts/views.rs +++ b/src/mastodon_api/accounts/views.rs @@ -512,15 +512,29 @@ async fn lookup_acct( #[get("/search")] async fn search_by_acct( + auth: Option, connection_info: ConnectionInfo, config: web::Data, db_pool: web::Data, query_params: web::Query, ) -> Result { - let db_client = &**get_database_client(&db_pool).await?; + let db_client = &mut **get_database_client(&db_pool).await?; + match auth { + Some(auth) => { + get_current_user(db_client, auth.token()).await?; + }, + None => { + // Only authorized users can make webfinger queries + if query_params.resolve { + return Err(MastodonError::PermissionError); + }; + }, + }; let profiles = search_profiles_only( + &config, db_client, &query_params.q, + query_params.resolve, query_params.limit.inner(), ).await?; let base_url = get_request_base_url(connection_info); diff --git a/src/mastodon_api/search/helpers.rs b/src/mastodon_api/search/helpers.rs index 65a7c33..ee3e167 100644 --- a/src/mastodon_api/search/helpers.rs +++ b/src/mastodon_api/search/helpers.rs @@ -107,6 +107,7 @@ async fn search_profiles_or_import( db_client: &mut impl DatabaseClient, username: String, mut maybe_hostname: Option, + resolve: bool, limit: u16, ) -> Result, DatabaseError> { let mut instance = config.instance(); @@ -122,7 +123,7 @@ async fn search_profiles_or_import( maybe_hostname.as_ref(), limit, ).await?; - if profiles.is_empty() { + if profiles.is_empty() && resolve { if let Some(hostname) = maybe_hostname { let actor_address = ActorAddress { username, hostname }; instance.fetcher_timeout = SEARCH_FETCHER_TIMEOUT; @@ -238,6 +239,7 @@ pub async fn search( db_client, username, maybe_hostname, + true, limit, ).await?; }, @@ -287,18 +289,22 @@ pub async fn search( } pub async fn search_profiles_only( - db_client: &impl DatabaseClient, + config: &Config, + db_client: &mut impl DatabaseClient, search_query: &str, + resolve: bool, limit: u16, ) -> Result, DatabaseError> { let (username, maybe_hostname) = match parse_profile_query(search_query) { Ok(result) => result, Err(_) => return Ok(vec![]), }; - let profiles = search_profiles( + let profiles = search_profiles_or_import( + config, db_client, - &username, - maybe_hostname.as_ref(), + username, + maybe_hostname, + resolve, limit, ).await?; Ok(profiles)