use subject as title

This commit is contained in:
Hongbo Wu
2022-02-17 18:59:07 +08:00
parent 7229c64da0
commit ad8bca3d84
3 changed files with 28 additions and 17 deletions

View File

@ -12,6 +12,7 @@ import { initModels } from '../../server'
import { kx } from '../../datalayer/knex_config'
import { analytics } from '../../utils/analytics'
import { getNewsletterEmail } from '../../services/newsletters'
import { setClaims } from '../../datalayer/helpers'
export function pdfAttachmentsRouter() {
const router = express.Router()
@ -82,9 +83,10 @@ export function pdfAttachmentsRouter() {
router.post('/create-article', async (req, res) => {
console.log('pdf-attachments/create-article')
const { email, uploadFileId } = req.body as {
const { email, uploadFileId, subject } = req.body as {
email: string
uploadFileId: string
subject: string
}
const token = req?.headers?.authorization
@ -130,13 +132,14 @@ export function pdfAttachmentsRouter() {
pageType: pageType,
hash: uploadFileHash,
uploadFileId: uploadFileId,
title: uploadFile.fileName,
title: subject || uploadFile.fileName,
content: '',
}
const uploadFileData = await models.uploadFile.setFileUploadComplete(
uploadFileId
)
const uploadFileData = await kx.transaction(async (tx) => {
await setClaims(tx, user.id)
return models.uploadFile.setFileUploadComplete(uploadFileId, tx)
})
if (!uploadFileData || !uploadFileData.id || !uploadFileData.fileName) {
return res.status(400).send('BAD REQUEST')
}
@ -147,6 +150,7 @@ export function pdfAttachmentsRouter() {
)
const link = await kx.transaction(async (tx) => {
await setClaims(tx, user.id)
const articleRecord = await models.article.create(articleToSave, tx)
return models.userArticle.create(
{

View File

@ -23,15 +23,16 @@ export const inboundEmailHandler = Sentry.GCPFunction.wrapHttpFunction(
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) {
parsed['pdf-attachment-data'] = data.toString()
parsed['pdf-attachment-filename'] = filename
? filename
: 'attachment.pdf'
pdfAttachment = data
pdfAttachmentName = filename
} else {
console.log('no data or name for ', part)
}
@ -82,12 +83,13 @@ export const inboundEmailHandler = Sentry.GCPFunction.wrapHttpFunction(
if (isConfirmationEmail(from)) {
console.log('handleConfirmation', from, recipientAddress)
await handleConfirmation(recipientAddress, subject)
} else if (parsed['pdf-attachment-filename']) {
} else if (pdfAttachment) {
console.log('handle PDF attachment', from, recipientAddress)
await handlePdfAttachment(
recipientAddress,
parsed['pdf-attachment-filename'],
parsed['pdf-attachment-data']
pdfAttachmentName,
pdfAttachment,
subject
)
}

View File

@ -11,11 +11,14 @@ type UploadResponse = {
export const handlePdfAttachment = async (
email: string,
fileName: string,
data: string
fileName: string | undefined,
data: Buffer,
subject: string
): Promise<void> => {
console.log('handlePdfAttachment', email, fileName)
fileName = fileName || 'attachment.pdf'
try {
const uploadResult = await getUploadIdAndSignedUrl(email, fileName)
if (!uploadResult.url || !uploadResult.id) {
@ -23,7 +26,7 @@ export const handlePdfAttachment = async (
return
}
await uploadToSignedUrl(uploadResult.url, data)
await createArticle(email, uploadResult.id)
await createArticle(email, uploadResult.id, subject)
} catch (error) {
console.error('handlePdfAttachment error', error)
}
@ -60,7 +63,7 @@ const getUploadIdAndSignedUrl = async (
const uploadToSignedUrl = async (
uploadUrl: string,
data: string
data: Buffer
): Promise<AxiosResponse> => {
return axios.put(uploadUrl, data, {
headers: {
@ -73,11 +76,13 @@ const uploadToSignedUrl = async (
const createArticle = async (
email: string,
uploadFileId: string
uploadFileId: string,
subject: string
): Promise<AxiosResponse> => {
const data = {
email,
uploadFileId,
subject,
}
if (process.env.JWT_SECRET === undefined) {