@@ -15,7 +15,7 @@ export default {
1515 name : "New Email Received" ,
1616 description : "Emit new event when a new email is received." ,
1717 type : "source" ,
18- version : "0.1.6 " ,
18+ version : "0.1.7 " ,
1919 dedupe : "unique" ,
2020 props : {
2121 gmail,
@@ -121,11 +121,14 @@ export default {
121121 ) ;
122122 }
123123
124- newProps . http = "$.interface.http" ;
124+ newProps . http = {
125+ type : "$.interface.http" ,
126+ customResponse : true ,
127+ } ;
125128 newProps . timer = {
126129 type : "$.interface.timer" ,
127130 default : {
128- intervalSeconds : 24 * 60 * 60 ,
131+ intervalSeconds : 60 * 60 ,
129132 } ,
130133 hidden : true ,
131134 } ;
@@ -203,12 +206,19 @@ export default {
203206
204207 props . latencyWarningAlert . hidden = false ;
205208
206- const historyId = await this . setupGmailNotifications ( topicName ) ;
209+ const {
210+ historyId, expiration,
211+ } = await this . setupGmailNotifications ( topicName ) ;
207212 newProps . initialHistoryId = {
208213 type : "string" ,
209214 default : historyId ,
210215 hidden : true ,
211216 } ;
217+ newProps . expiration = {
218+ type : "string" ,
219+ default : expiration ,
220+ hidden : true ,
221+ } ;
212222 }
213223 }
214224 props . label . hidden = false ;
@@ -279,6 +289,18 @@ export default {
279289 _setLastProcessedHistoryId ( lastProcessedHistoryId ) {
280290 this . db . set ( "lastProcessedHistoryId" , lastProcessedHistoryId ) ;
281291 } ,
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+ } ,
282304 sdkParams ( ) {
283305 const authKeyJSON = JSON . parse ( this . serviceAccountKeyJson ) ;
284306 const {
@@ -338,7 +360,7 @@ export default {
338360 } ,
339361 } ) ;
340362 console . log ( "Watch response:" , watchResponse ) ;
341- return watchResponse . historyId ;
363+ return watchResponse ;
342364 } ,
343365 async getOrCreateTopic ( name ) {
344366 const sdkParams = this . sdkParams ( ) ;
@@ -413,17 +435,32 @@ export default {
413435 // event was triggered by timer
414436 const topicName = this . _getTopicName ( ) ;
415437 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+ }
418449 return ;
419450 } else {
420451 // first run, no need to renew push notifications
421452 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 ) ;
423456 return ;
424457 }
425458 }
426459
460+ this . http . respond ( {
461+ status : 200 ,
462+ } ) ;
463+
427464 // Extract the Pub/Sub message data
428465 const pubsubMessage = event . body . message ;
429466 if ( ! pubsubMessage ) {
@@ -491,6 +528,8 @@ export default {
491528 this . _setLastProcessedHistoryId ( latestHistoryId ) ;
492529 console . log ( "Updated lastProcessedHistoryId:" , latestHistoryId ) ;
493530
531+ this . _setLastReceivedTime ( Date . now ( ) ) ;
532+
494533 messageDetails . forEach ( ( message ) => {
495534 if ( message ?. id ) {
496535 this . emitEvent ( message ) ;
0 commit comments