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 user_row = if input.repo.starts_with("did:") {
21 sqlx::query!("SELECT id, handle, did FROM users WHERE did = $1", input.repo)
22 .fetch_optional(&state.db)
23 .await
24 .map(|opt| opt.map(|r| (r.id, r.handle, r.did)))
25 } else {
26 sqlx::query!("SELECT id, handle, did FROM users WHERE handle = $1", input.repo)
27 .fetch_optional(&state.db)
28 .await
29 .map(|opt| opt.map(|r| (r.id, r.handle, r.did)))
30 };
31 let (user_id, handle, did) = match user_row {
32 Ok(Some((id, handle, did))) => (id, handle, did),
33 _ => {
34 return (
35 StatusCode::NOT_FOUND,
36 Json(json!({"error": "NotFound", "message": "Repo not found"})),
37 )
38 .into_response();
39 }
40 };
41 let collections_query =
42 sqlx::query!("SELECT DISTINCT collection FROM records WHERE repo_id = $1", user_id)
43 .fetch_all(&state.db)
44 .await;
45 let collections: Vec<String> = match collections_query {
46 Ok(rows) => rows.iter().map(|r| r.collection.clone()).collect(),
47 Err(_) => Vec::new(),
48 };
49 let did_doc = json!({
50 "id": did,
51 "alsoKnownAs": [format!("at://{}", handle)]
52 });
53 Json(json!({
54 "handle": handle,
55 "did": did,
56 "didDoc": did_doc,
57 "collections": collections,
58 "handleIsCorrect": true
59 }))
60 .into_response()
61}