Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: subman API #561

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ egui_virtual_list = "0.5.0"
ehttp = "0.2.0"
enostr = { path = "crates/enostr" }
ewebsock = { version = "0.2.0", features = ["tls"] }
futures = "0.3.31"
hex = "0.4.3"
image = { version = "0.25", features = ["jpeg", "png", "webp"] }
indexmap = "2.6.0"
Expand Down
59 changes: 58 additions & 1 deletion crates/enostr/src/relay/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ impl PoolRelay {
}

pub fn subscribe(&mut self, subid: String, filter: Vec<Filter>) -> Result<()> {
debug!(
"PoolRelay subscribe: sending sub {} {}: {:?}",
subid,
self.url(),
filter
.iter()
.map(|f| f.json().unwrap_or_default())
.collect::<Vec<_>>(),
);
self.send(&ClientMessage::req(subid, filter))
}

Expand Down Expand Up @@ -197,6 +206,7 @@ impl RelayPool {
if let Some(debug) = &mut self.debug {
debug.send_cmd(relay.url().to_owned(), &cmd);
}
debug!("RelayPool unsubscribe close {} {}", subid, relay.url());
if let Err(err) = relay.send(&cmd) {
error!(
"error unsubscribing from {} on {}: {err}",
Expand All @@ -215,10 +225,57 @@ impl RelayPool {
&ClientMessage::req(subid.clone(), filter.clone()),
);
}
debug!(
"RelayPool subscribe: sending sub {} {}: {:?}",
subid,
relay.url(),
filter
.iter()
.map(|f| f.json().unwrap_or_default())
.collect::<Vec<_>>(),
);
if let Err(err) = relay.send(&ClientMessage::req(subid.clone(), filter.clone())) {
error!("error subscribing to {}: {err}", relay.url());
}
}
}

// if the relay is in the pool already send the subscription, otherwise add the
// relay to the pool and we'll send it on open.
pub fn subscribe_relay(
&mut self,
subid: String,
filter: Vec<Filter>,
relaystr: String,
) -> bool {
if let Some(&mut ref mut relay) = self.relays.iter_mut().find(|r| r.url() == relaystr) {
if let Some(debug) = &mut self.debug {
debug.send_cmd(
relay.url().to_owned(),
&ClientMessage::req(subid.clone(), filter.clone()),
);
}
debug!(
"RelayPool subscribe_relay: sending sub {} {}: {:?}",
subid,
relay.url(),
filter
.iter()
.map(|f| f.json().unwrap_or_default())
.collect::<Vec<_>>(),
);
if let Err(err) = relay.send(&ClientMessage::req(subid.clone(), filter.clone())) {
error!("error subscribing to {}: {err}", relay.url());
}
true
} else {
let wakeup = move || {
// ignore
};
if let Err(err) = self.add_url(relaystr.clone(), wakeup) {
error!("trouble adding url {}: {:?}", relaystr, err);
}
false
}
}

Expand Down Expand Up @@ -340,7 +397,7 @@ impl RelayPool {
}

// standardize the format (ie, trailing slashes)
fn canonicalize_url(url: String) -> String {
pub 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.
Expand Down
3 changes: 3 additions & 0 deletions crates/notedeck/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dirs = { workspace = true }
enostr = { workspace = true }
egui = { workspace = true }
eframe = { workspace = true }
futures = { workspace = true }
image = { workspace = true }
base32 = { workspace = true }
poll-promise = { workspace = true }
Expand All @@ -25,9 +26,11 @@ thiserror = { workspace = true }
puffin = { workspace = true, optional = true }
puffin_egui = { workspace = true, optional = true }
sha2 = { workspace = true }
tokio = { workspace = true, features = ["macros", "rt-multi-thread", "fs"] }

[dev-dependencies]
tempfile = { workspace = true }
nostr = { workspace = true }

[target.'cfg(target_os = "macos")'.dependencies]
security-framework = { workspace = true }
Expand Down
Loading
Loading