@@ -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.9 " ,
18+ version : "0.1.10 " ,
1919 dedupe : "unique" ,
2020 props : {
2121 gmail,
@@ -72,12 +72,27 @@ export default {
7272 hidden : true ,
7373 reloadProps : true ,
7474 } ,
75- label : {
75+ labels : {
7676 propDefinition : [
7777 gmail ,
7878 "label" ,
7979 ] ,
80- default : "INBOX" ,
80+ type : "string[]" ,
81+ label : "Labels" ,
82+ default : [
83+ "INBOX" ,
84+ ] ,
85+ optional : true ,
86+ hidden : true ,
87+ } ,
88+ excludeLabels : {
89+ propDefinition : [
90+ gmail ,
91+ "label" ,
92+ ] ,
93+ type : "string[]" ,
94+ label : "Exclude Labels" ,
95+ description : "Emails with the specified labels will be excluded from results" ,
8196 optional : true ,
8297 hidden : true ,
8398 } ,
@@ -221,7 +236,8 @@ export default {
221236 } ;
222237 }
223238 }
224- props . label . hidden = false ;
239+ props . labels . hidden = false ;
240+ props . excludeLabels . hidden = false ;
225241 return newProps ;
226242 } ,
227243 hooks : {
@@ -354,8 +370,8 @@ export default {
354370 path : "/users/me/watch" ,
355371 data : {
356372 topicName,
357- labelIds : [
358- this . label || "INBOX" ,
373+ labelIds : this . labels || [
374+ "INBOX" ,
359375 ] ,
360376 } ,
361377 } ) ;
@@ -396,14 +412,18 @@ export default {
396412 } ;
397413 } ,
398414 filterHistory ( history ) {
399- return this . label
400- ? history . filter (
401- ( item ) =>
402- item . messagesAdded ?. length &&
403- item . messagesAdded [ 0 ] . message . labelIds &&
404- item . messagesAdded [ 0 ] . message . labelIds . includes ( this . label ) ,
405- )
406- : history . filter ( ( item ) => item . messagesAdded ?. length ) ;
415+ let filteredHistory = history . filter ( ( item ) => item . messagesAdded ?. length ) ;
416+ if ( this . labels ) {
417+ filteredHistory = filteredHistory . filter ( ( item ) =>
418+ item . messagesAdded [ 0 ] . message . labelIds &&
419+ item . messagesAdded [ 0 ] . message . labelIds . some ( ( i ) => this . labels . includes ( i ) ) ) ;
420+ }
421+ if ( this . excludeLabels ) {
422+ filteredHistory = filteredHistory . filter ( ( item ) =>
423+ item . messagesAdded [ 0 ] . message . labelIds &&
424+ ! ( item . messagesAdded [ 0 ] . message . labelIds . some ( ( i ) => this . excludeLabels . includes ( i ) ) ) ) ;
425+ }
426+ return filteredHistory ;
407427 } ,
408428 async getMessageDetails ( ids ) {
409429 const messages = await Promise . all ( ids . map ( async ( id ) => {
@@ -419,14 +439,19 @@ export default {
419439 } ) ) ;
420440 return messages ;
421441 } ,
422- getHistoryResponse ( startHistoryId ) {
423- return this . gmail . listHistory ( {
424- startHistoryId,
425- historyTypes : [
426- "messageAdded" ,
427- ] ,
428- labelId : this . label ,
429- } ) ;
442+ async getHistoryResponses ( startHistoryId ) {
443+ const historyResponses = [ ] ;
444+ for ( const labelId of this . labels ) {
445+ const response = await this . gmail . listHistory ( {
446+ startHistoryId,
447+ historyTypes : [
448+ "messageAdded" ,
449+ ] ,
450+ labelId,
451+ } ) ;
452+ historyResponses . push ( response ) ;
453+ }
454+ return historyResponses ;
430455 } ,
431456 } ,
432457 async run ( event ) {
@@ -495,9 +520,9 @@ export default {
495520 console . log ( "Using startHistoryId:" , startHistoryId ) ;
496521
497522 // Fetch the history
498- let historyResponse ;
523+ let historyResponses ;
499524 try {
500- historyResponse = await this . getHistoryResponse ( startHistoryId ) ;
525+ historyResponses = await this . getHistoryResponses ( startHistoryId ) ;
501526 } catch {
502527 // catch error thrown if startHistoryId is invalid or expired
503528
@@ -507,19 +532,20 @@ export default {
507532 // set startHistoryId to the historyId received from the webhook
508533 startHistoryId = parseInt ( receivedHistoryId ) ;
509534 console . log ( "Using startHistoryId:" , startHistoryId ) ;
510- historyResponse = await this . getHistoryResponse ( startHistoryId ) ;
535+ historyResponses = await this . getHistoryResponses ( startHistoryId ) ;
511536 }
512537
513538 console . log (
514- "History response :" ,
515- JSON . stringify ( historyResponse , null , 2 ) ,
539+ "History responses :" ,
540+ JSON . stringify ( historyResponses , null , 2 ) ,
516541 ) ;
517542
518543 // Process history to find new messages
519544 const newMessages = [ ] ;
520- if ( historyResponse . history ) {
521- for ( const historyItem of historyResponse . history ) {
522- if ( historyItem . messagesAdded ) {
545+ for ( const historyResponse of historyResponses ) {
546+ if ( historyResponse . history ) {
547+ const historyResponseFiltered = this . filterHistory ( historyResponse . history ) ;
548+ for ( const historyItem of historyResponseFiltered ) {
523549 newMessages . push (
524550 ...historyItem . messagesAdded . map ( ( msg ) => msg . message ) ,
525551 ) ;
@@ -540,7 +566,10 @@ export default {
540566 console . log ( "Fetched message details count:" , messageDetails . length ) ;
541567
542568 // Store the latest historyId in the db
543- const latestHistoryId = historyResponse . historyId || receivedHistoryId ;
569+ let latestHistoryId = receivedHistoryId ;
570+ for ( const historyResponse of historyResponses ) {
571+ latestHistoryId = Math . max ( latestHistoryId , historyResponse . historyId ) ;
572+ }
544573 this . _setLastProcessedHistoryId ( latestHistoryId ) ;
545574 console . log ( "Updated lastProcessedHistoryId:" , latestHistoryId ) ;
546575
0 commit comments