this repo has no description
1use crate::state::AppState;
2use axum::{
3 Json,
4 extract::{Query, State},
5 http::StatusCode,
6 response::{IntoResponse, Response},
7};
8use serde::Deserialize;
9use serde_json::json;
10
11#[derive(Deserialize)]
12pub struct DescribeRepoInput {
13 pub repo: String,
14}
15
16pub async fn describe_repo(
17 State(state): State<AppState>,
18 Query(input): Query<DescribeRepoInput>,
19) -> Response {
20 let hostname = std::env::var("PDS_HOSTNAME").unwrap_or_else(|_| "localhost".to_string());
21 let user_row = if input.repo.starts_with("did:") {
22 sqlx::query!(
23 "SELECT id, handle, did FROM users WHERE did = $1",
24 input.repo
25 )
26 .fetch_optional(&state.db)
27 .await
28 .map(|opt| opt.map(|r| (r.id, r.handle, r.did)))
29 } else {
30 let handle = if !input.repo.contains('.') {
31 format!("{}.{}", input.repo, hostname)
32 } else {
33 input.repo.clone()
34 };
35 sqlx::query!(
36 "SELECT id, handle, did FROM users WHERE handle = $1",
37 handle
38 )
39 .fetch_optional(&state.db)
40 .await
41 .map(|opt| opt.map(|r| (r.id, r.handle, r.did)))
42 };
43 let (user_id, handle, did) = match user_row {
44 Ok(Some((id, handle, did))) => (id, handle, did),
45 Ok(None) => {
46 return (
47 StatusCode::NOT_FOUND,
48 Json(json!({"error": "RepoNotFound", "message": "Repo not found"})),
49 )
50 .into_response();
51 }
52 Err(_) => {
53 return (
54 StatusCode::INTERNAL_SERVER_ERROR,
55 Json(json!({"error": "InternalError"})),
56 )
57 .into_response();
58 }
59 };
60 let collections_query = sqlx::query!(
61 "SELECT DISTINCT collection FROM records WHERE repo_id = $1",
62 user_id
63 )
64 .fetch_all(&state.db)
65 .await;
66 let collections: Vec<String> = match collections_query {
67 Ok(rows) => rows.iter().map(|r| r.collection.clone()).collect(),
68 Err(_) => Vec::new(),
69 };
70 let did_doc = json!({
71 "id": did,
72 "alsoKnownAs": [format!("at://{}", handle)]
73 });
74 Json(json!({
75 "handle": handle,
76 "did": did,
77 "didDoc": did_doc,
78 "collections": collections,
79 "handleIsCorrect": true
80 }))
81 .into_response()
82}