Add API method for searching profiles by DID

This commit is contained in:
silverpill 2022-08-04 11:14:54 +00:00
parent 21df4fa35c
commit acb139b0ee
3 changed files with 59 additions and 13 deletions

View file

@ -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.

View file

@ -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")]

View file

@ -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<Config>,
db_pool: web::Data<Pool>,
account_id: web::Path<Uuid>,
) -> Result<HttpResponse, HttpError> {
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<Config>,
db_pool: web::Data<Pool>,
query_params: web::Query<SearchDidQueryParams>,
) -> Result<HttpResponse, HttpError> {
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<Account> = 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<Config>,
db_pool: web::Data<Pool>,
account_id: web::Path<Uuid>,
) -> Result<HttpResponse, HttpError> {
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)