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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 | 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 16x 16x 16x 16x 16x 8x 5x 5x 4x 3x 4x 4x 16x 30x 30x 30x 3x 30x 12x 12x 12x 12x 12x 12x 9x 12x 9x 12x 1x 1x 1x 1x 1x 12x 1x 1x 12x 12x 1x 12x 6x 12x 1x 12x 12x 1x 12x 1x 30x 12x 30x 12x 12x 30x 2x 30x 30x 1x 30x 30x 1x 30x 30x 1x 1x 2x 30x 30x 3x 30x 30x 30x 30x 30x 30x 30x 30x 30x 3x 12x 4x 3x 2x 4x | /**
* @implements UC-APIKEY-006.1
*/
const { QueryCommand, DeleteCommand, GetCommand } = require('@aws-sdk/lib-dynamodb')
const { batchDelete } = require('./batchWrite')
const { deleteMembershipRequestsForFlow } = require('./membershipRequestCleanup')
const {
rankConsumerFlows,
deleteAllCountersForFlow,
deleteAllCountersForEntity
} = require('./referenceCounters')
const { transferEntityOwnership } = require('./entityOwnershipTransfer')
const { deleteApiKeysForFlow } = require('./apiKeyCleanup')
const { deleteAllReservationsForEntity } = require('./uniqueFieldValue')
const { deleteItemCount } = require('./entityItemCount')
const { deleteSubscriberCount } = require('./entitySubscriberCount')
const { deleteAllSubscriptionsForEntity } = require('./entitySubscriptions')
const { deleteLikeCount } = require('./itemLikeCount')
const { deleteAllLikesForItem } = require('./itemLikes')
const { deleteAllCommentsForItem, deleteAllCommentBlocksForEntity } = require('./commentCleanup')
const { deleteSearchIndexRows } = require('./directorySearchIndex')
const {
getItemFileKeys,
getBrandingKeys,
entityKeyGuard,
flowKeyGuard,
deleteKeys,
deletePrefix
} = require('./s3Cleanup')
/**
* Computes what deleting a flow would do to the public entities it owns:
* entities with active external links transfer to their top consumer flow
* (ranked by referencing item count, see referenceCounters.js), everything
* else cascades to a real delete. Read-only — used both to preview the impact
* (GET /v1/flows/:flowId/deletion-impact) and to decide the cascade itself.
*
* @param {import('@aws-sdk/lib-dynamodb').DynamoDBDocumentClient} docClient
* @param {{ entitiesTableName: string, entityLinksTableName: string, countersTableName: string, flowId: string }} params
*/
async function computeFlowDeletionImpact(
docClient,
{ entitiesTableName, entityLinksTableName, countersTableName, flowId }
) {
const entitiesResult = await docClient.send(
new QueryCommand({
TableName: entitiesTableName,
IndexName: 'flowIndex',
KeyConditionExpression: 'flowId = :flowId',
ExpressionAttributeValues: { ':flowId': flowId }
})
)
const entities = entitiesResult.Items || []
const entitiesToTransfer = []
const entitiesToDelete = []
for (const entity of entities) {
if (entity.visibility === 'public') {
const ranked = await rankConsumerFlows(docClient, {
entityLinksTableName,
countersTableName,
entityId: entity.id
})
if (ranked.length > 0) {
entitiesToTransfer.push({
entityId: entity.id,
name: entity.name,
targetFlowId: ranked[0].flowId,
otherLinkedFlowIds: ranked.slice(1).map((r) => r.flowId),
linkedFlowsCount: ranked.length
})
continue
}
}
entitiesToDelete.push({ entityId: entity.id, name: entity.name, visibility: entity.visibility })
}
return { entitiesToTransfer, entitiesToDelete }
}
/**
* Executes a flow deletion given a previously computed impact: transfers
* entities with active links, cascades-deletes items/entities/roles/
* memberships/outgoing-links for the rest, then deletes the flow itself.
*
* The deleted flow's own EntityReferenceCountersTable rows are wiped wholesale
* (deleteAllCountersForFlow) rather than decremented item-by-item — once the
* whole flow and all its items are gone, its contribution to every target
* entity's count is unconditionally zero, so precise per-item bookkeeping
* would be wasted work here (contrast with entities/delete.js, which deletes
* a single entity out of a flow that keeps existing, and does need the
* precise version).
*
* @param {import('@aws-sdk/lib-dynamodb').DynamoDBDocumentClient} docClient
* @param {{
* flowsTableName: string, flowSlugsTableName: string, entitiesTableName: string,
* itemsTableName: string, rolesTableName: string, flowMembershipsTableName: string,
* entityLinksTableName: string, countersTableName: string, apiKeysTableName?: string,
* fieldValueIndexTableName?: string,
* entityItemCountTableName?: string, entitySubscriberCountTableName?: string,
* entitySubscriptionsTableName?: string,
* itemLikesTableName?: string, itemLikeCountTableName?: string,
* commentTables?: object, commentBlocksTableName?: string,
* membershipRequestsTableName?: string, flowPagesTableName?: string,
* s3Client?: import('@aws-sdk/client-s3').S3Client, attachmentsBucketName?: string,
* flowId: string, slug: string, impact: { entitiesToTransfer: Array, entitiesToDelete: Array }
* }} params
*/
async function cascadeDeleteFlow(
docClient,
{
flowsTableName,
flowSlugsTableName,
entitiesTableName,
itemsTableName,
rolesTableName,
flowMembershipsTableName,
entityLinksTableName,
countersTableName,
apiKeysTableName,
fieldValueIndexTableName,
entityItemCountTableName,
entitySubscriberCountTableName,
entitySubscriptionsTableName,
itemLikesTableName,
itemLikeCountTableName,
commentTables,
commentBlocksTableName,
membershipRequestsTableName,
flowPagesTableName,
s3Client,
attachmentsBucketName,
flowId,
slug,
impact
}
) {
const { entitiesToTransfer, entitiesToDelete } = impact
// Files of the deleted entities and of the flow itself, removed at the very
// end (see s3Cleanup.js) — skipped entirely when no bucket is configured.
const entityFileCleanups = []
for (const entity of entitiesToTransfer) {
await transferEntityOwnership(docClient, {
entitiesTableName,
entityLinksTableName,
flowsTableName,
fieldValueIndexTableName,
entityId: entity.entityId,
previousFlowId: flowId,
newFlowId: entity.targetFlowId,
otherLinkedFlowIds: entity.otherLinkedFlowIds
})
}
for (const entity of entitiesToDelete) {
// `impact` deliberately carries no field definitions (it is also returned to
// the UI as the deletion preview), so read them here for the item files.
const entityRow = attachmentsBucketName
? (
await docClient.send(
new GetCommand({ TableName: entitiesTableName, Key: { id: entity.entityId } })
)
).Item
: undefined
entityFileCleanups.push({ entityId: entity.entityId, branding: getBrandingKeys(entityRow) })
let lastKey
do {
const itemsResult = await docClient.send(
new QueryCommand({
TableName: itemsTableName,
KeyConditionExpression: 'entityId = :entityId',
ExpressionAttributeValues: { ':entityId': entity.entityId },
ExclusiveStartKey: lastKey
})
)
const items = itemsResult.Items || []
await batchDelete(
docClient,
itemsTableName,
items.map((item) => ({ entityId: item.entityId, id: item.id }))
)
// Exact keys: items created before the entities/{id}/ layout keep their
// files under the older {flowId}/{entityId}/… one.
await deleteKeys(
s3Client,
attachmentsBucketName,
items.flatMap((item) => getItemFileKeys(entityRow?.fields, item)),
entityKeyGuard(entity.entityId)
)
// These items are gone — any likes on them are stale.
if (itemLikesTableName || itemLikeCountTableName) {
for (const item of items) {
Eif (itemLikesTableName) {
await deleteAllLikesForItem(docClient, itemLikesTableName, item.id)
}
Eif (itemLikeCountTableName) {
await deleteLikeCount(docClient, itemLikeCountTableName, item.id)
}
}
}
// ...and so are their comments (with their counters and pending moderation tasks).
if (commentTables) {
for (const item of items) {
await deleteAllCommentsForItem(docClient, commentTables, item.id)
}
}
lastKey = itemsResult.LastEvaluatedKey
} while (lastKey)
// The entity's list of users blocked from commenting goes with it.
if (commentBlocksTableName) {
await deleteAllCommentBlocksForEntity(docClient, commentBlocksTableName, entity.entityId)
}
if (entity.visibility === 'public') {
await deleteAllCountersForEntity(docClient, countersTableName, entity.entityId)
}
// Unlike the counters above, reservations exist regardless of visibility.
if (fieldValueIndexTableName) {
await deleteAllReservationsForEntity(docClient, fieldValueIndexTableName, entity.entityId)
}
Iif (entityItemCountTableName) {
await deleteItemCount(docClient, entityItemCountTableName, entity.entityId)
}
if (entitySubscriberCountTableName) {
await deleteSubscriberCount(docClient, entitySubscriberCountTableName, entity.entityId)
}
// Any user subscriptions to this now-gone entity are stale.
if (entitySubscriptionsTableName) {
await deleteAllSubscriptionsForEntity(
docClient,
entitySubscriptionsTableName,
entity.entityId
)
}
}
await batchDelete(
docClient,
entitiesTableName,
entitiesToDelete.map((e) => ({ id: e.entityId }))
)
for (const { entityId, branding } of entityFileCleanups) {
await deleteKeys(s3Client, attachmentsBucketName, branding, entityKeyGuard(entityId))
await deletePrefix(s3Client, attachmentsBucketName, `entities/${entityId}/`)
}
// The flow's API keys go first: they act with this flow's roles, so they
// must stop working before anything else about the flow disappears.
if (apiKeysTableName) {
await deleteApiKeysForFlow(docClient, { apiKeysTableName, flowId })
}
const rolesResult = await docClient.send(
new QueryCommand({
TableName: rolesTableName,
IndexName: 'flowIndex',
KeyConditionExpression: 'flowId = :flowId',
ExpressionAttributeValues: { ':flowId': flowId }
})
)
await batchDelete(
docClient,
rolesTableName,
(rolesResult.Items || []).map((r) => ({ PK: r.PK, SK: r.SK }))
)
const membershipsResult = await docClient.send(
new QueryCommand({
TableName: flowMembershipsTableName,
IndexName: 'flowIndex',
KeyConditionExpression: 'flowId = :flowId',
ExpressionAttributeValues: { ':flowId': flowId }
})
)
await batchDelete(
docClient,
flowMembershipsTableName,
(membershipsResult.Items || []).map((m) => ({ userId: m.userId, flowId: m.flowId }))
)
// Requests to join it, answers included
await deleteMembershipRequestsForFlow(docClient, membershipRequestsTableName, flowId)
// Its space pages (UC-SPACE-009.1 AC-9); their images go with flows/{flowId}/ below
if (flowPagesTableName) {
const pagesResult = await docClient.send(
new QueryCommand({
TableName: flowPagesTableName,
KeyConditionExpression: 'flowId = :flowId',
ExpressionAttributeValues: { ':flowId': flowId },
ProjectionExpression: 'flowId, slug'
})
)
await batchDelete(
docClient,
flowPagesTableName,
(pagesResult.Items || []).map((page) => ({ flowId: page.flowId, slug: page.slug }))
)
}
// This flow's own outgoing links (it was consuming other flows' public
// entities) and its own reference-counter contributions — previously never
// cleaned up, leaving permanently orphaned bookkeeping behind.
const outgoingLinksResult = await docClient.send(
new QueryCommand({
TableName: entityLinksTableName,
KeyConditionExpression: 'flowId = :flowId',
ExpressionAttributeValues: { ':flowId': flowId }
})
)
await batchDelete(
docClient,
entityLinksTableName,
(outgoingLinksResult.Items || []).map((l) => ({ flowId: l.flowId, entityId: l.entityId }))
)
await deleteAllCountersForFlow(docClient, countersTableName, flowId)
// Free the slug reservation so it can be reused by a future flow.
await docClient.send(
new DeleteCommand({
TableName: flowSlugsTableName,
Key: { slug }
})
)
const flowRow = attachmentsBucketName
? (await docClient.send(new GetCommand({ TableName: flowsTableName, Key: { id: flowId } })))
.Item
: undefined
await docClient.send(
new DeleteCommand({
TableName: flowsTableName,
Key: { id: flowId }
})
)
await deleteKeys(s3Client, attachmentsBucketName, getBrandingKeys(flowRow), flowKeyGuard(flowId))
await deletePrefix(s3Client, attachmentsBucketName, `flows/${flowId}/`)
// The images and files of the requests to join it (UC-SPACE-020.1)
await deletePrefix(s3Client, attachmentsBucketName, `membership-requests/${flowId}/`)
// The flow's own admin-search rows (unrelated to its entities' reservations above).
await deleteSearchIndexRows(docClient, fieldValueIndexTableName, 'FLOW', flowId)
return {
transferred: entitiesToTransfer.map((e) => ({
entityId: e.entityId,
newFlowId: e.targetFlowId
})),
deleted: entitiesToDelete.map((e) => e.entityId)
}
}
/**
* Strips internal-only fields (targetFlowId, otherLinkedFlowIds, visibility —
* needed by cascadeDeleteFlow to execute transfers, not for API consumers) so
* an impact object is safe to return from the deletion-impact endpoint or a
* 409 confirmation response. Deliberately never exposes which flow an entity
* would transfer to — that flow may belong to an account the caller has no
* visibility into.
*
* @param {{ entitiesToTransfer: Array, entitiesToDelete: Array }} impact
*/
function toPublicImpact(impact) {
return {
entitiesToTransfer: impact.entitiesToTransfer.map((entity) => ({
entityId: entity.entityId,
name: entity.name,
linkedFlowsCount: entity.linkedFlowsCount
})),
entitiesToDelete: impact.entitiesToDelete.map((entity) => ({
entityId: entity.entityId,
name: entity.name
}))
}
}
module.exports = { computeFlowDeletionImpact, cascadeDeleteFlow, toPublicImpact }
|