Let owners/operators mute or unmute new public/private sends from a human seat while retaining history, incoming messages, gameplay and credentials. Fence metadata and durable audit writes with live authority, membership and versions. Preserve exact admin/message retries and inherit restrictions through seat recovery. Validation: types/lint, 5431 offline tests, build, 52 HTTP integration tests, 24 administrator HTTP groups, independent SQL/privacy/UI review and desktop/mobile refresh acceptance. All 2230 prior games and 4746 seats preserved. Additive migration 0016. Verified runtime fingerprint 588967fbb9c2e3b6abe2990c4fce96ee69e2256fb7e3ee416314b87593cebba9; documentation-only grammar/button-label review gives b5e57bdd26812debede91c588e38f247f1fa895ad23e7ae30bc8550eedcd90ec. New-revision deployed acceptance remains open; full admin/rules scope is unfinished.
75 lines
2.3 KiB
TypeScript
75 lines
2.3 KiB
TypeScript
export const TALK_LIMIT = 1000;
|
|
export const TALK_PAGE_SIZE = 50;
|
|
export type TalkDraft = {
|
|
id: string;
|
|
recipientId: string | null;
|
|
text: string;
|
|
};
|
|
export type TalkMessage = {
|
|
id: string;
|
|
senderId: string;
|
|
senderName: string;
|
|
senderFaction: string;
|
|
recipientId: string | null;
|
|
recipientName: string | null;
|
|
text: string;
|
|
createdAt: number;
|
|
};
|
|
export type TalkPage = { messages: TalkMessage[]; before: string | null; muted: boolean };
|
|
export const talkId = (value: unknown): value is string =>
|
|
typeof value === 'string' &&
|
|
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.test(value);
|
|
export function validTalkDraft(value: unknown): value is TalkDraft {
|
|
if (!value || typeof value !== 'object' || Array.isArray(value)) return false;
|
|
const draft = value as Record<string, unknown>;
|
|
return (
|
|
Object.keys(draft).length === 3 &&
|
|
talkId(draft.id) &&
|
|
(draft.recipientId === null || talkId(draft.recipientId)) &&
|
|
typeof draft.text === 'string' &&
|
|
draft.text.length <= TALK_LIMIT &&
|
|
draft.text.trim().length > 0 &&
|
|
Array.from(draft.text).every((character) => {
|
|
const code = character.codePointAt(0)!;
|
|
return (
|
|
code === 9 || code === 10 || code === 13 || (code >= 32 && code !== 127)
|
|
);
|
|
})
|
|
);
|
|
}
|
|
export function talkDraftKey(room: string, seat: string) {
|
|
return `dune-table-talk-v1:${room}:${seat}`;
|
|
}
|
|
export function restoreTalkDraft(raw: string | null): TalkDraft | null {
|
|
if (!raw) return null;
|
|
try {
|
|
const value: unknown = JSON.parse(raw);
|
|
return validTalkDraft(value) ? value : null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
/** A bounded page merge: UUIDs identify only already-entitled messages. */
|
|
export function mergeTalkPages(older: TalkMessage[], newer: TalkMessage[]) {
|
|
const seen = new Set(newer.map((message) => message.id));
|
|
return [...older.filter((message) => !seen.has(message.id)), ...newer];
|
|
}
|
|
|
|
/** A late response may clear only its own tab-scoped retry proof. */
|
|
export function clearTalkDraft(
|
|
storage: Pick<Storage, 'getItem' | 'removeItem'>,
|
|
key: string,
|
|
draft: TalkDraft,
|
|
): boolean {
|
|
const saved = restoreTalkDraft(storage.getItem(key));
|
|
if (
|
|
!saved ||
|
|
saved.id !== draft.id ||
|
|
saved.recipientId !== draft.recipientId ||
|
|
saved.text !== draft.text
|
|
)
|
|
return false;
|
|
storage.removeItem(key);
|
|
return true;
|
|
}
|