diff --git a/functions/package.json b/functions/package.json index 64394cb8..804dccf0 100644 --- a/functions/package.json +++ b/functions/package.json @@ -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", diff --git a/functions/src/create-comment.ts b/functions/src/create-comment.ts index 717eca8d..3461e485 100644 --- a/functions/src/create-comment.ts +++ b/functions/src/create-comment.ts @@ -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 = z.lazy(() => z.intersection( @@ -31,7 +33,9 @@ const contentSchema: z.ZodType = 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, diff --git a/web/package.json b/web/package.json index 5a6b39f0..85a4e550 100644 --- a/web/package.json +++ b/web/package.json @@ -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", diff --git a/web/pages/api/v0/market/[id]/comment.ts b/web/pages/api/v0/market/[id]/comment.ts index 35d15ee2..fe450400 100644 --- a/web/pages/api/v0/market/[id]/comment.ts +++ b/web/pages/api/v0/market/[id]/comment.ts @@ -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')