Skip to content

Commit 8bd1bb3

Browse files
committed
Add support to specify subscription filters with a JsonArray
1 parent 7689f0d commit 8bd1bb3

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

src/NostrPool.cpp

+18-5
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,10 @@ void NostrPool::onEvent(NostrRelay *relay, NostrString message) {
7777

7878
NostrString NostrPool::subscribeMany(std::initializer_list<NostrString> urls, std::initializer_list<std::map<NostrString, std::initializer_list<NostrString>>> filters,
7979
NostrEventCallback eventCallback, NostrCloseCallback closeCallback, NostrEOSECallback eoseCallback) {
80-
NostrString subId = Utils::getNewSubscriptionId();
8180
JsonDocument doc;
82-
JsonArray req = doc["req"].to<JsonArray>();
83-
req.add("REQ");
84-
req.add(subId);
81+
JsonArray filtersArray = doc["filtersArray"].to<JsonArray>();
8582
for (const auto &filter : filters) {
86-
JsonObject filterObj = req.add<JsonObject>();
83+
JsonObject filterObj = filtersArray.add<JsonObject>();
8784
for (const auto &pair : filter) {
8885
NostrString key = pair.first;
8986
bool isIntList = NostrString_equals(key, "kinds");
@@ -141,6 +138,22 @@ NostrString NostrPool::subscribeMany(std::initializer_list<NostrString> urls, st
141138
}
142139
}
143140

141+
NostrString subId = this->subscribeMany(urls, filtersArray, eventCallback, closeCallback, eoseCallback);
142+
doc.clear();
143+
return subId;
144+
}
145+
146+
NostrString NostrPool::subscribeMany(std::initializer_list<NostrString> urls, JsonArray filters, NostrEventCallback eventCallback, NostrCloseCallback closeCallback, NostrEOSECallback eoseCallback) {
147+
NostrString subId = Utils::getNewSubscriptionId();
148+
JsonDocument doc;
149+
JsonArray req = doc["req"].to<JsonArray>();
150+
req.add("REQ");
151+
req.add(subId);
152+
for(int i = 0; i < filters.size(); i++){
153+
JsonObject filter = filters[i].as<JsonObject>();
154+
req.add(filter);
155+
}
156+
144157
NostrString json;
145158
Utils::jsonStringify(req, &json);
146159
doc.clear();

src/NostrPool.h

+13-1
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,20 @@ namespace nostr {
9494
NostrEOSECallback eoseCallback = nullptr
9595

9696
);
97-
9897

98+
/**
99+
* Subscribe to events from one or more relays
100+
* @param urls The relays to subscribe to
101+
* @param filters A json array of filters for each relay
102+
* @param eventCallback A callback to be called when an event is received (optional)
103+
* @param closeCallback A callback to be called when the subscription is closed (optional)
104+
* @param eoseCallback A callback to be called when the stored events are exhausted (optional)
105+
* @return The subscription ID
106+
*/
107+
NostrString subscribeMany(std::initializer_list<NostrString> urls, JsonArray filters,
108+
NostrEventCallback eventCallback = nullptr, NostrCloseCallback closeCallback = nullptr, NostrEOSECallback eoseCallback = nullptr
109+
110+
);
99111
/**
100112
* Publish a signed event to one or more relays. The pool will automatically start listening to the specified relays after publishing.
101113
* @param rs The relays to publish to

0 commit comments

Comments
 (0)