| 423 | } |
| 424 | |
| 425 | function filterEmailsByLabels(emails: GmailEmail[], config: GmailWebhookConfig): GmailEmail[] { |
| 426 | if (!config.labelIds.length) { |
| 427 | return emails |
| 428 | } |
| 429 | |
| 430 | return emails.filter((email) => { |
| 431 | const emailLabels = email.labelIds || [] |
| 432 | const hasMatchingLabel = config.labelIds.some((configLabel) => |
| 433 | emailLabels.includes(configLabel) |
| 434 | ) |
| 435 | return config.labelFilterBehavior === 'INCLUDE' ? hasMatchingLabel : !hasMatchingLabel |
| 436 | }) |
| 437 | } |
| 438 | |
| 439 | async function processEmails( |
| 440 | emails: GmailEmail[], |