use serde_json::{Value, json};
use tracing::{debug, info, warn};
use did_hosting_common::didcomm_types::{MSG_HEALTH_PING, MSG_SERVER_REGISTER_ACK, MSG_STATS_ACK};
use crate::server::AppState;
pub fn owns(type_uri: &str) -> bool {
matches!(
type_uri,
MSG_HEALTH_PING | MSG_SERVER_REGISTER_ACK | MSG_STATS_ACK
)
}
pub async fn dispatch(
state: &AppState,
sender: Option<&str>,
doc: trust_tasks_rs::TrustTask<Value>,
) -> Option<trust_tasks_rs::TrustTask<Value>> {
match doc.type_uri.to_string().as_str() {
MSG_HEALTH_PING => {
let verifier = crate::messaging::state_verifier(state)?;
crate::messaging::verify_control_plane(state, sender, &doc, &verifier)
.await
.ok()?;
let pong = do_health_ping(state).await;
debug!(sender, "health ping answered (trust task)");
Some(doc.respond_with(uuid::Uuid::new_v4().to_string(), pong))
}
MSG_SERVER_REGISTER_ACK => {
do_register_ack(&doc.payload);
None
}
MSG_STATS_ACK => 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");
}