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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 30x 30x 31x 30x 30x 29x 16x 6x 6x 6x 2x 6x 6x 6x 6x 5x 5x 3x 6x 5x 5x 3x 4x 4x 4x 4x 12x 1x 1x 33x 1x 1x 34x 2x 2x 3x 2x 3x 3x 2x 14x 10x 10x 9x 9x 5x 9x 5x 1x 5x | /**
* Deleting packs, whichever way it comes: from the Store, with their flow, or
* with their author's account. Installed copies are independent and never touched.
*
* S3 goes last and best-effort (see s3Cleanup.js). Like rows, installing flows,
* comments and comment blocks are unbounded, so they are removed off a queue
* (store/cleanup-worker.js) rather than within the request.
*
* @implements UC-STORE-007.1, UC-STORE-006.1, UC-STORE-009.1
*/
const { QueryCommand, DeleteCommand, UpdateCommand } = require('@aws-sdk/lib-dynamodb')
const { SQSClient, SendMessageCommand } = require('@aws-sdk/client-sqs')
const { batchDelete } = require('./batchWrite')
const { deletePrefix, deleteKeys } = require('./s3Cleanup')
const { withCountFloor } = require('./counterFloor')
const { packsTable, versionsTable, namesTable, statsTable, likesTable } = require('./packs')
const { s3Client } = require('./packSnapshot')
const { deleteAllCommentsForItem, deleteAllCommentBlocksForEntity } = require('./commentCleanup')
const { SUBJECT_TYPE } = require('./moderation/constants')
const sqsClient = new SQSClient({})
async function queryAll(docClient, params) {
const rows = []
let ExclusiveStartKey
do {
const result = await docClient.send(new QueryCommand({ ...params, ExclusiveStartKey }))
rows.push(...(result.Items || []))
ExclusiveStartKey = result.LastEvaluatedKey
} while (ExclusiveStartKey)
return rows
}
function listPackVersions(docClient, packId) {
return queryAll(docClient, {
TableName: versionsTable(),
KeyConditionExpression: 'packId = :packId',
ExpressionAttributeValues: { ':packId': packId }
})
}
/**
* Deletes a pack and everything that hangs off it. The pack row goes first, so
* that it stops being listed, opened and installed whatever happens next.
*/
async function deletePackData(docClient, pack) {
await docClient.send(new DeleteCommand({ TableName: packsTable(), Key: { id: pack.id } }))
const versions = await listPackVersions(docClient, pack.id)
await batchDelete(
docClient,
versionsTable(),
versions.map((version) => ({ packId: pack.id, version: version.version }))
)
await docClient.send(
new DeleteCommand({ TableName: namesTable(), Key: { nameKey: pack.nameKey } })
)
await docClient.send(new DeleteCommand({ TableName: statsTable(), Key: { packId: pack.id } }))
await sqsClient.send(
new SendMessageCommand({
QueueUrl: process.env.PACK_CLEANUP_QUEUE_URL,
MessageBody: JSON.stringify({ packId: pack.id })
})
)
await deletePrefix(s3Client, process.env.ATTACHMENTS_BUCKET_NAME, `packs/${pack.id}/`)
}
/** Every like row of a deleted pack: the queue's job, as they are unbounded. */
async function deleteAllLikesForPack(docClient, packId) {
const likes = await queryAll(docClient, {
TableName: likesTable(),
KeyConditionExpression: 'packId = :packId',
ExpressionAttributeValues: { ':packId': packId },
ProjectionExpression: 'packId, userId'
})
await batchDelete(
docClient,
likesTable(),
likes.map((like) => ({ packId: like.packId, userId: like.userId }))
)
}
/** The flows that installed a deleted pack: unbounded, the queue's job too. */
async function deleteAllImportsForPack(docClient, packId) {
if (!process.env.PACK_IMPORTS_TABLE_NAME) return
const imports = await queryAll(docClient, {
TableName: process.env.PACK_IMPORTS_TABLE_NAME,
KeyConditionExpression: 'packId = :packId',
ExpressionAttributeValues: { ':packId': packId },
ProjectionExpression: 'packId, flowId'
})
await batchDelete(
docClient,
process.env.PACK_IMPORTS_TABLE_NAME,
imports.map((row) => ({ packId: row.packId, flowId: row.flowId }))
)
}
/**
* Every comment of a deleted pack, its counter, the tasks of those still
* awaiting review and its block list: the queue's job too. Comments are
* partitioned under the pack's id, like an item's under the item's.
*/
async function deleteAllCommentsForPack(docClient, packId) {
Iif (!process.env.COMMENTS_TABLE_NAME) return
await deleteAllCommentsForItem(
docClient,
{
commentsTableName: process.env.COMMENTS_TABLE_NAME,
commentCountTableName: process.env.ITEM_COMMENT_COUNT_TABLE_NAME,
moderationTasksTableName: process.env.MODERATION_TASKS_TABLE_NAME
},
packId,
SUBJECT_TYPE.PACK_COMMENT
)
Eif (process.env.COMMENT_BLOCKS_TABLE_NAME) {
await deleteAllCommentBlocksForEntity(docClient, process.env.COMMENT_BLOCKS_TABLE_NAME, packId)
}
}
async function deleteAllPacksForFlow(docClient, flowId) {
if (!packsTable()) return
const packs = await queryAll(docClient, {
TableName: packsTable(),
IndexName: 'flowIndex',
KeyConditionExpression: 'flowId = :flowId',
ExpressionAttributeValues: { ':flowId': flowId }
})
for (const pack of packs) await deletePackData(docClient, pack)
}
async function deleteAllPacksForAuthor(docClient, authorId) {
if (!packsTable()) return
const packs = await queryAll(docClient, {
TableName: packsTable(),
IndexName: 'authorIndex',
KeyConditionExpression: 'authorId = :authorId',
ExpressionAttributeValues: { ':authorId': authorId }
})
for (const pack of packs) await deletePackData(docClient, pack)
}
/**
* Removes a deleted account's likes. The packs stay, so their counters go down,
* best-effort like the rest of the account cascade.
*/
async function deleteAllPackLikesForUser(docClient, userId) {
if (!likesTable()) return
const likes = await queryAll(docClient, {
TableName: likesTable(),
IndexName: 'userIndex',
KeyConditionExpression: 'userId = :userId',
ExpressionAttributeValues: { ':userId': userId }
})
await batchDelete(
docClient,
likesTable(),
likes.map((like) => ({ packId: like.packId, userId: like.userId }))
)
for (const like of likes) {
try {
await docClient.send(new UpdateCommand(buildPackLikesCountUpdate(like.packId, -1)))
} catch (error) {
// Already at its floor, or the pack is gone: nothing to decrement
if (error.name !== 'ConditionalCheckFailedException') throw error
}
}
}
/**
* The `Update` adjusting a pack's like count. A decrement never takes it below
* 0, and fails when there is no row: a deleted pack's counter is not recreated.
*/
function buildPackLikesCountUpdate(packId, delta) {
return withCountFloor(
{
TableName: statsTable(),
Key: { packId },
UpdateExpression: 'ADD #count :delta',
ExpressionAttributeNames: { '#count': 'likesCount' },
ExpressionAttributeValues: { ':delta': delta }
},
delta
)
}
/**
* Best-effort, once a publication has committed: every snapshot of the pack but
* the previous one and the new one, which bounds what a pack stores. An import
* queued for a removed snapshot fails with an explicit message
* (entities/import-worker.js).
*/
async function removeStaleSnapshots(docClient, { packId, keepVersions }) {
try {
const versions = await listPackVersions(docClient, packId)
// Each stale version's snapshot, and the copies of its items' images and files
const stale = versions
.filter((version) => !keepVersions.includes(version.version))
.flatMap((version) => [version.snapshotKey, ...(version.fileKeys || [])])
.filter(Boolean)
await deleteKeys(s3Client, process.env.ATTACHMENTS_BUCKET_NAME, stale, (key) =>
key.startsWith(`packs/${packId}/`)
)
} catch (error) {
console.error('[packCleanup] Could not remove stale snapshots:', packId, error.message)
}
}
module.exports = {
listPackVersions,
deletePackData,
deleteAllLikesForPack,
deleteAllImportsForPack,
deleteAllCommentsForPack,
deleteAllPacksForFlow,
deleteAllPacksForAuthor,
deleteAllPackLikesForUser,
buildPackLikesCountUpdate,
removeStaleSnapshots
}
|