Save received email
This commit is contained in:
@ -33,6 +33,9 @@ export class ReceivedEmail {
|
||||
@Column('text')
|
||||
html!: string
|
||||
|
||||
@Column('text')
|
||||
type!: 'article' | 'non-article'
|
||||
|
||||
@CreateDateColumn({ default: () => 'CURRENT_TIMESTAMP' })
|
||||
createdAt!: Date
|
||||
|
||||
|
||||
@ -15,6 +15,7 @@ import {
|
||||
} from '../../utils/parser'
|
||||
import { saveEmail } from '../../services/save_email'
|
||||
import { buildLogger } from '../../utils/logger'
|
||||
import { saveReceivedEmail } from '../../services/received_emails'
|
||||
|
||||
interface ForwardEmailMessage {
|
||||
from: string
|
||||
@ -91,6 +92,17 @@ export function emailsServiceRouter() {
|
||||
url: generateUniqueUrl(),
|
||||
originalContent: data.html || data.text,
|
||||
})
|
||||
|
||||
await saveReceivedEmail(
|
||||
data.from,
|
||||
data.to,
|
||||
data.subject,
|
||||
data.text,
|
||||
data.html,
|
||||
user.id,
|
||||
'article'
|
||||
)
|
||||
|
||||
res.status(200).send('Article')
|
||||
return
|
||||
}
|
||||
@ -103,6 +115,15 @@ export function emailsServiceRouter() {
|
||||
},
|
||||
})
|
||||
|
||||
await saveReceivedEmail(
|
||||
data.from,
|
||||
data.to,
|
||||
data.subject,
|
||||
data.text,
|
||||
data.html,
|
||||
user.id
|
||||
)
|
||||
|
||||
// forward non-newsletter emails to the registered email address
|
||||
const result = await sendEmail({
|
||||
from: env.sender.message,
|
||||
|
||||
@ -7,7 +7,8 @@ export const saveReceivedEmail = async (
|
||||
subject: string,
|
||||
text: string,
|
||||
html: string,
|
||||
userId: string
|
||||
userId: string,
|
||||
type: 'article' | 'non-article' = 'non-article'
|
||||
): Promise<ReceivedEmail> => {
|
||||
return getRepository(ReceivedEmail).save({
|
||||
from,
|
||||
@ -15,6 +16,7 @@ export const saveReceivedEmail = async (
|
||||
subject,
|
||||
text,
|
||||
html,
|
||||
type,
|
||||
user: { id: userId },
|
||||
})
|
||||
}
|
||||
|
||||
@ -13,8 +13,10 @@ import { NewsletterEmail } from '../entity/newsletter_email'
|
||||
import { fetchFavicon } from '../utils/parser'
|
||||
import { updatePage } from '../elastic/pages'
|
||||
import { isBase64Image } from '../utils/helpers'
|
||||
import { saveReceivedEmail } from './received_emails'
|
||||
|
||||
export interface NewsletterMessage {
|
||||
from: string
|
||||
email: string
|
||||
content: string
|
||||
url: string
|
||||
@ -23,6 +25,7 @@ export interface NewsletterMessage {
|
||||
unsubMailTo?: string
|
||||
unsubHttpUrl?: string
|
||||
newsletterEmail?: NewsletterEmail
|
||||
text: string
|
||||
}
|
||||
|
||||
// Returns true if the link was created successfully. Can still fail to
|
||||
@ -65,9 +68,29 @@ export const saveNewsletterEmail = async (
|
||||
const page = await saveEmail(saveCtx, input)
|
||||
if (!page) {
|
||||
console.log('newsletter not created:', input)
|
||||
|
||||
await saveReceivedEmail(
|
||||
data.from,
|
||||
data.email,
|
||||
data.title,
|
||||
data.text,
|
||||
data.content,
|
||||
newsletterEmail.user.id
|
||||
)
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
await saveReceivedEmail(
|
||||
data.from,
|
||||
data.email,
|
||||
data.title,
|
||||
data.text,
|
||||
data.content,
|
||||
newsletterEmail.user.id,
|
||||
'article'
|
||||
)
|
||||
|
||||
if (!page.siteIcon || isBase64Image(page.siteIcon)) {
|
||||
// fetch favicon if not already set or is a base64 image
|
||||
const favicon = await fetchFavicon(page.url)
|
||||
|
||||
@ -12,6 +12,7 @@ CREATE TABLE omnivore.received_emails (
|
||||
subject text NOT NULL DEFAULT '',
|
||||
"text" text NOT NULL,
|
||||
html text NOT NULL DEFAULT '',
|
||||
"type" text NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT current_timestamp,
|
||||
updated_at timestamptz NOT NULL DEFAULT current_timestamp
|
||||
);
|
||||
|
||||
@ -87,10 +87,11 @@ export const inboundEmailHandler = Sentry.GCPFunction.wrapHttpFunction(
|
||||
title: subject,
|
||||
})
|
||||
if (newsletterMessage) {
|
||||
await publishMessage(
|
||||
NEWSLETTER_EMAIL_RECEIVED_TOPIC,
|
||||
newsletterMessage
|
||||
)
|
||||
await publishMessage(NEWSLETTER_EMAIL_RECEIVED_TOPIC, {
|
||||
...newsletterMessage,
|
||||
text,
|
||||
from,
|
||||
})
|
||||
return res.status(200).send('newsletter received')
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user