commit 35ceb7cb6471ae567c31668d6c81d6ad54fbf606
parent e7d0ab1aca14c4e69508bf89852e5c4e9f1b74c8
Author: Greg Heartsfield <scsibug@imap.cc>
Date: Sun, 5 Dec 2021 17:33:40 -0600
feat: parse subscription close requests from websockets
Diffstat:
5 files changed, 35 insertions(+), 6 deletions(-)
diff --git a/src/close.rs 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<Close> {
+ 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
@@ -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
@@ -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
@@ -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
@@ -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