Skip to content

Commit

Permalink
Merge a few relay fixes from ken
Browse files Browse the repository at this point in the history
Pick a few fixes from #380

Link: #380

Ken Sedgwick (5):
      build: Cargo.lock updates to mitigate num_enum_derive problem
      add .rustfmt.toml to specify edition
      Fix parsing of subscription id for RelayMessage::Event
      Skip adding relays that are already in the pool
      canonicalize relay urls to avoid false duplicates
  • Loading branch information
jb55 committed Nov 11, 2024
2 parents 877a30d + 570d64c commit 702f8ff
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 16 deletions.
1 change: 1 addition & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
edition = "2021"
22 changes: 7 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions enostr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ nostrdb = { git = "https://github.com/damus-io/nostrdb-rs", rev = "9bbafd8a2e904
hex = "0.4.3"
tracing = "0.1.40"
env_logger = "0.11.1"
url = "2.5.2"
12 changes: 11 additions & 1 deletion enostr/src/relay/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,17 @@ impl<'a> RelayMessage<'a> {
// Event
// Relay response format: ["EVENT", <subscription id>, <event JSON>]
if &msg[0..=7] == "[\"EVENT\"" {
return Ok(Self::event(msg, "fixme"));
let mut start = 9;
while let Some(&b' ') = msg.as_bytes().get(start) {
start += 1; // Move past optional spaces
}
if let Some(comma_index) = msg[start..].find(',') {
let subid_end = start + comma_index;
let subid = &msg[start..subid_end].trim().trim_matches('"');
return Ok(Self::event(msg, subid));
} else {
return Ok(Self::event(msg, "fixme"));
}
}

// EOSE (NIP-15)
Expand Down
15 changes: 15 additions & 0 deletions enostr/src/relay/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use nostrdb::Filter;

use std::time::{Duration, Instant};

use url::Url;

#[cfg(not(target_arch = "wasm32"))]
use ewebsock::{WsEvent, WsMessage};

Expand Down Expand Up @@ -152,6 +154,11 @@ impl RelayPool {
url: String,
wakeup: impl Fn() + Send + Sync + Clone + 'static,
) -> Result<()> {
let url = Self::canonicalize_url(url);
// Check if the URL already exists in the pool.
if self.has(&url) {
return Ok(());
}
let relay = Relay::new(url, wakeup)?;
let pool_relay = PoolRelay::new(relay);

Expand All @@ -160,6 +167,14 @@ impl RelayPool {
Ok(())
}

// standardize the format (ie, trailing slashes)
fn canonicalize_url(url: String) -> String {
match Url::parse(&url) {
Ok(parsed_url) => parsed_url.to_string(),
Err(_) => url, // If parsing fails, return the original URL.
}
}

/// Attempts to receive a pool event from a list of relays. The
/// function searches each relay in the list in order, attempting to
/// receive a message from each. If a message is received, return it.
Expand Down

0 comments on commit 702f8ff

Please sign in to comment.