Skip to main content

tap_node/event/
trust_ping_handler.rs

1//! Trust Ping Response Event Handler
2//!
3//! This module provides an event handler that listens for Trust Ping response events
4//! and sends them through the appropriate channels.
5
6use crate::event::{EventSubscriber, NodeEvent};
7use crate::message::sender::PlainMessageSender;
8use async_trait::async_trait;
9use std::sync::Arc;
10use tap_msg::didcomm::PlainMessage;
11
12/// Event handler that processes Trust Ping response events and sends them
13#[derive(Debug)]
14pub struct TrustPingResponseHandler {
15    /// Message sender for dispatching Trust Ping responses
16    sender: Arc<dyn PlainMessageSender>,
17}
18
19impl TrustPingResponseHandler {
20    /// Create a new Trust Ping response handler
21    pub fn new(sender: Arc<dyn PlainMessageSender>) -> Self {
22        Self { sender }
23    }
24}
25
26#[async_trait]
27impl EventSubscriber for TrustPingResponseHandler {
28    async fn handle_event(&self, event: NodeEvent) {
29        if let NodeEvent::PlainMessageSent { message, from, to } = event {
30            // Check if this is a Trust Ping response by examining the message type
31            if let Ok(plain_message) = serde_json::from_value::<PlainMessage>(message) {
32                if plain_message.type_ == "https://didcomm.org/trust-ping/2.0/ping-response" {
33                    log::debug!(
34                        "Processing Trust Ping response event: from={}, to={}",
35                        from,
36                        to
37                    );
38
39                    // Serialize the message and send it
40                    match serde_json::to_string(&plain_message) {
41                        Ok(serialized_message) => {
42                            if let Err(e) =
43                                self.sender.send(serialized_message, vec![to.clone()]).await
44                            {
45                                log::warn!("Failed to send Trust Ping response to {}: {}", to, e);
46                            } else {
47                                log::info!("Successfully sent Trust Ping response to {}", to);
48                            }
49                        }
50                        Err(e) => {
51                            log::warn!("Failed to serialize Trust Ping response: {}", e);
52                        }
53                    }
54                }
55            }
56        }
57    }
58}