Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 14x 14x 11x 11x 11x 1x 10x 10x 10x 5x 5x 4x 8x 8x 1x 7x 7x 7x 2x 10x 5x 5x 7x 5x 2x 3x 10x 10x 9x 8x 34x 34x 20x 20x 19x 11x 1x 12x 12x 1x 1x 34x 34x 34x 15x 15x 15x 3x 12x 12x 11x 11x 9x 9x 9x 2x 1x 7x 1x 1x | /**
* POST /v1/entities/{entityId}/items/{itemId}/comments/{commentId}/report
* POST /v1/store/{packId}/comments/{commentId}/report
* POST /v1/conversations/{conversationId}/report
* POST /v1/store/{packId}/report
*
* A signed-in user reports a comment, a direct message conversation (a
* message request especially) or a store pack, with a reason and optional
* details. The report lands on the platform admins' Reports tab
* (admin/list-content-reports.js), the takedown path for abusive and illegal
* content — see docs/architecture/MODERATION.md "Reports".
*
* - A comment: whoever can read its thread, and only an approved comment —
* a pending one is the community's to decide, invisible to anybody but its
* author.
* - A conversation: one of its two participants.
* - A pack: anyone, as anyone may open it in the Store.
*
* Nobody reports their own content, nor anybody whose reporting a platform
* admin suspended (`403 REPORTING_SUSPENDED`, UC-REPORT-011.1). Reporting the same target again is
* idempotent: the first report stands (200 with `alreadyReported: true`).
*
* @implements UC-REPORT-001.1, UC-REPORT-002.1, UC-REPORT-003.1, UC-REPORT-011.1
*/
const { randomUUID } = require('crypto')
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const {
DynamoDBDocumentClient,
GetCommand,
PutCommand,
QueryCommand
} = require('@aws-sdk/lib-dynamodb')
const { resolveCommentContext, errorResponse, CORS_HEADERS } = require('../utils/commentAccess')
const { COMMENT_STATUS } = require('../utils/comments')
const { getPack } = require('../utils/packs')
const { isValidUuid } = require('../utils/isValidUuid')
const {
REPORT_TARGET_TYPE,
REPORT_LIST_KEY,
REPORT_STATUS,
REPORT_MESSAGES_SNAPSHOT,
reportTargetKey,
validateReportInput,
reportingSuspendedUntil
} = require('../utils/contentReports')
const client = new DynamoDBClient({})
const docClient = DynamoDBDocumentClient.from(client)
const OWN_CONTENT_ERROR = 'You cannot report your own content'
/**
* The reported comment, from its thread. Its id is client-supplied, so it is
* looked up in the thread's own partition and must belong to that thread.
*/
async function resolveCommentTarget(event) {
const context = await resolveCommentContext(docClient, event, { requireEnabled: false })
if (context.response) return context
const { auth, thread } = context
const commentId = event.pathParameters?.commentId
if (!commentId || !isValidUuid(commentId))
return { response: errorResponse(400, 'Invalid commentId') }
const result = await docClient.send(
new GetCommand({
TableName: process.env.COMMENTS_TABLE_NAME,
Key: { itemId: thread.key, id: commentId }
})
)
const comment = result.Item
if (!comment || !thread.owns(comment) || comment.status !== COMMENT_STATUS.APPROVED) {
return { response: errorResponse(404, 'Comment not found') }
}
if (comment.authorId === auth.userId) return { response: errorResponse(400, OWN_CONTENT_ERROR) }
return {
reporterId: auth.userId,
targetType: REPORT_TARGET_TYPE.COMMENT,
targetKey: reportTargetKey(REPORT_TARGET_TYPE.COMMENT, { threadKey: thread.key, commentId }),
reportedUserId: comment.authorId || null,
target: {
commentId,
...(thread.kind === 'pack'
? { packId: thread.pack.id }
: { entityId: thread.entity.id, itemId: thread.item.id }),
flowId: thread.scopeFlowId || null
},
snapshot: {
body: comment.body ?? '',
createdAt: comment.createdAt || null,
...(comment.packVersion && { packVersion: comment.packVersion })
}
}
}
/** The reported conversation, with its latest messages as the admin will need them. */
async function resolveConversationTarget(event, userId) {
const conversationId = event.pathParameters?.conversationId
if (!conversationId || !isValidUuid(conversationId)) {
return { response: errorResponse(400, 'Invalid conversationId') }
}
const result = await docClient.send(
new GetCommand({
TableName: process.env.CONVERSATIONS_TABLE_NAME,
Key: { id: conversationId }
})
)
const conversation = result.Item
// Same answer as for a conversation that does not exist: a non-participant learns nothing.
if (!conversation || !conversation.participantIds?.includes(userId)) {
return { response: errorResponse(404, 'Conversation not found') }
}
const otherUserId = conversation.participantIds.find((id) => id !== userId) || null
const messages = await docClient.send(
new QueryCommand({
TableName: process.env.MESSAGES_TABLE_NAME,
KeyConditionExpression: 'conversationId = :conversationId',
ExpressionAttributeValues: { ':conversationId': conversationId },
ScanIndexForward: false,
Limit: REPORT_MESSAGES_SNAPSHOT
})
)
const latest = (messages.Items || [])
.map((message) => ({
senderId: message.senderId,
body: message.body,
createdAt: message.createdAt
}))
.reverse()
// Nothing the other participant wrote: nothing of theirs to report.
if (!latest.some((message) => message.senderId === otherUserId)) {
return { response: errorResponse(400, 'This conversation has no message to report') }
}
return {
reporterId: userId,
targetType: REPORT_TARGET_TYPE.CONVERSATION,
targetKey: reportTargetKey(REPORT_TARGET_TYPE.CONVERSATION, { conversationId }),
reportedUserId: otherUserId,
target: { conversationId },
snapshot: {
status: conversation.status || 'accepted',
requesterId: conversation.requesterId || null,
messages: latest
}
}
}
async function resolvePackTarget(event, userId) {
const pack = await getPack(docClient, event.pathParameters?.packId)
if (!pack) return { response: errorResponse(404, 'Pack not found') }
if (pack.authorId === userId) return { response: errorResponse(400, OWN_CONTENT_ERROR) }
return {
reporterId: userId,
targetType: REPORT_TARGET_TYPE.PACK,
targetKey: reportTargetKey(REPORT_TARGET_TYPE.PACK, { packId: pack.id }),
reportedUserId: pack.authorId || null,
target: { packId: pack.id, flowId: pack.flowId || null },
snapshot: {
name: pack.name ?? '',
version: pack.currentVersion ?? null
}
}
}
/** Which of the four routes was called: the path parameters tell them apart. */
async function resolveTarget(event) {
const params = event.pathParameters || {}
if (params.commentId) return resolveCommentTarget(event)
// Conversations and packs belong to a person: an API key cannot report them.
const userId = event.requestContext?.authorizer?.claims?.sub
if (!userId) return { response: errorResponse(401, 'Unauthorized') }
if (params.conversationId) return resolveConversationTarget(event, userId)
if (params.packId) return resolvePackTarget(event, userId)
return { response: errorResponse(400, 'Missing report target') }
}
function parseBody(event) {
try {
return { payload: event.body ? JSON.parse(event.body) : null }
} catch {
return { response: errorResponse(400, 'Invalid JSON body') }
}
}
exports.handler = async (event) => {
try {
const target = await resolveTarget(event)
if (target.response) return target.response
// Reporting suspended for bad faith (UC-REPORT-011.1): refused until the end
const reporter = await docClient.send(
new GetCommand({ TableName: process.env.USERS_TABLE_NAME, Key: { id: target.reporterId } })
)
const suspendedUntil = reportingSuspendedUntil(reporter.Item)
if (suspendedUntil) {
return {
statusCode: 403,
headers: CORS_HEADERS,
body: JSON.stringify({ error: 'REPORTING_SUSPENDED', suspendedUntil })
}
}
const parsed = parseBody(event)
if (parsed.response) return parsed.response
const input = validateReportInput(parsed.payload)
if (!input.valid) return errorResponse(400, input.error)
const report = {
targetKey: target.targetKey,
reporterId: target.reporterId,
id: randomUUID(),
listKey: REPORT_LIST_KEY,
status: REPORT_STATUS.OPEN,
targetType: target.targetType,
target: target.target,
reportedUserId: target.reportedUserId,
reason: input.reason,
...(input.details && { details: input.details }),
snapshot: target.snapshot,
createdAt: new Date().toISOString()
}
try {
await docClient.send(
new PutCommand({
TableName: process.env.CONTENT_REPORTS_TABLE_NAME,
Item: report,
ConditionExpression: 'attribute_not_exists(targetKey)'
})
)
} catch (error) {
if (error.name !== 'ConditionalCheckFailedException') throw error
return {
statusCode: 200,
headers: CORS_HEADERS,
body: JSON.stringify({ alreadyReported: true })
}
}
return {
statusCode: 201,
headers: CORS_HEADERS,
body: JSON.stringify({ id: report.id, alreadyReported: false })
}
} catch (error) {
console.error('[content-reports/create] Exception:', error.message, error.stack)
return errorResponse(500, 'Failed to report content')
}
}
|