mirror of
https://github.com/scsibug/nostr-rs-relay.git
synced 2024-11-14 15:09:07 -05:00
Use notice function for sending messages
This commit is contained in:
parent
010c96b05f
commit
0f276a06b2
16
src/event.rs
16
src/event.rs
|
@ -26,22 +26,6 @@ where
|
||||||
Ok(opt.unwrap_or_else(|| vec![]))
|
Ok(opt.unwrap_or_else(|| vec![]))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn u32_from_string<'de, D>(deserializer: D) -> Result<u32, D::Error>
|
|
||||||
where
|
|
||||||
D: Deserializer<'de>,
|
|
||||||
{
|
|
||||||
let _s: String = Deserialize::deserialize(deserializer)?;
|
|
||||||
Ok(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn u64_from_string<'de, D>(deserializer: D) -> Result<u64, D::Error>
|
|
||||||
where
|
|
||||||
D: Deserializer<'de>,
|
|
||||||
{
|
|
||||||
let _s: String = Deserialize::deserialize(deserializer)?;
|
|
||||||
Ok(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Goals:
|
// Goals:
|
||||||
// Roundtrip from JSON-string to Event, and back to string.
|
// Roundtrip from JSON-string to Event, and back to string.
|
||||||
// Perform validation on an Event to ensure the id and signature are correct.
|
// Perform validation on an Event to ensure the id and signature are correct.
|
||||||
|
|
40
src/proto.rs
40
src/proto.rs
|
@ -41,45 +41,24 @@ impl Proto {
|
||||||
|
|
||||||
pub async fn process_client(&mut self) {
|
pub async fn process_client(&mut self) {
|
||||||
while let Some(mes_res) = self.stream.next().await {
|
while let Some(mes_res) = self.stream.next().await {
|
||||||
self.send_notice().await;
|
|
||||||
|
|
||||||
match mes_res {
|
match mes_res {
|
||||||
Ok(Message::Text(cmd)) => {
|
Ok(Message::Text(cmd)) => {
|
||||||
info!("Message received");
|
info!("Message received");
|
||||||
let length = cmd.len();
|
|
||||||
debug!("Message: {}", cmd);
|
debug!("Message: {}", cmd);
|
||||||
self.stream
|
|
||||||
.send(Message::Text(format!(
|
|
||||||
"got your message of length {}",
|
|
||||||
length
|
|
||||||
)))
|
|
||||||
.await
|
|
||||||
.expect("send failed");
|
|
||||||
// Handle this request. Everything else below is basically websocket error handling.
|
|
||||||
let proto_error = self.process_message(cmd);
|
let proto_error = self.process_message(cmd);
|
||||||
match proto_error {
|
match proto_error {
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
self.stream
|
self.send_notice("failed to process message.").await;
|
||||||
.send(Message::Text(
|
|
||||||
"[\"NOTICE\", \"Failed to process message.\"]".to_owned(),
|
|
||||||
))
|
|
||||||
.await
|
|
||||||
.expect("send failed");
|
|
||||||
}
|
}
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
info!("Message processed successfully");
|
info!("Message processed successfully");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Everything else below is basically websocket error handling.
|
||||||
Ok(Message::Binary(_)) => {
|
Ok(Message::Binary(_)) => {
|
||||||
info!("Ignoring Binary message");
|
info!("Ignoring Binary message");
|
||||||
self.stream
|
self.send_notice("binary messages not supported.").await;
|
||||||
.send(Message::Text(
|
|
||||||
"[\"NOTICE\", \"BINARY_INVALID: Binary messages are not supported.\"]"
|
|
||||||
.to_owned(),
|
|
||||||
))
|
|
||||||
.await
|
|
||||||
.expect("send failed");
|
|
||||||
}
|
}
|
||||||
Ok(Message::Ping(_)) | Ok(Message::Pong(_)) => debug!("Ping/Pong"),
|
Ok(Message::Ping(_)) | Ok(Message::Pong(_)) => debug!("Ping/Pong"),
|
||||||
Ok(Message::Close(_)) => {
|
Ok(Message::Close(_)) => {
|
||||||
|
@ -93,7 +72,7 @@ impl Proto {
|
||||||
"Message size too large, disconnecting this client. ({} > {})",
|
"Message size too large, disconnecting this client. ({} > {})",
|
||||||
size, max_size
|
size, max_size
|
||||||
);
|
);
|
||||||
self.stream.send(Message::Text("[\"NOTICE\", \"MAX_EVENT_SIZE_EXCEEDED: Exceeded maximum event size for this relay. Closing Connection.\"]".to_owned())).await.expect("send notice failed");
|
self.send_notice("binary messages not supported.").await;
|
||||||
self.stream
|
self.stream
|
||||||
.close(Some(CloseFrame {
|
.close(Some(CloseFrame {
|
||||||
code: CloseCode::Size,
|
code: CloseCode::Size,
|
||||||
|
@ -124,8 +103,14 @@ impl Proto {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn send_notice(&mut self) {
|
// sending notice to client. never fails.
|
||||||
self.stream.send(Message::Text(format!("foo"))).await;
|
pub async fn send_notice(&mut self, msg: &str) {
|
||||||
|
// TODO: real escaping
|
||||||
|
let s = msg.replace("\"", "");
|
||||||
|
self.stream
|
||||||
|
.send(Message::Text(format!("[\"NOTICE\",\"{}\"]", s)))
|
||||||
|
.await
|
||||||
|
.err();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Error results will be transformed into client NOTICEs
|
// Error results will be transformed into client NOTICEs
|
||||||
|
@ -178,6 +163,7 @@ impl Proto {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn unsubscribe(&mut self, c: Close) {
|
pub fn unsubscribe(&mut self, c: Close) {
|
||||||
|
// TODO: return notice if subscription did not exist.
|
||||||
self.subscriptions.remove(&c.get_id());
|
self.subscriptions.remove(&c.get_id());
|
||||||
info!(
|
info!(
|
||||||
"Removed subscription, currently have {} active subs",
|
"Removed subscription, currently have {} active subs",
|
||||||
|
|
Loading…
Reference in New Issue
Block a user