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 | 17x 17x 17x 17x 17x 35x 27x 16x 16x 11x 23x 23x 22x 16x 6x 6x 6x 6x 511x 511x 3x 3x 508x 1x 1x 507x 6x 1x 5x 5x 12x 12x 11x 2x 9x 9x 9x 508x 506x 504x 2x 7x 4x 3x 3x 8x 8x 8x 10x 10x 10x 1011x 10x 10x 4x 4x 4x 8x 8x 8x 5x 3x 17x | /**
* Ban-cascade for the suggestions surface.
*
* When an admin suspends a user, their existing "live" suggestions
* (status === 'accepted' or 'planned') get a `flaggedForReview: true`
* sticky note so an admin can decide case-by-case whether the suggestion
* stays on the roadmap. Suggestion status is left untouched — this preserves
* voter/subscriber state and lets unsuspend cleanly reverse the cascade by
* just clearing the flag fields.
*
* Why not change `status`?
* - `planned` -> `pending` would silently rip items off the public roadmap
* mid-flight, surprising voters/subscribers who are watching them.
* - Adding an `under_review` status would force changes to every transition
* guard in suggestions.js (lines 949-953) plus FE badges + 20 locales.
* - A flag is reversible by symmetry on unsuspend; status changes need a
* `priorStatus` stash to revert cleanly.
*
* Why not filter by status server-side?
* - `where('submitterUid','==',uid).where('status','in',[...])` needs a
* composite index. We expect <5 suggestions per typical user, so the
* single-field query + in-JS status filter avoids the index entirely.
*
* Idempotency:
* - flag: skip docs already carrying `flaggedReason === 'submitter_suspended'`.
* - unflag: only clear docs whose `flaggedReason === 'submitter_suspended'`
* (leaves unrelated manual admin flags alone).
*
* Contract mirrors evict-suspended-user.js for consistency across cascade utilities.
*/
const { db } = require('./firebase');
const { now } = require('./helpers');
const log = require('./log');
const FLAGGABLE_STATUSES = new Set(['accepted', 'planned']);
const FLAG_REASON = 'submitter_suspended';
/**
* `submitterUid` is stored numerically (sourced from `req.auth.uniqueId`)
* but suspension callers pass `req.params.uniqueId` (always a string).
* Firestore equality is strict, so the string form silently matches nothing —
* normalize once at the boundary.
*
* Edge-case behaviour (all routed to "silently match nothing" rather than
* coercing to 0 or crashing — Firestore strict equality is the loud signal):
* - `42` → `42` (already numeric, passthrough)
* - `'42'` → `42` (integer string, normalized)
* - `'-7'` → `-7` (negative integer string, normalized)
* - `'7.5'` → `'7.5'` (float string, regex rejects, passthrough)
* - `'abc'` → `'abc'` (non-numeric string, passthrough)
* - `''` → `''` (empty string, passthrough)
* - `NaN` → `NaN` (typeof 'number' passthrough; matches nothing in Firestore)
* - `null`/`undef` → original (typeof neither 'number' nor 'string', passthrough)
*
* For `NaN` specifically: Firestore equality treats NaN strictly, so a NaN-uid
* query returns empty rather than throwing. That is "match nothing" not "throw",
* which keeps the cascade route safe (admin response surfaces flaggedCount: 0)
* but means a buggy upstream sending NaN would not loudly fail at this layer —
* a worth-knowing limitation for future debugging.
*/
function normalizeUid(uid) {
if (typeof uid === 'number') return uid;
if (typeof uid === 'string' && uid !== '' && /^-?\d+$/.test(uid)) {
const n = Number(uid);
Eif (Number.isFinite(n)) return n;
}
return uid;
}
async function flagSuspendedUserSuggestions(uid, adminUid) {
const normalizedUid = normalizeUid(uid);
const snap = await db.collection('suggestions').where('submitterUid', '==', normalizedUid).get();
if (!snap || snap.empty) {
return {
flaggedCount: 0,
skippedCount: 0,
partial: false,
failedSuggestionIds: [],
error: null,
};
}
const flaggedAt = now();
const toFlag = [];
let skippedCount = 0;
for (const doc of snap.docs) {
const data = doc.data();
if (!FLAGGABLE_STATUSES.has(data.status)) {
skippedCount += 1;
continue;
}
if (data.flaggedForReview === true && data.flaggedReason === FLAG_REASON) {
skippedCount += 1;
continue;
}
toFlag.push(doc);
}
if (toFlag.length === 0) {
return {
flaggedCount: 0,
skippedCount,
partial: false,
failedSuggestionIds: [],
error: null,
};
}
const payload = {
flaggedForReview: true,
flaggedReason: FLAG_REASON,
flaggedAt,
flaggedBy: adminUid,
};
return commitChunked(toFlag, payload, 'flag', skippedCount);
}
async function unflagUnsuspendedUserSuggestions(uid) {
const normalizedUid = normalizeUid(uid);
const snap = await db.collection('suggestions').where('submitterUid', '==', normalizedUid).get();
if (!snap || snap.empty) {
return {
unflaggedCount: 0,
skippedCount: 0,
partial: false,
failedSuggestionIds: [],
error: null,
};
}
const toClear = [];
let skippedCount = 0;
for (const doc of snap.docs) {
const data = doc.data();
if (data.flaggedForReview === true && data.flaggedReason === FLAG_REASON) {
toClear.push(doc);
} else {
skippedCount += 1;
}
}
if (toClear.length === 0) {
return {
unflaggedCount: 0,
skippedCount,
partial: false,
failedSuggestionIds: [],
error: null,
};
}
const payload = {
flaggedForReview: false,
flaggedReason: null,
flaggedAt: null,
flaggedBy: null,
};
return commitChunked(toClear, payload, 'unflag', skippedCount);
}
async function commitChunked(docs, payload, mode, skippedCount = 0) {
const failedSuggestionIds = [];
let firstError = null;
for (let i = 0; i < docs.length; i += 500) {
const chunk = docs.slice(i, i + 500);
const batch = db.batch();
for (const doc of chunk) {
batch.update(doc.ref, payload);
}
try {
await batch.commit();
} catch (err) {
log.error('flag-suspended-user-suggestions', `Batch ${mode} commit failed`, {
chunkStart: i,
chunkSize: chunk.length,
error: err && err.message,
});
for (const doc of chunk) failedSuggestionIds.push(doc.id);
Eif (!firstError) firstError = err && err.message ? err.message : String(err);
}
}
const successCount = docs.length - failedSuggestionIds.length;
const partial = failedSuggestionIds.length > 0;
if (mode === 'flag') {
return {
flaggedCount: successCount,
skippedCount,
partial,
failedSuggestionIds,
error: firstError,
};
}
return {
unflaggedCount: successCount,
skippedCount,
partial,
failedSuggestionIds,
error: firstError,
};
}
module.exports = {
flagSuspendedUserSuggestions,
unflagUnsuspendedUserSuggestions,
FLAG_REASON,
FLAGGABLE_STATUSES,
};
|