diff options
author | Daniel Wigton <daniel@stonecottageweb.com> | 2024-07-02 07:11:37 -0500 |
---|---|---|
committer | Daniel Wigton <daniel@stonecottageweb.com> | 2024-07-02 07:11:37 -0500 |
commit | 87ab420599d4910a5ecab86a3752f68ddb72e26d (patch) | |
tree | c2529dd02ee2b0c77736b8564e11e7438cfb52b0 /src | |
parent | 43b61ff06e18a79a3e16e38445e92b0ad678bd9f (diff) |
deduplicate events in queryRelays
Diffstat (limited to 'src')
-rw-r--r-- | src/nostr_service.cpp | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/src/nostr_service.cpp b/src/nostr_service.cpp index 5adca3b..10d9fc6 100644 --- a/src/nostr_service.cpp +++ b/src/nostr_service.cpp @@ -1,5 +1,6 @@ #include "nostr.hpp" #include "client/web_socket_client.hpp" +#include <unordered_set> using namespace nlohmann; using namespace std; @@ -213,8 +214,10 @@ vector<shared_ptr<Event>> NostrService::queryRelays(shared_ptr<Filters> filters) vector<future<tuple<string, bool>>> requestFutures; + unordered_set<string> uniqueEventIds; + // Send the same query to each relay. As events trickle in from each relay, they will be added - // to the events vector. Multiple copies of an event may be received if the same event is + // to the events vector. Duplicate copies of the same event will be ignored, as events are // stored on multiple relays. The function will block until all of the relays send an EOSE or // CLOSE message. for (const string relay : this->_activeRelays) @@ -225,13 +228,17 @@ vector<shared_ptr<Event>> NostrService::queryRelays(shared_ptr<Filters> filters) auto [uri, success] = this->_client->send( request, relay, - [this, &relay, &events, &eosePromise](string payload) + [this, &relay, &events, &eosePromise, &uniqueEventIds](string payload) { this->onSubscriptionMessage( payload, - [&events](const string&, shared_ptr<Event> event) + [&events, &uniqueEventIds](const string&, shared_ptr<Event> event) { - events.push_back(event); + // Check if the event is unique before adding. + if (uniqueEventIds.insert(event->id).second) + { + events.push_back(event); + } }, [relay, &eosePromise](const string&) { @@ -272,8 +279,6 @@ vector<shared_ptr<Event>> NostrService::queryRelays(shared_ptr<Filters> filters) } this->closeSubscription(subscriptionId); - // TODO: De-duplicate events in the vector before returning. - return events; }; |