canonicalize relay urls to avoid false duplicates
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -1271,6 +1271,7 @@ dependencies = [
|
|||||||
"serde_derive",
|
"serde_derive",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"tracing",
|
"tracing",
|
||||||
|
"url",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@@ -15,3 +15,4 @@ nostrdb = { git = "https://github.com/damus-io/nostrdb-rs", rev = "9bbafd8a2e904
|
|||||||
hex = "0.4.3"
|
hex = "0.4.3"
|
||||||
tracing = "0.1.40"
|
tracing = "0.1.40"
|
||||||
env_logger = "0.11.1"
|
env_logger = "0.11.1"
|
||||||
|
url = "2.5.2"
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ use nostrdb::Filter;
|
|||||||
|
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
|
use url::Url;
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
use ewebsock::{WsEvent, WsMessage};
|
use ewebsock::{WsEvent, WsMessage};
|
||||||
|
|
||||||
@@ -152,6 +154,7 @@ impl RelayPool {
|
|||||||
url: String,
|
url: String,
|
||||||
wakeup: impl Fn() + Send + Sync + Clone + 'static,
|
wakeup: impl Fn() + Send + Sync + Clone + 'static,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
|
let url = Self::canonicalize_url(url);
|
||||||
// Check if the URL already exists in the pool.
|
// Check if the URL already exists in the pool.
|
||||||
if self.has(&url) {
|
if self.has(&url) {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@@ -164,6 +167,14 @@ impl RelayPool {
|
|||||||
Ok(())
|
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
|
/// Attempts to receive a pool event from a list of relays. The
|
||||||
/// function searches each relay in the list in order, attempting to
|
/// function searches each relay in the list in order, attempting to
|
||||||
/// receive a message from each. If a message is received, return it.
|
/// receive a message from each. If a message is received, return it.
|
||||||
|
|||||||
Reference in New Issue
Block a user