diff --git a/.rustfmt.toml b/.rustfmt.toml new file mode 100644 index 00000000..3a26366d --- /dev/null +++ b/.rustfmt.toml @@ -0,0 +1 @@ +edition = "2021" diff --git a/Cargo.lock b/Cargo.lock index 66fb8849..8edacc81 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1271,6 +1271,7 @@ dependencies = [ "serde_derive", "serde_json", "tracing", + "url", ] [[package]] @@ -2741,7 +2742,7 @@ version = "0.5.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dcbff9bc912032c62bf65ef1d5aea88983b420f4f839db1e9b0c281a25c9c799" dependencies = [ - "proc-macro-crate 1.3.1", + "proc-macro-crate", "proc-macro2", "quote", "syn 1.0.109", @@ -2753,7 +2754,7 @@ version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "96667db765a921f7b295ffee8b60472b686a51d4f21c2ee4ffdb94c7013b65a6" dependencies = [ - "proc-macro-crate 1.3.1", + "proc-macro-crate", "proc-macro2", "quote", "syn 2.0.77", @@ -2765,7 +2766,7 @@ version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af1844ef2428cc3e1cb900be36181049ef3d3193c63e43026cfe202983b27a56" dependencies = [ - "proc-macro-crate 3.2.0", + "proc-macro-crate", "proc-macro2", "quote", "syn 2.0.77", @@ -3166,15 +3167,6 @@ dependencies = [ "toml_edit 0.19.15", ] -[[package]] -name = "proc-macro-crate" -version = "3.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ecf48c7ca261d60b74ab1a7b20da18bede46776b2e55535cb958eb595c5fa7b" -dependencies = [ - "toml_edit 0.22.20", -] - [[package]] name = "proc-macro2" version = "1.0.86" @@ -4452,7 +4444,7 @@ dependencies = [ "serde", "serde_spanned", "toml_datetime", - "toml_edit 0.22.20", + "toml_edit 0.22.21", ] [[package]] @@ -4477,9 +4469,9 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.22.20" +version = "0.22.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "583c44c02ad26b0c3f3066fe629275e50627026c51ac2e595cca4c230ce1ce1d" +checksum = "3b072cee73c449a636ffd6f32bd8de3a9f7119139aff882f44943ce2986dc5cf" dependencies = [ "indexmap", "serde", diff --git a/enostr/Cargo.toml b/enostr/Cargo.toml index 4b742289..2af528a0 100644 --- a/enostr/Cargo.toml +++ b/enostr/Cargo.toml @@ -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" diff --git a/enostr/src/relay/message.rs b/enostr/src/relay/message.rs index b48e5aea..dea89e8d 100644 --- a/enostr/src/relay/message.rs +++ b/enostr/src/relay/message.rs @@ -90,7 +90,17 @@ impl<'a> RelayMessage<'a> { // Event // Relay response format: ["EVENT", , ] 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) diff --git a/enostr/src/relay/pool.rs b/enostr/src/relay/pool.rs index f4aab0ce..de4e1efc 100644 --- a/enostr/src/relay/pool.rs +++ b/enostr/src/relay/pool.rs @@ -4,6 +4,8 @@ use nostrdb::Filter; use std::time::{Duration, Instant}; +use url::Url; + #[cfg(not(target_arch = "wasm32"))] use ewebsock::{WsEvent, WsMessage}; @@ -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); @@ -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.