Fix not setting confirmation code from Google Japan confiramtion emails

This commit is contained in:
Hongbo Wu
2022-09-09 15:12:19 +08:00
parent a891cc16a0
commit 02ab4e45e5
3 changed files with 44 additions and 12 deletions

View File

@ -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')

View File

@ -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 <forwarding-noreply@google.com>',
]
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 = /<mailto:([^>]*)>/
@ -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 (

View File

@ -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 <forwarding-noreply@google.com>'
let from: string
let subject: string
expect(isConfirmationEmail(from)).to.be.true
it('returns true when email is from Gmail Team', () => {
from = 'Gmail Team <forwarding-noreply@google.com>'
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 チーム <forwarding-noreply@google.com>'
subject =
'#745359589SWG の転送の確認 - 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)
})