use serde_json::{Value, json};
use tracing::{debug, info, warn};
use did_hosting_common::didcomm_types::{MSG_HEALTH_PING, MSG_SERVER_REGISTER_ACK};
use crate::server::AppState;
pub fn owns(type_uri: &str) -> bool {
matches!(type_uri, MSG_HEALTH_PING | MSG_SERVER_REGISTER_ACK)
}
pub async fn dispatch(
state: &AppState,
sender: &str,
doc: trust_tasks_rs::TrustTask<Value>,
) -> Option<Value> {
match doc.type_uri.to_string().as_str() {
MSG_HEALTH_PING => {
let pong = do_health_ping(state).await;
let resp = doc.respond_with(uuid::Uuid::new_v4().to_string(), pong);
debug!(sender, "health ping answered (trust task)");
Some(serde_json::to_value(&resp).expect("pong document serialises"))
}
MSG_SERVER_REGISTER_ACK => {
do_register_ack(&doc.payload);
None
}
other => {
warn!(type_uri = other, "trust_tasks_infra: unowned type URI");
None
}
}
}
pub(crate) async fn do_health_ping(state: &AppState) -> Value {
let did_count = state
.dids_ks
.prefix_iter_raw("did:")
.await
.map(|v| v.len() as u64)
.unwrap_or(0);
json!({
"status": "ok",
"version": env!("CARGO_PKG_VERSION"),
"did_count": did_count,
})
}
pub(crate) fn do_register_ack(body: &Value) {
let instance_id = body
.get("instance_id")
.and_then(|v| v.as_str())
.unwrap_or("unknown");
info!(instance_id, "registration acknowledged by control plane");
}