Skip to content

Commit 0037c28

Browse files
committed
nostr: add NEG-ERR relay message
1 parent d0e3340 commit 0037c28

File tree

1 file changed

+90
-1
lines changed

1 file changed

+90
-1
lines changed

crates/nostr/src/message/relay.rs

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
77
use alloc::boxed::Box;
88
use alloc::string::{String, ToString};
9+
use core::fmt;
910

1011
use bitcoin::secp256k1::{Secp256k1, Verification};
11-
#[cfg(feature = "std")]
1212
use serde::{Deserialize, Deserializer};
1313
use serde::{Serialize, Serializer};
1414
use serde_json::{json, Value};
@@ -18,6 +18,70 @@ use super::MessageHandleError;
1818
use crate::SECP256K1;
1919
use crate::{Event, EventId, SubscriptionId};
2020

21+
/// Negentropy error code
22+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
23+
pub enum NegentropyErrorCode {
24+
/// Results too big
25+
ResultsTooBig,
26+
/// Because the NEG-OPEN queries are stateful, relays may choose to time-out inactive queries to recover memory resources
27+
Closed,
28+
/// If an event ID is used as the filter, this error will be returned if the relay does not have this event.
29+
/// The client should retry with the full filter, or upload the event to the relay.
30+
FilterNotFound,
31+
/// The event's content was not valid JSON, or the filter was invalid for some other reason.
32+
FilterInvalid,
33+
/// Other
34+
Other(String),
35+
}
36+
37+
impl fmt::Display for NegentropyErrorCode {
38+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39+
match self {
40+
Self::ResultsTooBig => write!(f, "RESULTS_TOO_BIG"),
41+
Self::Closed => write!(f, "CLOSED"),
42+
Self::FilterNotFound => write!(f, "FILTER_NOT_FOUND"),
43+
Self::FilterInvalid => write!(f, "FILTER_INVALID"),
44+
Self::Other(e) => write!(f, "{e}"),
45+
}
46+
}
47+
}
48+
49+
impl<S> From<S> for NegentropyErrorCode
50+
where
51+
S: Into<String>,
52+
{
53+
fn from(code: S) -> Self {
54+
let code: String = code.into();
55+
match code.as_str() {
56+
"RESULTS_TOO_BIG" => Self::ResultsTooBig,
57+
"CLOSED" => Self::Closed,
58+
"FILTER_NOT_FOUND" => Self::FilterNotFound,
59+
"FILTER_INVALID" => Self::FilterInvalid,
60+
o => Self::Other(o.to_string()),
61+
}
62+
}
63+
}
64+
65+
impl Serialize for NegentropyErrorCode {
66+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
67+
where
68+
S: Serializer,
69+
{
70+
serializer.serialize_str(&self.to_string())
71+
}
72+
}
73+
74+
impl<'de> Deserialize<'de> for NegentropyErrorCode {
75+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
76+
where
77+
D: Deserializer<'de>,
78+
{
79+
let value = Value::deserialize(deserializer)?;
80+
let alphaber: String = serde_json::from_value(value).map_err(serde::de::Error::custom)?;
81+
Ok(Self::from(alphaber))
82+
}
83+
}
84+
2185
/// Messages sent by relays, received by clients
2286
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
2387
pub enum RelayMessage {
@@ -63,6 +127,13 @@ pub enum RelayMessage {
63127
/// Message
64128
message: String,
65129
},
130+
/// Negentropy Error
131+
NegErr {
132+
/// Subscription ID
133+
subscription_id: SubscriptionId,
134+
/// Error code
135+
code: NegentropyErrorCode,
136+
},
66137
}
67138

68139
impl Serialize for RelayMessage {
@@ -164,6 +235,10 @@ impl RelayMessage {
164235
subscription_id,
165236
message,
166237
} => json!(["NEG-MSG", subscription_id, message]),
238+
Self::NegErr {
239+
subscription_id,
240+
code,
241+
} => json!(["NEG-ERR", subscription_id, code]),
167242
}
168243
}
169244

@@ -291,6 +366,20 @@ impl RelayMessage {
291366
});
292367
}
293368

369+
// Negentropy Error
370+
// ["NEG-ERR", <subscription ID string>, <reason-code>]
371+
if v[0] == "NEG-ERR" {
372+
if v_len != 3 {
373+
return Err(MessageHandleError::InvalidMessageFormat);
374+
}
375+
let subscription_id: SubscriptionId = SubscriptionId::new(v[1].to_string());
376+
let code: NegentropyErrorCode = NegentropyErrorCode::from(v[2].to_string());
377+
return Ok(Self::NegErr {
378+
subscription_id,
379+
code,
380+
});
381+
}
382+
294383
Err(MessageHandleError::InvalidMessageFormat)
295384
}
296385

0 commit comments

Comments
 (0)