Skip to content

Commit a614bd1

Browse files
committed
Restore Nostr Zap subscription after loss of connection
1 parent b0ec068 commit a614bd1

File tree

3 files changed

+57
-26
lines changed

3 files changed

+57
-26
lines changed

data

Submodule data updated from 9867988 to 384b431

src/lib/nostr_notify.cpp

+52-24
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@ std::vector<nostr::NostrPool *> pools;
44
nostr::Transport *transport;
55
TaskHandle_t nostrTaskHandle = NULL;
66
boolean nostrIsConnected = false;
7+
boolean nostrIsSubscribed = false;
8+
boolean nostrIsSubscribing = true;
9+
10+
String subIdZap;
711

812
void setupNostrNotify(bool asDatasource, bool zapNotify)
913
{
1014
nostr::esp32::ESP32Platform::initNostr(false);
11-
time_t now;
12-
time(&now);
13-
struct tm *utcTimeInfo;
14-
utcTimeInfo = gmtime(&now);
15-
time_t utcNow = mktime(utcTimeInfo);
16-
time_t timestamp60MinutesAgo = utcNow - 3600;
15+
// time_t now;
16+
// time(&now);
17+
// struct tm *utcTimeInfo;
18+
// utcTimeInfo = gmtime(&now);
19+
// time_t utcNow = mktime(utcTimeInfo);
20+
// time_t timestamp60MinutesAgo = utcNow - 3600;
1721

1822
try
1923
{
@@ -27,20 +31,7 @@ void setupNostrNotify(bool asDatasource, bool zapNotify)
2731

2832
if (zapNotify)
2933
{
30-
String subIdZap = pool->subscribeMany(
31-
{relay},
32-
{
33-
{
34-
{"kinds", {"9735"}},
35-
{"limit", {"1"}},
36-
{"since", {String(timestamp60MinutesAgo)}},
37-
{"#p", {preferences.getString("nostrZapPubkey", DEFAULT_ZAP_NOTIFY_PUBKEY)}},
38-
},
39-
},
40-
handleNostrZapCallback,
41-
onNostrSubscriptionClosed,
42-
onNostrSubscriptionEose);
43-
Serial.println("[ Nostr ] Subscribing to Zap Notifications");
34+
subscribeZaps(pool, relay, 60);
4435
}
4536

4637
if (asDatasource)
@@ -50,7 +41,7 @@ void setupNostrNotify(bool asDatasource, bool zapNotify)
5041
{// First filter
5142
{
5243
{"kinds", {"1"}},
53-
{"since", {String(timestamp60MinutesAgo)}},
44+
{"since", {String(getMinutesAgo(60))}},
5445
{"authors", {pubKey}},
5546
}},
5647
handleNostrEventCallback,
@@ -72,11 +63,12 @@ void setupNostrNotify(bool asDatasource, bool zapNotify)
7263
sstatus="CONNECTED";
7364
}else if(status==nostr::ConnectionStatus::DISCONNECTED){
7465
nostrIsConnected = false;
66+
nostrIsSubscribed = false;
7567
sstatus="DISCONNECTED";
7668
}else if(status==nostr::ConnectionStatus::ERROR){
7769
sstatus = "ERROR";
7870
}
79-
//Serial.println("[ Nostr ] Connection status changed: " + sstatus);
71+
Serial.println("[ Nostr ] Connection status changed: " + sstatus);
8072
});
8173
}
8274
}
@@ -88,8 +80,10 @@ void setupNostrNotify(bool asDatasource, bool zapNotify)
8880

8981
void nostrTask(void *pvParameters)
9082
{
91-
int blockFetch = getBlockFetch();
92-
processNewBlock(blockFetch);
83+
if(preferences.getBool("useNostr", DEFAULT_USE_NOSTR)) {
84+
int blockFetch = getBlockFetch();
85+
processNewBlock(blockFetch);
86+
}
9387

9488
while (1)
9589
{
@@ -98,6 +92,10 @@ void nostrTask(void *pvParameters)
9892
// Run internal loop: refresh relays, complete pending connections, send
9993
// pending messages
10094
pool->loop();
95+
if (!nostrIsSubscribed && !nostrIsSubscribing) {
96+
Serial.println(F("Not subscribed"));
97+
subscribeZaps(pool, preferences.getString("nostrRelay"), 1);
98+
}
10199
}
102100
vTaskDelay(pdMS_TO_TICKS(100));
103101
}
@@ -125,6 +123,8 @@ void onNostrSubscriptionEose(const String &subId)
125123
// This is the callback that will be called when the subscription is
126124
// EOSE
127125
Serial.println("[ Nostr ] Subscription EOSE: " + subId);
126+
nostrIsSubscribing = false;
127+
nostrIsSubscribed = true;
128128
}
129129

130130
void handleNostrEventCallback(const String &subId, nostr::SignedNostrEvent *event)
@@ -183,6 +183,34 @@ void handleNostrEventCallback(const String &subId, nostr::SignedNostrEvent *even
183183
}
184184
}
185185

186+
time_t getMinutesAgo(int min) {
187+
time_t now;
188+
time(&now);
189+
return now - (min * 60);
190+
}
191+
192+
void subscribeZaps(nostr::NostrPool *pool, const String &relay, int minutesAgo) {
193+
if (subIdZap) {
194+
pool->closeSubscription(subIdZap);
195+
}
196+
nostrIsSubscribing = true;
197+
198+
subIdZap = pool->subscribeMany(
199+
{relay},
200+
{
201+
{
202+
{"kinds", {"9735"}},
203+
{"limit", {"1"}},
204+
{"since", {String(getMinutesAgo(minutesAgo))}},
205+
{"#p", {preferences.getString("nostrZapPubkey", DEFAULT_ZAP_NOTIFY_PUBKEY)}},
206+
},
207+
},
208+
handleNostrZapCallback,
209+
onNostrSubscriptionClosed,
210+
onNostrSubscriptionEose);
211+
Serial.println("[ Nostr ] Subscribing to Zap Notifications since " + String(getMinutesAgo(minutesAgo)));
212+
}
213+
186214
void handleNostrZapCallback(const String &subId, nostr::SignedNostrEvent *event) {
187215
// Received events callback, we can access the event content with
188216
// event->getContent() Here you should handle the event, for this

src/lib/nostr_notify.hpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,7 @@ void handleNostrEventCallback(const String &subId, nostr::SignedNostrEvent *even
2424
void handleNostrZapCallback(const String &subId, nostr::SignedNostrEvent *event);
2525

2626
void onNostrSubscriptionClosed(const String &subId, const String &reason);
27-
void onNostrSubscriptionEose(const String &subId);
27+
void onNostrSubscriptionEose(const String &subId);
28+
29+
time_t getMinutesAgo(int min);
30+
void subscribeZaps(nostr::NostrPool *pool, const String &relay, int minutesAgo);

0 commit comments

Comments
 (0)