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 152 153 | 7x 7x 7x 7x 7x 7x 7x 7x 25x 25x 8x 6x 8x 2x 7x 7x 2x 5x 5x 4x 5x 5x 7x | /**
* What the two comment subjects (item-comment, pack-comment) share: a comment
* is decided the same way whatever it hangs from, since CommentsTable keys every
* comment by its thread (`itemId`: an item id, or a pack id) and the counter
* follows the same key.
*
* @implements UC-MOD-012.1
*/
const { GetCommand } = require('@aws-sdk/lib-dynamodb')
const { COMMENT_STATUS, approvedParentKey } = require('../../comments')
const { buildCommentCountUpdate } = require('../../commentCount')
const { isE2eTestComment } = require('../../e2eTestUser')
const { NOTIFICATION_TYPE } = require('../../notifications')
const { DECISION, DISCARDED } = require('../constants')
/** Characters of the comment kept in its rejection or removal notification. */
const NOTIFICATION_EXCERPT_CHARS = 200
/** Which outcomes notify the author, and how. An approval needs nothing. */
const NOTIFICATION_TYPE_BY_OUTCOME = {
[DECISION.REJECTED]: NOTIFICATION_TYPE.COMMENT_REJECTED,
[DISCARDED]: NOTIFICATION_TYPE.COMMENT_REMOVED
}
async function getRow(docClient, tableName, key) {
const result = await docClient.send(new GetCommand({ TableName: tableName, Key: key }))
return result.Item
}
/** The notification type of an outcome, or undefined when it notifies nobody. */
function notificationTypeFor(outcome) {
return NOTIFICATION_TYPE_BY_OUTCOME[outcome]
}
/** What a notification keeps of the comment, which may be gone when it is read. */
function commentExcerpt(comment) {
return (comment.body ?? '').slice(0, NOTIFICATION_EXCERPT_CHARS)
}
/**
* Keeps e2e test comments and real ones out of each other's review queue on
* a shared dev deployment: a test account is only ever offered a comment
* bearing the test prefix, and a real account is never offered one that
* does. Optional on the subject contract — the engine treats a subject
* without it as visible to everyone.
*/
function isVisibleToReviewer(context, { reviewerIsTest }) {
return isE2eTestComment(context.comment.body) === reviewerIsTest
}
/**
* The write that deletes a comment that can no longer be reviewed. Only a
* pending comment is affected — not counted, and never replied to (replies
* need an approved parent) — so there is no counter or thread to update. The
* condition makes it lose against a decision or a withdrawal meanwhile.
*/
function buildCommentDiscardWrites(comment) {
return [
{
Delete: {
TableName: process.env.COMMENTS_TABLE_NAME,
Key: { itemId: comment.itemId, id: comment.id },
ConditionExpression: '#status = :pending',
ExpressionAttributeNames: { '#status': 'status' },
ExpressionAttributeValues: { ':pending': COMMENT_STATUS.PENDING }
}
}
]
}
/**
* The writes that apply a decision to the comment, run in the same
* transaction as the task's own update. The `status = pending` condition makes
* a decision apply exactly once: if the comment changed meanwhile (deleted,
* already decided) the whole transaction is refused.
*/
function buildCommentDecisionWrites(comment, decision) {
const commentsTable = process.env.COMMENTS_TABLE_NAME
if (decision === DECISION.REJECTED) {
// Stays in `unapprovedIndex` so its author still sees it, as rejected
return [
{
Update: {
TableName: commentsTable,
Key: { itemId: comment.itemId, id: comment.id },
UpdateExpression: 'SET #status = :rejected',
ConditionExpression: '#status = :pending',
ExpressionAttributeNames: { '#status': 'status' },
ExpressionAttributeValues: {
':rejected': COMMENT_STATUS.REJECTED,
':pending': COMMENT_STATUS.PENDING
}
}
}
]
}
const writes = [
{
Update: {
TableName: commentsTable,
Key: { itemId: comment.itemId, id: comment.id },
// Joins the public lists (approvedIndex) and leaves the author's
// "awaiting review" list (unapprovedIndex)
UpdateExpression:
'SET #status = :approved, approvedParentKey = :approvedKey REMOVE unapprovedKey',
ConditionExpression: '#status = :pending',
ExpressionAttributeNames: { '#status': 'status' },
ExpressionAttributeValues: {
':approved': COMMENT_STATUS.APPROVED,
':pending': COMMENT_STATUS.PENDING,
':approvedKey': approvedParentKey(comment.itemId, comment.parentId)
}
}
}
]
if (comment.parentId) {
writes.push({
Update: {
TableName: commentsTable,
Key: { itemId: comment.itemId, id: comment.parentId },
UpdateExpression: 'ADD replyCount :one',
// A parent deleted meanwhile is still shown as a tombstone, so it
// still hosts its replies
ConditionExpression: '#status IN (:approved, :deleted)',
ExpressionAttributeNames: { '#status': 'status' },
ExpressionAttributeValues: {
':one': 1,
':approved': COMMENT_STATUS.APPROVED,
':deleted': COMMENT_STATUS.DELETED
}
}
})
}
// The counter must be last: see isCountFloorCancellation (only decrements
// carry a floor, but the convention keeps the transaction predictable)
writes.push(buildCommentCountUpdate(process.env.ITEM_COMMENT_COUNT_TABLE_NAME, comment.itemId, 1))
return writes
}
module.exports = {
getRow,
notificationTypeFor,
commentExcerpt,
isVisibleToReviewer,
buildCommentDiscardWrites,
buildCommentDecisionWrites
}
|