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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 | 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 19x 5x 4x 9x 1x 21x 3x 18x 18x 3x 15x 1x 14x 14x 1x 13x 8x 38x 2x 36x 36x 2x 34x 1x 33x 33x 1x 32x 1x 31x 8x 3x 5x 23x 1x 22x 2x 20x 5x 2x 15x 1x 17x 23x 1x 22x 22x 2x 20x 15x 1x 14x 14x 1x 13x 1x 12x 12x 11x 1x 10x 20x 1x 19x 19x 1x 18x 1x 17x 1x 16x 16x 14x 12x 1x 11x 11x 1x 10x 5x 2x 5x 1x 7x 7x 6x 18x 18x 4x 3x 5x 5x 74x 74x 70x 8x | /**
* User reports of abusive or illegal content — see
* docs/architecture/MODERATION.md "Reports".
*
* A report targets a comment (on an item or on a store pack), a direct message
* conversation, or a store pack. ContentReportsTable holds one row per
* reporter and target: `targetKey` (below) + `reporterId`, so reporting the
* same thing twice changes nothing. Each row carries a snapshot of what was
* reported, taken when it was reported, so the report still says what it was
* about after the content is edited or deleted. `listKey` feeds the
* `listIndex` GSI the admin list reads, newest first: `REPORT` while the
* report is open, `REPORT#CLOSED` once an admin decided it. A decision covers
* every open report of its target (UC-REPORT-005.1).
*/
const { isValidUuid } = require('./isValidUuid')
const REPORT_TARGET_TYPE = {
COMMENT: 'comment',
CONVERSATION: 'conversation',
PACK: 'pack'
}
const REPORT_REASONS = ['spam', 'harassment', 'inappropriate', 'illegal', 'other']
/** Partition of the `listIndex` GSI: the open reports, newest first. */
const REPORT_LIST_KEY = 'REPORT'
/** Partition of the `listIndex` GSI: the decided reports, newest first. */
const REPORT_CLOSED_LIST_KEY = 'REPORT#CLOSED'
const REPORT_STATUS = {
OPEN: 'open',
// The content broke the rules: removed (when it can be) and its author sanctioned
ACTIONED: 'actioned',
// Nothing wrong found
DISMISSED: 'dismissed'
}
/**
* What befalls the author of actioned content, on top of a strike:
* - warning: the strike alone
* - restriction: no commenting, messaging or publishing for some days
* - ban: the account is disabled, they can no longer sign in
*/
const REPORT_SANCTIONS = ['warning', 'restriction', 'ban']
const MAX_RESTRICTION_DAYS = 365
const MAX_DECISION_NOTE_LENGTH = 1000
/** A strike older than this no longer counts (MODERATION.md "Sanctions"). */
const STRIKE_WINDOW_MS = 365 * 24 * 60 * 60 * 1000
/**
* Appeals (UC-REPORT-009.1): the author of actioned content, or a reporter
* whose report was dismissed, contests the decision once, within six months
* (the DSA's minimum, Art. 20). ModerationAppealsTable holds one row per
* decision and appellant; `listKey` feeds its `listIndex` GSI like the
* reports' own.
*/
const APPEAL_LIST_KEY = 'APPEAL'
const APPEAL_CLOSED_LIST_KEY = 'APPEAL#CLOSED'
const APPEAL_WINDOW_MS = 183 * 24 * 60 * 60 * 1000
const MAX_APPEAL_MESSAGE_LENGTH = 2000
const APPEAL_ROLE = {
// The author of actioned content contests the action and the sanction
AUTHOR: 'author',
// A reporter contests a dismissal
REPORTER: 'reporter'
}
const APPEAL_STATUS = {
OPEN: 'open',
// The decision stands
UPHELD: 'upheld',
// The decision is undone: the sanction lifted, or the reports reopened
OVERTURNED: 'overturned'
}
/**
* Reporting in bad faith (UC-REPORT-011.1, DSA Art. 23): a reporter whose
* reports are often dismissed is warned first, then may have reporting
* suspended for some days.
*/
const REPORTING_ACTIONS = ['warn', 'suspend']
const MAX_REPORTING_SUSPENSION_DAYS = 365
/** How much of a comment a notification about it quotes. */
const EXCERPT_LENGTH = 140
const MAX_REPORT_DETAILS_LENGTH = 1000
/** How many of a reported conversation's latest messages its report keeps. */
const REPORT_MESSAGES_SNAPSHOT = 10
/**
* The partition of a target's reports. A comment's id is only unique within
* its thread (CommentsTable is keyed by thread + id), so the thread is part
* of its key.
*/
function reportTargetKey(targetType, ids) {
switch (targetType) {
case REPORT_TARGET_TYPE.COMMENT:
return `comment#${ids.threadKey}#${ids.commentId}`
case REPORT_TARGET_TYPE.CONVERSATION:
return `conversation#${ids.conversationId}`
case REPORT_TARGET_TYPE.PACK:
return `pack#${ids.packId}`
default:
throw new Error(`Unknown report target type: ${targetType}`)
}
}
/**
* Validates a report's body: `{ reason, details? }`.
*
* @returns {{ valid: true, reason: string, details: string | null } | { valid: false, error: string }}
*/
function validateReportInput(payload) {
if (!payload || typeof payload !== 'object' || Array.isArray(payload)) {
return { valid: false, error: 'Invalid request body' }
}
const { reason, details } = payload
if (!REPORT_REASONS.includes(reason)) {
return { valid: false, error: `reason must be one of: ${REPORT_REASONS.join(', ')}` }
}
if (details !== undefined && details !== null && typeof details !== 'string') {
return { valid: false, error: 'details must be a string' }
}
const trimmed = typeof details === 'string' ? details.trim() : ''
if (trimmed.length > MAX_REPORT_DETAILS_LENGTH) {
return {
valid: false,
error: `details must be at most ${MAX_REPORT_DETAILS_LENGTH} characters`
}
}
return { valid: true, reason, details: trimmed || null }
}
/** `targetKey` as the admin list returns it: a known type, then ids. */
const TARGET_KEY_PATTERN = /^(comment|conversation|pack)#[A-Za-z0-9#-]{1,300}$/
/**
* Validates an admin's decision on a target's reports:
* `{ targetKey, outcome, removed?, sanction?, restrictionDays?, note? }`.
* - `outcome` 'dismissed' takes nothing else
* - `outcome` 'actioned' takes a `sanction` ('restriction' with
* `restrictionDays`), and `removed: true` when the admin removed the content
* - `note`, shown to the sanctioned user: why, in the admin's words
*
* @returns {{ valid: true, targetKey: string, outcome: string, removed: boolean,
* sanction: string | null, restrictionDays: number | null, note: string | null }
* | { valid: false, error: string }}
*/
function validateDecisionInput(payload) {
if (!payload || typeof payload !== 'object' || Array.isArray(payload)) {
return { valid: false, error: 'Invalid request body' }
}
const { targetKey, outcome, removed, sanction, restrictionDays, note } = payload
if (typeof targetKey !== 'string' || !TARGET_KEY_PATTERN.test(targetKey)) {
return { valid: false, error: 'Invalid targetKey' }
}
if (note !== undefined && note !== null && typeof note !== 'string') {
return { valid: false, error: 'note must be a string' }
}
const trimmedNote = typeof note === 'string' ? note.trim() : ''
if (trimmedNote.length > MAX_DECISION_NOTE_LENGTH) {
return { valid: false, error: `note must be at most ${MAX_DECISION_NOTE_LENGTH} characters` }
}
if (removed !== undefined && typeof removed !== 'boolean') {
return { valid: false, error: 'removed must be a boolean' }
}
if (outcome === REPORT_STATUS.DISMISSED) {
if (removed || sanction !== undefined || restrictionDays !== undefined) {
return { valid: false, error: 'A dismissed report takes no removal and no sanction' }
}
return {
valid: true,
targetKey,
outcome,
removed: false,
sanction: null,
restrictionDays: null,
note: trimmedNote || null
}
}
if (outcome !== REPORT_STATUS.ACTIONED) {
return { valid: false, error: 'outcome must be one of: actioned, dismissed' }
}
if (!REPORT_SANCTIONS.includes(sanction)) {
return { valid: false, error: `sanction must be one of: ${REPORT_SANCTIONS.join(', ')}` }
}
if (sanction === 'restriction') {
if (
!Number.isInteger(restrictionDays) ||
restrictionDays < 1 ||
restrictionDays > MAX_RESTRICTION_DAYS
) {
return {
valid: false,
error: `restrictionDays must be an integer between 1 and ${MAX_RESTRICTION_DAYS}`
}
}
} else if (restrictionDays !== undefined) {
return { valid: false, error: 'restrictionDays only goes with a restriction' }
}
return {
valid: true,
targetKey,
outcome,
removed: removed === true,
sanction,
restrictionDays: sanction === 'restriction' ? restrictionDays : null,
note: trimmedNote || null
}
}
function validateNote(note) {
if (note !== undefined && note !== null && typeof note !== 'string') {
return { error: 'note must be a string' }
}
const trimmed = typeof note === 'string' ? note.trim() : ''
if (trimmed.length > MAX_DECISION_NOTE_LENGTH) {
return { error: `note must be at most ${MAX_DECISION_NOTE_LENGTH} characters` }
}
return { note: trimmed || null }
}
/**
* Validates an appeal: `{ targetKey, decisionId, message }`, the message saying
* why the decision is wrong.
*
* @returns {{ valid: true, targetKey: string, decisionId: string, message: string }
* | { valid: false, error: string }}
*/
function validateAppealInput(payload) {
if (!payload || typeof payload !== 'object' || Array.isArray(payload)) {
return { valid: false, error: 'Invalid request body' }
}
const { targetKey, decisionId, message } = payload
if (typeof targetKey !== 'string' || !TARGET_KEY_PATTERN.test(targetKey)) {
return { valid: false, error: 'Invalid targetKey' }
}
if (typeof decisionId !== 'string' || !isValidUuid(decisionId)) {
return { valid: false, error: 'Invalid decisionId' }
}
const trimmed = typeof message === 'string' ? message.trim() : ''
if (!trimmed) return { valid: false, error: 'message is required' }
if (trimmed.length > MAX_APPEAL_MESSAGE_LENGTH) {
return {
valid: false,
error: `message must be at most ${MAX_APPEAL_MESSAGE_LENGTH} characters`
}
}
return { valid: true, targetKey, decisionId, message: trimmed }
}
/**
* Validates an admin's decision on an appeal:
* `{ decisionId, appellantId, outcome: 'upheld' | 'overturned', note? }`.
*
* @returns {{ valid: true, decisionId: string, appellantId: string, outcome: string,
* note: string | null } | { valid: false, error: string }}
*/
function validateAppealDecisionInput(payload) {
if (!payload || typeof payload !== 'object' || Array.isArray(payload)) {
return { valid: false, error: 'Invalid request body' }
}
const { decisionId, appellantId, outcome } = payload
if (typeof decisionId !== 'string' || !isValidUuid(decisionId)) {
return { valid: false, error: 'Invalid decisionId' }
}
if (typeof appellantId !== 'string' || !appellantId || appellantId.length > 128) {
return { valid: false, error: 'Invalid appellantId' }
}
if (outcome !== APPEAL_STATUS.UPHELD && outcome !== APPEAL_STATUS.OVERTURNED) {
return { valid: false, error: 'outcome must be one of: upheld, overturned' }
}
const { note, error } = validateNote(payload.note)
if (error) return { valid: false, error }
return { valid: true, decisionId, appellantId, outcome, note }
}
/**
* Validates an admin's action on a reporter: `{ action: 'warn' }` or
* `{ action: 'suspend', days }`, with an optional `note`.
*
* @returns {{ valid: true, action: string, days: number | null, note: string | null }
* | { valid: false, error: string }}
*/
function validateReportingActionInput(payload) {
if (!payload || typeof payload !== 'object' || Array.isArray(payload)) {
return { valid: false, error: 'Invalid request body' }
}
const { action, days } = payload
if (!REPORTING_ACTIONS.includes(action)) {
return { valid: false, error: `action must be one of: ${REPORTING_ACTIONS.join(', ')}` }
}
if (action === 'suspend') {
if (!Number.isInteger(days) || days < 1 || days > MAX_REPORTING_SUSPENSION_DAYS) {
return {
valid: false,
error: `days must be an integer between 1 and ${MAX_REPORTING_SUSPENSION_DAYS}`
}
}
} else if (days !== undefined) {
return { valid: false, error: 'days only goes with a suspension' }
}
const { note, error } = validateNote(payload.note)
if (error) return { valid: false, error }
return { valid: true, action, days: action === 'suspend' ? days : null, note }
}
/** Whether `user` (a UsersTable row) may not report at `now`, and until when. */
function reportingSuspendedUntil(user, now = Date.now()) {
const until = Date.parse(user?.reportingSuspendedUntil)
return Number.isFinite(until) && until > now ? user.reportingSuspendedUntil : null
}
/** The strikes of a user's row still counting at `now` (ISO dates, oldest first). */
function activeStrikes(strikes, now = Date.now()) {
if (!Array.isArray(strikes)) return []
return strikes.filter((at) => {
const time = Date.parse(at)
return Number.isFinite(time) && now - time < STRIKE_WINDOW_MS
})
}
/** A short quote of a report's content, for the notifications about it. */
function reportExcerpt(report) {
const body = report.snapshot?.body
if (typeof body !== 'string' || !body) return null
return body.length > EXCERPT_LENGTH ? `${body.slice(0, EXCERPT_LENGTH)}…` : body
}
module.exports = {
REPORT_TARGET_TYPE,
REPORT_REASONS,
REPORT_LIST_KEY,
REPORT_CLOSED_LIST_KEY,
REPORT_STATUS,
REPORT_SANCTIONS,
MAX_RESTRICTION_DAYS,
MAX_DECISION_NOTE_LENGTH,
STRIKE_WINDOW_MS,
APPEAL_LIST_KEY,
APPEAL_CLOSED_LIST_KEY,
APPEAL_WINDOW_MS,
APPEAL_ROLE,
APPEAL_STATUS,
MAX_APPEAL_MESSAGE_LENGTH,
MAX_REPORTING_SUSPENSION_DAYS,
validateDecisionInput,
validateAppealInput,
validateAppealDecisionInput,
validateReportingActionInput,
reportingSuspendedUntil,
activeStrikes,
reportExcerpt,
MAX_REPORT_DETAILS_LENGTH,
REPORT_MESSAGES_SNAPSHOT,
reportTargetKey,
validateReportInput
}
|