aboutsummaryrefslogtreecommitdiff
path: root/src/nostr_service.cpp
diff options
context:
space:
mode:
authorLibravatar Michael Jurkoic <mjjurkoic@gmail.com>2024-03-19 09:12:00 -0500
committerLibravatar Michael Jurkoic <mjjurkoic@gmail.com>2024-03-19 09:12:00 -0500
commit8dbce9cd5aab9129e66a0c04e31467d172344f19 (patch)
treea5f76cebf5af6928ca4c6778920a8b09b7e96bec /src/nostr_service.cpp
parent6dde23e6c66e846c64d49c5258f0dbf44e3d0374 (diff)
Move relay payload parsing into NostrService
Preserve separation of concerns.
Diffstat (limited to 'src/nostr_service.cpp')
-rw-r--r--src/nostr_service.cpp27
1 files changed, 22 insertions, 5 deletions
diff --git a/src/nostr_service.cpp b/src/nostr_service.cpp
index 7efc11e..ac63f23 100644
--- a/src/nostr_service.cpp
+++ b/src/nostr_service.cpp
@@ -164,7 +164,7 @@ string NostrService::queryRelays(Filters filters)
});
};
-string NostrService::queryRelays(Filters filters, function<void(string, Event)> responseHandler)
+string NostrService::queryRelays(Filters filters, function<void(const string&, Event)> responseHandler)
{
RelayList successfulRelays;
RelayList failedRelays;
@@ -182,10 +182,8 @@ string NostrService::queryRelays(Filters filters, function<void(string, Event)>
});
requestFutures.push_back(move(requestFuture));
- this->_client->receive(relay, [responseHandler](string subscriptionId, string message) {
- Event event;
- event.deserialize(message);
- responseHandler(subscriptionId, event);
+ this->_client->receive(relay, [this, responseHandler](string payload) {
+ this->onMessage(payload, responseHandler);
});
}
@@ -460,6 +458,25 @@ bool NostrService::hasSubscription(string relay, string subscriptionId)
return false;
};
+void NostrService::onMessage(string message, function<void(const string&, Event)> eventHandler)
+{
+ json jarr = json::array();
+ jarr = json::parse(message);
+
+ string messageType = jarr[0];
+
+ if (messageType == "EVENT")
+ {
+ string subscriptionId = jarr[1];
+ string serializedEvent = jarr[2].dump();
+ Event event;
+ event.deserialize(message);
+ eventHandler(subscriptionId, event);
+ }
+
+ // Support other message types here, if necessary.
+};
+
void NostrService::onEvent(string subscriptionId, Event event)
{
lock_guard<mutex> lock(this->_propertyMutex);