From 02ab4e45e5e61e407b3038378410262c98d0e124 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Fri, 9 Sep 2022 15:12:19 +0800 Subject: [PATCH] Fix not setting confirmation code from Google Japan confiramtion emails --- packages/inbound-email-handler/src/index.ts | 2 +- .../inbound-email-handler/src/newsletter.ts | 22 +++++++++---- .../test/newsletter.test.ts | 32 ++++++++++++++++--- 3 files changed, 44 insertions(+), 12 deletions(-) diff --git a/packages/inbound-email-handler/src/index.ts b/packages/inbound-email-handler/src/index.ts index 4611d41c5..ee11511dc 100644 --- a/packages/inbound-email-handler/src/index.ts +++ b/packages/inbound-email-handler/src/index.ts @@ -107,7 +107,7 @@ export const inboundEmailHandler = Sentry.GCPFunction.wrapHttpFunction( console.log('non-newsletter email from', from, 'to', to) - if (isConfirmationEmail(from)) { + if (isConfirmationEmail(from, subject)) { console.log('handleConfirmation', from) await handleConfirmation(to, subject) return res.send('ok') diff --git a/packages/inbound-email-handler/src/newsletter.ts b/packages/inbound-email-handler/src/newsletter.ts index c3ff83972..1a6bbd5ef 100644 --- a/packages/inbound-email-handler/src/newsletter.ts +++ b/packages/inbound-email-handler/src/newsletter.ts @@ -11,10 +11,9 @@ interface Unsubscribe { const pubsub = new PubSub() const NEWSLETTER_EMAIL_RECEIVED_TOPIC = 'newsletterEmailReceived' const EMAIL_CONFIRMATION_CODE_RECEIVED_TOPIC = 'emailConfirmationCodeReceived' -const EMAIL_FORWARDING_SENDER_ADDRESSES = [ - 'Gmail Team ', -] -const CONFIRMATION_CODE_PATTERN = /^\(#\d+\)/ +const CONFIRMATION_EMAIL = 'forwarding-noreply@google.com' +// check unicode parentheses too +const CONFIRMATION_CODE_PATTERN = /^[((]#\d+[))]/u const UNSUBSCRIBE_HTTP_URL_PATTERN = /<(https?:\/\/[^>]*)>/ const UNSUBSCRIBE_MAIL_TO_PATTERN = /]*)>/ @@ -28,6 +27,14 @@ export const parseUnsubscribe = (unSubHeader: string): Unsubscribe => { } } +const parseAddress = (address: string): string => { + const parsed = addressparser(address) + if (parsed.length > 0) { + return parsed[0].address + } + return '' +} + export class NewsletterHandler { protected senderRegex = /NEWSLETTER_SENDER_REGEX/ protected urlRegex = /NEWSLETTER_URL_REGEX/ @@ -123,8 +130,11 @@ export const getConfirmationCode = (subject: string): string | undefined => { return undefined } -export const isConfirmationEmail = (from: string): boolean => { - return EMAIL_FORWARDING_SENDER_ADDRESSES.includes(from) +export const isConfirmationEmail = (from: string, subject: string): boolean => { + return ( + parseAddress(from) === CONFIRMATION_EMAIL && + CONFIRMATION_CODE_PATTERN.test(subject) + ) } const publishMessage = async ( diff --git a/packages/inbound-email-handler/test/newsletter.test.ts b/packages/inbound-email-handler/test/newsletter.test.ts index 58ffe2ec7..0a7ab8ced 100644 --- a/packages/inbound-email-handler/test/newsletter.test.ts +++ b/packages/inbound-email-handler/test/newsletter.test.ts @@ -14,17 +14,39 @@ import { MorningBrewHandler } from '../src/morning-brew-handler' describe('Confirmation email test', () => { describe('#isConfirmationEmail()', () => { - it('returns true when email is from Gmail Team', () => { - const from = 'Gmail Team ' + let from: string + let subject: string - expect(isConfirmationEmail(from)).to.be.true + it('returns true when email is from Gmail Team', () => { + from = 'Gmail Team ' + subject = `(#123456789) Gmail Forwarding Confirmation - Receive Mail from sam@omnivore.com` + + expect(isConfirmationEmail(from, subject)).to.be.true + }) + + it('returns true when email is from Japan Gmail Team', () => { + from = 'SWG チーム ' + subject = + '(#745359589)SWG の転送の確認 - miyanohara@nbi.academy からメールを受信' + + expect(isConfirmationEmail(from, subject)).to.be.true }) }) describe('#getConfirmationCode()', () => { + let code: string + let subject: string + it('returns the confirmation code from the email', () => { - const code = '593781109' - const subject = `(#${code}) Gmail Forwarding Confirmation - Receive Mail from sam@omnivore.com` + code = '593781109' + subject = `(#${code}) Gmail Forwarding Confirmation - Receive Mail from sam@omnivore.com` + + expect(getConfirmationCode(subject)).to.equal(code) + }) + + it('returns the confirmation code from the Google Japan email', () => { + code = '745359589' + subject = `(#${code})SWG の転送の確認 - miyanohara@nbi.academy からメールを受信` expect(getConfirmationCode(subject)).to.equal(code) })