Merge pull request #2106 from omnivore-app/fix/save-url-via-email

fix/save url via email
This commit is contained in:
Hongbo Wu 2023-04-25 10:41:31 +08:00 committed by GitHub
commit d0e4cbaf08
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 16 deletions

View file

@ -20,6 +20,16 @@ interface SetConfirmationCodeMessage {
confirmationCode: string
}
const isNewsletterMessage = (data: any): data is NewsletterMessage => {
return (
'email' in data &&
'title' in data &&
'author' in data &&
'url' in data &&
'receivedEmailId' in data
)
}
export function newsletterServiceRouter() {
const router = express.Router()
@ -90,17 +100,10 @@ export function newsletterServiceRouter() {
}
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const data = JSON.parse(message) as NewsletterMessage
if (
!('email' in data) ||
!('content' in data) ||
!('title' in data) ||
!('author' in data)
) {
const data = JSON.parse(message) as unknown
if (!isNewsletterMessage(data)) {
console.log('invalid newsletter message', data)
res.status(400).send('Bad Request')
return
return res.status(400).send('Bad Request')
}
// get user from newsletter email

View file

@ -14,12 +14,12 @@ import { SaveContext, saveEmail, SaveEmailInput } from './save_email'
import { saveSubscription } from './subscriptions'
export interface NewsletterMessage {
from: string
email: string
content: string
url: string
title: string
author: string
content?: string
from?: string
unsubMailTo?: string
unsubHttpUrl?: string
receivedEmailId: string
@ -43,6 +43,11 @@ export const saveNewsletterEmail = async (
},
})
if (!data.content) {
console.log('newsletter not created, no content:', data.email)
return false
}
const saveCtx = ctx || {
pubsub: createPubSubClient(),
uid: newsletterEmail.user.id,

View file

@ -18,6 +18,7 @@ import {
} from '../generated/graphql'
import { CreateArticlesSuccessPartial } from '../resolvers'
import { Claims, WithDataSourcesContext } from '../resolvers/types'
import { validateUrl } from '../services/create_page_save_request'
import { Merge } from '../util'
interface InputObject {
@ -292,9 +293,10 @@ export const unescapeHtml = (html: string): string => {
export const isUrl = (str: string): boolean => {
try {
new URL(str)
validateUrl(str)
return true
} catch {
console.log('not an url', str)
return false
}
}

View file

@ -175,14 +175,12 @@ export const inboundEmailHandler = Sentry.GCPFunction.wrapHttpFunction(
await pubsub.topic(NEWSLETTER_EMAIL_RECEIVED_TOPIC).publishMessage({
json: {
email: to,
content: html,
content: html || text, // html is preferred
url: generateUniqueUrl(),
title: subject,
author: parseAuthor(from),
text,
unsubMailTo: unsubscribe?.mailTo,
unsubHttpUrl: unsubscribe?.httpUrl,
forwardedFrom,
receivedEmailId,
...newsletterMessage,
},