keri_core/processor/
notification.rs

1use std::{collections::HashMap, sync::Arc};
2
3#[cfg(feature = "query")]
4use crate::query::reply_event::SignedReply;
5
6use crate::{
7    error::Error,
8    event_message::signed_event_message::{
9        SignedEventMessage, SignedNontransferableReceipt, SignedTransferableReceipt,
10    },
11};
12
13pub struct NotificationBus {
14    observers: HashMap<JustNotification, Vec<Arc<dyn Notifier + Send + Sync>>>,
15}
16
17impl NotificationBus {
18    pub fn new() -> Self {
19        Self {
20            observers: HashMap::new(),
21        }
22    }
23    pub fn register_observer(
24        &mut self,
25        escrow: Arc<dyn Notifier + Send + Sync>,
26        notification: Vec<JustNotification>,
27    ) {
28        notification.into_iter().for_each(|notification| {
29            self.observers
30                .entry(notification)
31                .or_default()
32                .push(escrow.clone());
33        });
34    }
35
36    pub fn notify(&self, notification: &Notification) -> Result<(), Error> {
37        if let Some(obs) = self.observers.get(&notification.into()) {
38            for esc in obs.iter() {
39                esc.notify(notification, self)?;
40            }
41        };
42        Ok(())
43    }
44}
45
46impl Default for NotificationBus {
47    fn default() -> Self {
48        Self::new()
49    }
50}
51
52pub trait Notifier {
53    fn notify(&self, notification: &Notification, bus: &NotificationBus) -> Result<(), Error>;
54}
55
56#[derive(PartialEq, Debug, Clone)]
57pub enum Notification {
58    KeyEventAdded(SignedEventMessage),
59    OutOfOrder(SignedEventMessage),
60    PartiallySigned(SignedEventMessage),
61    PartiallyWitnessed(SignedEventMessage),
62    ReceiptAccepted,
63    ReceiptEscrowed,
64    ReceiptOutOfOrder(SignedNontransferableReceipt),
65    TransReceiptOutOfOrder(SignedTransferableReceipt),
66    DupliciousEvent(SignedEventMessage),
67    MissingDelegatingEvent(SignedEventMessage),
68    #[cfg(feature = "query")]
69    KsnOutOfOrder(SignedReply),
70}
71
72#[derive(PartialEq, Hash, Eq, Clone, Debug)]
73pub enum JustNotification {
74    KeyEventAdded,
75    OutOfOrder,
76    PartiallySigned,
77    PartiallyWitnessed,
78    ReceiptAccepted,
79    ReceiptEscrowed,
80    ReceiptOutOfOrder,
81    TransReceiptOutOfOrder,
82    DupliciousEvent,
83    MissingDelegatingEvent,
84    #[cfg(feature = "query")]
85    KsnOutOfOrder,
86    #[cfg(feature = "query")]
87    KsnUpdated,
88    #[cfg(feature = "oobi")]
89    GotOobi,
90    #[cfg(feature = "query")]
91    ReplayLog,
92    #[cfg(feature = "query")]
93    ReplyKsn,
94    #[cfg(feature = "query")]
95    GetMailbox,
96}
97
98impl From<&Notification> for JustNotification {
99    fn from(notification: &Notification) -> Self {
100        match notification {
101            Notification::KeyEventAdded(_) => JustNotification::KeyEventAdded,
102            Notification::OutOfOrder(_) => JustNotification::OutOfOrder,
103            Notification::PartiallySigned(_) => JustNotification::PartiallySigned,
104            Notification::PartiallyWitnessed(_) => JustNotification::PartiallyWitnessed,
105            Notification::ReceiptAccepted => JustNotification::ReceiptAccepted,
106            Notification::ReceiptEscrowed => JustNotification::ReceiptEscrowed,
107            Notification::ReceiptOutOfOrder(_) => JustNotification::ReceiptOutOfOrder,
108            Notification::TransReceiptOutOfOrder(_) => JustNotification::TransReceiptOutOfOrder,
109            Notification::DupliciousEvent(_) => JustNotification::DupliciousEvent,
110            #[cfg(feature = "query")]
111            Notification::KsnOutOfOrder(_) => JustNotification::KsnOutOfOrder,
112            Notification::MissingDelegatingEvent(_) => JustNotification::MissingDelegatingEvent,
113        }
114    }
115}