omnivore/packages/inbound-email-handler/src/index.ts
Hongbo Wu 207f77c419
Add support for Golang Weekly newsletters (#96)
* abstract different newsletter handler to a class

* create a generic newsletter handler class

* add support for golang-weekly newsletters

* add tests for golang weekly newsletters

* use addressparser lib to parse author from from address
2022-02-21 14:37:48 +08:00

140 lines
4.1 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unsafe-argument */
/* eslint-disable @typescript-eslint/no-unused-vars */
import * as Sentry from '@sentry/serverless'
import parseHeaders from 'parse-headers'
import * as multipart from 'parse-multipart-data'
import {
handleConfirmation,
isConfirmationEmail,
NewsletterHandler,
} from './newsletter'
import { PubSub } from '@google-cloud/pubsub'
import { handlePdfAttachment } from './pdf'
import { SubstackHandler } from './substack-handler'
import { AxiosHandler } from './axios-handler'
import { BloombergHandler } from './bloomberg-handler'
import { GolangHandler } from './golang-handler'
const NON_NEWSLETTER_EMAIL_TOPIC = 'nonNewsletterEmailReceived'
const pubsub = new PubSub()
const NEWSLETTER_HANDLERS = [
new SubstackHandler(),
new AxiosHandler(),
new BloombergHandler(),
new GolangHandler(),
]
export const getNewsletterHandler = (
rawUrl: string,
from: string
): NewsletterHandler | undefined => {
return NEWSLETTER_HANDLERS.find((h) => {
return h.isNewsletter(rawUrl, from)
})
}
export const inboundEmailHandler = Sentry.GCPFunction.wrapHttpFunction(
async (req, res) => {
const parts = multipart.parse(req.body, 'xYzZY')
const parsed: Record<string, string> = {}
let pdfAttachment: Buffer | undefined
let pdfAttachmentName: string | undefined
for (const part of parts) {
const { name, data, type, filename } = part
if (name && data) {
parsed[name] = data.toString()
} else if (type === 'application/pdf' && data) {
pdfAttachment = data
pdfAttachmentName = filename
} else {
console.log('no data or name for ', part)
}
}
const headers = parseHeaders(parsed.headers)
console.log('parsed: ', parsed)
console.log('headers: ', headers)
try {
const from = parsed.from.toString()
const subject = parsed.subject
const html = parsed.html
const text = parsed.text
const forwardedAddress = headers['x-forwarded-to']
const recipientAddress = forwardedAddress
? forwardedAddress.toString()
: parsed.to
const rawUrl = headers['list-post'] ? headers['list-post'].toString() : ''
// check if it is a forwarding confirmation email or newsletter
const newsletterHandler = getNewsletterHandler(rawUrl, from)
if (newsletterHandler) {
try {
console.log('handleNewsletter', from, recipientAddress)
await newsletterHandler.handleNewsletter(
recipientAddress,
html,
rawUrl,
subject,
from
)
} catch (error) {
console.log(
'error handling newsletter, will forward.',
from,
recipientAddress,
subject
)
// queue non-newsletter emails
await pubsub.topic(NON_NEWSLETTER_EMAIL_TOPIC).publishMessage({
json: {
from: from,
to: recipientAddress,
subject: subject,
html: html,
text: text,
},
})
}
} 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
await pubsub.topic(NON_NEWSLETTER_EMAIL_TOPIC).publishMessage({
json: {
from: from,
to: recipientAddress,
subject: subject,
html: html,
text: text,
},
})
}
res.send('ok')
} catch (e) {
console.log(e)
res.send(e)
}
}
)