From 1417e31b8d9c181b4c35ff4f50d65125d958689b Mon Sep 17 00:00:00 2001 From: Michael Jurkoic Date: Tue, 30 Apr 2024 00:20:05 -0500 Subject: Ensure first queryRelay unit test passes --- src/event.cpp | 3 +-- src/filters.cpp | 8 +++----- src/nostr_service.cpp | 25 ++++++++++++++++++++----- 3 files changed, 24 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/event.cpp b/src/event.cpp index 5c98028..703efae 100644 --- a/src/event.cpp +++ b/src/event.cpp @@ -24,8 +24,7 @@ string Event::serialize() {"kind", this->kind}, {"tags", this->tags}, {"content", this->content}, - {"sig", this->sig} - }; + {"sig", this->sig}}; j["id"] = this->generateId(j.dump()); diff --git a/src/filters.cpp b/src/filters.cpp index cfbc5bf..40596eb 100644 --- a/src/filters.cpp +++ b/src/filters.cpp @@ -3,10 +3,9 @@ using namespace nlohmann; using namespace std; - namespace nostr { -string Filters::serialize(string subscriptionId) +string Filters::serialize(string& subscriptionId) { try { @@ -23,9 +22,8 @@ string Filters::serialize(string subscriptionId) {"kinds", this->kinds}, {"since", this->since}, {"until", this->until}, - {"limit", this->limit} - }; - + {"limit", this->limit}}; + for (auto& tag : this->tags) { stringstream ss; diff --git a/src/nostr_service.cpp b/src/nostr_service.cpp index f8565cc..a1adbbb 100644 --- a/src/nostr_service.cpp +++ b/src/nostr_service.cpp @@ -3,7 +3,6 @@ using namespace nlohmann; using namespace std; -using namespace UUIDv4; namespace nostr { @@ -189,7 +188,23 @@ vector> NostrService::queryRelays(shared_ptr filters) vector> events; string subscriptionId = this->generateSubscriptionId(); - string request = filters->serialize(subscriptionId); + string request; + + try + { + request = filters->serialize(subscriptionId); + } + catch (const invalid_argument& e) + { + PLOG_ERROR << "Failed to serialize filters - invalid object: " << e.what(); + throw e; + } + catch (const json::exception& je) + { + PLOG_ERROR << "Failed to serialize filters - JSON exception: " << je.what(); + throw je; + } + vector>> requestFutures; // Send the same query to each relay. As events trickle in from each relay, they will be added @@ -487,9 +502,9 @@ void NostrService::disconnect(string relay) string NostrService::generateSubscriptionId() { - UUIDGenerator uuidGenerator; - UUID uuid = uuidGenerator.getUUID(); - return uuid.bytes(); + UUIDv4::UUIDGenerator uuidGenerator; + UUIDv4::UUID uuid = uuidGenerator.getUUID(); + return uuid.str(); }; string NostrService::generateCloseRequest(string subscriptionId) -- cgit