All files / utils/moderation/subjects itemComment.js

100% Statements 35/35
100% Branches 33/33
100% Functions 10/10
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 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                        7x 7x 7x               7x                   13x 13x 13x 20x   13x 13x 18x   13x     7x                       15x               8x 8x   6x       6x   5x             5x   2x                 6x                           13x                                               5x 5x 4x 4x                         1x     5x    
/**
 * Moderation subject: a comment on an item of a public entity.
 *
 * The engine (utils/moderation/engine.js) knows nothing about comments; this
 * file is everything it needs to know about them. Adding another kind of
 * moderated content means writing a file like this one and registering it in
 * subjects/index.js — see subjects/packComment.js for the second one, and
 * docs/architecture/MODERATION.md "Generic framework".
 *
 * @implements UC-MOD-012.1
 */
 
const { COMMENT_STATUS } = require('../../comments')
const { SUBJECT_TYPE } = require('../constants')
const { pickItemLabel } = require('../../itemLabel')
const {
  getRow,
  isVisibleToReviewer,
  notificationTypeFor,
  commentExcerpt,
  buildCommentDiscardWrites,
  buildCommentDecisionWrites
} = require('./commentWrites')
 
/**
 * The item as its entity's default form shows it (the first form, the one
 * item cards use), so a reviewer can tell whether the comment is on topic.
 * Only the fields that form displays are sent, never the whole item: a
 * reviewer gets no more of it than a card would show. Without any form, every
 * field is shown, as on a card.
 */
function buildItemCard(entity, item) {
  const layout = entity.forms?.[0]?.fields ?? []
  const shownIds = new Set(layout.map((placement) => placement.fieldId))
  const fields = (entity.fields || []).filter(
    (field) => layout.length === 0 || shownIds.has(field.fieldId)
  )
  const values = {}
  for (const field of fields) {
    if (item[field.fieldId] !== undefined) values[field.fieldId] = item[field.fieldId]
  }
  return { fields, layout, values }
}
 
module.exports = {
  type: SUBJECT_TYPE.ITEM_COMMENT,
 
  /** Three concordant votes out of at most five always decide: no ties, no dispute queue. */
  requiredVotes: 3,
  maxVotes: 5,
 
  /** Reviews a user must have done to submit one comment. */
  quotaCost: 3,
 
  /** What is stored on the task so the subject can be found again cheaply. */
  buildSubjectRef({ entityId, itemId }) {
    return { entityId, itemId }
  },
 
  /**
   * Everything needed to show and decide the comment, or null when it is gone
   * (comment, item, entity or parent deleted): the engine then discards the task.
   */
  async loadContext(docClient, task) {
    const { itemId, entityId } = task.subjectRef || {}
    if (!itemId || !entityId) return null
 
    const comment = await getRow(docClient, process.env.COMMENTS_TABLE_NAME, {
      itemId,
      id: task.subjectId
    })
    if (!comment) return null
 
    const [entity, item, parent] = await Promise.all([
      getRow(docClient, process.env.ENTITIES_TABLE_NAME, { id: entityId }),
      getRow(docClient, process.env.ITEMS_TABLE_NAME, { entityId, id: itemId }),
      comment.parentId
        ? getRow(docClient, process.env.COMMENTS_TABLE_NAME, { itemId, id: comment.parentId })
        : Promise.resolve(null)
    ])
    if (!entity || !item || (comment.parentId && !parent)) return null
 
    return { comment, entity, item, parent }
  },
 
  /**
   * Whether reviewers should still be shown the comment. A comment of an entity
   * that is no longer public, or no longer accepts comments, is discarded:
   * deleted, and its author notified (see buildDiscardWrites).
   */
  isReviewable(context) {
    return (
      context.comment.status === COMMENT_STATUS.PENDING &&
      context.entity.visibility === 'public' &&
      context.entity.commentsEnabled === true
    )
  },
 
  isVisibleToReviewer,
 
  /**
   * What the reviewer sees. The author is deliberately not shown: the comment
   * is judged on its own.
   */
  describe(context) {
    return {
      comment: {
        id: context.comment.id,
        body: context.comment.body,
        createdAt: context.comment.createdAt
      },
      parent: context.parent ? { id: context.parent.id, body: context.parent.body ?? null } : null,
      entity: { id: context.entity.id, name: context.entity.name },
      item: {
        id: context.item.id,
        label: pickItemLabel(context.entity, context.item),
        card: buildItemCard(context.entity, context.item)
      }
    }
  },
 
  /**
   * A rejection, or a removal because the entity no longer takes public
   * comments (DISCARDED), is worth telling the author about: an approved
   * comment simply shows up. The notification carries a snapshot of what the
   * author needs to recognize their comment, since it may be read long after
   * — and, for a removal, after the comment is gone.
   */
  buildAuthorNotification(task, outcome, context) {
    const type = notificationTypeFor(outcome)
    if (!type) return null
    const { comment, entity, item } = context
    return {
      type,
      data: {
        commentId: comment.id,
        commentExcerpt: commentExcerpt(comment),
        entityId: entity.id,
        entityName: entity.name,
        itemId: item.id,
        itemLabel: pickItemLabel(entity, item)
      }
    }
  },
 
  buildDiscardWrites: (task, context) => buildCommentDiscardWrites(context.comment),
 
  buildDecisionWrites: (task, decision, context) =>
    buildCommentDecisionWrites(context.comment, decision)
}