From 35ceb7cb6471ae567c31668d6c81d6ad54fbf606 Mon Sep 17 00:00:00 2001 From: Greg Heartsfield Date: Sun, 5 Dec 2021 17:33:40 -0600 Subject: [PATCH] feat: parse subscription close requests from websockets --- src/close.rs | 22 ++++++++++++++++++++++ src/error.rs | 5 ++++- src/lib.rs | 5 +++-- src/main.rs | 6 ++++-- src/protostream.rs | 3 ++- 5 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 src/close.rs diff --git a/src/close.rs b/src/close.rs new file mode 100644 index 0000000..7a739aa --- /dev/null +++ b/src/close.rs @@ -0,0 +1,22 @@ +use crate::error::{Error, Result}; +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)] +pub struct Close { + cmd: String, + id: String, +} + +impl Close { + pub fn parse(json: &str) -> Result { + let c: Close = serde_json::from_str(json)?; //.map_err(|e| Error::JsonParseFailed(e)); + if c.cmd != "CLOSE" { + return Err(Error::CloseParseFailed); + } + return Ok(c); + } + + pub fn get_id(&self) -> String { + self.id.clone() + } +} diff --git a/src/error.rs b/src/error.rs index b91b3a5..8476303 100644 --- a/src/error.rs +++ b/src/error.rs @@ -14,10 +14,13 @@ pub enum Error { ConnError, #[error("Client write error")] ConnWriteError, - #[error("Event parse failed")] + #[error("EVENT parse failed")] EventParseFailed, + #[error("ClOSE message parse failed")] + CloseParseFailed, #[error("Event validation failed")] EventInvalid, + // this should be used if the JSON is invalid #[error("JSON parsing failed")] JsonParseFailed(serde_json::Error), #[error("WebSocket proto error")] diff --git a/src/lib.rs b/src/lib.rs index 8b94275..b247749 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ pub mod conn; -pub mod error; -pub mod event; pub mod protostream; +pub mod event; pub mod subscription; +pub mod close; +pub mod error; diff --git a/src/main.rs b/src/main.rs index 0df575e..63fa500 100644 --- a/src/main.rs +++ b/src/main.rs @@ -82,9 +82,11 @@ async fn nostr_server( } }, Some(Ok(SubMsg(s))) => { - info!("Sub request from client: {:?}", s); + info!("Sub-open request from client: {:?}", s); + }, + Some(Ok(CloseMsg(c))) => { + info!("Sub-close request from client: {:?}", c); }, - Some(Ok(CloseMsg)) => {}, None => { info!("stream ended"); //conn_good = true; diff --git a/src/protostream.rs b/src/protostream.rs index 1bef291..6296b61 100644 --- a/src/protostream.rs +++ b/src/protostream.rs @@ -1,3 +1,4 @@ +use crate::close::Close; use crate::error::{Error, Result}; use crate::event::EventCmd; use crate::subscription::Subscription; @@ -19,7 +20,7 @@ use tungstenite::protocol::Message; pub enum NostrMessage { EventMsg(EventCmd), SubMsg(Subscription), - CloseMsg, + CloseMsg(Close), } // Either an event w/ subscription, or a notice