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