@@ -15,7 +15,7 @@ export default {
15
15
name : "New Email Received" ,
16
16
description : "Emit new event when a new email is received." ,
17
17
type : "source" ,
18
- version : "0.1.6 " ,
18
+ version : "0.1.7 " ,
19
19
dedupe : "unique" ,
20
20
props : {
21
21
gmail,
@@ -121,11 +121,14 @@ export default {
121
121
) ;
122
122
}
123
123
124
- newProps . http = "$.interface.http" ;
124
+ newProps . http = {
125
+ type : "$.interface.http" ,
126
+ customResponse : true ,
127
+ } ;
125
128
newProps . timer = {
126
129
type : "$.interface.timer" ,
127
130
default : {
128
- intervalSeconds : 24 * 60 * 60 ,
131
+ intervalSeconds : 60 * 60 ,
129
132
} ,
130
133
hidden : true ,
131
134
} ;
@@ -203,12 +206,19 @@ export default {
203
206
204
207
props . latencyWarningAlert . hidden = false ;
205
208
206
- const historyId = await this . setupGmailNotifications ( topicName ) ;
209
+ const {
210
+ historyId, expiration,
211
+ } = await this . setupGmailNotifications ( topicName ) ;
207
212
newProps . initialHistoryId = {
208
213
type : "string" ,
209
214
default : historyId ,
210
215
hidden : true ,
211
216
} ;
217
+ newProps . expiration = {
218
+ type : "string" ,
219
+ default : expiration ,
220
+ hidden : true ,
221
+ } ;
212
222
}
213
223
}
214
224
props . label . hidden = false ;
@@ -279,6 +289,18 @@ export default {
279
289
_setLastProcessedHistoryId ( lastProcessedHistoryId ) {
280
290
this . db . set ( "lastProcessedHistoryId" , lastProcessedHistoryId ) ;
281
291
} ,
292
+ _getExpiration ( ) {
293
+ return this . db . get ( "expiration" ) ;
294
+ } ,
295
+ _setExpiration ( expiration ) {
296
+ this . db . set ( "expiration" , expiration ) ;
297
+ } ,
298
+ _getLastReceivedTime ( ) {
299
+ return this . db . get ( "lastReceivedTime" ) ;
300
+ } ,
301
+ _setLastReceivedTime ( lastReceivedTime ) {
302
+ this . db . set ( "lastReceivedTime" , lastReceivedTime ) ;
303
+ } ,
282
304
sdkParams ( ) {
283
305
const authKeyJSON = JSON . parse ( this . serviceAccountKeyJson ) ;
284
306
const {
@@ -338,7 +360,7 @@ export default {
338
360
} ,
339
361
} ) ;
340
362
console . log ( "Watch response:" , watchResponse ) ;
341
- return watchResponse . historyId ;
363
+ return watchResponse ;
342
364
} ,
343
365
async getOrCreateTopic ( name ) {
344
366
const sdkParams = this . sdkParams ( ) ;
@@ -413,17 +435,32 @@ export default {
413
435
// event was triggered by timer
414
436
const topicName = this . _getTopicName ( ) ;
415
437
if ( topicName ) {
416
- // renew Gmail push notifications
417
- await this . setupGmailNotifications ( topicName ) ;
438
+ // renew Gmail push notifications if expiring within the next hour
439
+ // or if no email has been received within the last hour
440
+ const currentExpiration = this . _getExpiration ( ) ;
441
+ const lastReceivedTime = this . _getLastReceivedTime ( ) ;
442
+ if (
443
+ ( + currentExpiration < ( event . timestamp + 3600 ) * 1000 )
444
+ || ( lastReceivedTime < ( event . timestamp - 3600 ) * 1000 )
445
+ ) {
446
+ const { expiration } = await this . setupGmailNotifications ( topicName ) ;
447
+ this . _setExpiration ( expiration ) ;
448
+ }
418
449
return ;
419
450
} else {
420
451
// first run, no need to renew push notifications
421
452
this . _setTopicName ( this . topic ) ;
422
- this . _setLastProcessedHistoryId ( this . initialHistoryId ) ;
453
+ const initialHistoryId = this . initialHistoryId || this . _getLastHistoryId ( ) ;
454
+ this . _setLastProcessedHistoryId ( initialHistoryId ) ;
455
+ this . _setExpiration ( this . expiration ) ;
423
456
return ;
424
457
}
425
458
}
426
459
460
+ this . http . respond ( {
461
+ status : 200 ,
462
+ } ) ;
463
+
427
464
// Extract the Pub/Sub message data
428
465
const pubsubMessage = event . body . message ;
429
466
if ( ! pubsubMessage ) {
@@ -491,6 +528,8 @@ export default {
491
528
this . _setLastProcessedHistoryId ( latestHistoryId ) ;
492
529
console . log ( "Updated lastProcessedHistoryId:" , latestHistoryId ) ;
493
530
531
+ this . _setLastReceivedTime ( Date . now ( ) ) ;
532
+
494
533
messageDetails . forEach ( ( message ) => {
495
534
if ( message ?. id ) {
496
535
this . emitEvent ( message ) ;
0 commit comments