{"version":3,"file":"status.mjs","names":[],"sources":["../../../../../../../src/astro/routes/api/admin/comments/[id]/status.ts"],"sourcesContent":["/**\n * Comment status change\n *\n * PUT /_emdash/api/admin/comments/:id/status - Change comment status\n */\n\nimport type { APIRoute } from \"astro\";\n\nimport { requirePerm } from \"#api/authorize.js\";\nimport { apiError, apiSuccess, handleError, requireDb, unwrapResult } from \"#api/error.js\";\nimport { handleCommentGet } from \"#api/handlers/comments.js\";\nimport { isParseError, parseBody } from \"#api/parse.js\";\nimport { commentStatusBody } from \"#api/schemas.js\";\nimport { getSiteBaseUrl } from \"#api/site-url.js\";\nimport { lookupContentAuthor, sendCommentNotification } from \"#comments/notifications.js\";\nimport { moderateComment, type CommentHookRunner } from \"#comments/service.js\";\nimport type { ModerationDecision } from \"#plugins/types.js\";\n\nexport const prerender = false;\n\nexport const PUT: APIRoute = async ({ params, request, locals }) => {\n\tconst { emdash, user } = locals;\n\tconst { id } = params;\n\n\tif (!id) {\n\t\treturn apiError(\"VALIDATION_ERROR\", \"Comment ID required\", 400);\n\t}\n\n\tconst dbErr = requireDb(emdash?.db);\n\tif (dbErr) return dbErr;\n\n\tconst denied = requirePerm(user, \"comments:moderate\");\n\tif (denied) return denied;\n\n\ttry {\n\t\tconst body = await parseBody(request, commentStatusBody);\n\t\tif (isParseError(body)) return body;\n\n\t\tconst newStatus = body.status;\n\n\t\t// Build hook runner for the service\n\t\tconst hookRunner: CommentHookRunner = {\n\t\t\tasync runBeforeCreate(event) {\n\t\t\t\treturn emdash.hooks.runCommentBeforeCreate(event);\n\t\t\t},\n\t\t\tasync runModerate(event) {\n\t\t\t\tconst result = await emdash.hooks.invokeExclusiveHook(\"comment:moderate\", event);\n\t\t\t\tif (!result) return { status: \"pending\" as const, reason: \"No moderator configured\" };\n\t\t\t\tif (result.error) return { status: \"pending\" as const, reason: \"Moderation error\" };\n\t\t\t\treturn result.result as ModerationDecision;\n\t\t\t},\n\t\t\tfireAfterCreate(event) {\n\t\t\t\temdash.hooks\n\t\t\t\t\t.runCommentAfterCreate(event)\n\t\t\t\t\t.catch((err) =>\n\t\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t\t\"[comments] afterCreate error:\",\n\t\t\t\t\t\t\terr instanceof Error ? err.message : err,\n\t\t\t\t\t\t),\n\t\t\t\t\t);\n\t\t\t},\n\t\t\tfireAfterModerate(event) {\n\t\t\t\temdash.hooks\n\t\t\t\t\t.runCommentAfterModerate(event)\n\t\t\t\t\t.catch((err) =>\n\t\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t\t\"[comments] afterModerate error:\",\n\t\t\t\t\t\t\terr instanceof Error ? err.message : err,\n\t\t\t\t\t\t),\n\t\t\t\t\t);\n\t\t\t},\n\t\t};\n\n\t\t// Read the comment before updating so we know the previous status\n\t\tconst existing = await handleCommentGet(emdash.db, id);\n\t\tif (!existing.success) {\n\t\t\treturn unwrapResult(existing);\n\t\t}\n\t\tconst previousStatus = existing.data.status;\n\n\t\tconst updated = await moderateComment(\n\t\t\temdash.db,\n\t\t\tid,\n\t\t\tnewStatus,\n\t\t\t{ id: user!.id, name: user!.name ?? null },\n\t\t\thookRunner,\n\t\t);\n\n\t\tif (!updated) {\n\t\t\treturn apiError(\"NOT_FOUND\", \"Comment not found\", 404);\n\t\t}\n\n\t\t// Send notification when a comment is newly approved\n\t\tif (newStatus === \"approved\" && previousStatus !== \"approved\" && emdash.email) {\n\t\t\ttry {\n\t\t\t\tconst adminBaseUrl = await getSiteBaseUrl(emdash.db, request, emdash.config);\n\t\t\t\tconst content = await lookupContentAuthor(emdash.db, updated.collection, updated.contentId);\n\t\t\t\tif (content?.author) {\n\t\t\t\t\tawait sendCommentNotification({\n\t\t\t\t\t\temail: emdash.email,\n\t\t\t\t\t\tcomment: updated,\n\t\t\t\t\t\tcontentAuthor: content.author,\n\t\t\t\t\t\tadminBaseUrl,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t} catch (err) {\n\t\t\t\tconsole.error(\"[comments] notification error:\", err instanceof Error ? err.message : err);\n\t\t\t}\n\t\t}\n\n\t\treturn apiSuccess(updated);\n\t} catch (error) {\n\t\treturn handleError(error, \"Failed to update comment status\", \"COMMENT_STATUS_ERROR\");\n\t}\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAkBA,MAAa,YAAY;AAEzB,MAAa,MAAgB,OAAO,EAAE,QAAQ,SAAS,aAAa;CACnE,MAAM,EAAE,QAAQ,SAAS;CACzB,MAAM,EAAE,OAAO;AAEf,KAAI,CAAC,GACJ,QAAO,SAAS,oBAAoB,uBAAuB,IAAI;CAGhE,MAAM,QAAQ,UAAU,QAAQ,GAAG;AACnC,KAAI,MAAO,QAAO;CAElB,MAAM,SAAS,YAAY,MAAM,oBAAoB;AACrD,KAAI,OAAQ,QAAO;AAEnB,KAAI;EACH,MAAM,OAAO,MAAM,UAAU,SAAS,kBAAkB;AACxD,MAAI,aAAa,KAAK,CAAE,QAAO;EAE/B,MAAM,YAAY,KAAK;EAGvB,MAAM,aAAgC;GACrC,MAAM,gBAAgB,OAAO;AAC5B,WAAO,OAAO,MAAM,uBAAuB,MAAM;;GAElD,MAAM,YAAY,OAAO;IACxB,MAAM,SAAS,MAAM,OAAO,MAAM,oBAAoB,oBAAoB,MAAM;AAChF,QAAI,CAAC,OAAQ,QAAO;KAAE,QAAQ;KAAoB,QAAQ;KAA2B;AACrF,QAAI,OAAO,MAAO,QAAO;KAAE,QAAQ;KAAoB,QAAQ;KAAoB;AACnF,WAAO,OAAO;;GAEf,gBAAgB,OAAO;AACtB,WAAO,MACL,sBAAsB,MAAM,CAC5B,OAAO,QACP,QAAQ,MACP,iCACA,eAAe,QAAQ,IAAI,UAAU,IACrC,CACD;;GAEH,kBAAkB,OAAO;AACxB,WAAO,MACL,wBAAwB,MAAM,CAC9B,OAAO,QACP,QAAQ,MACP,mCACA,eAAe,QAAQ,IAAI,UAAU,IACrC,CACD;;GAEH;EAGD,MAAM,WAAW,MAAM,iBAAiB,OAAO,IAAI,GAAG;AACtD,MA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