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