Add reversible administrator room archiving
Organize closed rooms with archive filters, live-authorized atomic receipts and exact saved retries. Preserve saved games, timestamps, credentials, discussion and AI deadlines. Unarchive leaves play closed; reopen requires unarchive, and removal/restoration preserves both flags. Verified types, lint, 5379 offline tests, 52 HTTP tests and build. Twenty dedicated administrator HTTP groups and independent privacy/persistence review passed. Desktop/mobile browser QA confirmed a single archive after a lost response and refresh, readable recovery text, mobile unarchive and separate reopening, and saved-seat restoration. All 2,049 baseline rooms, 4,348 seats and related records preserved. Authenticated production admin acceptance awaits authorized QA access; full admin and rules gates remain open.
This commit is contained in:
1 parent
98b1b3261b
commit
e918e2e0c0
35 files changed
+3624
-38
No files matched your search
@@ -0,0 +1,65 @@
|
||||
import { randomId } from './random-id';
|
||||
import { validAdminArchiveInput, type AdminArchiveInput, type AdminArchiveView, type AdminArchiveResult } from './admin-archive';
|
||||
|
||||
type Storage = Pick<globalThis.Storage, 'getItem' | 'setItem' | 'removeItem'>;
|
||||
const key = (account: string, code: string) => `dune.admin-archive-request.v1:${account}:${code}`;
|
||||
const object = (value: unknown): value is Record<string, unknown> => !!value && typeof value === 'object' && !Array.isArray(value);
|
||||
const keys = (value: Record<string, unknown>, names: string[]) => Object.keys(value).length === names.length && names.every(name => Object.hasOwn(value, name));
|
||||
const integer = (value: unknown): value is number => Number.isSafeInteger(value) && Number(value) >= 0;
|
||||
const timestamp = (value: unknown) => value === null || integer(value) && value <= 8_640_000_000_000_000;
|
||||
|
||||
export function validAdminArchiveView(value: unknown): value is AdminArchiveView {
|
||||
return object(value) && keys(value, ['closed', 'removed', 'code', 'version', 'archived', 'revision', 'archivedAt', 'updatedAt', 'paused', 'joinLocked']) &&
|
||||
typeof value.code === 'string' && /^[A-Z2-9]{8}$/.test(value.code) && integer(value.version) && integer(value.revision) &&
|
||||
typeof value.closed === 'boolean' && typeof value.removed === 'boolean' && typeof value.archived === 'boolean' && typeof value.paused === 'boolean' && typeof value.joinLocked === 'boolean' &&
|
||||
timestamp(value.archivedAt) && timestamp(value.updatedAt);
|
||||
}
|
||||
|
||||
export function adminArchiveResponse(value: unknown, code: string): AdminArchiveView {
|
||||
if (!validAdminArchiveView(value) || value.code !== code) throw new Error('The server returned unreadable room availability.');
|
||||
return value;
|
||||
}
|
||||
|
||||
export function adminArchiveConfirmation(value: unknown, code: string, input: AdminArchiveInput): AdminArchiveResult {
|
||||
if (!object(value) || !keys(value, ['operationId', 'replayed', 'appliedRevision', 'appliedVersion', 'room']) ||
|
||||
value.operationId !== input.operationId || typeof value.replayed !== 'boolean' ||
|
||||
!integer(value.appliedVersion) || value.appliedVersion !== input.expectedVersion + 1 ||
|
||||
!integer(value.appliedRevision) || value.appliedRevision !== input.expectedRevision + 1)
|
||||
throw new Error('The server did not confirm this archive or unarchive request.');
|
||||
const room = adminArchiveResponse(value.room, code);
|
||||
if (room.version < value.appliedVersion || room.revision < value.appliedRevision)
|
||||
throw new Error('The server returned room availability older than the confirmed change.');
|
||||
return value as AdminArchiveResult;
|
||||
}
|
||||
|
||||
export function newAdminArchiveRequest(view: AdminArchiveView, archived: boolean, reason: string): AdminArchiveInput {
|
||||
const input = { operationId: randomId(), expectedVersion: view.version, expectedRevision: view.revision, archived, reason: reason.trim() };
|
||||
if (!validAdminArchiveInput(input)) throw new Error('Enter a reason of 1–300 characters on one line.');
|
||||
if (view.removed) throw new Error('Restore this removed room before changing its archive.');
|
||||
if (!view.closed) throw new Error('Close this room before changing its archive state.');
|
||||
if (archived === view.archived) throw new Error('Refresh the room before choosing an archive or unarchive.');
|
||||
return input;
|
||||
}
|
||||
|
||||
export function readAdminArchiveRequest(storage: Storage, account: string, code: string): AdminArchiveInput | null {
|
||||
const raw = storage.getItem(key(account, code));
|
||||
if (raw === null) return null;
|
||||
let value: unknown;
|
||||
try { value = raw.length <= 2048 ? JSON.parse(raw) : null; } catch { value = null; }
|
||||
if (!validAdminArchiveInput(value)) throw new Error('This tab’s saved archive or unarchive request is unreadable. Inspect the current room before discarding its local retry record.');
|
||||
return value;
|
||||
}
|
||||
|
||||
/** Save before sending; a different unresolved request must never be replaced. */
|
||||
export function saveAdminArchiveRequest(storage: Storage, account: string, code: string, input: AdminArchiveInput) {
|
||||
if (!validAdminArchiveInput(input)) throw new Error('Invalid archive or unarchive request.');
|
||||
const previous = readAdminArchiveRequest(storage, account, code), encoded = JSON.stringify(input);
|
||||
if (previous && JSON.stringify(previous) !== encoded) throw new Error('Resolve the saved archive or unarchive request before sending a different change.');
|
||||
storage.setItem(key(account, code), encoded);
|
||||
if (storage.getItem(key(account, code)) !== encoded) throw new Error('The archive or unarchive request could not be preserved in this tab. Nothing was sent.');
|
||||
}
|
||||
|
||||
export function clearAdminArchiveRequest(storage: Storage, account: string, code: string) {
|
||||
storage.removeItem(key(account, code));
|
||||
if (storage.getItem(key(account, code)) !== null) throw new Error('The saved archive or unarchive request could not be cleared.');
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
export type AdminArchiveInput = {
|
||||
operationId: string;
|
||||
expectedVersion: number;
|
||||
expectedRevision: number;
|
||||
archived: boolean;
|
||||
reason: string;
|
||||
};
|
||||
|
||||
export type AdminArchiveView = {
|
||||
closed: boolean;
|
||||
removed: boolean;
|
||||
code: string;
|
||||
version: number;
|
||||
archived: boolean;
|
||||
revision: number;
|
||||
archivedAt: number | null;
|
||||
updatedAt: number | null;
|
||||
paused: boolean;
|
||||
joinLocked: boolean;
|
||||
};
|
||||
|
||||
export type AdminArchiveResult = {
|
||||
operationId: string;
|
||||
replayed: boolean;
|
||||
appliedRevision: number;
|
||||
appliedVersion: number;
|
||||
room: AdminArchiveView;
|
||||
};
|
||||
|
||||
export function validAdminArchiveInput(
|
||||
value: unknown,
|
||||
): value is AdminArchiveInput {
|
||||
if (!value || typeof value !== 'object' || Array.isArray(value)) return false;
|
||||
const input = value as Record<string, unknown>;
|
||||
const fields = [
|
||||
'operationId',
|
||||
'expectedVersion',
|
||||
'expectedRevision',
|
||||
'archived',
|
||||
'reason',
|
||||
];
|
||||
return (
|
||||
Object.keys(input).length === fields.length &&
|
||||
fields.every((field) => Object.hasOwn(input, field)) &&
|
||||
typeof input.operationId === 'string' &&
|
||||
input.operationId.length === 36 &&
|
||||
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.test(
|
||||
input.operationId,
|
||||
) &&
|
||||
[input.expectedVersion, input.expectedRevision].every(
|
||||
(version) =>
|
||||
Number.isSafeInteger(version) &&
|
||||
Number(version) >= 0 &&
|
||||
Number(version) < Number.MAX_SAFE_INTEGER,
|
||||
) &&
|
||||
typeof input.archived === 'boolean' &&
|
||||
typeof input.reason === 'string' &&
|
||||
!!input.reason.trim() &&
|
||||
input.reason.length <= 300 &&
|
||||
!input.reason
|
||||
.split('')
|
||||
.some((char) => char.charCodeAt(0) < 32 || char.charCodeAt(0) === 127)
|
||||
);
|
||||
}
|
||||
@@ -9,7 +9,7 @@ const integer = (value: unknown): value is number => Number.isSafeInteger(value)
|
||||
const timestamp = (value: unknown) => value === null || integer(value) && value <= 8_640_000_000_000_000;
|
||||
|
||||
export function validAdminClosureView(value: unknown): value is AdminClosureView {
|
||||
return object(value) && keys(value, ['removed', 'code', 'version', 'closed', 'revision', 'closedAt', 'updatedAt', 'paused', 'joinLocked']) &&
|
||||
return object(value) && keys(value, ['removed', 'code', 'version', 'closed', 'revision', 'closedAt', 'updatedAt', 'paused', 'joinLocked', ...(value.archived === true ? ['archived'] : [])]) &&
|
||||
typeof value.code === 'string' && /^[A-Z2-9]{8}$/.test(value.code) && integer(value.version) && integer(value.revision) &&
|
||||
typeof value.removed === 'boolean' && typeof value.closed === 'boolean' && typeof value.paused === 'boolean' && typeof value.joinLocked === 'boolean' &&
|
||||
timestamp(value.closedAt) && timestamp(value.updatedAt);
|
||||
@@ -36,6 +36,7 @@ export function newAdminClosureRequest(view: AdminClosureView, closed: boolean,
|
||||
const input = { operationId: randomId(), expectedVersion: view.version, expectedRevision: view.revision, closed, reason: reason.trim() };
|
||||
if (!validAdminClosureInput(input)) throw new Error('Enter a reason of 1–300 characters on one line.');
|
||||
if (view.removed) throw new Error('Restore this removed room before changing its closure.');
|
||||
if (view.archived) throw new Error('Unarchive this room before reopening it.');
|
||||
if (closed === view.closed) throw new Error('Refresh the room before choosing a closure or reopening.');
|
||||
return input;
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ export type AdminClosureInput = {
|
||||
};
|
||||
|
||||
export type AdminClosureView = {
|
||||
archived?: true;
|
||||
removed: boolean;
|
||||
code: string;
|
||||
version: number;
|
||||
|
||||
@@ -4,6 +4,7 @@ export type AdminRoom = {
|
||||
code: string;
|
||||
version: number;
|
||||
removed: boolean;
|
||||
archived: boolean;
|
||||
updatedAt: number;
|
||||
status: 'lobby' | 'setup' | 'playing' | 'finished' | 'unreadable';
|
||||
host: string | null;
|
||||
|
||||
@@ -9,7 +9,7 @@ const integer = (value: unknown): value is number => Number.isSafeInteger(value)
|
||||
const timestamp = (value: unknown) => value === null || integer(value) && value <= 8_640_000_000_000_000;
|
||||
|
||||
export function validAdminRemovalView(value: unknown): value is AdminRemovalView {
|
||||
return object(value) && keys(value, ['code', 'version', 'removed', 'revision', 'removedAt', 'updatedAt', 'paused', 'joinLocked', ...(value.closed === true ? ['closed'] : [])]) &&
|
||||
return object(value) && keys(value, ['code', 'version', 'removed', 'revision', 'removedAt', 'updatedAt', 'paused', 'joinLocked', ...(value.closed === true ? ['closed'] : []), ...(value.archived === true ? ['archived'] : [])]) &&
|
||||
typeof value.code === 'string' && /^[A-Z2-9]{8}$/.test(value.code) && integer(value.version) && integer(value.revision) &&
|
||||
typeof value.removed === 'boolean' && typeof value.paused === 'boolean' && typeof value.joinLocked === 'boolean' &&
|
||||
timestamp(value.removedAt) && timestamp(value.updatedAt);
|
||||
|
||||
@@ -7,6 +7,7 @@ export type AdminRemovalInput = {
|
||||
};
|
||||
|
||||
export type AdminRemovalView = {
|
||||
archived?: true;
|
||||
closed?: true;
|
||||
code: string;
|
||||
version: number;
|
||||
|
||||
Reference in new issue
Block a user