@molecule/api-resource-comment 1.4.1 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -2
- package/dist/scaffold-router.d.ts +3 -0
- package/dist/scaffold-router.d.ts.map +1 -0
- package/dist/scaffold-router.js +255 -0
- package/dist/service.d.ts.map +1 -1
- package/dist/service.js +4 -2
- package/dist/types.d.ts +2 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +7 -1
- package/src/scaffold-router.ts +343 -0
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@ AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
|
3
3
|
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
4
|
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
5
|
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
-
Generated: 2026-11-
|
|
6
|
+
Generated: 2026-11-30T19:05:14.944Z
|
|
7
7
|
-->
|
|
8
8
|
|
|
9
9
|
# @molecule/api-resource-comment
|
|
@@ -45,7 +45,8 @@ import { routes, requestHandlerMap } from '@molecule/api-resource-comment'
|
|
|
45
45
|
## Installation
|
|
46
46
|
|
|
47
47
|
```bash
|
|
48
|
-
npm install @molecule/api-resource-comment @molecule/api-database @molecule/api-i18n @molecule/api-logger zod
|
|
48
|
+
npm install @molecule/api-resource-comment @molecule/api-database @molecule/api-i18n @molecule/api-logger @molecule/api-rate-limit express zod
|
|
49
|
+
npm install -D @types/express
|
|
49
50
|
```
|
|
50
51
|
|
|
51
52
|
## API
|
|
@@ -153,6 +154,8 @@ interface PaginationOptions {
|
|
|
153
154
|
limit?: number
|
|
154
155
|
/** Number of results to skip. */
|
|
155
156
|
offset?: number
|
|
157
|
+
/** Sort direction by creation time (`'asc'` or `'desc'`). */
|
|
158
|
+
order?: 'asc' | 'desc'
|
|
156
159
|
}
|
|
157
160
|
```
|
|
158
161
|
|
|
@@ -678,7 +681,9 @@ Peer dependencies:
|
|
|
678
681
|
- `@molecule/api-database` ^1.1.0
|
|
679
682
|
- `@molecule/api-i18n` ^1.0.1
|
|
680
683
|
- `@molecule/api-logger` ^1.0.1
|
|
684
|
+
- `@molecule/api-rate-limit` ^1.0.1
|
|
681
685
|
- `@molecule/api-resource` ^1.0.1 _(optional)_
|
|
686
|
+
- `express` ^5.0.0
|
|
682
687
|
- `zod` ^4.0.0
|
|
683
688
|
|
|
684
689
|
### Runtime Dependencies
|
|
@@ -686,7 +691,9 @@ Peer dependencies:
|
|
|
686
691
|
- `@molecule/api-database`
|
|
687
692
|
- `@molecule/api-i18n`
|
|
688
693
|
- `@molecule/api-logger`
|
|
694
|
+
- `@molecule/api-rate-limit`
|
|
689
695
|
- `@molecule/api-resource` _(optional)_
|
|
696
|
+
- `express`
|
|
690
697
|
- `zod`
|
|
691
698
|
|
|
692
699
|
- **List endpoints return a PAGINATED envelope** `{ data, total, limit, offset }`, not a
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scaffold-router.d.ts","sourceRoot":"","sources":["../src/scaffold-router.ts"],"names":[],"mappings":"AA2BA,QAAA,MAAM,MAAM,4CAAW,CAAA;AA2TvB,eAAe,MAAM,CAAA"}
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import { Router } from 'express';
|
|
2
|
+
import { t } from '@molecule/api-i18n';
|
|
3
|
+
import { logger } from '@molecule/api-logger';
|
|
4
|
+
import { consume as consumeRateLimit, hasProvider as hasRateLimitProvider, } from '@molecule/api-rate-limit';
|
|
5
|
+
import { canAccessCommentResource, CommentResourceAccessError, createComment, createCommentSchema, deleteComment, getCommentById, getCommentCount, getCommentsByResource, getReplies, updateComment, updateCommentSchema, } from '@molecule/api-resource-comment';
|
|
6
|
+
// Edit paths, allowlisted resource types, and JSON here. Comment services own
|
|
7
|
+
// persistence, sequential numbering, author hydration, and ownership checks.
|
|
8
|
+
// Fail-closed access: once any sibling resource registers a policy via
|
|
9
|
+
// registerResourceAccessPolicy() (e.g. articles.ts), unregistered resourceTypes
|
|
10
|
+
// and private/draft parents are refused with 404.
|
|
11
|
+
const router = Router();
|
|
12
|
+
/** Maximum page size accepted by comment listing endpoints. */
|
|
13
|
+
const MAX_LIST_LIMIT = 100;
|
|
14
|
+
/** Valid slug pattern for polymorphic `resourceType` path segments. */
|
|
15
|
+
const RESOURCE_TYPE_PATTERN = /^[a-z][a-z0-9_-]{0,63}$/i;
|
|
16
|
+
/** Read the authenticated session user ID without type casts. */
|
|
17
|
+
function viewerId(res) {
|
|
18
|
+
const session = res.locals.session;
|
|
19
|
+
if (typeof session !== 'object' || session === null || !('userId' in session))
|
|
20
|
+
return undefined;
|
|
21
|
+
return typeof session.userId === 'string' && session.userId ? session.userId : undefined;
|
|
22
|
+
}
|
|
23
|
+
/** Read a single string route param. */
|
|
24
|
+
function paramString(value) {
|
|
25
|
+
return typeof value === 'string' && value.trim() ? value : undefined;
|
|
26
|
+
}
|
|
27
|
+
/** Read an optional bounded page size (`1..MAX_LIST_LIMIT`). */
|
|
28
|
+
function queryLimit(value) {
|
|
29
|
+
const parsed = parseInt(String(value ?? ''), 10);
|
|
30
|
+
return Number.isFinite(parsed) ? Math.min(MAX_LIST_LIMIT, Math.max(1, parsed)) : 20;
|
|
31
|
+
}
|
|
32
|
+
/** Read an optional non-negative pagination offset. */
|
|
33
|
+
function queryOffset(value) {
|
|
34
|
+
const parsed = parseInt(String(value ?? ''), 10);
|
|
35
|
+
return Number.isFinite(parsed) ? Math.max(0, parsed) : 0;
|
|
36
|
+
}
|
|
37
|
+
/** Read an optional sort direction (`'asc'` or `'desc'`). */
|
|
38
|
+
function queryOrder(value) {
|
|
39
|
+
if (value === 'asc' || value === 'desc')
|
|
40
|
+
return value;
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
/** Send an i18n-formatted error response. */
|
|
44
|
+
function errorResponse(res, status, key, fallback) {
|
|
45
|
+
res.status(status).json({ error: t(key, undefined, { defaultValue: fallback }), errorKey: key });
|
|
46
|
+
}
|
|
47
|
+
/** Answer a request missing a valid signed-in session. */
|
|
48
|
+
function unauthorized(res) {
|
|
49
|
+
errorResponse(res, 401, 'resource.error.unauthorized', 'Unauthorized.');
|
|
50
|
+
}
|
|
51
|
+
/** Answer a request for a missing or inaccessible comment or parent resource. */
|
|
52
|
+
function notFound(res) {
|
|
53
|
+
errorResponse(res, 404, 'resource.error.notFound', 'Not found.');
|
|
54
|
+
}
|
|
55
|
+
/** Answer a request with invalid input fields. */
|
|
56
|
+
function invalid(res, issues) {
|
|
57
|
+
res.status(400).json({
|
|
58
|
+
error: issues
|
|
59
|
+
.map((issue) => `${issue.path.map(String).join('.')}: ${issue.message}`)
|
|
60
|
+
.join(', '),
|
|
61
|
+
errorKey: 'comment.error.validationFailed',
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
/** Validate `resourceType` and `resourceId` path params. */
|
|
65
|
+
function resourceParams(req, res) {
|
|
66
|
+
const resourceType = paramString(req.params.resourceType);
|
|
67
|
+
const resourceId = paramString(req.params.resourceId);
|
|
68
|
+
if (!resourceType || !resourceId || !RESOURCE_TYPE_PATTERN.test(resourceType)) {
|
|
69
|
+
errorResponse(res, 400, 'comment.error.missingResource', 'Resource type and ID are required');
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
return { resourceType, resourceId };
|
|
73
|
+
}
|
|
74
|
+
/** Throttle comment creation when a rate-limit provider is bonded. */
|
|
75
|
+
async function rateLimitComments(req, res, next) {
|
|
76
|
+
if (!hasRateLimitProvider()) {
|
|
77
|
+
next();
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
const key = `comments:create:${viewerId(res) ?? req.ip ?? 'unknown'}`;
|
|
81
|
+
const result = await consumeRateLimit(key);
|
|
82
|
+
if (!result.allowed) {
|
|
83
|
+
const retryAfter = result.retryAfter ?? Math.max(1, Math.ceil((result.resetAt.getTime() - Date.now()) / 1000));
|
|
84
|
+
res.set('Retry-After', String(retryAfter));
|
|
85
|
+
errorResponse(res, 429, 'rateLimit.error.tooManyRequests', 'Too many requests. Please try again later.');
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
next();
|
|
89
|
+
}
|
|
90
|
+
router.post('/:resourceType/:resourceId/comments', rateLimitComments, async (req, res) => {
|
|
91
|
+
const userId = viewerId(res);
|
|
92
|
+
if (!userId) {
|
|
93
|
+
unauthorized(res);
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
const params = resourceParams(req, res);
|
|
97
|
+
if (!params)
|
|
98
|
+
return;
|
|
99
|
+
const parsed = createCommentSchema.safeParse(req.body);
|
|
100
|
+
if (!parsed.success) {
|
|
101
|
+
invalid(res, parsed.error.issues);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
try {
|
|
105
|
+
const comment = await createComment(params.resourceType, params.resourceId, userId, parsed.data);
|
|
106
|
+
res.status(201).json(comment);
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
if (error instanceof CommentResourceAccessError) {
|
|
110
|
+
notFound(res);
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
logger.error('Failed to create comment', { ...params, userId, error });
|
|
114
|
+
errorResponse(res, 500, 'comment.error.createFailed', 'Failed to create comment');
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
router.get('/:resourceType/:resourceId/comments', async (req, res) => {
|
|
118
|
+
const params = resourceParams(req, res);
|
|
119
|
+
if (!params)
|
|
120
|
+
return;
|
|
121
|
+
const limit = queryLimit(req.query.limit);
|
|
122
|
+
const offset = queryOffset(req.query.offset);
|
|
123
|
+
const order = queryOrder(req.query.order);
|
|
124
|
+
try {
|
|
125
|
+
if (!(await canAccessCommentResource(params.resourceType, params.resourceId, 'read', viewerId(res)))) {
|
|
126
|
+
notFound(res);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
res.json(await getCommentsByResource(params.resourceType, params.resourceId, { limit, offset, order }));
|
|
130
|
+
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
logger.error('Failed to list comments', { ...params, error });
|
|
133
|
+
errorResponse(res, 500, 'comment.error.listFailed', 'Failed to list comments');
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
router.get('/:resourceType/:resourceId/comments/count', async (req, res) => {
|
|
137
|
+
const params = resourceParams(req, res);
|
|
138
|
+
if (!params)
|
|
139
|
+
return;
|
|
140
|
+
try {
|
|
141
|
+
if (!(await canAccessCommentResource(params.resourceType, params.resourceId, 'read', viewerId(res)))) {
|
|
142
|
+
notFound(res);
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
const count = await getCommentCount(params.resourceType, params.resourceId);
|
|
146
|
+
res.json({ count });
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
logger.error('Failed to count comments', { ...params, error });
|
|
150
|
+
errorResponse(res, 500, 'comment.error.countFailed', 'Failed to count comments');
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
router.get('/comments/:commentId', async (req, res) => {
|
|
154
|
+
const commentId = paramString(req.params.commentId);
|
|
155
|
+
if (!commentId) {
|
|
156
|
+
errorResponse(res, 400, 'comment.error.missingId', 'Comment ID is required');
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
try {
|
|
160
|
+
const comment = await getCommentById(commentId);
|
|
161
|
+
if (!comment) {
|
|
162
|
+
notFound(res);
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (!(await canAccessCommentResource(comment.resourceType, comment.resourceId, 'read', viewerId(res)))) {
|
|
166
|
+
notFound(res);
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
res.json(comment);
|
|
170
|
+
}
|
|
171
|
+
catch (error) {
|
|
172
|
+
logger.error('Failed to read comment', { commentId, error });
|
|
173
|
+
errorResponse(res, 500, 'comment.error.readFailed', 'Failed to read comment');
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
router.put('/comments/:commentId', async (req, res) => {
|
|
177
|
+
const userId = viewerId(res);
|
|
178
|
+
if (!userId) {
|
|
179
|
+
unauthorized(res);
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
const commentId = paramString(req.params.commentId);
|
|
183
|
+
if (!commentId) {
|
|
184
|
+
errorResponse(res, 400, 'comment.error.missingId', 'Comment ID is required');
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
const parsed = updateCommentSchema.safeParse(req.body);
|
|
188
|
+
if (!parsed.success) {
|
|
189
|
+
invalid(res, parsed.error.issues);
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
try {
|
|
193
|
+
const updated = await updateComment(commentId, userId, parsed.data);
|
|
194
|
+
if (!updated) {
|
|
195
|
+
errorResponse(res, 404, 'comment.error.notFoundOrForbidden', 'Comment not found or not owned by user');
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
res.json(updated);
|
|
199
|
+
}
|
|
200
|
+
catch (error) {
|
|
201
|
+
logger.error('Failed to update comment', { commentId, userId, error });
|
|
202
|
+
errorResponse(res, 500, 'comment.error.updateFailed', 'Failed to update comment');
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
router.delete('/comments/:commentId', async (req, res) => {
|
|
206
|
+
const userId = viewerId(res);
|
|
207
|
+
if (!userId) {
|
|
208
|
+
unauthorized(res);
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
const commentId = paramString(req.params.commentId);
|
|
212
|
+
if (!commentId) {
|
|
213
|
+
errorResponse(res, 400, 'comment.error.missingId', 'Comment ID is required');
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
try {
|
|
217
|
+
const deleted = await deleteComment(commentId, userId);
|
|
218
|
+
if (!deleted) {
|
|
219
|
+
errorResponse(res, 404, 'comment.error.notFoundOrForbidden', 'Comment not found or not owned by user');
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
res.status(204).end();
|
|
223
|
+
}
|
|
224
|
+
catch (error) {
|
|
225
|
+
logger.error('Failed to delete comment', { commentId, userId, error });
|
|
226
|
+
errorResponse(res, 500, 'comment.error.deleteFailed', 'Failed to delete comment');
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
router.get('/comments/:commentId/replies', async (req, res) => {
|
|
230
|
+
const commentId = paramString(req.params.commentId);
|
|
231
|
+
if (!commentId) {
|
|
232
|
+
errorResponse(res, 400, 'comment.error.missingId', 'Comment ID is required');
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
const limit = queryLimit(req.query.limit);
|
|
236
|
+
const offset = queryOffset(req.query.offset);
|
|
237
|
+
const order = queryOrder(req.query.order);
|
|
238
|
+
try {
|
|
239
|
+
const parent = await getCommentById(commentId);
|
|
240
|
+
if (!parent) {
|
|
241
|
+
notFound(res);
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
if (!(await canAccessCommentResource(parent.resourceType, parent.resourceId, 'read', viewerId(res)))) {
|
|
245
|
+
notFound(res);
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
res.json(await getReplies(commentId, { limit, offset, order }));
|
|
249
|
+
}
|
|
250
|
+
catch (error) {
|
|
251
|
+
logger.error('Failed to list replies', { commentId, error });
|
|
252
|
+
errorResponse(res, 500, 'comment.error.repliesFailed', 'Failed to list replies');
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
export default router;
|
package/dist/service.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../src/service.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAqBH,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AACnD,OAAO,KAAK,EACV,OAAO,EAEP,kBAAkB,EAClB,eAAe,EACf,iBAAiB,EACjB,kBAAkB,EACnB,MAAM,YAAY,CAAA;AAqCnB;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAsB,aAAa,CACjC,YAAY,EAAE,MAAM,EACpB,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,kBAAkB,GACvB,OAAO,CAAC,eAAe,CAAC,CAoE1B;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAsB,qBAAqB,CACzC,YAAY,EAAE,MAAM,EACpB,UAAU,EAAE,MAAM,EAClB,OAAO,GAAE,iBAAsB,GAC9B,OAAO,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC,
|
|
1
|
+
{"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../src/service.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAqBH,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AACnD,OAAO,KAAK,EACV,OAAO,EAEP,kBAAkB,EAClB,eAAe,EACf,iBAAiB,EACjB,kBAAkB,EACnB,MAAM,YAAY,CAAA;AAqCnB;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAsB,aAAa,CACjC,YAAY,EAAE,MAAM,EACpB,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,kBAAkB,GACvB,OAAO,CAAC,eAAe,CAAC,CAoE1B;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAsB,qBAAqB,CACzC,YAAY,EAAE,MAAM,EACpB,UAAU,EAAE,MAAM,EAClB,OAAO,GAAE,iBAAsB,GAC9B,OAAO,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC,CAsBnC;AAED;;;;;;GAMG;AACH,wBAAsB,cAAc,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC,CAK/E;AAED;;;;;;;;GAQG;AACH,wBAAsB,aAAa,CACjC,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,kBAAkB,GACvB,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC,CAgBzB;AAED;;;;;;;;GAQG;AACH,wBAAsB,aAAa,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAMvF;AAED;;;;;;;GAOG;AACH,wBAAsB,UAAU,CAC9B,SAAS,EAAE,MAAM,EACjB,OAAO,GAAE,iBAAsB,GAC9B,OAAO,CAAC,eAAe,CAAC,OAAO,CAAC,CAAC,CAkBnC;AAED;;;;;;GAMG;AACH,wBAAsB,eAAe,CAAC,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAK/F"}
|
package/dist/service.js
CHANGED
|
@@ -141,6 +141,7 @@ export async function createComment(resourceType, resourceId, userId, data) {
|
|
|
141
141
|
export async function getCommentsByResource(resourceType, resourceId, options = {}) {
|
|
142
142
|
const limit = options.limit ?? 20;
|
|
143
143
|
const offset = options.offset ?? 0;
|
|
144
|
+
const direction = options.order === 'asc' ? 'asc' : 'desc';
|
|
144
145
|
const where = [
|
|
145
146
|
{ field: 'resourceType', operator: '=', value: resourceType },
|
|
146
147
|
{ field: 'resourceId', operator: '=', value: resourceId },
|
|
@@ -149,7 +150,7 @@ export async function getCommentsByResource(resourceType, resourceId, options =
|
|
|
149
150
|
const [data, total] = await Promise.all([
|
|
150
151
|
findMany(TABLE, {
|
|
151
152
|
where,
|
|
152
|
-
orderBy: [{ field: 'createdAt', direction
|
|
153
|
+
orderBy: [{ field: 'createdAt', direction }],
|
|
153
154
|
limit,
|
|
154
155
|
offset,
|
|
155
156
|
}),
|
|
@@ -223,11 +224,12 @@ export async function deleteComment(commentId, userId) {
|
|
|
223
224
|
export async function getReplies(commentId, options = {}) {
|
|
224
225
|
const limit = options.limit ?? 20;
|
|
225
226
|
const offset = options.offset ?? 0;
|
|
227
|
+
const direction = options.order === 'desc' ? 'desc' : 'asc';
|
|
226
228
|
const where = [{ field: 'parentId', operator: '=', value: commentId }];
|
|
227
229
|
const [data, total] = await Promise.all([
|
|
228
230
|
findMany(TABLE, {
|
|
229
231
|
where,
|
|
230
|
-
orderBy: [{ field: 'createdAt', direction
|
|
232
|
+
orderBy: [{ field: 'createdAt', direction }],
|
|
231
233
|
limit,
|
|
232
234
|
offset,
|
|
233
235
|
}),
|
package/dist/types.d.ts
CHANGED
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,iGAAiG;AACjG,MAAM,WAAW,aAAa;IAC5B,+BAA+B;IAC/B,EAAE,EAAE,MAAM,CAAA;IACV,qCAAqC;IACrC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAA;IACvB,yCAAyC;IACzC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAA;IACnB,sCAAsC;IACtC,GAAG,EAAE,MAAM,GAAG,IAAI,CAAA;IAClB,uCAAuC;IACvC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAA;CACtB;AAED;;GAEG;AACH,MAAM,WAAW,OAAO;IACtB,iCAAiC;IACjC,EAAE,EAAE,MAAM,CAAA;IACV,4FAA4F;IAC5F,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,iFAAiF;IACjF,YAAY,EAAE,MAAM,CAAA;IACpB,0DAA0D;IAC1D,UAAU,EAAE,MAAM,CAAA;IAClB,mDAAmD;IACnD,MAAM,EAAE,MAAM,CAAA;IACd,mFAAmF;IACnF,MAAM,CAAC,EAAE,aAAa,GAAG,IAAI,CAAA;IAC7B,oFAAoF;IACpF,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAA;IACvB,6BAA6B;IAC7B,IAAI,EAAE,MAAM,CAAA;IACZ,mEAAmE;IACnE,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAA;IACvB,+CAA+C;IAC/C,SAAS,EAAE,MAAM,CAAA;IACjB,oDAAoD;IACpD,SAAS,EAAE,MAAM,CAAA;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,6BAA6B;IAC7B,IAAI,EAAE,MAAM,CAAA;IACZ,uDAAuD;IACvD,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,qCAAqC;IACrC,IAAI,EAAE,MAAM,CAAA;CACb;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,2CAA2C;IAC3C,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,iCAAiC;IACjC,MAAM,CAAC,EAAE,MAAM,CAAA;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,iGAAiG;AACjG,MAAM,WAAW,aAAa;IAC5B,+BAA+B;IAC/B,EAAE,EAAE,MAAM,CAAA;IACV,qCAAqC;IACrC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAA;IACvB,yCAAyC;IACzC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAA;IACnB,sCAAsC;IACtC,GAAG,EAAE,MAAM,GAAG,IAAI,CAAA;IAClB,uCAAuC;IACvC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAA;CACtB;AAED;;GAEG;AACH,MAAM,WAAW,OAAO;IACtB,iCAAiC;IACjC,EAAE,EAAE,MAAM,CAAA;IACV,4FAA4F;IAC5F,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,iFAAiF;IACjF,YAAY,EAAE,MAAM,CAAA;IACpB,0DAA0D;IAC1D,UAAU,EAAE,MAAM,CAAA;IAClB,mDAAmD;IACnD,MAAM,EAAE,MAAM,CAAA;IACd,mFAAmF;IACnF,MAAM,CAAC,EAAE,aAAa,GAAG,IAAI,CAAA;IAC7B,oFAAoF;IACpF,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAA;IACvB,6BAA6B;IAC7B,IAAI,EAAE,MAAM,CAAA;IACZ,mEAAmE;IACnE,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAA;IACvB,+CAA+C;IAC/C,SAAS,EAAE,MAAM,CAAA;IACjB,oDAAoD;IACpD,SAAS,EAAE,MAAM,CAAA;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,6BAA6B;IAC7B,IAAI,EAAE,MAAM,CAAA;IACZ,uDAAuD;IACvD,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,qCAAqC;IACrC,IAAI,EAAE,MAAM,CAAA;CACb;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC,2CAA2C;IAC3C,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,iCAAiC;IACjC,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,6DAA6D;IAC7D,KAAK,CAAC,EAAE,KAAK,GAAG,MAAM,CAAA;CACvB;AAED;;GAEG;AACH,MAAM,WAAW,eAAe,CAAC,CAAC;IAChC,6CAA6C;IAC7C,IAAI,EAAE,CAAC,EAAE,CAAA;IACT,uDAAuD;IACvD,KAAK,EAAE,MAAM,CAAA;IACb,0CAA0C;IAC1C,KAAK,EAAE,MAAM,CAAA;IACb,iCAAiC;IACjC,MAAM,EAAE,MAAM,CAAA;CACf"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@molecule/api-resource-comment",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "Threaded comments on any resource with polymorphic attachment, pagination and sequential integer comment numbers (createNumberedComment)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
"files": [
|
|
20
20
|
"dist",
|
|
21
21
|
"src/__setup__",
|
|
22
|
+
"src/scaffold-router.ts",
|
|
22
23
|
"README.md"
|
|
23
24
|
],
|
|
24
25
|
"keywords": [
|
|
@@ -31,7 +32,10 @@
|
|
|
31
32
|
"devDependencies": {
|
|
32
33
|
"@molecule/api-database": "1.2.2",
|
|
33
34
|
"@molecule/api-database-sqlite": "1.4.4",
|
|
35
|
+
"@molecule/api-rate-limit": "1.0.4",
|
|
36
|
+
"@types/express": "5.0.6",
|
|
34
37
|
"@types/node": "26.6.5",
|
|
38
|
+
"express": "5.3.0",
|
|
35
39
|
"typescript": "6.0.3",
|
|
36
40
|
"vitest": "4.1.12",
|
|
37
41
|
"zod": "4.6.7"
|
|
@@ -40,7 +44,9 @@
|
|
|
40
44
|
"@molecule/api-database": "^1.1.0",
|
|
41
45
|
"@molecule/api-i18n": "^1.0.1",
|
|
42
46
|
"@molecule/api-logger": "^1.0.1",
|
|
47
|
+
"@molecule/api-rate-limit": "^1.0.1",
|
|
43
48
|
"@molecule/api-resource": "^1.0.1",
|
|
49
|
+
"express": "^5.0.0",
|
|
44
50
|
"zod": "^4.0.0"
|
|
45
51
|
},
|
|
46
52
|
"peerDependenciesMeta": {
|
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
import { type NextFunction, type Request, type Response, Router } from 'express'
|
|
2
|
+
|
|
3
|
+
import { t } from '@molecule/api-i18n'
|
|
4
|
+
import { logger } from '@molecule/api-logger'
|
|
5
|
+
import {
|
|
6
|
+
consume as consumeRateLimit,
|
|
7
|
+
hasProvider as hasRateLimitProvider,
|
|
8
|
+
} from '@molecule/api-rate-limit'
|
|
9
|
+
import {
|
|
10
|
+
canAccessCommentResource,
|
|
11
|
+
CommentResourceAccessError,
|
|
12
|
+
createComment,
|
|
13
|
+
createCommentSchema,
|
|
14
|
+
deleteComment,
|
|
15
|
+
getCommentById,
|
|
16
|
+
getCommentCount,
|
|
17
|
+
getCommentsByResource,
|
|
18
|
+
getReplies,
|
|
19
|
+
updateComment,
|
|
20
|
+
updateCommentSchema,
|
|
21
|
+
} from '@molecule/api-resource-comment'
|
|
22
|
+
|
|
23
|
+
// Edit paths, allowlisted resource types, and JSON here. Comment services own
|
|
24
|
+
// persistence, sequential numbering, author hydration, and ownership checks.
|
|
25
|
+
// Fail-closed access: once any sibling resource registers a policy via
|
|
26
|
+
// registerResourceAccessPolicy() (e.g. articles.ts), unregistered resourceTypes
|
|
27
|
+
// and private/draft parents are refused with 404.
|
|
28
|
+
const router = Router()
|
|
29
|
+
|
|
30
|
+
/** Maximum page size accepted by comment listing endpoints. */
|
|
31
|
+
const MAX_LIST_LIMIT = 100
|
|
32
|
+
/** Valid slug pattern for polymorphic `resourceType` path segments. */
|
|
33
|
+
const RESOURCE_TYPE_PATTERN = /^[a-z][a-z0-9_-]{0,63}$/i
|
|
34
|
+
|
|
35
|
+
/** Read the authenticated session user ID without type casts. */
|
|
36
|
+
function viewerId(res: Response): string | undefined {
|
|
37
|
+
const session: unknown = res.locals.session
|
|
38
|
+
if (typeof session !== 'object' || session === null || !('userId' in session)) return undefined
|
|
39
|
+
return typeof session.userId === 'string' && session.userId ? session.userId : undefined
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Read a single string route param. */
|
|
43
|
+
function paramString(value: unknown): string | undefined {
|
|
44
|
+
return typeof value === 'string' && value.trim() ? value : undefined
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Read an optional bounded page size (`1..MAX_LIST_LIMIT`). */
|
|
48
|
+
function queryLimit(value: unknown): number {
|
|
49
|
+
const parsed = parseInt(String(value ?? ''), 10)
|
|
50
|
+
return Number.isFinite(parsed) ? Math.min(MAX_LIST_LIMIT, Math.max(1, parsed)) : 20
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Read an optional non-negative pagination offset. */
|
|
54
|
+
function queryOffset(value: unknown): number {
|
|
55
|
+
const parsed = parseInt(String(value ?? ''), 10)
|
|
56
|
+
return Number.isFinite(parsed) ? Math.max(0, parsed) : 0
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Read an optional sort direction (`'asc'` or `'desc'`). */
|
|
60
|
+
function queryOrder(value: unknown): 'asc' | 'desc' | undefined {
|
|
61
|
+
if (value === 'asc' || value === 'desc') return value
|
|
62
|
+
return undefined
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Send an i18n-formatted error response. */
|
|
66
|
+
function errorResponse(res: Response, status: number, key: string, fallback: string): void {
|
|
67
|
+
res.status(status).json({ error: t(key, undefined, { defaultValue: fallback }), errorKey: key })
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Answer a request missing a valid signed-in session. */
|
|
71
|
+
function unauthorized(res: Response): void {
|
|
72
|
+
errorResponse(res, 401, 'resource.error.unauthorized', 'Unauthorized.')
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Answer a request for a missing or inaccessible comment or parent resource. */
|
|
76
|
+
function notFound(res: Response): void {
|
|
77
|
+
errorResponse(res, 404, 'resource.error.notFound', 'Not found.')
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Answer a request with invalid input fields. */
|
|
81
|
+
function invalid(
|
|
82
|
+
res: Response,
|
|
83
|
+
issues: ReadonlyArray<{ path: PropertyKey[]; message: string }>,
|
|
84
|
+
): void {
|
|
85
|
+
res.status(400).json({
|
|
86
|
+
error: issues
|
|
87
|
+
.map((issue) => `${issue.path.map(String).join('.')}: ${issue.message}`)
|
|
88
|
+
.join(', '),
|
|
89
|
+
errorKey: 'comment.error.validationFailed',
|
|
90
|
+
})
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Validate `resourceType` and `resourceId` path params. */
|
|
94
|
+
function resourceParams(
|
|
95
|
+
req: Request,
|
|
96
|
+
res: Response,
|
|
97
|
+
): { resourceType: string; resourceId: string } | null {
|
|
98
|
+
const resourceType = paramString(req.params.resourceType)
|
|
99
|
+
const resourceId = paramString(req.params.resourceId)
|
|
100
|
+
if (!resourceType || !resourceId || !RESOURCE_TYPE_PATTERN.test(resourceType)) {
|
|
101
|
+
errorResponse(res, 400, 'comment.error.missingResource', 'Resource type and ID are required')
|
|
102
|
+
return null
|
|
103
|
+
}
|
|
104
|
+
return { resourceType, resourceId }
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Throttle comment creation when a rate-limit provider is bonded. */
|
|
108
|
+
async function rateLimitComments(req: Request, res: Response, next: NextFunction): Promise<void> {
|
|
109
|
+
if (!hasRateLimitProvider()) {
|
|
110
|
+
next()
|
|
111
|
+
return
|
|
112
|
+
}
|
|
113
|
+
const key = `comments:create:${viewerId(res) ?? req.ip ?? 'unknown'}`
|
|
114
|
+
const result = await consumeRateLimit(key)
|
|
115
|
+
if (!result.allowed) {
|
|
116
|
+
const retryAfter =
|
|
117
|
+
result.retryAfter ?? Math.max(1, Math.ceil((result.resetAt.getTime() - Date.now()) / 1000))
|
|
118
|
+
res.set('Retry-After', String(retryAfter))
|
|
119
|
+
errorResponse(
|
|
120
|
+
res,
|
|
121
|
+
429,
|
|
122
|
+
'rateLimit.error.tooManyRequests',
|
|
123
|
+
'Too many requests. Please try again later.',
|
|
124
|
+
)
|
|
125
|
+
return
|
|
126
|
+
}
|
|
127
|
+
next()
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
router.post('/:resourceType/:resourceId/comments', rateLimitComments, async (req, res) => {
|
|
131
|
+
const userId = viewerId(res)
|
|
132
|
+
if (!userId) {
|
|
133
|
+
unauthorized(res)
|
|
134
|
+
return
|
|
135
|
+
}
|
|
136
|
+
const params = resourceParams(req, res)
|
|
137
|
+
if (!params) return
|
|
138
|
+
|
|
139
|
+
const parsed = createCommentSchema.safeParse(req.body)
|
|
140
|
+
if (!parsed.success) {
|
|
141
|
+
invalid(res, parsed.error.issues)
|
|
142
|
+
return
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
try {
|
|
146
|
+
const comment = await createComment(params.resourceType, params.resourceId, userId, parsed.data)
|
|
147
|
+
res.status(201).json(comment)
|
|
148
|
+
} catch (error) {
|
|
149
|
+
if (error instanceof CommentResourceAccessError) {
|
|
150
|
+
notFound(res)
|
|
151
|
+
return
|
|
152
|
+
}
|
|
153
|
+
logger.error('Failed to create comment', { ...params, userId, error })
|
|
154
|
+
errorResponse(res, 500, 'comment.error.createFailed', 'Failed to create comment')
|
|
155
|
+
}
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
router.get('/:resourceType/:resourceId/comments', async (req, res) => {
|
|
159
|
+
const params = resourceParams(req, res)
|
|
160
|
+
if (!params) return
|
|
161
|
+
|
|
162
|
+
const limit = queryLimit(req.query.limit)
|
|
163
|
+
const offset = queryOffset(req.query.offset)
|
|
164
|
+
const order = queryOrder(req.query.order)
|
|
165
|
+
|
|
166
|
+
try {
|
|
167
|
+
if (
|
|
168
|
+
!(await canAccessCommentResource(
|
|
169
|
+
params.resourceType,
|
|
170
|
+
params.resourceId,
|
|
171
|
+
'read',
|
|
172
|
+
viewerId(res),
|
|
173
|
+
))
|
|
174
|
+
) {
|
|
175
|
+
notFound(res)
|
|
176
|
+
return
|
|
177
|
+
}
|
|
178
|
+
res.json(
|
|
179
|
+
await getCommentsByResource(params.resourceType, params.resourceId, { limit, offset, order }),
|
|
180
|
+
)
|
|
181
|
+
} catch (error) {
|
|
182
|
+
logger.error('Failed to list comments', { ...params, error })
|
|
183
|
+
errorResponse(res, 500, 'comment.error.listFailed', 'Failed to list comments')
|
|
184
|
+
}
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
router.get('/:resourceType/:resourceId/comments/count', async (req, res) => {
|
|
188
|
+
const params = resourceParams(req, res)
|
|
189
|
+
if (!params) return
|
|
190
|
+
|
|
191
|
+
try {
|
|
192
|
+
if (
|
|
193
|
+
!(await canAccessCommentResource(
|
|
194
|
+
params.resourceType,
|
|
195
|
+
params.resourceId,
|
|
196
|
+
'read',
|
|
197
|
+
viewerId(res),
|
|
198
|
+
))
|
|
199
|
+
) {
|
|
200
|
+
notFound(res)
|
|
201
|
+
return
|
|
202
|
+
}
|
|
203
|
+
const count = await getCommentCount(params.resourceType, params.resourceId)
|
|
204
|
+
res.json({ count })
|
|
205
|
+
} catch (error) {
|
|
206
|
+
logger.error('Failed to count comments', { ...params, error })
|
|
207
|
+
errorResponse(res, 500, 'comment.error.countFailed', 'Failed to count comments')
|
|
208
|
+
}
|
|
209
|
+
})
|
|
210
|
+
|
|
211
|
+
router.get('/comments/:commentId', async (req, res) => {
|
|
212
|
+
const commentId = paramString(req.params.commentId)
|
|
213
|
+
if (!commentId) {
|
|
214
|
+
errorResponse(res, 400, 'comment.error.missingId', 'Comment ID is required')
|
|
215
|
+
return
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
try {
|
|
219
|
+
const comment = await getCommentById(commentId)
|
|
220
|
+
if (!comment) {
|
|
221
|
+
notFound(res)
|
|
222
|
+
return
|
|
223
|
+
}
|
|
224
|
+
if (
|
|
225
|
+
!(await canAccessCommentResource(
|
|
226
|
+
comment.resourceType,
|
|
227
|
+
comment.resourceId,
|
|
228
|
+
'read',
|
|
229
|
+
viewerId(res),
|
|
230
|
+
))
|
|
231
|
+
) {
|
|
232
|
+
notFound(res)
|
|
233
|
+
return
|
|
234
|
+
}
|
|
235
|
+
res.json(comment)
|
|
236
|
+
} catch (error) {
|
|
237
|
+
logger.error('Failed to read comment', { commentId, error })
|
|
238
|
+
errorResponse(res, 500, 'comment.error.readFailed', 'Failed to read comment')
|
|
239
|
+
}
|
|
240
|
+
})
|
|
241
|
+
|
|
242
|
+
router.put('/comments/:commentId', async (req, res) => {
|
|
243
|
+
const userId = viewerId(res)
|
|
244
|
+
if (!userId) {
|
|
245
|
+
unauthorized(res)
|
|
246
|
+
return
|
|
247
|
+
}
|
|
248
|
+
const commentId = paramString(req.params.commentId)
|
|
249
|
+
if (!commentId) {
|
|
250
|
+
errorResponse(res, 400, 'comment.error.missingId', 'Comment ID is required')
|
|
251
|
+
return
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const parsed = updateCommentSchema.safeParse(req.body)
|
|
255
|
+
if (!parsed.success) {
|
|
256
|
+
invalid(res, parsed.error.issues)
|
|
257
|
+
return
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
try {
|
|
261
|
+
const updated = await updateComment(commentId, userId, parsed.data)
|
|
262
|
+
if (!updated) {
|
|
263
|
+
errorResponse(
|
|
264
|
+
res,
|
|
265
|
+
404,
|
|
266
|
+
'comment.error.notFoundOrForbidden',
|
|
267
|
+
'Comment not found or not owned by user',
|
|
268
|
+
)
|
|
269
|
+
return
|
|
270
|
+
}
|
|
271
|
+
res.json(updated)
|
|
272
|
+
} catch (error) {
|
|
273
|
+
logger.error('Failed to update comment', { commentId, userId, error })
|
|
274
|
+
errorResponse(res, 500, 'comment.error.updateFailed', 'Failed to update comment')
|
|
275
|
+
}
|
|
276
|
+
})
|
|
277
|
+
|
|
278
|
+
router.delete('/comments/:commentId', async (req, res) => {
|
|
279
|
+
const userId = viewerId(res)
|
|
280
|
+
if (!userId) {
|
|
281
|
+
unauthorized(res)
|
|
282
|
+
return
|
|
283
|
+
}
|
|
284
|
+
const commentId = paramString(req.params.commentId)
|
|
285
|
+
if (!commentId) {
|
|
286
|
+
errorResponse(res, 400, 'comment.error.missingId', 'Comment ID is required')
|
|
287
|
+
return
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
try {
|
|
291
|
+
const deleted = await deleteComment(commentId, userId)
|
|
292
|
+
if (!deleted) {
|
|
293
|
+
errorResponse(
|
|
294
|
+
res,
|
|
295
|
+
404,
|
|
296
|
+
'comment.error.notFoundOrForbidden',
|
|
297
|
+
'Comment not found or not owned by user',
|
|
298
|
+
)
|
|
299
|
+
return
|
|
300
|
+
}
|
|
301
|
+
res.status(204).end()
|
|
302
|
+
} catch (error) {
|
|
303
|
+
logger.error('Failed to delete comment', { commentId, userId, error })
|
|
304
|
+
errorResponse(res, 500, 'comment.error.deleteFailed', 'Failed to delete comment')
|
|
305
|
+
}
|
|
306
|
+
})
|
|
307
|
+
|
|
308
|
+
router.get('/comments/:commentId/replies', async (req, res) => {
|
|
309
|
+
const commentId = paramString(req.params.commentId)
|
|
310
|
+
if (!commentId) {
|
|
311
|
+
errorResponse(res, 400, 'comment.error.missingId', 'Comment ID is required')
|
|
312
|
+
return
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
const limit = queryLimit(req.query.limit)
|
|
316
|
+
const offset = queryOffset(req.query.offset)
|
|
317
|
+
const order = queryOrder(req.query.order)
|
|
318
|
+
|
|
319
|
+
try {
|
|
320
|
+
const parent = await getCommentById(commentId)
|
|
321
|
+
if (!parent) {
|
|
322
|
+
notFound(res)
|
|
323
|
+
return
|
|
324
|
+
}
|
|
325
|
+
if (
|
|
326
|
+
!(await canAccessCommentResource(
|
|
327
|
+
parent.resourceType,
|
|
328
|
+
parent.resourceId,
|
|
329
|
+
'read',
|
|
330
|
+
viewerId(res),
|
|
331
|
+
))
|
|
332
|
+
) {
|
|
333
|
+
notFound(res)
|
|
334
|
+
return
|
|
335
|
+
}
|
|
336
|
+
res.json(await getReplies(commentId, { limit, offset, order }))
|
|
337
|
+
} catch (error) {
|
|
338
|
+
logger.error('Failed to list replies', { commentId, error })
|
|
339
|
+
errorResponse(res, 500, 'comment.error.repliesFailed', 'Failed to list replies')
|
|
340
|
+
}
|
|
341
|
+
})
|
|
342
|
+
|
|
343
|
+
export default router
|