use std::sync::Arc;
use affinidi_tdk::didcomm::Message;
use affinidi_tdk::messaging::ATM;
use affinidi_tdk::messaging::profiles::ATMProfile;
use chrono::Utc;
use serde_json::Value;
use crate::errors::OpenVTCError;
pub struct Delivery<'a> {
pub atm: &'a ATM,
pub profile: &'a Arc<ATMProfile>,
pub member_did: &'a str,
pub vtc_did: &'a str,
pub mediator_did: &'a str,
pub tsp_mediator_did: Option<&'a str>,
}
pub async fn tsp_mediator_for(over_tsp: bool, vtc_did: &str) -> Option<String> {
if over_tsp {
crate::config::peer_tsp_mediator(vtc_did).await
} else {
None
}
}
pub fn didcomm_request(
document_id: String,
body: Value,
member_did: &str,
vtc_did: &str,
) -> Message {
let now = Utc::now().timestamp().max(0) as u64;
Message::build(
document_id,
crate::capabilities::TRUST_TASK_ENVELOPE_TYPE.to_string(),
body,
)
.from(member_did.to_string())
.to(vtc_did.to_string())
.created_time(now)
.finalize()
}
pub async fn send_document(
route: &Delivery<'_>,
document_id: String,
document: Value,
) -> Result<(), OpenVTCError> {
if let Some(tsp_mediator) = route.tsp_mediator_did {
return crate::tsp::send_trust_task(
route.atm,
route.profile,
&document,
route.vtc_did,
tsp_mediator,
)
.await;
}
let msg = didcomm_request(document_id, document, route.member_did, route.vtc_did);
crate::pack_and_send(
route.atm,
route.profile,
&msg,
route.member_did,
route.vtc_did,
route.mediator_did,
)
.await
}