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 | 13x 13x 13x 13x 15x 3x 3x 3x 2x 31x 19x 20x 19x 19x 19x 19x 20x 19x 4x 19x 18x 18x 31x 31x 31x 1x 31x 1x 31x 31x 31x 1x 31x 83x 83x 83x 56x 56x 56x 62x 56x 58x 56x 13x | const {
UpdateCommand,
DeleteCommand,
BatchGetCommand,
QueryCommand
} = require('@aws-sdk/lib-dynamodb')
const { batchDelete } = require('./batchWrite')
const { withCountFloor } = require('./counterFloor')
// DynamoDB BatchGetItem accepts at most 100 keys per table per request.
const BATCH_GET_LIMIT = 100
/**
* A TransactWriteItems `Update` action adjusting a user's follower count by
* `delta`. Auto-vivifies the row on first ADD, mirrors
* entitySubscriberCount.js's buildSubscriberCountUpdate. A decrement carries
* a floor condition so the count never goes below 0; when it fails the
* transaction is cancelled and the caller must retry without the count update
* (see isCountFloorCancellation in counterFloor.js).
*
* @param {string} tableName
* @param {string} followeeId
* @param {number} delta
*/
function buildFollowerCountUpdate(tableName, followeeId, delta) {
return {
Update: withCountFloor(
{
TableName: tableName,
Key: { userId: followeeId },
UpdateExpression: 'ADD #count :delta',
ExpressionAttributeNames: { '#count': 'count' },
ExpressionAttributeValues: { ':delta': delta }
},
delta
)
}
}
/**
* Non-transactional +/- adjustment, for best-effort call sites that aren't
* already inside a TransactWriteCommand (account-deletion cascade).
*
* @param {import('@aws-sdk/lib-dynamodb').DynamoDBDocumentClient} docClient
* @param {string} tableName
* @param {string} followeeId
* @param {number} delta
*/
async function adjustFollowerCount(docClient, tableName, followeeId, delta) {
const { Update: update } = buildFollowerCountUpdate(tableName, followeeId, delta)
try {
await docClient.send(new UpdateCommand(update))
} catch (error) {
// Decrement below 0 is refused: the count is already at its floor.
if (error.name !== 'ConditionalCheckFailedException') throw error
}
}
/**
* Deletes a user's follower-count row entirely (their account is deleted, so
* "how many people follow them" is meaningless from then on).
*
* @param {import('@aws-sdk/lib-dynamodb').DynamoDBDocumentClient} docClient
* @param {string} tableName
* @param {string} userId
*/
async function deleteFollowerCount(docClient, tableName, userId) {
await docClient.send(
new DeleteCommand({
TableName: tableName,
Key: { userId }
})
)
}
/**
* Batch-reads follower counts for many users at once. Users with no row yet
* (never followed) default to 0. Mirrors entitySubscriberCount.js's
* getSubscriberCounts.
*
* @param {import('@aws-sdk/lib-dynamodb').DynamoDBDocumentClient} docClient
* @param {string} tableName
* @param {string[]} userIds
* @returns {Promise<Map<string, number>>}
*/
async function getFollowerCounts(docClient, tableName, userIds) {
const uniqueIds = Array.from(new Set(userIds)).filter(Boolean)
const counts = new Map(uniqueIds.map((userId) => [userId, 0]))
Iif (uniqueIds.length === 0) return counts
for (let offset = 0; offset < uniqueIds.length; offset += BATCH_GET_LIMIT) {
const chunk = uniqueIds.slice(offset, offset + BATCH_GET_LIMIT)
const result = await docClient.send(
new BatchGetCommand({
RequestItems: {
[tableName]: { Keys: chunk.map((userId) => ({ userId })) }
}
})
)
for (const item of result.Responses?.[tableName] || []) {
// Floor at 0: never display a negative count from historical desyncs.
counts.set(item.userId, Math.max(0, item.count || 0))
}
}
return counts
}
/**
* Following counts are not pre-aggregated (see storage-stack.ts comment on
* UserFollowerCountTable for why) — computed on read via a COUNT-only Query
* against the base table, which never transfers the actual follow rows.
*
* @param {import('@aws-sdk/lib-dynamodb').DynamoDBDocumentClient} docClient
* @param {string} tableName
* @param {string} followerId
* @returns {Promise<number>}
*/
async function getFollowingCount(docClient, tableName, followerId) {
const result = await docClient.send(
new QueryCommand({
TableName: tableName,
KeyConditionExpression: 'followerId = :followerId',
ExpressionAttributeValues: { ':followerId': followerId },
Select: 'COUNT'
})
)
return result.Count || 0
}
/**
* Deletes every follow relationship involving a user whose account is being
* deleted, in both directions:
* - rows where this user is the follower (their outgoing follows) — decrement
* each followee's follower count, since those followees lose a follower.
* - rows where this user is the followee (other users' follows of them),
* found via followeeIndex — no counter adjustment needed for the
* followers' own following-counts, since those are computed on read, not
* stored.
* Finally drops this user's own follower-count row.
*
* @param {import('@aws-sdk/lib-dynamodb').DynamoDBDocumentClient} docClient
* @param {string} followsTableName
* @param {string} followerCountTableName
* @param {string} userId
*/
async function deleteAllFollowsForUser(
docClient,
followsTableName,
followerCountTableName,
userId
) {
const outgoing = await docClient.send(
new QueryCommand({
TableName: followsTableName,
KeyConditionExpression: 'followerId = :userId',
ExpressionAttributeValues: { ':userId': userId }
})
)
const outgoingFollows = outgoing.Items || []
await batchDelete(
docClient,
followsTableName,
outgoingFollows.map((f) => ({ followerId: f.followerId, followeeId: f.followeeId }))
)
await Promise.all(
outgoingFollows.map((f) =>
adjustFollowerCount(docClient, followerCountTableName, f.followeeId, -1)
)
)
const incoming = await docClient.send(
new QueryCommand({
TableName: followsTableName,
IndexName: 'followeeIndex',
KeyConditionExpression: 'followeeId = :userId',
ExpressionAttributeValues: { ':userId': userId }
})
)
const incomingFollows = incoming.Items || []
await batchDelete(
docClient,
followsTableName,
incomingFollows.map((f) => ({ followerId: f.followerId, followeeId: f.followeeId }))
)
await deleteFollowerCount(docClient, followerCountTableName, userId)
}
/**
* Batch-resolves user ids to the same PublicUser projection users/get.js and
* users/list.js expose (no email), preserving no particular order — callers
* that need an order (most-recent-first) should sort by the source rows'
* followedAt before calling this. Ids with no matching row (deleted user) are
* silently dropped.
*
* @param {import('@aws-sdk/lib-dynamodb').DynamoDBDocumentClient} docClient
* @param {string} usersTableName
* @param {string[]} userIds
* @returns {Promise<Map<string, {id: string, nickname: string|null, name: string, status: string, profilePicture: string|null}>>}
*/
async function resolvePublicUsers(docClient, usersTableName, userIds) {
const uniqueIds = Array.from(new Set(userIds)).filter(Boolean)
const usersById = new Map()
if (uniqueIds.length === 0) return usersById
for (let offset = 0; offset < uniqueIds.length; offset += BATCH_GET_LIMIT) {
const chunk = uniqueIds.slice(offset, offset + BATCH_GET_LIMIT)
const result = await docClient.send(
new BatchGetCommand({
RequestItems: {
[usersTableName]: { Keys: chunk.map((id) => ({ id })) }
}
})
)
for (const user of result.Responses?.[usersTableName] || []) {
usersById.set(user.id, {
id: user.id,
nickname: user.nickname || null,
name: user.name,
status: user.status,
profilePicture: user.profilePicture || null
})
}
}
return usersById
}
module.exports = {
buildFollowerCountUpdate,
adjustFollowerCount,
deleteFollowerCount,
getFollowerCounts,
getFollowingCount,
deleteAllFollowsForUser,
resolvePublicUsers
}
|