Rework /comment endpoint

This commit is contained in:
Austin Chen 2022-10-06 11:33:30 -04:00
parent 3391e3fb2e
commit cfe7ee49ec
4 changed files with 22 additions and 14 deletions

View File

@ -40,6 +40,7 @@
"firebase-functions": "3.21.2",
"lodash": "4.17.21",
"mailgun-js": "0.22.0",
"micromark": "3.0.10",
"module-alias": "2.2.2",
"node-fetch": "2",
"stripe": "8.194.0",

View File

@ -5,6 +5,8 @@ import { APIError, newEndpoint, validate } from './api'
import { JSONContent } from '@tiptap/core'
import { z } from 'zod'
import { removeUndefinedProps } from '../../common/util/object'
import { htmlToRichText } from 'common/util/parse'
import { micromark } from 'micromark'
const contentSchema: z.ZodType<JSONContent> = z.lazy(() =>
z.intersection(
@ -31,7 +33,9 @@ const contentSchema: z.ZodType<JSONContent> = z.lazy(() =>
const postSchema = z.object({
contractId: z.string(),
content: contentSchema,
content: contentSchema.optional(),
html: z.string().optional(),
markdown: z.string().optional(),
})
const MAX_COMMENT_JSON_LENGTH = 20000
@ -40,7 +44,7 @@ const MAX_COMMENT_JSON_LENGTH = 20000
// Replies, posts, chats are not supported yet.
export const createcomment = newEndpoint({}, async (req, auth) => {
const firestore = admin.firestore()
const { contractId, content } = validate(postSchema, req.body)
const { contractId, content, html, markdown } = validate(postSchema, req.body)
const creator = await getUser(auth.uid)
const contract = await getContract(contractId)
@ -51,6 +55,20 @@ export const createcomment = newEndpoint({}, async (req, auth) => {
if (!contract) {
throw new APIError(400, 'No contract exists with the given ID.')
}
let contentJson = null
if (content) {
contentJson = content
} else if (html && !content) {
contentJson = htmlToRichText(html)
} else if (markdown && !content) {
contentJson = htmlToRichText(micromark(markdown))
}
if (!contentJson) {
throw new APIError(400, 'No comment content provided.')
}
if (JSON.stringify(content).length > MAX_COMMENT_JSON_LENGTH) {
throw new APIError(
400,

View File

@ -51,7 +51,6 @@
"gridjs": "5.0.2",
"gridjs-react": "5.0.2",
"lodash": "4.17.21",
"micromark": "3.0.10",
"nanoid": "^3.3.4",
"next": "12.3.1",
"node-fetch": "3.2.4",

View File

@ -5,8 +5,6 @@ import {
} from 'common/envs/constants'
import { applyCorsHeaders } from 'web/lib/api/cors'
import { fetchBackend, forwardResponse } from 'web/lib/api/proxy'
import { htmlToRichText } from 'common/util/parse'
import { micromark } from 'micromark'
export const config = { api: { bodyParser: true } }
@ -19,15 +17,7 @@ export default async function route(req: NextApiRequest, res: NextApiResponse) {
const { id } = req.query
const contractId = id as string
if (req.body) {
req.body.contractId = contractId
if (req.body.html && !req.body.content) {
req.body.content = htmlToRichText(req.body.html)
} else if (req.body.markdown && !req.body.content) {
req.body.content = htmlToRichText(micromark(req.body.markdown))
}
}
if (req.body) req.body.contractId = contractId
try {
const backendRes = await fetchBackend(req, 'createcomment')