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 | 6x 6x 6x 153x 144x 141x 141x 4x 137x 5x 150x 148x 148x 6x | /**
* Sanctions a platform admin puts on an account when acting on a report
* (docs/architecture/MODERATION.md "Sanctions"), stored on its UsersTable row:
* - `restrictedUntil` (ISO date): until then, the user may not comment, send
* direct messages or publish to the Store — reading stays open
* - `bannedAt` (ISO date): the account is disabled in Cognito, so they cannot
* sign in. Checked here too: an access token issued before the ban stays
* valid until it expires, and must not post meanwhile.
*
* @implements UC-REPORT-007.1, UC-REPORT-008.1
*/
const { GetCommand } = require('@aws-sdk/lib-dynamodb')
const ACCOUNT_RESTRICTED_ERROR = 'ACCOUNT_RESTRICTED'
const corsHeaders = {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
}
/**
* Whether `user` (a UsersTable row) may not post at `now`, and until when:
* `null` for a ban, which has no end.
*
* @returns {{ blocked: false } | { blocked: true, banned: boolean, until: string | null }}
*/
function postingBlock(user, now = Date.now()) {
if (!user) return { blocked: false }
if (user.bannedAt) return { blocked: true, banned: true, until: null }
const until = Date.parse(user.restrictedUntil)
if (Number.isFinite(until) && until > now) {
return { blocked: true, banned: false, until: user.restrictedUntil }
}
return { blocked: false }
}
/** The 403 a restricted or banned user gets when they try to post. */
function restrictedResponse(block) {
return {
statusCode: 403,
headers: corsHeaders,
body: JSON.stringify({ error: ACCOUNT_RESTRICTED_ERROR, restrictedUntil: block.until })
}
}
/**
* Loads `userId`'s row and answers the 403 to return when they may not post,
* or null when they may. Needs `USERS_TABLE_NAME` and read access to it.
*/
async function checkCanPost(docClient, userId) {
const result = await docClient.send(
new GetCommand({ TableName: process.env.USERS_TABLE_NAME, Key: { id: userId } })
)
const block = postingBlock(result.Item)
return block.blocked ? restrictedResponse(block) : null
}
module.exports = { ACCOUNT_RESTRICTED_ERROR, postingBlock, restrictedResponse, checkCanPost }
|