From acb139b0ee0808654e53cb3926b9eace59c79154 Mon Sep 17 00:00:00 2001 From: silverpill Date: Thu, 4 Aug 2022 11:14:54 +0000 Subject: [PATCH] Add API method for searching profiles by DID --- docs/openapi.yaml | 22 +++++++++++++++ src/mastodon_api/accounts/types.rs | 5 ++++ src/mastodon_api/accounts/views.rs | 45 +++++++++++++++++++++--------- 3 files changed, 59 insertions(+), 13 deletions(-) diff --git a/docs/openapi.yaml b/docs/openapi.yaml index d5d096e..1d8ab6a 100644 --- a/docs/openapi.yaml +++ b/docs/openapi.yaml @@ -196,6 +196,28 @@ paths: type: array items: $ref: '#/components/schemas/Relationship' + /api/v1/accounts/search_did: + get: + summary: Search profile by DID + parameters: + - name: did + in: query + description: Decentralized identifier (DID) + required: true + schema: + type: string + responses: + 200: + description: Successful operation + content: + application/json: + schema: + description: Profile list + type: array + items: + $ref: '#/components/schemas/Account' + 400: + description: Invalid DID /api/v1/accounts/{account_id}: get: summary: View information about a profile. diff --git a/src/mastodon_api/accounts/types.rs b/src/mastodon_api/accounts/types.rs index c17c58a..f1a5c37 100644 --- a/src/mastodon_api/accounts/types.rs +++ b/src/mastodon_api/accounts/types.rs @@ -293,6 +293,11 @@ impl Default for RelationshipMap { } } +#[derive(Deserialize)] +pub struct SearchDidQueryParams { + pub did: String, +} + #[derive(Deserialize)] pub struct FollowData { #[serde(default = "default_showing_reblogs")] diff --git a/src/mastodon_api/accounts/views.rs b/src/mastodon_api/accounts/views.rs index 9b8a063..38831f5 100644 --- a/src/mastodon_api/accounts/views.rs +++ b/src/mastodon_api/accounts/views.rs @@ -29,6 +29,7 @@ use crate::mastodon_api::statuses::types::Status; use crate::models::posts::queries::get_posts_by_author; use crate::models::profiles::queries::{ get_profile_by_id, + search_profile_by_did, update_profile, }; use crate::models::profiles::types::{ @@ -68,6 +69,7 @@ use super::types::{ IdentityClaim, IdentityProofData, RelationshipQueryParams, + SearchDidQueryParams, StatusListQueryParams, SubscriptionQueryParams, }; @@ -153,18 +155,6 @@ pub async fn create_account( Ok(HttpResponse::Created().json(account)) } -#[get("/{account_id}")] -async fn get_account( - config: web::Data, - db_pool: web::Data, - account_id: web::Path, -) -> Result { - let db_client = &**get_database_client(&db_pool).await?; - let profile = get_profile_by_id(db_client, &account_id).await?; - let account = Account::from_profile(profile, &config.instance_url()); - Ok(HttpResponse::Ok().json(account)) -} - #[get("/verify_credentials")] async fn verify_credentials( auth: BearerAuth, @@ -365,6 +355,34 @@ async fn get_relationships_view( Ok(HttpResponse::Ok().json(vec![relationship])) } +#[get("/search_did")] +async fn search_by_did( + config: web::Data, + db_pool: web::Data, + query_params: web::Query, +) -> Result { + let db_client = &**get_database_client(&db_pool).await?; + let did: DidPkh = query_params.did.parse() + .map_err(|_| ValidationError("invalid DID"))?; + let profiles = search_profile_by_did(db_client, &did, false).await?; + let accounts: Vec = profiles.into_iter() + .map(|profile| Account::from_profile(profile, &config.instance_url())) + .collect(); + Ok(HttpResponse::Ok().json(accounts)) +} + +#[get("/{account_id}")] +async fn get_account( + config: web::Data, + db_pool: web::Data, + account_id: web::Path, +) -> Result { + let db_client = &**get_database_client(&db_pool).await?; + let profile = get_profile_by_id(db_client, &account_id).await?; + let account = Account::from_profile(profile, &config.instance_url()); + Ok(HttpResponse::Ok().json(account)) +} + #[post("/{account_id}/follow")] async fn follow_account( auth: BearerAuth, @@ -548,13 +566,14 @@ pub fn account_api_scope() -> Scope { web::scope("/api/v1/accounts") // Routes without account ID .service(create_account) - .service(get_relationships_view) .service(verify_credentials) .service(update_credentials) .service(get_identity_claim) .service(create_identity_proof) .service(authorize_subscription) .service(subscriptions_enabled) + .service(get_relationships_view) + .service(search_by_did) // Routes with account ID .service(get_account) .service(follow_account)