feat(safety): report, block and hide, MODULE_API 1.21.0
USER_SAFETY.md step 2 (core), with S11-S14 decided while building it. - Reports: POST /player/reports takes any target type, core's forum types or a module's registered ones. One resolver (model/safety/targets.js) answers "what is this item, for this reader" and 404s an item the reader cannot see. Every report stores content_reports.snapshot; the queue shows a module item from it. - Hide for everyone: POST /admin/moderation/reports/:id/hide hides the item through its own moderation (forum hide as staff, or the module's hide) and actions the report with actioned_with = 'hide'. - Blocks (user_blocks) name an item, never a person, and land on the author's account first (S12); an unlinked game identity is blocked as itself and becomes an account block once linked (S14). Staff cannot be blocked; a block on someone later promoted stops applying (S8). - Hide for me (user_hidden_items), /auth/me/blocks and /auth/me/hidden. - Forum reads filter blocked authors and hidden items on the server and carry canBlock per thread and post. - MODULE_API 1.21.0: api.registerReportTargets, api.registerGameIdentities, ctx.safety.forViewer and ctx.safety.staffAmong, so a module filters its own per-viewer feeds on the server (S11) and marks lines canBlock (S13). - Web: Hide for me / Block author in the forum menus, a Blocked section on both account pages, Hide for everyone in the Reports queue. - Tests, OpenAPI, route/guard/engagement manifests regenerated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01E14m6SuuY6i1vASFeGDBeY
This commit is contained in:
@@ -21,10 +21,11 @@ const express = require('express')
|
||||
const { body, param, query } = require('express-validator')
|
||||
|
||||
const account = require('./account.controller')
|
||||
const safetyCtrl = require('./safety.controller')
|
||||
const { requireAuth } = require('../../../auth/session.middleware')
|
||||
const noindex = require('../../../middleware/noindex')
|
||||
const validate = require('../../../middleware/validate')
|
||||
const { accountChangeLimiter } = require('../../../middleware/rateLimit')
|
||||
const { accountChangeLimiter, makeLimiter } = require('../../../middleware/rateLimit')
|
||||
const { slowLogin, backoffGuard } = require('../../../middleware/loginProtection')
|
||||
|
||||
const meRouter = express.Router()
|
||||
@@ -106,6 +107,124 @@ meRouter.delete(
|
||||
account.deleteAccount,
|
||||
)
|
||||
|
||||
// ── Block and "Hide for me" (USER_SAFETY.md §4.1) ─────────────────────────
|
||||
//
|
||||
// Saved on the account, so they apply on the website and in the app on every
|
||||
// device (S2). A block names an ITEM, never a person: the server reads who wrote
|
||||
// it, so a member can only block someone whose words they could see, and the
|
||||
// block lands on the author's account whenever they have one (S12).
|
||||
|
||||
const safetyLimiter = makeLimiter({
|
||||
windowMs: 10 * 60 * 1000,
|
||||
max: 60,
|
||||
label: 'user-safety',
|
||||
message: 'Too many changes. Please slow down.',
|
||||
})
|
||||
|
||||
meRouter.get(
|
||||
'/blocks',
|
||||
// #swagger.tags = ['Auth · Me']
|
||||
// #swagger.summary = 'List the accounts and players this account has blocked'
|
||||
// #swagger.description = 'Each block is an account block or, for a game identity no account has linked (a Steam id in a chat feed), a game block. A game block whose identity has since been linked is answered — and stored — as an account block (S14). A block on an account that is now staff does not apply, and says so (S8).'
|
||||
// #swagger.security = [{ "cookieAuth": [] }, { "bearerAuth": [] }]
|
||||
/* #swagger.responses[200] = { description: 'The blocks, newest first', content: { "application/json": { schema: { type: 'object', properties: { blocks: { type: 'array', items: { $ref: "#/components/schemas/UserBlock" } } } } } } } */
|
||||
/* #swagger.responses[401] = { description: 'Not authenticated', content: { "application/json": { schema: { $ref: "#/components/schemas/Error" } } } } */
|
||||
safetyCtrl.listBlocks,
|
||||
)
|
||||
|
||||
meRouter.post(
|
||||
'/blocks',
|
||||
// #swagger.tags = ['Auth · Me']
|
||||
// #swagger.summary = 'Block the author of an item'
|
||||
// #swagger.description = 'Names the item (a forum thread or post, or a game module’s item such as a `rust.chat` line), never the person. The block lands on the author’s website account; a chat speaker with no linked account is blocked by their game identity. Everything the blocked author writes then disappears from this account’s view, on the website and in the app. The blocked person is not told (S9). Staff cannot be blocked (S8). An item this account cannot see answers 404.'
|
||||
// #swagger.security = [{ "cookieAuth": [] }, { "bearerAuth": [] }]
|
||||
/* #swagger.requestBody = { required: true, content: { "application/json": { schema: { $ref: "#/components/schemas/SafetyTarget" } } } } */
|
||||
/* #swagger.responses[201] = { description: 'Blocked', content: { "application/json": { schema: { type: 'object', properties: { block: { $ref: "#/components/schemas/UserBlock" } } } } } } */
|
||||
/* #swagger.responses[400] = { description: 'unknown_target, self or no_author', content: { "application/json": { schema: { $ref: "#/components/schemas/SafetyRefusal" } } } } */
|
||||
/* #swagger.responses[401] = { description: 'Not authenticated', content: { "application/json": { schema: { $ref: "#/components/schemas/Error" } } } } */
|
||||
/* #swagger.responses[403] = { description: 'staff — a staff member cannot be blocked', content: { "application/json": { schema: { $ref: "#/components/schemas/SafetyRefusal" } } } } */
|
||||
/* #swagger.responses[404] = { description: 'No such item, or not one this account can see', content: { "application/json": { schema: { $ref: "#/components/schemas/SafetyRefusal" } } } } */
|
||||
/* #swagger.responses[409] = { description: 'already_blocked', content: { "application/json": { schema: { $ref: "#/components/schemas/SafetyRefusal" } } } } */
|
||||
/* #swagger.responses[429] = { description: 'Too many changes (rate limited)', content: { "application/json": { schema: { $ref: "#/components/schemas/Error" } } } } */
|
||||
/* #swagger.responses[503] = { description: 'module_unavailable — the game module did not answer', content: { "application/json": { schema: { $ref: "#/components/schemas/SafetyRefusal" } } } } */
|
||||
safetyLimiter,
|
||||
body('targetType').isString().trim().isLength({ min: 1, max: 32 }),
|
||||
body('targetId').isInt({ min: 1 }).toInt(),
|
||||
validate,
|
||||
safetyCtrl.createBlock,
|
||||
)
|
||||
|
||||
meRouter.delete(
|
||||
'/blocks/:id',
|
||||
// #swagger.tags = ['Auth · Me']
|
||||
// #swagger.summary = 'Undo a block'
|
||||
// #swagger.security = [{ "cookieAuth": [] }, { "bearerAuth": [] }]
|
||||
// #swagger.parameters['id'] = { in: 'path', required: true, schema: { type: 'integer' } }
|
||||
/* #swagger.responses[200] = { description: 'Unblocked', content: { "application/json": { schema: { $ref: "#/components/schemas/OkFlag" } } } } */
|
||||
/* #swagger.responses[401] = { description: 'Not authenticated', content: { "application/json": { schema: { $ref: "#/components/schemas/Error" } } } } */
|
||||
/* #swagger.responses[404] = { description: 'Not one of this account’s blocks', content: { "application/json": { schema: { $ref: "#/components/schemas/SafetyRefusal" } } } } */
|
||||
safetyLimiter,
|
||||
param('id').isInt({ min: 1 }),
|
||||
validate,
|
||||
safetyCtrl.deleteBlock,
|
||||
)
|
||||
|
||||
meRouter.get(
|
||||
'/hidden',
|
||||
// #swagger.tags = ['Auth · Me']
|
||||
// #swagger.summary = 'List the items this account hid for itself'
|
||||
// #swagger.security = [{ "cookieAuth": [] }, { "bearerAuth": [] }]
|
||||
/* #swagger.responses[200] = { description: 'The hidden items, newest first', content: { "application/json": { schema: { type: 'object', properties: { hidden: { type: 'array', items: { type: 'object', properties: { targetType: { type: 'string' }, targetId: { type: 'integer' }, createdAt: { type: 'string', format: 'date-time' } } } } } } } } } */
|
||||
/* #swagger.responses[401] = { description: 'Not authenticated', content: { "application/json": { schema: { $ref: "#/components/schemas/Error" } } } } */
|
||||
safetyCtrl.listHidden,
|
||||
)
|
||||
|
||||
meRouter.post(
|
||||
'/hidden',
|
||||
// #swagger.tags = ['Auth · Me']
|
||||
// #swagger.summary = 'Hide one item for this account'
|
||||
// #swagger.description = 'The item disappears from this account’s own view, on the website and in the app. No report is filed and nothing about the item changes. Hiding twice is still hidden.'
|
||||
// #swagger.security = [{ "cookieAuth": [] }, { "bearerAuth": [] }]
|
||||
/* #swagger.requestBody = { required: true, content: { "application/json": { schema: { $ref: "#/components/schemas/SafetyTarget" } } } } */
|
||||
/* #swagger.responses[200] = { description: 'Hidden', content: { "application/json": { schema: { $ref: "#/components/schemas/OkFlag" } } } } */
|
||||
/* #swagger.responses[400] = { description: 'unknown_target', content: { "application/json": { schema: { $ref: "#/components/schemas/SafetyRefusal" } } } } */
|
||||
/* #swagger.responses[401] = { description: 'Not authenticated', content: { "application/json": { schema: { $ref: "#/components/schemas/Error" } } } } */
|
||||
/* #swagger.responses[429] = { description: 'Too many changes (rate limited)', content: { "application/json": { schema: { $ref: "#/components/schemas/Error" } } } } */
|
||||
safetyLimiter,
|
||||
body('targetType').isString().trim().isLength({ min: 1, max: 32 }),
|
||||
body('targetId').isInt({ min: 1 }).toInt(),
|
||||
validate,
|
||||
safetyCtrl.createHidden,
|
||||
)
|
||||
|
||||
meRouter.delete(
|
||||
'/hidden',
|
||||
// #swagger.tags = ['Auth · Me']
|
||||
// #swagger.summary = 'Show every hidden item again'
|
||||
// #swagger.security = [{ "cookieAuth": [] }, { "bearerAuth": [] }]
|
||||
/* #swagger.responses[200] = { description: 'Cleared', content: { "application/json": { schema: { type: 'object', properties: { ok: { type: 'boolean' }, cleared: { type: 'integer' } } } } } } */
|
||||
/* #swagger.responses[401] = { description: 'Not authenticated', content: { "application/json": { schema: { $ref: "#/components/schemas/Error" } } } } */
|
||||
safetyLimiter,
|
||||
safetyCtrl.clearHidden,
|
||||
)
|
||||
|
||||
meRouter.delete(
|
||||
'/hidden/:targetType/:targetId',
|
||||
// #swagger.tags = ['Auth · Me']
|
||||
// #swagger.summary = 'Show one hidden item again'
|
||||
// #swagger.security = [{ "cookieAuth": [] }, { "bearerAuth": [] }]
|
||||
// #swagger.parameters['targetType'] = { in: 'path', required: true, schema: { type: 'string' }, example: 'rust.chat' }
|
||||
// #swagger.parameters['targetId'] = { in: 'path', required: true, schema: { type: 'integer' } }
|
||||
/* #swagger.responses[200] = { description: 'Shown again', content: { "application/json": { schema: { $ref: "#/components/schemas/OkFlag" } } } } */
|
||||
/* #swagger.responses[400] = { description: 'unknown_target', content: { "application/json": { schema: { $ref: "#/components/schemas/SafetyRefusal" } } } } */
|
||||
/* #swagger.responses[401] = { description: 'Not authenticated', content: { "application/json": { schema: { $ref: "#/components/schemas/Error" } } } } */
|
||||
safetyLimiter,
|
||||
param('targetType').isString().isLength({ min: 1, max: 32 }),
|
||||
param('targetId').isInt({ min: 1 }),
|
||||
validate,
|
||||
safetyCtrl.deleteHidden,
|
||||
)
|
||||
|
||||
// Email address (engagement Phase 1b). The change is STAGED and only a tokened
|
||||
// link installs it, so these three routes never alter the address that is
|
||||
// currently receiving mail. The confirm half is public and lives at
|
||||
|
||||
85
server/src/router/v1/auth/safety.controller.js
Normal file
85
server/src/router/v1/auth/safety.controller.js
Normal file
@@ -0,0 +1,85 @@
|
||||
// /auth/me/blocks and /auth/me/hidden — a member's own blocks and hides
|
||||
// (USER_SAFETY.md §4.1). Self-scoped on req.user.id; requireAuth is the group's.
|
||||
|
||||
const safety = require('../../../model/safety/userSafety.model')
|
||||
const log = require('../../../utils/logger')('safety')
|
||||
|
||||
const fail = (res, err, what) => {
|
||||
if (err && err.status === 503) {
|
||||
return res.status(503).json({ code: 'module_unavailable', message: 'The game module did not answer. Try again in a moment.' })
|
||||
}
|
||||
log.error(`${what} failed`, { message: err.message })
|
||||
return res.status(500).json({ message: 'Internal Server Error' })
|
||||
}
|
||||
|
||||
const refused = (res, result) => res.status(result.status || 400).json({ code: result.code, message: result.error })
|
||||
|
||||
async function listBlocks(req, res) {
|
||||
try {
|
||||
return res.json({ blocks: await safety.listBlocks(req.user) })
|
||||
} catch (err) {
|
||||
return fail(res, err, 'list blocks')
|
||||
}
|
||||
}
|
||||
|
||||
async function createBlock(req, res) {
|
||||
try {
|
||||
const result = await safety.blockAuthorOf({
|
||||
actor: req.user,
|
||||
targetType: req.body.targetType,
|
||||
targetId: req.body.targetId,
|
||||
})
|
||||
if (!result.ok) return refused(res, result)
|
||||
return res.status(201).json({ block: result.block })
|
||||
} catch (err) {
|
||||
return fail(res, err, 'create block')
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteBlock(req, res) {
|
||||
try {
|
||||
const result = await safety.unblock(req.user, Number(req.params.id))
|
||||
if (!result.ok) return refused(res, result)
|
||||
return res.json({ ok: true })
|
||||
} catch (err) {
|
||||
return fail(res, err, 'delete block')
|
||||
}
|
||||
}
|
||||
|
||||
async function listHidden(req, res) {
|
||||
try {
|
||||
return res.json({ hidden: await safety.listHidden(req.user) })
|
||||
} catch (err) {
|
||||
return fail(res, err, 'list hidden')
|
||||
}
|
||||
}
|
||||
|
||||
async function createHidden(req, res) {
|
||||
try {
|
||||
const result = await safety.hide(req.user, req.body.targetType, req.body.targetId)
|
||||
if (!result.ok) return refused(res, result)
|
||||
return res.json({ ok: true })
|
||||
} catch (err) {
|
||||
return fail(res, err, 'hide item')
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteHidden(req, res) {
|
||||
try {
|
||||
const result = await safety.unhide(req.user, req.params.targetType, Number(req.params.targetId))
|
||||
if (!result.ok) return refused(res, result)
|
||||
return res.json({ ok: true })
|
||||
} catch (err) {
|
||||
return fail(res, err, 'unhide item')
|
||||
}
|
||||
}
|
||||
|
||||
async function clearHidden(req, res) {
|
||||
try {
|
||||
return res.json({ ok: true, cleared: await safety.clearHidden(req.user) })
|
||||
} catch (err) {
|
||||
return fail(res, err, 'clear hidden')
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { listBlocks, createBlock, deleteBlock, listHidden, createHidden, deleteHidden, clearHidden }
|
||||
Reference in New Issue
Block a user