rust_nostr_server/msgapi/
outgoing.rs

1use async_trait::async_trait;
2use nostr::message::subscription;
3use nostr::RelayMessage;
4use nostr::{event, SubscriptionId};
5
6const unauthenticated: &'static str = "we can't serve DMs to unauthenticated users";
7
8#[derive(Debug)]
9pub enum Error {
10    Event(event::Error),
11    ToClientMessage(serde_json::Error),
12}
13
14impl std::fmt::Display for Error {
15    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
16        match self {
17            Self::Event(e) => write!(f, "event: {}", e),
18            Self::ToClientMessage(e) => write!(f, "to client message error: {}", e),
19        }
20    }
21}
22
23impl From<nostr::event::Error> for Error {
24    fn from(e: nostr::event::Error) -> Self {
25        Self::Event(e)
26    }
27}
28
29impl From<serde_json::Error> for Error {
30    fn from(e: serde_json::Error) -> Self {
31        Self::ToClientMessage(e)
32    }
33}
34
35pub struct OutgoingMessage {}
36
37impl OutgoingMessage {
38    pub fn new() -> Self {
39        OutgoingMessage {}
40    }
41}
42
43#[derive(Debug)]
44pub struct ChallengeMsg {
45    pub challenge_msg: String,
46}
47#[derive(Debug)]
48pub struct notice_msg {
49    pub notice_msg: String,
50}
51
52impl ChallengeMsg {
53    pub async fn new(challenge_msg: String) -> Self {
54        ChallengeMsg { challenge_msg }
55    }
56    pub async fn get_challenge_msg(&self) -> String {
57        self.challenge_msg.clone()
58    }
59}
60
61impl notice_msg {
62    pub async fn new(notice_msg: String) -> Self {
63        notice_msg { notice_msg }
64    }
65    pub async fn get_notice_msg(&self) -> String {
66        self.notice_msg.clone()
67    }
68}
69
70#[derive(Debug, Clone)]
71pub struct EoseMsg {
72    pub eose_msg: String,
73}
74
75impl EoseMsg {
76    pub async fn new(eose_msg: String) -> Self {
77        EoseMsg { eose_msg }
78    }
79    pub async fn get_subscription_id(&self) -> String {
80        self.eose_msg.clone()
81    }
82}
83
84#[derive(Debug)]
85pub enum OutgoingMessageTypes {
86    Challenge(ChallengeMsg),
87    Notice(notice_msg),
88    Eose(EoseMsg),
89}
90
91#[async_trait]
92pub trait OutgoingHandler {
93    async fn send_challenge<'a, 'b: 'a>(
94        &'a self,
95        challenge_msg: &'b str,
96    ) -> Result<OutgoingMessageTypes, Error>;
97    async fn send_notice(&self, notice_msg: String) -> Result<OutgoingMessageTypes, Error>;
98    async fn send_eose(
99        &self,
100        subscription_id: SubscriptionId,
101    ) -> Result<OutgoingMessageTypes, Error>;
102}
103
104#[async_trait]
105impl OutgoingHandler for OutgoingMessage {
106    async fn send_challenge<'a, 'b: 'a>(
107        &'a self,
108        challenge_msg: &'b str,
109    ) -> Result<OutgoingMessageTypes, Error> {
110        let relay_message: RelayMessage = RelayMessage::auth(challenge_msg);
111        let challenge_str: String = serde_json::to_string(&relay_message)?;
112        let ret = ChallengeMsg::new(challenge_str).await;
113        Ok(OutgoingMessageTypes::Challenge(ret))
114    }
115    async fn send_notice(&self, notice_msg: String) -> Result<OutgoingMessageTypes, Error> {
116        let relay_message: RelayMessage = RelayMessage::notice(notice_msg);
117        let notice_str: String = serde_json::to_string(&relay_message)?;
118        let ret = notice_msg::new(notice_str).await;
119        Ok(OutgoingMessageTypes::Notice(ret))
120    }
121    async fn send_eose(
122        &self,
123        subscription_id: SubscriptionId,
124    ) -> Result<OutgoingMessageTypes, Error> {
125        let end_of_send_event: RelayMessage = RelayMessage::eose(subscription_id);
126        let end_of_send_event_str: String = serde_json::to_string(&end_of_send_event)?;
127        let ret = EoseMsg::new(end_of_send_event_str).await;
128        Ok(OutgoingMessageTypes::Eose(ret))
129    }
130}