Support calling /api/v1/accounts/search with "resolve" parameter
This commit is contained in:
parent
edebae0dc6
commit
59e5f12016
5 changed files with 39 additions and 6 deletions
|
@ -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.
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
|
|
|
@ -512,15 +512,29 @@ async fn lookup_acct(
|
|||
|
||||
#[get("/search")]
|
||||
async fn search_by_acct(
|
||||
auth: Option<BearerAuth>,
|
||||
connection_info: ConnectionInfo,
|
||||
config: web::Data<Config>,
|
||||
db_pool: web::Data<DbPool>,
|
||||
query_params: web::Query<SearchAcctQueryParams>,
|
||||
) -> 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(
|
||||
&config,
|
||||
db_client,
|
||||
&query_params.q,
|
||||
query_params.resolve,
|
||||
query_params.limit.inner(),
|
||||
).await?;
|
||||
let base_url = get_request_base_url(connection_info);
|
||||
|
|
|
@ -107,6 +107,7 @@ async fn search_profiles_or_import(
|
|||
db_client: &mut impl DatabaseClient,
|
||||
username: String,
|
||||
mut maybe_hostname: Option<String>,
|
||||
resolve: bool,
|
||||
limit: u16,
|
||||
) -> Result<Vec<DbActorProfile>, 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<Vec<DbActorProfile>, 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)
|
||||
|
|
Loading…
Reference in a new issue