Skip to content

Commit

Permalink
nostr, nostr-ffi: Implement CLOSED messages
Browse files Browse the repository at this point in the history
  • Loading branch information
proudmuslim-dev committed Dec 7, 2023
1 parent ef019ba commit b33c293
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 3 deletions.
11 changes: 11 additions & 0 deletions bindings/nostr-ffi/src/message/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ pub enum RelayMessage {
Notice {
message: String,
},
Closed {
subscription_id: String,
message: String,
},
EndOfStoredEvents {
subscription_id: String,
},
Expand Down Expand Up @@ -48,6 +52,13 @@ impl From<NRelayMessage> for RelayMessage {
subscription_id: subscription_id.to_string(),
event: event.as_json(),
},
NRelayMessage::Closed {
subscription_id,
message,
} => Self::Closed {
subscription_id: subscription_id.to_string(),
message,
},
NRelayMessage::Notice { message } => Self::Notice { message },
NRelayMessage::EndOfStoredEvents(sub_id) => Self::EndOfStoredEvents {
subscription_id: sub_id.to_string(),
Expand Down
59 changes: 56 additions & 3 deletions crates/nostr/src/message/relay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ pub enum RelayMessage {
/// Message
message: String,
},
/// `["CLOSED", <subscription_id>, <message>]` (NIP01)
Closed {
/// Subscription ID
subscription_id: SubscriptionId,
/// Message
message: String,
},
/// `["AUTH", <challenge-string>]` (NIP42)
Auth {
/// Challenge
Expand Down Expand Up @@ -175,6 +182,17 @@ impl RelayMessage {
}
}

/// Create new `CLOSED` message
pub fn new_closed<S>(subscription_id: SubscriptionId, message: S) -> Self
where
S: Into<String>,
{
Self::Closed {
subscription_id,
message: message.into(),
}
}

/// Create new `EOSE` message
pub fn new_eose(subscription_id: SubscriptionId) -> Self {
Self::EndOfStoredEvents(subscription_id)
Expand Down Expand Up @@ -217,6 +235,10 @@ impl RelayMessage {
subscription_id,
} => json!(["EVENT", subscription_id, event]),
Self::Notice { message } => json!(["NOTICE", message]),
Self::Closed {
subscription_id,
message,
} => json!(["CLOSED", subscription_id, message]),
Self::EndOfStoredEvents(subscription_id) => {
json!(["EOSE", subscription_id])
}
Expand Down Expand Up @@ -294,6 +316,13 @@ impl TryFrom<RawRelayMessage> for RelayMessage {
SubscriptionId::new(subscription_id),
)),
RawRelayMessage::Notice { message } => Ok(Self::Notice { message }),
RawRelayMessage::Closed {
subscription_id,
message,
} => Ok(Self::Closed {
subscription_id: SubscriptionId::new(subscription_id),
message,
}),
RawRelayMessage::Auth { challenge } => Ok(Self::Auth { challenge }),
RawRelayMessage::Count {
subscription_id,
Expand Down Expand Up @@ -339,15 +368,39 @@ mod tests {
}
#[test]
fn test_handle_invalid_notice() {
//Missing content
// Missing content
let invalid_notice_msg = r#"["NOTICE"]"#;
//The content is not string
// The content is not string
let invalid_notice_msg_content = r#"["NOTICE": 404]"#;

assert!(RelayMessage::from_json(invalid_notice_msg).is_err(),);
assert!(RelayMessage::from_json(invalid_notice_msg_content).is_err(),);
}

#[test]
fn test_handle_valid_closed() {
let valid_closed_msg = r#"["CLOSED","random-subscription-id","reason"]"#;
let handled_valid_closed_msg =
RelayMessage::new_closed(SubscriptionId::new("random-subscription-id"), "reason");

assert_eq!(
RelayMessage::from_json(valid_closed_msg).unwrap(),
handled_valid_closed_msg
);
}

#[test]
fn test_handle_invalid_closed() {
// Missing subscription ID
assert!(RelayMessage::from_json(r#"["CLOSED"]"#).is_err());

// The subscription ID is not a string
assert!(RelayMessage::from_json(r#"["CLOSED", 404]"#).is_err());

// The content is not a string
assert!(RelayMessage::from_json(r#"["CLOSED", "random-subscription-id", 404]"#).is_err())
}

#[test]
fn test_handle_valid_event() {
let valid_event_msg = r#"["EVENT", "random_string", {"id":"70b10f70c1318967eddf12527799411b1a9780ad9c43858f5e5fcd45486a13a5","pubkey":"379e863e8357163b5bce5d2688dc4f1dcc2d505222fb8d74db600f30535dfdfe","created_at":1612809991,"kind":1,"tags":[],"content":"test","sig":"273a9cd5d11455590f4359500bccb7a89428262b96b3ea87a756b770964472f8c3e87f5d5e64d8d2e859a71462a3f477b554565c4f2f326cb01dd7620db71502"}]"#;
Expand All @@ -372,7 +425,7 @@ mod tests {
fn test_handle_invalid_event() {
// Missing Event field
let invalid_event_msg = r#"["EVENT", "random_string"]"#;
//Event JSON with incomplete content
// Event JSON with incomplete content
let invalid_event_msg_content = r#"["EVENT", "random_string", {"id":"70b10f70c1318967eddf12527799411b1a9780ad9c43858f5e5fcd45486a13a5","pubkey":"379e863e8357163b5bce5d2688dc4f1dcc2d505222fb8d74db600f30535dfdfe"}]"#;

assert!(RelayMessage::from_json(invalid_event_msg).is_err(),);
Expand Down
20 changes: 20 additions & 0 deletions crates/nostr/src/message/relay/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ pub enum RawRelayMessage {
/// Message
message: String,
},
/// `["CLOSED", <subscription_id>, <message>]` (NIP01)
Closed {
/// Subscription ID
subscription_id: String,
/// Message
message: String,
},
/// `["AUTH", <challenge-string>]` (NIP42)
Auth {
/// Challenge
Expand Down Expand Up @@ -86,6 +93,19 @@ impl RawRelayMessage {
});
}

// Closed
// Relay response format: ["CLOSED", <subscription_id>, <message>]
if v[0] == "CLOSED" {
if v_len != 3 {
return Err(MessageHandleError::InvalidMessageFormat);
}

return Ok(Self::Closed {
subscription_id: serde_json::from_value(v[1].clone())?,
message: serde_json::from_value(v[2].clone())?,
});
}

// Event
// Relay response format: ["EVENT", <subscription id>, <event JSON>]
if v[0] == "EVENT" {
Expand Down

0 comments on commit b33c293

Please sign in to comment.