feat: parse subscription close requests from websockets

This commit is contained in:
Greg Heartsfield 2021-12-05 17:33:40 -06:00
parent e7d0ab1aca
commit 35ceb7cb64
5 changed files with 35 additions and 6 deletions

22
src/close.rs Normal file
View File

@ -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()
}
}

View File

@ -14,10 +14,13 @@ pub enum Error {
ConnError, ConnError,
#[error("Client write error")] #[error("Client write error")]
ConnWriteError, ConnWriteError,
#[error("Event parse failed")] #[error("EVENT parse failed")]
EventParseFailed, EventParseFailed,
#[error("ClOSE message parse failed")]
CloseParseFailed,
#[error("Event validation failed")] #[error("Event validation failed")]
EventInvalid, EventInvalid,
// this should be used if the JSON is invalid
#[error("JSON parsing failed")] #[error("JSON parsing failed")]
JsonParseFailed(serde_json::Error), JsonParseFailed(serde_json::Error),
#[error("WebSocket proto error")] #[error("WebSocket proto error")]

View File

@ -1,5 +1,6 @@
pub mod conn; pub mod conn;
pub mod error;
pub mod event;
pub mod protostream; pub mod protostream;
pub mod event;
pub mod subscription; pub mod subscription;
pub mod close;
pub mod error;

View File

@ -82,9 +82,11 @@ async fn nostr_server(
} }
}, },
Some(Ok(SubMsg(s))) => { 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 => { None => {
info!("stream ended"); info!("stream ended");
//conn_good = true; //conn_good = true;

View File

@ -1,3 +1,4 @@
use crate::close::Close;
use crate::error::{Error, Result}; use crate::error::{Error, Result};
use crate::event::EventCmd; use crate::event::EventCmd;
use crate::subscription::Subscription; use crate::subscription::Subscription;
@ -19,7 +20,7 @@ use tungstenite::protocol::Message;
pub enum NostrMessage { pub enum NostrMessage {
EventMsg(EventCmd), EventMsg(EventCmd),
SubMsg(Subscription), SubMsg(Subscription),
CloseMsg, CloseMsg(Close),
} }
// Either an event w/ subscription, or a notice // Either an event w/ subscription, or a notice