diff --git a/packages/api/src/routers/svc/emails.ts b/packages/api/src/routers/svc/emails.ts index 6aae07d74..5fa138812 100644 --- a/packages/api/src/routers/svc/emails.ts +++ b/packages/api/src/routers/svc/emails.ts @@ -23,7 +23,7 @@ interface ForwardEmailMessage { html: string unsubMailTo?: string unsubHttpUrl?: string - text?: string + text: string forwardedFrom?: string } @@ -37,7 +37,7 @@ export function emailsServiceRouter() { logger.info('email forward router') const { message, expired } = readPushSubscription(req) - logger.info('pubsub message:', message, 'expired:', expired) + logger.info('pubsub message:', { message, expired }) if (!message) { res.status(400).send('Bad Request') @@ -45,7 +45,7 @@ export function emailsServiceRouter() { } if (expired) { - logger.log('discards expired message:', message) + logger.info('discards expired message.') res.status(200).send('Expired') return } @@ -58,9 +58,9 @@ export function emailsServiceRouter() { !('from' in data) || !('to' in data) || !('subject' in data) || - !('html' in data) + (!('html' in data) && !('text' in data)) ) { - logger.info('Invalid message') + logger.error('Invalid message') res.status(400).send('Bad Request') return } @@ -69,7 +69,7 @@ export function emailsServiceRouter() { const newsletterEmail = await getNewsletterEmail(data.to) if (!newsletterEmail) { - logger.info('newsletter email not found', data.to) + logger.info('newsletter email not found', { email: data.to }) res.status(200).send('Not Found') return } @@ -83,12 +83,12 @@ export function emailsServiceRouter() { data.subject ) ) { - logger.info('handling as article', data) + logger.info('handling as article') await saveEmail(ctx, { title: getTitleFromEmailSubject(data.subject), author: parsedFrom.name, url: generateUniqueUrl(), - originalContent: data.html, + originalContent: data.html || data.text, }) res.status(200).send('Article') return @@ -113,8 +113,8 @@ export function emailsServiceRouter() { }) if (!result) { - logger.info('Email not forwarded', data) - res.status(200).send('Failed to send email') + logger.error('Email not forwarded') + res.status(500).send('Failed to send email') return } diff --git a/packages/inbound-email-handler/src/newsletter.ts b/packages/inbound-email-handler/src/newsletter.ts index 1f3294541..b3239f55d 100644 --- a/packages/inbound-email-handler/src/newsletter.ts +++ b/packages/inbound-email-handler/src/newsletter.ts @@ -10,7 +10,7 @@ interface Unsubscribe { const EMAIL_CONFIRMATION_CODE_RECEIVED_TOPIC = 'emailConfirmationCodeReceived' const CONFIRMATION_EMAIL_SENDER_ADDRESS = 'forwarding-noreply@google.com' // check unicode parentheses too -const CONFIRMATION_CODE_PATTERN = /^[((]#\d+[))]/u +const CONFIRMATION_CODE_PATTERN = /\d+/u const UNSUBSCRIBE_HTTP_URL_PATTERN = /<(https?:\/\/[^>]*)>/ const UNSUBSCRIBE_MAIL_TO_PATTERN = /]*)>/ @@ -55,7 +55,7 @@ export const getConfirmationCode = (subject: string): string | undefined => { if (matches) { // get the number code only // e.g. (#123456) => 123456 - return matches[0].slice(2, -1) + return matches[0] } return undefined } diff --git a/packages/inbound-email-handler/test/newsletter.test.ts b/packages/inbound-email-handler/test/newsletter.test.ts index 369cf9ab5..598b0a50d 100644 --- a/packages/inbound-email-handler/test/newsletter.test.ts +++ b/packages/inbound-email-handler/test/newsletter.test.ts @@ -25,6 +25,14 @@ describe('Confirmation email test', () => { expect(isConfirmationEmail(from, subject)).to.be.true }) + + it('returns true when email is in Spanish', () => { + from = 'Equipo de Gmail ' + subject = + 'Confirmación de reenvío de 123456789 (n.º Gmail) - Recibir correo de test@omnivore.app' + + expect(isConfirmationEmail(from, subject)).to.be.true + }) }) describe('#getConfirmationCode()', () => { @@ -44,6 +52,13 @@ describe('Confirmation email test', () => { expect(getConfirmationCode(subject)).to.equal(code) }) + + it('returns the confirmation code from the Spanish email', () => { + code = '123456789' + subject = `Confirmación de reenvío de ${code} (n.º Gmail) - Recibir correo de test@omnivore.app` + + expect(getConfirmationCode(subject)).to.equal(code) + }) }) })