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 | 69x 158x 158x 112x 1x 111x 111x 666x 651x 111x 111x 73x 73x 365x 158x | /**
* The suspension lifecycle, stated once (SHY-0463).
*
* `isSuspended` is written from eight places across four route files — report
* resolution, two admin suspend routes, two admin unsuspend routes, the
* appeal-approval path, and the expired-suspension lift. Each carried its own
* hand-written field list, and the lists disagreed.
*
* `suspensionAppealStatus` is what that cost. It is set when somebody appeals
* and was cleared by exactly ONE of those writers, so it outlived the
* suspension it described. The consequences were not subtle: the appeal
* endpoint read it and refused every later appeal `409 Appeal already
* pending`, and the suspension screen renders the appeal form from it, so a
* person suspended a second time was told they could appeal and shown no way
* to do it. Fixing the readers and leaving the data behind is half a fix.
*
* A field describing a suspension must not outlive that suspension. These
* helpers are how that stays true for the next such field — one place to add
* it, rather than eight places to remember.
*/
/**
* Every field that ends a suspension.
*
* A fresh object each call, not a shared constant: callers spread restored
* profile values over the top, and a shared object would accumulate one
* caller's restore into the next caller's clear.
*
* @param {object} restore profile values to put back (displayName etc.)
*/
function suspensionEndedFields(restore = {}) {
return {
isSuspended: false,
suspensionReason: null,
suspensionStartDate: null,
suspensionEndDate: null,
suspensionCanAppeal: null,
suspensionAppealStatus: null,
suspendedBy: null,
preSuspensionDisplayName: null,
preSuspensionProfilePhotoUrl: null,
preSuspensionCoverPhotoUrl: null,
preSuspensionDescription: null,
preSuspensionAvatarUrl: null,
suspensionRunStartedAt: null,
...restore,
};
}
/**
* Fields that must NOT survive into a new suspension.
*
* Spread into every write that begins one. A new accusation has not been
* appealed yet, whatever happened to the last one.
*/
const SUSPENSION_STARTED_RESET = Object.freeze({ suspensionAppealStatus: null });
/** The display name every suspended account shows in place of its own. */
const SUSPENDED_DISPLAY_NAME = 'Suspended Account';
/**
* The profile fields that begin a suspension: save the profile, then mask it.
*
* Unsuspend puts the saved profile back (`suspensionEndedFields`). A user who
* is already suspended keeps the profile saved by the first suspension (SHY-0597):
* saving again would save the mask as the "real" name, and unsuspend would then
* restore the mask for good. Where nothing was saved (identity-graph suspend-all
* marks a user suspended without masking), the current profile is saved instead,
* but never the mask itself.
*
* It also records when the run of suspensions began (`suspensionRunStartedAt`,
* SHY-0605). A second suspend rewrites `suspensionStartDate`, but an upheld
* appeal gives back the GCS taken since the FIRST, so the first start is kept
* the same way the first saved profile is.
*
* @param {object} user the user document as read before the write
* @param {number} startedAt when this suspension begins (ms)
*/
function suspensionStartedProfileFields(user, startedAt) {
if (!Number.isFinite(startedAt)) {
throw new TypeError('suspensionStartedProfileFields: startedAt (ms) is required');
}
const alreadySuspended = (user.isSuspended ?? user.is_suspended) === true;
const keep = (saved, current) => {
if (alreadySuspended && saved !== null && saved !== undefined) return saved;
return current ?? null;
};
const savedName = keep(
user.preSuspensionDisplayName ?? user.pre_suspension_display_name,
user.displayName ?? user.display_name,
);
return {
preSuspensionDisplayName:
alreadySuspended && savedName === SUSPENDED_DISPLAY_NAME ? null : savedName,
preSuspensionProfilePhotoUrl: keep(
user.preSuspensionProfilePhotoUrl ?? user.pre_suspension_profile_photo_url,
user.profilePhotoUrl ?? user.profile_photo_url,
),
preSuspensionCoverPhotoUrl: keep(
user.preSuspensionCoverPhotoUrl ?? user.pre_suspension_cover_photo_url,
user.coverPhotoUrl ?? user.cover_photo_url,
),
preSuspensionDescription: keep(user.preSuspensionDescription, user.description),
preSuspensionAvatarUrl: keep(user.preSuspensionAvatarUrl, user.avatarUrl),
displayName: SUSPENDED_DISPLAY_NAME,
profilePhotoUrl: null,
coverPhotoUrl: null,
avatarUrl: null,
description: null,
currentRoomId: null,
suspensionRunStartedAt: keep(
user.suspensionRunStartedAt ?? user.suspensionStartDate,
startedAt,
),
};
}
/**
* The profile fields that end a suspension: put back what the start saved.
*
* Spread into `suspensionEndedFields(restore)`. A null or missing save restores
* nothing, so a field the user changed after the start is left as it is. The
* bio and sign-in photo saves are new with SHY-0605 and only ever written in
* camelCase, so they have no snake_case form to read.
*
* @param {object} user the user document as read before the write
*/
function profileToRestore(user) {
const saves = {
displayName: user.preSuspensionDisplayName ?? user.pre_suspension_display_name,
profilePhotoUrl: user.preSuspensionProfilePhotoUrl ?? user.pre_suspension_profile_photo_url,
coverPhotoUrl: user.preSuspensionCoverPhotoUrl ?? user.pre_suspension_cover_photo_url,
description: user.preSuspensionDescription,
avatarUrl: user.preSuspensionAvatarUrl,
};
return Object.fromEntries(
Object.entries(saves).filter(([, saved]) => saved !== null && saved !== undefined),
);
}
module.exports = {
suspensionEndedFields,
profileToRestore,
SUSPENSION_STARTED_RESET,
SUSPENDED_DISPLAY_NAME,
suspensionStartedProfileFields,
};
|