@@ -4,16 +4,20 @@ std::vector<nostr::NostrPool *> pools;
4
4
nostr::Transport *transport;
5
5
TaskHandle_t nostrTaskHandle = NULL ;
6
6
boolean nostrIsConnected = false ;
7
+ boolean nostrIsSubscribed = false ;
8
+ boolean nostrIsSubscribing = true ;
9
+
10
+ String subIdZap;
7
11
8
12
void setupNostrNotify (bool asDatasource, bool zapNotify)
9
13
{
10
14
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;
17
21
18
22
try
19
23
{
@@ -27,20 +31,7 @@ void setupNostrNotify(bool asDatasource, bool zapNotify)
27
31
28
32
if (zapNotify)
29
33
{
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 );
44
35
}
45
36
46
37
if (asDatasource)
@@ -50,7 +41,7 @@ void setupNostrNotify(bool asDatasource, bool zapNotify)
50
41
{// First filter
51
42
{
52
43
{" kinds" , {" 1" }},
53
- {" since" , {String (timestamp60MinutesAgo )}},
44
+ {" since" , {String (getMinutesAgo ( 60 ) )}},
54
45
{" authors" , {pubKey}},
55
46
}},
56
47
handleNostrEventCallback,
@@ -72,11 +63,12 @@ void setupNostrNotify(bool asDatasource, bool zapNotify)
72
63
sstatus=" CONNECTED" ;
73
64
}else if (status==nostr::ConnectionStatus::DISCONNECTED){
74
65
nostrIsConnected = false ;
66
+ nostrIsSubscribed = false ;
75
67
sstatus=" DISCONNECTED" ;
76
68
}else if (status==nostr::ConnectionStatus::ERROR){
77
69
sstatus = " ERROR" ;
78
70
}
79
- // Serial.println("[ Nostr ] Connection status changed: " + sstatus);
71
+ Serial.println (" [ Nostr ] Connection status changed: " + sstatus);
80
72
});
81
73
}
82
74
}
@@ -88,8 +80,10 @@ void setupNostrNotify(bool asDatasource, bool zapNotify)
88
80
89
81
void nostrTask (void *pvParameters)
90
82
{
91
- int blockFetch = getBlockFetch ();
92
- processNewBlock (blockFetch);
83
+ if (preferences.getBool (" useNostr" , DEFAULT_USE_NOSTR)) {
84
+ int blockFetch = getBlockFetch ();
85
+ processNewBlock (blockFetch);
86
+ }
93
87
94
88
while (1 )
95
89
{
@@ -98,6 +92,10 @@ void nostrTask(void *pvParameters)
98
92
// Run internal loop: refresh relays, complete pending connections, send
99
93
// pending messages
100
94
pool->loop ();
95
+ if (!nostrIsSubscribed && !nostrIsSubscribing) {
96
+ Serial.println (F (" Not subscribed" ));
97
+ subscribeZaps (pool, preferences.getString (" nostrRelay" ), 1 );
98
+ }
101
99
}
102
100
vTaskDelay (pdMS_TO_TICKS (100 ));
103
101
}
@@ -125,6 +123,8 @@ void onNostrSubscriptionEose(const String &subId)
125
123
// This is the callback that will be called when the subscription is
126
124
// EOSE
127
125
Serial.println (" [ Nostr ] Subscription EOSE: " + subId);
126
+ nostrIsSubscribing = false ;
127
+ nostrIsSubscribed = true ;
128
128
}
129
129
130
130
void handleNostrEventCallback (const String &subId, nostr::SignedNostrEvent *event)
@@ -183,6 +183,34 @@ void handleNostrEventCallback(const String &subId, nostr::SignedNostrEvent *even
183
183
}
184
184
}
185
185
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
+
186
214
void handleNostrZapCallback (const String &subId, nostr::SignedNostrEvent *event) {
187
215
// Received events callback, we can access the event content with
188
216
// event->getContent() Here you should handle the event, for this
0 commit comments