Move parseUnsubscribeHeader to index.ts

This commit is contained in:
Hongbo Wu
2022-07-07 17:12:26 +08:00
parent 620ace93d1
commit 0b15fefeb9
2 changed files with 18 additions and 17 deletions

View File

@ -19,8 +19,15 @@ import { BloombergHandler } from './bloomberg-handler'
import { GolangHandler } from './golang-handler'
import { MorningBrewHandler } from './morning-brew-handler'
interface Unsubscribe {
mailTo?: string
httpUrl?: string
}
const NON_NEWSLETTER_EMAIL_TOPIC = 'nonNewsletterEmailReceived'
const pubsub = new PubSub()
const UNSUBSCRIBE_HTTP_URL_PATTERN = /<(https?:\/\/[^>]*)>/
const UNSUBSCRIBE_MAIL_TO_PATTERN = /<mailto:([^>]*)>/
const NEWSLETTER_HANDLERS = [
new SubstackHandler(),
@ -30,6 +37,15 @@ const NEWSLETTER_HANDLERS = [
new MorningBrewHandler(),
]
export const parseUnsubscribe = (unSubHeader: string): Unsubscribe => {
// parse list-unsubscribe header
// e.g. List-Unsubscribe: <https://omnivore.com/unsub>, <mailto:unsub@omnivore.com>
return {
mailTo: unSubHeader.match(UNSUBSCRIBE_MAIL_TO_PATTERN)?.[1],
httpUrl: unSubHeader.match(UNSUBSCRIBE_HTTP_URL_PATTERN)?.[1],
}
}
export const getNewsletterHandler = (
postHeader: string,
from: string,

View File

@ -1,6 +1,7 @@
import { PubSub } from '@google-cloud/pubsub'
import { v4 as uuidv4 } from 'uuid'
import addressparser from 'addressparser'
import { parseUnsubscribe } from './index'
const pubsub = new PubSub()
const NEWSLETTER_EMAIL_RECEIVED_TOPIC = 'newsletterEmailReceived'
@ -9,13 +10,6 @@ const EMAIL_FORWARDING_SENDER_ADDRESSES = [
'Gmail Team <forwarding-noreply@google.com>',
]
const CONFIRMATION_CODE_PATTERN = /^\(#\d+\)/
const UNSUBSCRIBE_HTTP_URL_PATTERN = /<(https?:\/\/[^>]*)>/
const UNSUBSCRIBE_MAIL_TO_PATTERN = /<mailto:([^>]*)>/
interface Unsubscribe {
mailTo?: string
httpUrl?: string
}
export class NewsletterHandler {
protected senderRegex = /NEWSLETTER_SENDER_REGEX/
@ -48,15 +42,6 @@ export class NewsletterHandler {
return from
}
parseUnsubscribe(unSubHeader: string): Unsubscribe {
// parse list-unsubscribe header
// e.g. List-Unsubscribe: <https://omnivore.com/unsub>, <mailto:unsub@omnivore.com>
return {
mailTo: unSubHeader.match(UNSUBSCRIBE_MAIL_TO_PATTERN)?.[1],
httpUrl: unSubHeader.match(UNSUBSCRIBE_HTTP_URL_PATTERN)?.[1],
}
}
async handleNewsletter(
email: string,
html: string,
@ -78,7 +63,7 @@ export class NewsletterHandler {
this.parseNewsletterUrl(postHeader, html) ||
`${this.defaultUrl}?source=newsletters&id=${uuidv4()}`
const author = this.parseAuthor(from)
const unsubscribe = this.parseUnsubscribe(unSubHeader)
const unsubscribe = parseUnsubscribe(unSubHeader)
const message = {
email,
content: html,