All files / utils/moderation/subjects packComment.js

100% Statements 26/26
96.29% Branches 26/27
100% Functions 7/7
100% Lines 22/22

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                  5x 5x 5x               5x   5x                 5x               6x 6x   5x       5x   4x                 4x   2x               3x                       3x 3x                             3x 3x 2x 2x                     1x     2x    
/**
 * Moderation subject: a comment on a store pack. Packs are public by nature, so
 * every comment on one is reviewed by the community first, as on a public
 * entity. The comment is stored in CommentsTable under the pack's id, which is
 * why its decision is the same as an item comment's (commentWrites.js).
 *
 * @implements UC-STORE-009.1, UC-MOD-012.1
 */
 
const { COMMENT_STATUS } = require('../../comments')
const { getPack } = require('../../packs')
const { SUBJECT_TYPE } = require('../constants')
const {
  getRow,
  isVisibleToReviewer,
  notificationTypeFor,
  commentExcerpt,
  buildCommentDiscardWrites,
  buildCommentDecisionWrites
} = require('./commentWrites')
 
module.exports = {
  type: SUBJECT_TYPE.PACK_COMMENT,
 
  // The same consensus and quota as an item comment: see itemComment.js
  requiredVotes: 3,
  maxVotes: 5,
  quotaCost: 3,
 
  buildSubjectRef({ packId }) {
    return { packId }
  },
 
  /**
   * The comment, its pack and the comment it replies to, or null when one is
   * gone: the engine then discards the task.
   */
  async loadContext(docClient, task) {
    const { packId } = task.subjectRef || {}
    if (!packId) return null
 
    const comment = await getRow(docClient, process.env.COMMENTS_TABLE_NAME, {
      itemId: packId,
      id: task.subjectId
    })
    if (!comment) return null
 
    const [pack, parent] = await Promise.all([
      getPack(docClient, packId),
      comment.parentId
        ? getRow(docClient, process.env.COMMENTS_TABLE_NAME, {
            itemId: packId,
            id: comment.parentId
          })
        : Promise.resolve(null)
    ])
    if (!pack || (comment.parentId && !parent)) return null
 
    return { comment, pack, parent }
  },
 
  /**
   * A comment of a pack whose managers switched comments off is discarded:
   * deleted, and its author notified.
   */
  isReviewable(context) {
    return (
      context.comment.status === COMMENT_STATUS.PENDING && context.pack.commentsEnabled === true
    )
  },
 
  isVisibleToReviewer,
 
  /**
   * What the reviewer sees: the pack as the Store's card shows it and the
   * version the comment was written on, never the comment's author.
   */
  describe(context) {
    const { comment, pack, parent } = context
    return {
      comment: { id: comment.id, body: comment.body, createdAt: comment.createdAt },
      parent: parent ? { id: parent.id, body: parent.body ?? null } : null,
      pack: {
        id: pack.id,
        name: pack.name,
        description: pack.description ?? '',
        version: comment.packVersion ?? pack.currentVersion,
        entities: pack.entitiesPreview ?? []
      }
    }
  },
 
  /** As for an item comment: a rejection or a removal is told, an approval shows. */
  buildAuthorNotification(task, outcome, context) {
    const type = notificationTypeFor(outcome)
    if (!type) return null
    const { comment, pack } = context
    return {
      type,
      data: {
        commentId: comment.id,
        commentExcerpt: commentExcerpt(comment),
        packId: pack.id,
        packName: pack.name
      }
    }
  },
 
  buildDiscardWrites: (task, context) => buildCommentDiscardWrites(context.comment),
 
  buildDecisionWrites: (task, decision, context) =>
    buildCommentDecisionWrites(context.comment, decision)
}