All files / utils adminReportViews.js

100% Statements 34/34
100% Branches 36/36
100% Functions 7/7
100% Lines 29/29

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                2x   2x 2x         2x                   9x 9x   2x   7x   13x 7x 8x                     24x 24x 24x 24x 2x   22x 22x 5x 5x 4x     18x                 16x 12x 12x                   8x 8x 2x             2x                
/**
 * What the platform admins' Reports and Appeals tabs show of users and lists
 * (admin/list-content-reports.js, admin/list-appeals.js): a user's summary and
 * standing, and the paging of a `listIndex` GSI.
 *
 * @implements UC-REPORT-004.1, UC-REPORT-010.1, UC-REPORT-011.1
 */
 
const { activeStrikes } = require('./contentReports')
 
const DEFAULT_LIMIT = 25
const MAX_LIMIT = 100
 
class InvalidParamsError extends Error {}
 
function encodeCursor(key) {
  return Buffer.from(JSON.stringify(key)).toString('base64url')
}
 
/**
 * A cursor is client-controlled: it must be a `listIndex` key of the list
 * asked for, made of `keyNames` strings, never one that points the Query
 * anywhere else.
 */
function decodeCursor(cursor, listKey, keyNames) {
  let key
  try {
    key = JSON.parse(Buffer.from(cursor, 'base64url').toString('utf8'))
  } catch {
    throw new InvalidParamsError('Invalid cursor')
  }
  const names = ['listKey', 'createdAt', ...keyNames]
  const valid =
    key && key.listKey === listKey && names.every((name) => typeof key[name] === 'string')
  if (!valid) throw new InvalidParamsError('Invalid cursor')
  return Object.fromEntries(names.map((name) => [name, key[name]]))
}
 
/**
 * `?status=&limit=&cursor=` of an admin list.
 *
 * @param {object} event
 * @param {Record<string, string>} listKeys the `listIndex` partition of each status
 * @param {string[]} keyNames the table's own key attributes, part of a cursor
 */
function parseListParams(event, listKeys, keyNames) {
  const params = event.queryStringParameters || {}
  const status = params.status ?? 'open'
  const listKey = listKeys[status]
  if (!listKey) {
    throw new InvalidParamsError(`status must be one of: ${Object.keys(listKeys).join(', ')}`)
  }
  let limit = DEFAULT_LIMIT
  if (params.limit !== undefined) {
    limit = Number(params.limit)
    if (!Number.isInteger(limit) || limit < 1 || limit > MAX_LIMIT) {
      throw new InvalidParamsError(`limit must be an integer between 1 and ${MAX_LIMIT}`)
    }
  }
  return {
    listKey,
    limit,
    cursor: params.cursor ? decodeCursor(params.cursor, listKey, keyNames) : undefined
  }
}
 
/** Name and email, or a deleted account: only its id is left. */
function toUserSummary(userId, users) {
  if (!userId) return null
  const user = users.get(userId)
  return {
    id: userId,
    name: user?.name || null,
    nickname: user?.nickname || null,
    email: user?.email || null
  }
}
 
/** Where a reported user stands: what the admin weighs a sanction against. */
function toStanding(userId, users) {
  const user = userId && users.get(userId)
  if (!user) return null
  return {
    activeStrikes: activeStrikes(user.strikes).length,
    restrictedUntil: user.restrictedUntil || null,
    bannedAt: user.bannedAt || null
  }
}
 
module.exports = {
  InvalidParamsError,
  encodeCursor,
  decodeCursor,
  parseListParams,
  toUserSummary,
  toStanding
}