use std::sync::Arc;
use affinidi_tdk::messaging::ATM;
use affinidi_tdk::messaging::profiles::ATMProfile;
#[derive(Clone)]
pub struct TspTransport {
atm: ATM,
profile: Arc<ATMProfile>,
mediator_did: String,
}
impl TspTransport {
pub fn new(atm: ATM, profile: Arc<ATMProfile>) -> Option<Self> {
let mediator_did = profile.dids().ok()?.1.to_string();
Some(Self {
atm,
profile,
mediator_did,
})
}
pub fn from_app_state(state: &crate::server::AppState) -> Option<Self> {
Self::new(state.didcomm_bridge.atm()?, state.didcomm_bridge.profile()?)
}
pub fn mediator_did(&self) -> &str {
&self.mediator_did
}
pub fn atm(&self) -> &ATM {
&self.atm
}
pub fn profile(&self) -> &Arc<ATMProfile> {
&self.profile
}
pub async fn send_to(
&self,
recipient: &str,
body: &[u8],
) -> Result<(), affinidi_messaging_sdk::errors::ATMError> {
self.atm
.tsp()
.send_routed(
&self.profile,
&[self.mediator_did.clone(), recipient.to_string()],
body,
)
.await
}
pub async fn send_metadata_private(
&self,
recipient: &str,
peer_mediator: Option<&str>,
body: &[u8],
) -> Result<(), affinidi_messaging_sdk::errors::ATMError> {
match peer_mediator {
Some(peer_mediator) if peer_mediator != self.mediator_did => {
self.atm
.tsp()
.send_nested_routed(
&self.profile,
&[self.mediator_did.clone(), peer_mediator.to_string()],
recipient,
body,
)
.await
}
_ => self.send_to(recipient, body).await,
}
}
pub fn our_vid(&self) -> Option<String> {
self.profile.dids().ok().map(|(ours, _)| ours.to_string())
}
pub async fn reset_relationship(
&self,
recipient: &str,
) -> Result<(), affinidi_messaging_sdk::errors::ATMError> {
self.atm
.tsp()
.reset_relationship(&self.profile, recipient)
.await
}
pub async fn send_reestablishing(
&self,
recipient: &str,
body: &[u8],
) -> Result<(), affinidi_messaging_sdk::errors::ATMError> {
self.atm
.tsp()
.send_reestablishing(
&self.profile,
recipient,
&[self.mediator_did.clone(), recipient.to_string()],
body,
)
.await
}
pub async fn relate(
&self,
recipient: &str,
) -> Result<(), affinidi_messaging_sdk::errors::ATMError> {
self.atm
.tsp()
.form_relationship_routed(&self.profile, recipient)
.await
.map(|_| ())
}
}