rust-nostr / nostr

Nostr protocol implementation, SDK and FFI
https://rust-nostr.org/
MIT License
416 stars 90 forks source link

Add URL where an Event is from when obtaining events through get_events_of() #190

Open nobu-maeda opened 11 months ago

nobu-maeda commented 11 months ago

Describe the enhancement
As titled. Currently Events are received without knowing which Relay its from if they are obtained through the get_events_of() client function. Suggestion here is to also return the URL as part of the callback (eg. in pool.rs)

Use case
It would help to determine which Relay to reply to, or DM to, if an application wishes to reply to the author of a particular event.

nobu-maeda commented 11 months ago

I can create a PR for this if this sounds like a reasonable proposal. Thanks

yukibtc commented 11 months ago

Hi, from commit f20c77660691f895879ec523c234ee9cb3f68ea6 (PR #178) it's possible to query the local database (in-memory or persistent) to know in which relays the event was seen. You can try this code using master branch:

[dependencies]
nostr-sdk = { git = "https://github.com/rust-nostr/nostr", branch = "master" }
# OR
nostr-sdk = { git = "https://github.com/rust-nostr/nostr", rev = "696ffd596c7fc69d299c4389fc2148c66538f226" }
// ...
let database = client.database();
let relays = database
    .event_recently_seen_on_relays(event_id)
    .await?;
println!("Seen on: {relays:?}");
nobu-maeda commented 11 months ago

Seems to work, at least in my integration test environment. I am using event_recently_seen_on_relays() after calling get_events_of(). The first time I see an event, I call event_recently_seen_on_relays() and keep track of that event along with the relay url HashSet, and drop all subsequent occurrence of that eventID. The expectation is that the number of urls seen from event_recently_seen_on_relays() will always match the actual number of occurrences the events gets seen as a result of get_events_of().

I guess the most ideal is that the relay URL is part of the Event struct so that this somewhat hacky correlation is not needed?