All files / moderation tasks.js

100% Statements 24/24
100% Branches 4/4
100% Functions 2/2
100% Lines 23/23

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                                1x 1x 1x 1x 1x 1x 1x   1x 1x   1x 13x 13x 13x 12x   12x 12x             12x     12x   12x         11x 3x                 1x 1x      
/**
 * GET /v1/moderation/tasks
 *
 * A few pending contents for the caller to review, oldest first, together with
 * their quota progress. Never the caller's own content, never something they
 * already voted on, and never real content for an e2e test account or test
 * content for a real one (see subjects/itemComment.js's isVisibleToReviewer).
 * Bounded: see listReviewableTasks — a fixed number of Queries over the
 * sharded review queue, never a scan.
 *
 * Query params:
 * - limit: tasks wanted (default and max 5)
 *
 * @implements UC-MOD-002.1
 */
 
const { DynamoDBClient } = require('@aws-sdk/client-dynamodb')
const { DynamoDBDocumentClient, GetCommand } = require('@aws-sdk/lib-dynamodb')
const { authenticateModerator, errorResponse, jsonResponse } = require('../utils/moderation/access')
const { listReviewableTasks, getStats } = require('../utils/moderation/engine')
const { MAX_TASKS_PER_REQUEST } = require('../utils/moderation/constants')
const itemComment = require('../utils/moderation/subjects/itemComment')
const { isE2eTestEmail } = require('../utils/e2eTestUser')
 
const client = new DynamoDBClient({})
const docClient = DynamoDBDocumentClient.from(client)
 
exports.handler = async (event) => {
  try {
    const caller = await authenticateModerator(event)
    if (caller.response) return caller.response
    const { auth } = caller
 
    const rawLimit = parseInt(event.queryStringParameters?.limit, 10)
    const limit = Number.isFinite(rawLimit)
      ? Math.min(Math.max(rawLimit, 1), MAX_TASKS_PER_REQUEST)
      : MAX_TASKS_PER_REQUEST
 
    // Whether the queue should hand this caller test content instead of real
    // content (see subjects/itemComment.js's isVisibleToReviewer) — one bounded
    // read by primary key, not a scan.
    const userResult = await docClient.send(
      new GetCommand({ TableName: process.env.USERS_TABLE_NAME, Key: { id: auth.userId } })
    )
    const reviewerIsTest = isE2eTestEmail(userResult.Item?.email)
 
    const [found, stats] = await Promise.all([
      listReviewableTasks(docClient, auth.userId, { limit, reviewerIsTest }),
      getStats(docClient, auth.userId)
    ])
 
    return jsonResponse(200, {
      tasks: found.map(({ task, view }) => ({
        taskId: task.taskId,
        subjectType: task.subjectType,
        createdAt: task.createdAt,
        view
      })),
      quota: { required: itemComment.quotaCost, have: stats.reviewPoints }
    })
  } catch (error) {
    console.error('[moderation/tasks] Exception:', error.message, error.stack)
    return errorResponse(500, 'Failed to list moderation tasks')
  }
}