aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/nostr.hpp18
-rw-r--r--src/nostr_service.cpp45
2 files changed, 36 insertions, 27 deletions
diff --git a/include/nostr.hpp b/include/nostr.hpp
index 1e462e7..5f5ce25 100644
--- a/include/nostr.hpp
+++ b/include/nostr.hpp
@@ -167,7 +167,7 @@ public:
* @remarks The service will store a limited number of events returned from the relay for the
* given filters. These events may be retrieved via `getNewEvents`.
*/
- std::string queryRelays(Filters filters);
+ std::string queryRelays(std::shared_ptr<Filters> filters);
/**
* @brief Queries all open relay connections for events matching the given set of filters.
@@ -179,21 +179,23 @@ public:
* events returned from the relay for the given filters. The service will not store the
* events, and they will not be accessible via `getNewEvents`.
*/
- std::string queryRelays(Filters filters, std::function<void(const std::string&, Event)> responseHandler);
+ std::string queryRelays(
+ std::shared_ptr<Filters> filters,
+ std::function<void(const std::string&, std::shared_ptr<Event>)> responseHandler);
/**
* @brief Get any new events received since the last call to this method, across all
* subscriptions.
* @returns A pointer to a vector of new events.
*/
- std::vector<Event> getNewEvents();
+ std::vector<std::shared_ptr<Event>> getNewEvents();
/**
* @brief Get any new events received since the last call to this method, for the given
* subscription.
* @returns A pointer to a vector of new events.
*/
- std::vector<Event> getNewEvents(std::string subscriptionId);
+ std::vector<std::shared_ptr<Event>> getNewEvents(std::string subscriptionId);
/**
* @brief Closes the subscription with the given ID on all open relay connections.
@@ -237,7 +239,7 @@ private:
///< A map from relay URIs to the subscription IDs open on each relay.
std::unordered_map<std::string, std::vector<std::string>> _subscriptions;
///< A map from subscription IDs to the events returned by the relays for each subscription.
- std::unordered_map<std::string, std::vector<Event>> _events;
+ std::unordered_map<std::string, std::vector<std::shared_ptr<Event>>> _events;
///< A map from the subscription IDs to the ID of the latest read event for each subscription.
std::unordered_map<std::string, std::string> _lastRead;
@@ -296,7 +298,9 @@ private:
/**
* @brief Parses messages received from the relay and invokes the appropriate message handler.
*/
- void onMessage(std::string message, std::function<void(const std::string&, Event)> eventHandler);
+ void onMessage(
+ std::string message,
+ std::function<void(const std::string&, std::shared_ptr<Event>)> eventHandler);
/**
* @brief A default message handler for events returned from relay queries.
@@ -305,7 +309,7 @@ private:
* @remark By default, new events are stored in a map of subscription IDs to vectors of events.
* Events are retrieved by calling `getNewEvents` or `getNewEvents(subscriptionId)`.
*/
- void onEvent(std::string subscriptionId, Event event);
+ void onEvent(std::string subscriptionId, std::shared_ptr<Event> event);
};
class ISigner
diff --git a/src/nostr_service.cpp b/src/nostr_service.cpp
index c7e3158..73ce95e 100644
--- a/src/nostr_service.cpp
+++ b/src/nostr_service.cpp
@@ -20,6 +20,7 @@ using std::find_if;
using std::function;
using std::future;
using std::lock_guard;
+using std::make_shared;
using std::make_tuple;
using std::move;
using std::mutex;
@@ -172,16 +173,18 @@ tuple<RelayList, RelayList> NostrService::publishEvent(shared_ptr<Event> event)
return make_tuple(successfulRelays, failedRelays);
};
-string NostrService::queryRelays(Filters filters)
+string NostrService::queryRelays(shared_ptr<Filters> filters)
{
- return this->queryRelays(filters, [this](string subscriptionId, Event event) {
+ return this->queryRelays(filters, [this](string subscriptionId, shared_ptr<Event> event) {
lock_guard<mutex> lock(this->_propertyMutex);
- this->_lastRead[subscriptionId] = event.id;
+ this->_lastRead[subscriptionId] = event->id;
this->onEvent(subscriptionId, event);
});
};
-string NostrService::queryRelays(Filters filters, function<void(const string&, Event)> responseHandler)
+string NostrService::queryRelays(
+ shared_ptr<Filters> filters,
+ function<void(const string&, shared_ptr<Event>)> responseHandler)
{
RelayList successfulRelays;
RelayList failedRelays;
@@ -192,7 +195,7 @@ string NostrService::queryRelays(Filters filters, function<void(const string&, E
{
lock_guard<mutex> lock(this->_propertyMutex);
this->_subscriptions[relay].push_back(subscriptionId);
- string request = filters.serialize(subscriptionId);
+ string request = filters->serialize(subscriptionId);
future<tuple<string, bool>> requestFuture = async([this, &relay, &request]() {
return this->_client->send(request, relay);
@@ -224,20 +227,20 @@ string NostrService::queryRelays(Filters filters, function<void(const string&, E
return subscriptionId;
};
-vector<Event> NostrService::getNewEvents()
+vector<shared_ptr<Event>> NostrService::getNewEvents()
{
- vector<Event> newEvents;
+ vector<shared_ptr<Event>> newEvents;
for (auto& [subscriptionId, events] : this->_events)
{
- vector<Event> subscriptionEvents = this->getNewEvents(subscriptionId);
+ vector<shared_ptr<Event>> subscriptionEvents = this->getNewEvents(subscriptionId);
newEvents.insert(newEvents.end(), subscriptionEvents.begin(), subscriptionEvents.end());
}
return newEvents;
};
-vector<Event> NostrService::getNewEvents(string subscriptionId)
+vector<shared_ptr<Event>> NostrService::getNewEvents(string subscriptionId)
{
if (this->_events.find(subscriptionId) == this->_events.end())
{
@@ -252,13 +255,13 @@ vector<Event> NostrService::getNewEvents(string subscriptionId)
}
lock_guard<mutex> lock(this->_propertyMutex);
- vector<Event> newEvents;
- vector<Event> receivedEvents = this->_events[subscriptionId];
- vector<Event>::iterator eventIt = find_if(
+ vector<shared_ptr<Event>> newEvents;
+ vector<shared_ptr<Event>> receivedEvents = this->_events[subscriptionId];
+ vector<shared_ptr<Event>>::iterator eventIt = find_if(
receivedEvents.begin(),
receivedEvents.end(),
- [this,subscriptionId](Event event) {
- return event.id == this->_lastRead[subscriptionId];
+ [this,subscriptionId](shared_ptr<Event> event) {
+ return event->id == this->_lastRead[subscriptionId];
}) + 1;
while (eventIt != receivedEvents.end())
@@ -480,7 +483,9 @@ bool NostrService::hasSubscription(string relay, string subscriptionId)
return false;
};
-void NostrService::onMessage(string message, function<void(const string&, Event)> eventHandler)
+void NostrService::onMessage(
+ string message,
+ function<void(const string&, shared_ptr<Event>)> eventHandler)
{
json jarr = json::array();
jarr = json::parse(message);
@@ -497,10 +502,10 @@ void NostrService::onMessage(string message, function<void(const string&, Event)
// Support other message types here, if necessary.
};
-void NostrService::onEvent(string subscriptionId, Event event)
+void NostrService::onEvent(string subscriptionId, shared_ptr<Event> event)
{
lock_guard<mutex> lock(this->_propertyMutex);
- this->_events[subscriptionId].push_back(event);
+ this->_events[subscriptionId].push_back(move(event));
PLOG_INFO << "Received event for subscription: " << subscriptionId;
// To protect memory, only keep a limited number of events per subscription.
@@ -510,8 +515,8 @@ void NostrService::onEvent(string subscriptionId, Event event)
auto eventIt = find_if(
events.begin(),
events.end(),
- [this, subscriptionId](Event event) {
- return event.id == this->_lastRead[subscriptionId];
+ [this, subscriptionId](shared_ptr<Event> event) {
+ return event->id == this->_lastRead[subscriptionId];
});
if (eventIt == events.begin())
@@ -519,7 +524,7 @@ void NostrService::onEvent(string subscriptionId, Event event)
eventIt++;
}
- this->_lastRead[subscriptionId] = eventIt->id;
+ this->_lastRead[subscriptionId] = (*eventIt)->id;
_events[subscriptionId].erase(_events[subscriptionId].begin());
}
};