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;
10use sqlx::Row;
11
12#[derive(Deserialize)]
13pub struct DescribeRepoInput {
14 pub repo: String,
15}
16
17pub async fn describe_repo(
18 State(state): State<AppState>,
19 Query(input): Query<DescribeRepoInput>,
20) -> Response {
21 let user_row = if input.repo.starts_with("did:") {
22 sqlx::query("SELECT id, handle, did FROM users WHERE did = $1")
23 .bind(&input.repo)
24 .fetch_optional(&state.db)
25 .await
26 } else {
27 sqlx::query("SELECT id, handle, did FROM users WHERE handle = $1")
28 .bind(&input.repo)
29 .fetch_optional(&state.db)
30 .await
31 };
32
33 let (user_id, handle, did) = match user_row {
34 Ok(Some(row)) => (
35 row.get::<uuid::Uuid, _>("id"),
36 row.get::<String, _>("handle"),
37 row.get::<String, _>("did"),
38 ),
39 _ => {
40 return (
41 StatusCode::NOT_FOUND,
42 Json(json!({"error": "NotFound", "message": "Repo not found"})),
43 )
44 .into_response();
45 }
46 };
47
48 let collections_query =
49 sqlx::query("SELECT DISTINCT collection FROM records WHERE repo_id = $1")
50 .bind(user_id)
51 .fetch_all(&state.db)
52 .await;
53
54 let collections: Vec<String> = match collections_query {
55 Ok(rows) => rows.iter().map(|r| r.get("collection")).collect(),
56 Err(_) => Vec::new(),
57 };
58
59 let did_doc = json!({
60 "id": did,
61 "alsoKnownAs": [format!("at://{}", handle)]
62 });
63
64 Json(json!({
65 "handle": handle,
66 "did": did,
67 "didDoc": did_doc,
68 "collections": collections,
69 "handleIsCorrect": true
70 }))
71 .into_response()
72}