|
| 1 | +/** |
| 2 | + * Server ops behind the public-comments feature: a `public` submit op (a |
| 3 | + * visitor posts a comment, which lands as `pending`) and a `settings.manage` |
| 4 | + * moderation op (approve / spam / trash / unapprove). Runtime-only — safe to |
| 5 | + * import `@tanstack/cms-core/server` here. |
| 6 | + */ |
| 7 | +import { z } from 'zod' |
| 8 | +import { defineOp } from '@tanstack/cms-core' |
| 9 | +import { commitDocument, getAs, systemContext } from '@tanstack/cms-core/server' |
| 10 | +import { |
| 11 | + COMMENT_STATUSES, |
| 12 | + DEFAULT_COMMENTS_COLLECTION, |
| 13 | + moderatedStatus, |
| 14 | +} from './comments.ts' |
| 15 | + |
| 16 | +/** Op names (stable; the public site and the moderation screen call these). */ |
| 17 | +export const SUBMIT_COMMENT_OP = 'compatWpSubmitComment' |
| 18 | +export const MODERATE_COMMENT_OP = 'compatWpModerateComment' |
| 19 | + |
| 20 | +/** A short unique comment id. */ |
| 21 | +function commentId(): string { |
| 22 | + return `wpc-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}` |
| 23 | +} |
| 24 | + |
| 25 | +const submitInput = z.object({ |
| 26 | + author: z.string().min(1).max(120), |
| 27 | + body: z.string().min(1).max(10000), |
| 28 | + email: z.string().email().optional(), |
| 29 | + targetCollection: z.string().max(120).optional(), |
| 30 | + targetId: z.string().max(200).optional(), |
| 31 | + targetPath: z.string().max(400).optional(), |
| 32 | +}) |
| 33 | + |
| 34 | +/** Public: accept a visitor comment. It is stored `pending` for moderation. */ |
| 35 | +export const submitCommentOp = defineOp({ |
| 36 | + name: SUBMIT_COMMENT_OP, |
| 37 | + method: 'POST', |
| 38 | + access: 'public', |
| 39 | + description: 'Submit a public comment. Stored as pending until moderated.', |
| 40 | + input: submitInput, |
| 41 | + run: async (input) => { |
| 42 | + const id = commentId() |
| 43 | + await commitDocument(DEFAULT_COMMENTS_COLLECTION, id, { |
| 44 | + author: input.author, |
| 45 | + body: input.body, |
| 46 | + email: input.email ?? '', |
| 47 | + targetCollection: input.targetCollection ?? '', |
| 48 | + targetId: input.targetId ?? '', |
| 49 | + targetPath: input.targetPath ?? '', |
| 50 | + status: 'pending', |
| 51 | + submittedAt: new Date().toISOString(), |
| 52 | + }) |
| 53 | + return { ok: true, id, status: 'pending' as const } |
| 54 | + }, |
| 55 | +}) |
| 56 | + |
| 57 | +const moderateInput = z.object({ |
| 58 | + id: z.string().min(1), |
| 59 | + action: z.enum(['approve', 'unapprove', 'spam', 'trash']), |
| 60 | +}) |
| 61 | + |
| 62 | +/** Moderator: move a comment to a new status (approve / spam / trash). */ |
| 63 | +export const moderateCommentOp = defineOp({ |
| 64 | + name: MODERATE_COMMENT_OP, |
| 65 | + method: 'POST', |
| 66 | + access: 'settings.manage', |
| 67 | + description: 'Moderate a comment: approve, unapprove, mark spam, or trash.', |
| 68 | + input: moderateInput, |
| 69 | + run: async (input) => { |
| 70 | + const status = moderatedStatus(input.action) |
| 71 | + if (!status || !COMMENT_STATUSES.includes(status)) { |
| 72 | + throw new Error(`Unknown moderation action: ${input.action}`) |
| 73 | + } |
| 74 | + const existing = ( |
| 75 | + await getAs(systemContext('wp comment moderation'), { |
| 76 | + collection: DEFAULT_COMMENTS_COLLECTION, |
| 77 | + id: input.id, |
| 78 | + staged: false, |
| 79 | + populate: false, |
| 80 | + }) |
| 81 | + )?.doc |
| 82 | + if (!existing) throw new Error(`Comment not found: ${input.id}`) |
| 83 | + await commitDocument(DEFAULT_COMMENTS_COLLECTION, input.id, { |
| 84 | + ...existing, |
| 85 | + status, |
| 86 | + }) |
| 87 | + return { ok: true, id: input.id, status } |
| 88 | + }, |
| 89 | +}) |
| 90 | + |
| 91 | +/** Both comment ops, for the plugin runtime. */ |
| 92 | +export const commentOps = [submitCommentOp, moderateCommentOp] |
0 commit comments