mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
* forward all emails we failed to handle * update Regex to get confirmation code from email subject * add tests * removed real email from tests
This commit is contained in:
parent
e910863935
commit
705b67946d
3 changed files with 57 additions and 41 deletions
|
|
@ -75,8 +75,8 @@ export const inboundEmailHandler = Sentry.GCPFunction.wrapHttpFunction(
|
|||
|
||||
// check if it is a forwarding confirmation email or newsletter
|
||||
const newsletterHandler = getNewsletterHandler(rawUrl, from)
|
||||
if (newsletterHandler) {
|
||||
try {
|
||||
try {
|
||||
if (newsletterHandler) {
|
||||
console.log('handleNewsletter', from, recipientAddress)
|
||||
await newsletterHandler.handleNewsletter(
|
||||
recipientAddress,
|
||||
|
|
@ -85,13 +85,22 @@ export const inboundEmailHandler = Sentry.GCPFunction.wrapHttpFunction(
|
|||
subject,
|
||||
from
|
||||
)
|
||||
} catch (error) {
|
||||
console.log(
|
||||
'error handling newsletter, will forward.',
|
||||
from,
|
||||
recipientAddress,
|
||||
subject
|
||||
)
|
||||
} else {
|
||||
console.log('non-newsletter email from:', from, recipientAddress)
|
||||
|
||||
if (isConfirmationEmail(from)) {
|
||||
console.log('handleConfirmation', from)
|
||||
await handleConfirmation(recipientAddress, subject)
|
||||
} else if (pdfAttachment) {
|
||||
console.log('handle PDF attachment', from, recipientAddress)
|
||||
await handlePdfAttachment(
|
||||
recipientAddress,
|
||||
pdfAttachmentName,
|
||||
pdfAttachment,
|
||||
subject
|
||||
)
|
||||
}
|
||||
|
||||
// queue non-newsletter emails
|
||||
await pubsub.topic(NON_NEWSLETTER_EMAIL_TOPIC).publishMessage({
|
||||
json: {
|
||||
|
|
@ -103,23 +112,14 @@ export const inboundEmailHandler = Sentry.GCPFunction.wrapHttpFunction(
|
|||
},
|
||||
})
|
||||
}
|
||||
} else {
|
||||
console.log('non-newsletter email from:', from, recipientAddress)
|
||||
|
||||
if (isConfirmationEmail(from)) {
|
||||
console.log('handleConfirmation', from, recipientAddress)
|
||||
await handleConfirmation(recipientAddress, subject)
|
||||
} else if (pdfAttachment) {
|
||||
console.log('handle PDF attachment', from, recipientAddress)
|
||||
await handlePdfAttachment(
|
||||
recipientAddress,
|
||||
pdfAttachmentName,
|
||||
pdfAttachment,
|
||||
subject
|
||||
)
|
||||
}
|
||||
|
||||
// queue non-newsletter emails
|
||||
} catch (error) {
|
||||
console.log(
|
||||
'error handling emails, will forward.',
|
||||
from,
|
||||
recipientAddress,
|
||||
subject
|
||||
)
|
||||
// queue error emails
|
||||
await pubsub.topic(NON_NEWSLETTER_EMAIL_TOPIC).publishMessage({
|
||||
json: {
|
||||
from: from,
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ 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_CODE_PATTERN = /^\(#\d+\)/
|
||||
|
||||
export class NewsletterHandler {
|
||||
protected senderRegex = /NEWSLETTER_SENDER_REGEX/
|
||||
|
|
@ -70,16 +70,9 @@ export class NewsletterHandler {
|
|||
}
|
||||
|
||||
export const handleConfirmation = async (email: string, subject: string) => {
|
||||
console.log('confirmation email')
|
||||
|
||||
let confirmationCode = ''
|
||||
const matches = subject.match(CONFIRMATION_CODE_PATTERN)
|
||||
if (matches) {
|
||||
// get the number code only
|
||||
// e.g. (#123456) => 123456
|
||||
confirmationCode = matches[0].slice(2, -1)
|
||||
}
|
||||
console.log('confirmation email', email, subject)
|
||||
|
||||
const confirmationCode = getConfirmationCode(subject)
|
||||
if (!email || !confirmationCode) {
|
||||
console.log(
|
||||
'confirmation email error, user email:',
|
||||
|
|
@ -94,6 +87,20 @@ export const handleConfirmation = async (email: string, subject: string) => {
|
|||
return publishMessage(EMAIL_CONFIRMATION_CODE_RECEIVED_TOPIC, message)
|
||||
}
|
||||
|
||||
export const getConfirmationCode = (subject: string): string | undefined => {
|
||||
const matches = subject.match(CONFIRMATION_CODE_PATTERN)
|
||||
if (matches) {
|
||||
// get the number code only
|
||||
// e.g. (#123456) => 123456
|
||||
return matches[0].slice(2, -1)
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
export const isConfirmationEmail = (from: string): boolean => {
|
||||
return EMAIL_FORWARDING_SENDER_ADDRESSES.includes(from)
|
||||
}
|
||||
|
||||
const publishMessage = async (
|
||||
topic: string,
|
||||
message: Record<string, string>
|
||||
|
|
@ -106,7 +113,3 @@ const publishMessage = async (
|
|||
return undefined
|
||||
})
|
||||
}
|
||||
|
||||
export const isConfirmationEmail = (from: string): boolean => {
|
||||
return EMAIL_FORWARDING_SENDER_ADDRESSES.includes(from)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,9 @@
|
|||
import { expect } from 'chai'
|
||||
import { isConfirmationEmail, NewsletterHandler } from '../src/newsletter'
|
||||
import {
|
||||
getConfirmationCode,
|
||||
isConfirmationEmail,
|
||||
NewsletterHandler,
|
||||
} from '../src/newsletter'
|
||||
import { SubstackHandler } from '../src/substack-handler'
|
||||
import { AxiosHandler } from '../src/axios-handler'
|
||||
import { BloombergHandler } from '../src/bloomberg-handler'
|
||||
|
|
@ -14,6 +18,15 @@ describe('Confirmation email test', () => {
|
|||
expect(isConfirmationEmail(from)).to.be.true
|
||||
})
|
||||
})
|
||||
|
||||
describe('#getConfirmationCode()', () => {
|
||||
it('returns the confirmation code from the email', () => {
|
||||
const code = '593781109'
|
||||
const subject = `(#${code}) Gmail Forwarding Confirmation - Receive Mail from sam@omnivore.com`
|
||||
|
||||
expect(getConfirmationCode(subject)).to.equal(code)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('Newsletter email test', () => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue