Support calling /api/v1/accounts/search with "resolve" parameter

This commit is contained in:
silverpill 2023-04-02 22:12:45 +00:00
parent edebae0dc6
commit 59e5f12016
5 changed files with 39 additions and 6 deletions

View file

@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## [Unreleased] ## [Unreleased]
### Added
- Support calling `/api/v1/accounts/search` with `resolve` parameter.
### Changed ### Changed
- Increase maximum number of custom emojis per post to 50. - Increase maximum number of custom emojis per post to 50.

View file

@ -352,6 +352,12 @@ paths:
required: true required: true
schema: schema:
type: string type: string
- name: resolve
in: query
description: Attempt WebFinger lookup.
required: false
schema:
type: boolean
- name: limit - name: limit
in: query in: query
description: Maximum number of results. Defaults to 40. description: Maximum number of results. Defaults to 40.

View file

@ -470,6 +470,9 @@ fn default_search_page_size() -> PageSize { PageSize::new(40) }
pub struct SearchAcctQueryParams { pub struct SearchAcctQueryParams {
pub q: String, pub q: String,
#[serde(default)]
pub resolve: bool,
#[serde(default = "default_search_page_size")] #[serde(default = "default_search_page_size")]
pub limit: PageSize, pub limit: PageSize,
} }

View file

@ -512,15 +512,29 @@ async fn lookup_acct(
#[get("/search")] #[get("/search")]
async fn search_by_acct( async fn search_by_acct(
auth: Option<BearerAuth>,
connection_info: ConnectionInfo, connection_info: ConnectionInfo,
config: web::Data<Config>, config: web::Data<Config>,
db_pool: web::Data<DbPool>, db_pool: web::Data<DbPool>,
query_params: web::Query<SearchAcctQueryParams>, query_params: web::Query<SearchAcctQueryParams>,
) -> Result<HttpResponse, MastodonError> { ) -> Result<HttpResponse, MastodonError> {
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( let profiles = search_profiles_only(
&config,
db_client, db_client,
&query_params.q, &query_params.q,
query_params.resolve,
query_params.limit.inner(), query_params.limit.inner(),
).await?; ).await?;
let base_url = get_request_base_url(connection_info); let base_url = get_request_base_url(connection_info);

View file

@ -107,6 +107,7 @@ async fn search_profiles_or_import(
db_client: &mut impl DatabaseClient, db_client: &mut impl DatabaseClient,
username: String, username: String,
mut maybe_hostname: Option<String>, mut maybe_hostname: Option<String>,
resolve: bool,
limit: u16, limit: u16,
) -> Result<Vec<DbActorProfile>, DatabaseError> { ) -> Result<Vec<DbActorProfile>, DatabaseError> {
let mut instance = config.instance(); let mut instance = config.instance();
@ -122,7 +123,7 @@ async fn search_profiles_or_import(
maybe_hostname.as_ref(), maybe_hostname.as_ref(),
limit, limit,
).await?; ).await?;
if profiles.is_empty() { if profiles.is_empty() && resolve {
if let Some(hostname) = maybe_hostname { if let Some(hostname) = maybe_hostname {
let actor_address = ActorAddress { username, hostname }; let actor_address = ActorAddress { username, hostname };
instance.fetcher_timeout = SEARCH_FETCHER_TIMEOUT; instance.fetcher_timeout = SEARCH_FETCHER_TIMEOUT;
@ -238,6 +239,7 @@ pub async fn search(
db_client, db_client,
username, username,
maybe_hostname, maybe_hostname,
true,
limit, limit,
).await?; ).await?;
}, },
@ -287,18 +289,22 @@ pub async fn search(
} }
pub async fn search_profiles_only( pub async fn search_profiles_only(
db_client: &impl DatabaseClient, config: &Config,
db_client: &mut impl DatabaseClient,
search_query: &str, search_query: &str,
resolve: bool,
limit: u16, limit: u16,
) -> Result<Vec<DbActorProfile>, DatabaseError> { ) -> Result<Vec<DbActorProfile>, DatabaseError> {
let (username, maybe_hostname) = match parse_profile_query(search_query) { let (username, maybe_hostname) = match parse_profile_query(search_query) {
Ok(result) => result, Ok(result) => result,
Err(_) => return Ok(vec![]), Err(_) => return Ok(vec![]),
}; };
let profiles = search_profiles( let profiles = search_profiles_or_import(
config,
db_client, db_client,
&username, username,
maybe_hostname.as_ref(), maybe_hostname,
resolve,
limit, limit,
).await?; ).await?;
Ok(profiles) Ok(profiles)