Skip to content
21 changes: 17 additions & 4 deletions crates/gitlawb-node/src/api/certs.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
//! API handlers for ref certificates.

use axum::extract::{Extension, Path, State};
use std::collections::HashMap;

use axum::extract::{Extension, Path, Query, State};
use axum::Json;

use crate::auth::AuthenticatedDid;
use crate::error::{AppError, Result};
use crate::state::AppState;

/// GET /api/v1/repos/{owner}/{repo}/certs
/// GET /api/v1/repos/{owner}/{repo}/certs?limit=50
pub async fn list_certs(
State(state): State<AppState>,
Path((owner, name)): Path<(String, String)>,
Query(params): Query<HashMap<String, String>>,
auth: Option<Extension<AuthenticatedDid>>,
) -> Result<Json<serde_json::Value>> {
let limit = params
.get("limit")
.and_then(|v| v.parse::<i64>().ok())
.map(|v| v.max(1))
.unwrap_or(50)
.min(200);
Comment thread
coderabbitai[bot] marked this conversation as resolved.

let caller = auth.as_ref().map(|e| e.0 .0.as_str());
let (record, _rules) =
crate::api::authorize_repo_read(&state, &owner, &name, caller, "/").await?;

let certs = state.db.list_ref_certificates(&record.id).await?;
let certs = state.db.list_ref_certificates(&record.id, limit).await?;
let certs_json: Vec<serde_json::Value> = certs
.iter()
.map(|c| {
Expand All @@ -35,7 +45,10 @@ pub async fn list_certs(
})
.collect();

Ok(Json(serde_json::json!({ "certificates": certs_json })))
let count = certs_json.len();
Ok(Json(
serde_json::json!({ "certificates": certs_json, "count": count }),
))
}

/// GET /api/v1/repos/{owner}/{repo}/certs/{id}
Expand Down
4 changes: 3 additions & 1 deletion crates/gitlawb-node/src/api/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ pub async fn list_ref_updates(
let limit = params
.get("limit")
.and_then(|v| v.parse::<i64>().ok())
.map(|v| v.max(0))
.unwrap_or(50)
.clamp(0, MAX_VISIBLE_REF_UPDATES);

Expand Down Expand Up @@ -177,6 +178,7 @@ pub async fn list_repo_events(
let limit = params
.get("limit")
.and_then(|v| v.parse::<i64>().ok())
.map(|v| v.max(0))
.unwrap_or(50)
.clamp(0, MAX_VISIBLE_REF_UPDATES);

Expand Down Expand Up @@ -209,7 +211,7 @@ pub async fn list_repo_events(
// into an empty 200, matching the gossip half below.
let cert_events: Vec<serde_json::Value> = state
.db
.list_ref_certificates(&record.id)
.list_ref_certificates(&record.id, limit)
.await?
.iter()
.map(|c| {
Expand Down
5 changes: 3 additions & 2 deletions crates/gitlawb-node/src/cert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub async fn issue_ref_certificate(
issued_at,
};

state.db.insert_ref_certificate(&cert).await?;
Ok(cert)
// Persist and return the row as it exists in the database (on a
// conflict the existing row survives when it is newer).
state.db.insert_ref_certificate(&cert).await
}
Loading
Loading