Rework /comment endpoint
This commit is contained in:
parent
3391e3fb2e
commit
cfe7ee49ec
|
@ -40,6 +40,7 @@
|
||||||
"firebase-functions": "3.21.2",
|
"firebase-functions": "3.21.2",
|
||||||
"lodash": "4.17.21",
|
"lodash": "4.17.21",
|
||||||
"mailgun-js": "0.22.0",
|
"mailgun-js": "0.22.0",
|
||||||
|
"micromark": "3.0.10",
|
||||||
"module-alias": "2.2.2",
|
"module-alias": "2.2.2",
|
||||||
"node-fetch": "2",
|
"node-fetch": "2",
|
||||||
"stripe": "8.194.0",
|
"stripe": "8.194.0",
|
||||||
|
|
|
@ -5,6 +5,8 @@ import { APIError, newEndpoint, validate } from './api'
|
||||||
import { JSONContent } from '@tiptap/core'
|
import { JSONContent } from '@tiptap/core'
|
||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
import { removeUndefinedProps } from '../../common/util/object'
|
import { removeUndefinedProps } from '../../common/util/object'
|
||||||
|
import { htmlToRichText } from 'common/util/parse'
|
||||||
|
import { micromark } from 'micromark'
|
||||||
|
|
||||||
const contentSchema: z.ZodType<JSONContent> = z.lazy(() =>
|
const contentSchema: z.ZodType<JSONContent> = z.lazy(() =>
|
||||||
z.intersection(
|
z.intersection(
|
||||||
|
@ -31,7 +33,9 @@ const contentSchema: z.ZodType<JSONContent> = z.lazy(() =>
|
||||||
|
|
||||||
const postSchema = z.object({
|
const postSchema = z.object({
|
||||||
contractId: z.string(),
|
contractId: z.string(),
|
||||||
content: contentSchema,
|
content: contentSchema.optional(),
|
||||||
|
html: z.string().optional(),
|
||||||
|
markdown: z.string().optional(),
|
||||||
})
|
})
|
||||||
|
|
||||||
const MAX_COMMENT_JSON_LENGTH = 20000
|
const MAX_COMMENT_JSON_LENGTH = 20000
|
||||||
|
@ -40,7 +44,7 @@ const MAX_COMMENT_JSON_LENGTH = 20000
|
||||||
// Replies, posts, chats are not supported yet.
|
// Replies, posts, chats are not supported yet.
|
||||||
export const createcomment = newEndpoint({}, async (req, auth) => {
|
export const createcomment = newEndpoint({}, async (req, auth) => {
|
||||||
const firestore = admin.firestore()
|
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 creator = await getUser(auth.uid)
|
||||||
const contract = await getContract(contractId)
|
const contract = await getContract(contractId)
|
||||||
|
@ -51,6 +55,20 @@ export const createcomment = newEndpoint({}, async (req, auth) => {
|
||||||
if (!contract) {
|
if (!contract) {
|
||||||
throw new APIError(400, 'No contract exists with the given ID.')
|
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) {
|
if (JSON.stringify(content).length > MAX_COMMENT_JSON_LENGTH) {
|
||||||
throw new APIError(
|
throw new APIError(
|
||||||
400,
|
400,
|
||||||
|
|
|
@ -51,7 +51,6 @@
|
||||||
"gridjs": "5.0.2",
|
"gridjs": "5.0.2",
|
||||||
"gridjs-react": "5.0.2",
|
"gridjs-react": "5.0.2",
|
||||||
"lodash": "4.17.21",
|
"lodash": "4.17.21",
|
||||||
"micromark": "3.0.10",
|
|
||||||
"nanoid": "^3.3.4",
|
"nanoid": "^3.3.4",
|
||||||
"next": "12.3.1",
|
"next": "12.3.1",
|
||||||
"node-fetch": "3.2.4",
|
"node-fetch": "3.2.4",
|
||||||
|
|
|
@ -5,8 +5,6 @@ import {
|
||||||
} from 'common/envs/constants'
|
} from 'common/envs/constants'
|
||||||
import { applyCorsHeaders } from 'web/lib/api/cors'
|
import { applyCorsHeaders } from 'web/lib/api/cors'
|
||||||
import { fetchBackend, forwardResponse } from 'web/lib/api/proxy'
|
import { fetchBackend, forwardResponse } from 'web/lib/api/proxy'
|
||||||
import { htmlToRichText } from 'common/util/parse'
|
|
||||||
import { micromark } from 'micromark'
|
|
||||||
|
|
||||||
export const config = { api: { bodyParser: true } }
|
export const config = { api: { bodyParser: true } }
|
||||||
|
|
||||||
|
@ -19,15 +17,7 @@ export default async function route(req: NextApiRequest, res: NextApiResponse) {
|
||||||
const { id } = req.query
|
const { id } = req.query
|
||||||
const contractId = id as string
|
const contractId = id as string
|
||||||
|
|
||||||
if (req.body) {
|
if (req.body) req.body.contractId = contractId
|
||||||
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))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const backendRes = await fetchBackend(req, 'createcomment')
|
const backendRes = await fetchBackend(req, 'createcomment')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user