From a614bd15dbe8f95a9fbb00b3cf99c8d6ecb5a47b Mon Sep 17 00:00:00 2001 From: Djuri Baars Date: Wed, 23 Oct 2024 00:43:53 +0200 Subject: [PATCH] Restore Nostr Zap subscription after loss of connection --- data | 2 +- src/lib/nostr_notify.cpp | 76 +++++++++++++++++++++++++++------------- src/lib/nostr_notify.hpp | 5 ++- 3 files changed, 57 insertions(+), 26 deletions(-) diff --git a/data b/data index 9867988..384b431 160000 --- a/data +++ b/data @@ -1 +1 @@ -Subproject commit 9867988a095a09a693aa2dc567605d15cfe4ceb1 +Subproject commit 384b4317c4b58afd981f410a9b732700e733b00b diff --git a/src/lib/nostr_notify.cpp b/src/lib/nostr_notify.cpp index 2e044bb..237eabe 100644 --- a/src/lib/nostr_notify.cpp +++ b/src/lib/nostr_notify.cpp @@ -4,16 +4,20 @@ std::vector pools; nostr::Transport *transport; TaskHandle_t nostrTaskHandle = NULL; boolean nostrIsConnected = false; +boolean nostrIsSubscribed = false; +boolean nostrIsSubscribing = true; + +String subIdZap; void setupNostrNotify(bool asDatasource, bool zapNotify) { nostr::esp32::ESP32Platform::initNostr(false); - time_t now; - time(&now); - struct tm *utcTimeInfo; - utcTimeInfo = gmtime(&now); - time_t utcNow = mktime(utcTimeInfo); - time_t timestamp60MinutesAgo = utcNow - 3600; + // time_t now; + // time(&now); + // struct tm *utcTimeInfo; + // utcTimeInfo = gmtime(&now); + // time_t utcNow = mktime(utcTimeInfo); + // time_t timestamp60MinutesAgo = utcNow - 3600; try { @@ -27,20 +31,7 @@ void setupNostrNotify(bool asDatasource, bool zapNotify) if (zapNotify) { - String subIdZap = pool->subscribeMany( - {relay}, - { - { - {"kinds", {"9735"}}, - {"limit", {"1"}}, - {"since", {String(timestamp60MinutesAgo)}}, - {"#p", {preferences.getString("nostrZapPubkey", DEFAULT_ZAP_NOTIFY_PUBKEY)}}, - }, - }, - handleNostrZapCallback, - onNostrSubscriptionClosed, - onNostrSubscriptionEose); - Serial.println("[ Nostr ] Subscribing to Zap Notifications"); + subscribeZaps(pool, relay, 60); } if (asDatasource) @@ -50,7 +41,7 @@ void setupNostrNotify(bool asDatasource, bool zapNotify) {// First filter { {"kinds", {"1"}}, - {"since", {String(timestamp60MinutesAgo)}}, + {"since", {String(getMinutesAgo(60))}}, {"authors", {pubKey}}, }}, handleNostrEventCallback, @@ -72,11 +63,12 @@ void setupNostrNotify(bool asDatasource, bool zapNotify) sstatus="CONNECTED"; }else if(status==nostr::ConnectionStatus::DISCONNECTED){ nostrIsConnected = false; + nostrIsSubscribed = false; sstatus="DISCONNECTED"; }else if(status==nostr::ConnectionStatus::ERROR){ sstatus = "ERROR"; } - //Serial.println("[ Nostr ] Connection status changed: " + sstatus); + Serial.println("[ Nostr ] Connection status changed: " + sstatus); }); } } @@ -88,8 +80,10 @@ void setupNostrNotify(bool asDatasource, bool zapNotify) void nostrTask(void *pvParameters) { - int blockFetch = getBlockFetch(); - processNewBlock(blockFetch); + if(preferences.getBool("useNostr", DEFAULT_USE_NOSTR)) { + int blockFetch = getBlockFetch(); + processNewBlock(blockFetch); + } while (1) { @@ -98,6 +92,10 @@ void nostrTask(void *pvParameters) // Run internal loop: refresh relays, complete pending connections, send // pending messages pool->loop(); + if (!nostrIsSubscribed && !nostrIsSubscribing) { + Serial.println(F("Not subscribed")); + subscribeZaps(pool, preferences.getString("nostrRelay"), 1); + } } vTaskDelay(pdMS_TO_TICKS(100)); } @@ -125,6 +123,8 @@ void onNostrSubscriptionEose(const String &subId) // This is the callback that will be called when the subscription is // EOSE Serial.println("[ Nostr ] Subscription EOSE: " + subId); + nostrIsSubscribing = false; + nostrIsSubscribed = true; } void handleNostrEventCallback(const String &subId, nostr::SignedNostrEvent *event) @@ -183,6 +183,34 @@ void handleNostrEventCallback(const String &subId, nostr::SignedNostrEvent *even } } +time_t getMinutesAgo(int min) { + time_t now; + time(&now); + return now - (min * 60); +} + +void subscribeZaps(nostr::NostrPool *pool, const String &relay, int minutesAgo) { + if (subIdZap) { + pool->closeSubscription(subIdZap); + } + nostrIsSubscribing = true; + + subIdZap = pool->subscribeMany( + {relay}, + { + { + {"kinds", {"9735"}}, + {"limit", {"1"}}, + {"since", {String(getMinutesAgo(minutesAgo))}}, + {"#p", {preferences.getString("nostrZapPubkey", DEFAULT_ZAP_NOTIFY_PUBKEY)}}, + }, + }, + handleNostrZapCallback, + onNostrSubscriptionClosed, + onNostrSubscriptionEose); + Serial.println("[ Nostr ] Subscribing to Zap Notifications since " + String(getMinutesAgo(minutesAgo))); +} + void handleNostrZapCallback(const String &subId, nostr::SignedNostrEvent *event) { // Received events callback, we can access the event content with // event->getContent() Here you should handle the event, for this diff --git a/src/lib/nostr_notify.hpp b/src/lib/nostr_notify.hpp index 045574d..ebbe2cb 100644 --- a/src/lib/nostr_notify.hpp +++ b/src/lib/nostr_notify.hpp @@ -24,4 +24,7 @@ void handleNostrEventCallback(const String &subId, nostr::SignedNostrEvent *even void handleNostrZapCallback(const String &subId, nostr::SignedNostrEvent *event); void onNostrSubscriptionClosed(const String &subId, const String &reason); -void onNostrSubscriptionEose(const String &subId); \ No newline at end of file +void onNostrSubscriptionEose(const String &subId); + +time_t getMinutesAgo(int min); +void subscribeZaps(nostr::NostrPool *pool, const String &relay, int minutesAgo); \ No newline at end of file