Restore Nostr Zap subscription after loss of connection
This commit is contained in:
parent
b0ec0685a1
commit
a29f909ec6
3 changed files with 57 additions and 26 deletions
2
data
2
data
|
@ -1 +1 @@
|
||||||
Subproject commit 9867988a095a09a693aa2dc567605d15cfe4ceb1
|
Subproject commit 384b4317c4b58afd981f410a9b732700e733b00b
|
|
@ -4,16 +4,20 @@ std::vector<nostr::NostrPool *> pools;
|
||||||
nostr::Transport *transport;
|
nostr::Transport *transport;
|
||||||
TaskHandle_t nostrTaskHandle = NULL;
|
TaskHandle_t nostrTaskHandle = NULL;
|
||||||
boolean nostrIsConnected = false;
|
boolean nostrIsConnected = false;
|
||||||
|
boolean nostrIsSubscribed = false;
|
||||||
|
boolean nostrIsSubscribing = true;
|
||||||
|
|
||||||
|
String subIdZap;
|
||||||
|
|
||||||
void setupNostrNotify(bool asDatasource, bool zapNotify)
|
void setupNostrNotify(bool asDatasource, bool zapNotify)
|
||||||
{
|
{
|
||||||
nostr::esp32::ESP32Platform::initNostr(false);
|
nostr::esp32::ESP32Platform::initNostr(false);
|
||||||
time_t now;
|
// time_t now;
|
||||||
time(&now);
|
// time(&now);
|
||||||
struct tm *utcTimeInfo;
|
// struct tm *utcTimeInfo;
|
||||||
utcTimeInfo = gmtime(&now);
|
// utcTimeInfo = gmtime(&now);
|
||||||
time_t utcNow = mktime(utcTimeInfo);
|
// time_t utcNow = mktime(utcTimeInfo);
|
||||||
time_t timestamp60MinutesAgo = utcNow - 3600;
|
// time_t timestamp60MinutesAgo = utcNow - 3600;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -27,20 +31,7 @@ void setupNostrNotify(bool asDatasource, bool zapNotify)
|
||||||
|
|
||||||
if (zapNotify)
|
if (zapNotify)
|
||||||
{
|
{
|
||||||
String subIdZap = pool->subscribeMany(
|
subscribeZaps(pool, relay, 60);
|
||||||
{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");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (asDatasource)
|
if (asDatasource)
|
||||||
|
@ -50,7 +41,7 @@ void setupNostrNotify(bool asDatasource, bool zapNotify)
|
||||||
{// First filter
|
{// First filter
|
||||||
{
|
{
|
||||||
{"kinds", {"1"}},
|
{"kinds", {"1"}},
|
||||||
{"since", {String(timestamp60MinutesAgo)}},
|
{"since", {String(getMinutesAgo(60))}},
|
||||||
{"authors", {pubKey}},
|
{"authors", {pubKey}},
|
||||||
}},
|
}},
|
||||||
handleNostrEventCallback,
|
handleNostrEventCallback,
|
||||||
|
@ -72,11 +63,12 @@ void setupNostrNotify(bool asDatasource, bool zapNotify)
|
||||||
sstatus="CONNECTED";
|
sstatus="CONNECTED";
|
||||||
}else if(status==nostr::ConnectionStatus::DISCONNECTED){
|
}else if(status==nostr::ConnectionStatus::DISCONNECTED){
|
||||||
nostrIsConnected = false;
|
nostrIsConnected = false;
|
||||||
|
nostrIsSubscribed = false;
|
||||||
sstatus="DISCONNECTED";
|
sstatus="DISCONNECTED";
|
||||||
}else if(status==nostr::ConnectionStatus::ERROR){
|
}else if(status==nostr::ConnectionStatus::ERROR){
|
||||||
sstatus = "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)
|
void nostrTask(void *pvParameters)
|
||||||
{
|
{
|
||||||
|
if(preferences.getBool("useNostr", DEFAULT_USE_NOSTR)) {
|
||||||
int blockFetch = getBlockFetch();
|
int blockFetch = getBlockFetch();
|
||||||
processNewBlock(blockFetch);
|
processNewBlock(blockFetch);
|
||||||
|
}
|
||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
|
@ -98,6 +92,10 @@ void nostrTask(void *pvParameters)
|
||||||
// Run internal loop: refresh relays, complete pending connections, send
|
// Run internal loop: refresh relays, complete pending connections, send
|
||||||
// pending messages
|
// pending messages
|
||||||
pool->loop();
|
pool->loop();
|
||||||
|
if (!nostrIsSubscribed && !nostrIsSubscribing) {
|
||||||
|
Serial.println(F("Not subscribed"));
|
||||||
|
subscribeZaps(pool, preferences.getString("nostrRelay"), 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
vTaskDelay(pdMS_TO_TICKS(100));
|
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
|
// This is the callback that will be called when the subscription is
|
||||||
// EOSE
|
// EOSE
|
||||||
Serial.println("[ Nostr ] Subscription EOSE: " + subId);
|
Serial.println("[ Nostr ] Subscription EOSE: " + subId);
|
||||||
|
nostrIsSubscribing = false;
|
||||||
|
nostrIsSubscribed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void handleNostrEventCallback(const String &subId, nostr::SignedNostrEvent *event)
|
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) {
|
void handleNostrZapCallback(const String &subId, nostr::SignedNostrEvent *event) {
|
||||||
// Received events callback, we can access the event content with
|
// Received events callback, we can access the event content with
|
||||||
// event->getContent() Here you should handle the event, for this
|
// event->getContent() Here you should handle the event, for this
|
||||||
|
|
|
@ -25,3 +25,6 @@ void handleNostrZapCallback(const String &subId, nostr::SignedNostrEvent *event)
|
||||||
|
|
||||||
void onNostrSubscriptionClosed(const String &subId, const String &reason);
|
void onNostrSubscriptionClosed(const String &subId, const String &reason);
|
||||||
void onNostrSubscriptionEose(const String &subId);
|
void onNostrSubscriptionEose(const String &subId);
|
||||||
|
|
||||||
|
time_t getMinutesAgo(int min);
|
||||||
|
void subscribeZaps(nostr::NostrPool *pool, const String &relay, int minutesAgo);
|
Loading…
Reference in a new issue