From 7ccd29abf2963af890c29c5b4e76fffce57b72f2 Mon Sep 17 00:00:00 2001 From: silverpill Date: Wed, 28 Dec 2022 00:13:49 +0000 Subject: [PATCH] Add /api/v1/accounts/lookup Mastodon API endpoint --- CHANGELOG.md | 4 ++++ docs/openapi.yaml | 19 +++++++++++++++++++ src/mastodon_api/accounts/types.rs | 5 +++++ src/mastodon_api/accounts/views.rs | 14 ++++++++++++++ src/mastodon_api/mod.rs | 2 +- 5 files changed, 43 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 465fbb3..d2173b5 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 + +- Added `/api/v1/accounts/lookup` Mastodon API endpoint. + ### Changed - Updated installation instructions, default mitra config and recommended nginx config. diff --git a/docs/openapi.yaml b/docs/openapi.yaml index 04cc79b..b7b0ff9 100644 --- a/docs/openapi.yaml +++ b/docs/openapi.yaml @@ -345,6 +345,25 @@ paths: type: array items: $ref: '#/components/schemas/Relationship' + /api/v1/accounts/lookup: + get: + summary: Lookup webfinger address. + parameters: + - name: acct + in: query + description: The username or Webfinger address to lookup. + required: true + schema: + type: string + responses: + 200: + description: Successful operation + content: + application/json: + schema: + $ref: '#/components/schemas/Account' + 404: + description: Profile not found /api/v1/accounts/search: get: summary: Search for matching profiles by username. diff --git a/src/mastodon_api/accounts/types.rs b/src/mastodon_api/accounts/types.rs index 0ec3810..cb22c41 100644 --- a/src/mastodon_api/accounts/types.rs +++ b/src/mastodon_api/accounts/types.rs @@ -379,6 +379,11 @@ impl Default for RelationshipMap { } } +#[derive(Deserialize)] +pub struct LookupAcctQueryParams { + pub acct: String, +} + fn default_search_page_size() -> PageSize { PageSize::new(40) } #[derive(Deserialize)] diff --git a/src/mastodon_api/accounts/views.rs b/src/mastodon_api/accounts/views.rs index 8efe4f2..46c645d 100644 --- a/src/mastodon_api/accounts/views.rs +++ b/src/mastodon_api/accounts/views.rs @@ -105,6 +105,7 @@ use super::types::{ IdentityClaim, IdentityClaimQueryParams, IdentityProofData, + LookupAcctQueryParams, MoveFollowersRequest, RelationshipQueryParams, SearchAcctQueryParams, @@ -552,6 +553,18 @@ async fn get_relationships_view( Ok(HttpResponse::Ok().json(vec![relationship])) } +#[get("/lookup")] +async fn lookup_acct( + config: web::Data, + db_pool: web::Data, + query_params: web::Query, +) -> Result { + let db_client = &**get_database_client(&db_pool).await?; + let profile = get_profile_by_acct(db_client, &query_params.acct).await?; + let account = Account::from_profile(profile, &config.instance_url()); + Ok(HttpResponse::Ok().json(account)) +} + #[get("/search")] async fn search_by_acct( config: web::Data, @@ -839,6 +852,7 @@ pub fn account_api_scope() -> Scope { .service(get_identity_claim) .service(create_identity_proof) .service(get_relationships_view) + .service(lookup_acct) .service(search_by_acct) .service(search_by_did) // Routes with account ID diff --git a/src/mastodon_api/mod.rs b/src/mastodon_api/mod.rs index 127c9b4..455679a 100644 --- a/src/mastodon_api/mod.rs +++ b/src/mastodon_api/mod.rs @@ -13,5 +13,5 @@ pub mod subscriptions; pub mod timelines; mod uploads; -const MASTODON_API_VERSION: &str = "3.0.0"; +const MASTODON_API_VERSION: &str = "4.0.0"; pub use uploads::UPLOAD_MAX_SIZE;