Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
A chat mute can last 10 min, 1 h, 24 h or for good, and ends on its own - in src/chat.ts #204
smith/arm:smith/191-a-chat-mute-can-last-10-min-1-h-24-h-or → main 1 commit fork
2 files
changed +57
−8
Software/so101/src/chat.test.ts +29 −1
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | import { describe, expect, test } from "bun:test"; |
| 2 | import { mkdtempSync } from "node:fs"; | |
| 2 | import { mkdtempSync, writeFileSync } from "node:fs"; | |
| 3 | 3 | import { tmpdir } from "node:os"; |
| 4 | 4 | import { join } from "node:path"; |
| 5 | 5 | import { Chat, KEEP, MAX_CHARS } from "./chat.ts"; |
| @@ -53,4 +53,32 @@ describe("chat", () => { | ||
| 53 | 53 | expect(chat.delete(line.id)).toBe(false); |
| 54 | 54 | expect(chat.messages).toEqual([]); |
| 55 | 55 | }); |
| 56 | ||
| 57 | test("a timed mute blocks until it ends, then drops out of muted", () => { | |
| 58 | let clock = 0; | |
| 59 | const chat = new Chat({ rateMs: 0, now: () => clock }); | |
| 60 | chat.mute("a", "alice", 10 * 60_000); | |
| 61 | clock = 9 * 60_000 + 59_000; | |
| 62 | expect(() => chat.post(alice, "hi")).toThrow(/muted/); | |
| 63 | expect(chat.muted.has("a")).toBe(true); | |
| 64 | clock = 10 * 60_000 + 1000; | |
| 65 | expect(chat.post(alice, "hi").text).toBe("hi"); | |
| 66 | expect(chat.muted.size).toBe(0); | |
| 67 | }); | |
| 68 | ||
| 69 | test("a permanent mute survives a reload, a timed one keeps its end, and an old-format file loads", () => { | |
| 70 | const file = join(mkdtempSync(join(tmpdir(), "so101-chat-")), "chat.json"); | |
| 71 | let clock = 0; | |
| 72 | const chat = new Chat({ file, now: () => clock }); | |
| 73 | chat.mute("a", "alice", null); | |
| 74 | chat.mute("b", "bob", 60_000); | |
| 75 | clock = 3_600_000; | |
| 76 | const again = new Chat({ file, now: () => clock }); | |
| 77 | expect([...again.muted]).toEqual([["a", "alice"]]); | |
| 78 | expect([...new Chat({ file, now: () => 30_000 }).muted.keys()]).toEqual(["a", "b"]); | |
| 79 | ||
| 80 | const old = join(mkdtempSync(join(tmpdir(), "so101-chat-")), "chat.json"); | |
| 81 | writeFileSync(old, JSON.stringify({ messages: [], muted: { c: "carol" } })); | |
| 82 | expect(new Chat({ file: old, now: () => 1e15 }).muted.get("c")).toBe("carol"); | |
| 83 | }); | |
| 56 | 84 | }); |
Software/so101/src/chat.ts +28 −7
| @@ -42,8 +42,8 @@ export function cleanText(text: unknown): string { | ||
| 42 | 42 | |
| 43 | 43 | export class Chat { |
| 44 | 44 | messages: Message[] = []; |
| 45 | /** id → name of everyone muted. */ | |
| 46 | readonly muted = new Map<string, string>(); | |
| 45 | /** id → name and end time (null: for good) of everyone muted, expired or not. */ | |
| 46 | private readonly mutes = new Map<string, { name: string; until: number | null }>(); | |
| 47 | 47 | private next = 1; |
| 48 | 48 | private readonly lastPost = new Map<string, number>(); |
| 49 | 49 | private readonly file: string | undefined; |
| @@ -57,6 +57,17 @@ export class Chat { | ||
| 57 | 57 | if (this.file) this.load(this.file); |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | /** id → name of everyone muted right now; a mute that has run out is dropped. */ | |
| 61 | get muted(): Map<string, string> { | |
| 62 | const now = this.now(); | |
| 63 | const live = new Map<string, string>(); | |
| 64 | for (const [id, m] of this.mutes) { | |
| 65 | if (m.until !== null && now >= m.until) this.mutes.delete(id); | |
| 66 | else live.set(id, m.name); | |
| 67 | } | |
| 68 | return live; | |
| 69 | } | |
| 70 | ||
| 60 | 71 | post(who: Person, text: unknown): Message { |
| 61 | 72 | if (this.muted.has(who.id)) throw new ChatError("you are muted"); |
| 62 | 73 | const clean = cleanText(text); |
| @@ -88,9 +99,10 @@ export class Chat { | ||
| 88 | 99 | this.save(); |
| 89 | 100 | } |
| 90 | 101 | |
| 91 | mute(id: string, name: string, on: boolean): void { | |
| 92 | if (on) this.muted.set(id, name); | |
| 93 | else this.muted.delete(id); | |
| 102 | /** `until` is the time the mute ends, null for good; `true` is for good and `false` lifts it. */ | |
| 103 | mute(id: string, name: string, until: number | null | boolean = null): void { | |
| 104 | if (until === false) this.mutes.delete(id); | |
| 105 | else this.mutes.set(id, { name, until: until === true ? null : until }); | |
| 94 | 106 | this.save(); |
| 95 | 107 | } |
| 96 | 108 | |
| @@ -114,14 +126,23 @@ export class Chat { | ||
| 114 | 126 | .filter((m): m is Message => typeof m?.id === "number" && typeof m.at === "number" && typeof m.text === "string" && (m.from === null || (typeof m.from?.id === "string" && typeof m.from?.name === "string"))) |
| 115 | 127 | .slice(-KEEP); |
| 116 | 128 | } |
| 117 | if (stored.muted && typeof stored.muted === "object") for (const [id, name] of Object.entries(stored.muted)) if (typeof name === "string") this.muted.set(id, name); | |
| 129 | if (stored.muted && typeof stored.muted === "object") { | |
| 130 | for (const [id, m] of Object.entries(stored.muted as Record<string, unknown>)) { | |
| 131 | if (typeof m === "string") this.mutes.set(id, { name: m, until: null }); // the old format: for good | |
| 132 | else if (m && typeof (m as { name?: unknown }).name === "string") { | |
| 133 | const { name, until } = m as { name: string; until?: unknown }; | |
| 134 | this.mutes.set(id, { name, until: typeof until === "number" && Number.isFinite(until) ? until : null }); | |
| 135 | } | |
| 136 | } | |
| 137 | } | |
| 118 | 138 | this.next = Math.max(0, ...this.messages.map((m) => m.id)) + 1; |
| 119 | 139 | } |
| 120 | 140 | |
| 121 | 141 | private save(): void { |
| 122 | 142 | if (!this.file) return; |
| 143 | void this.muted; // drops the expired | |
| 123 | 144 | mkdirSync(dirname(this.file), { recursive: true }); |
| 124 | writeFileSync(`${this.file}.tmp`, JSON.stringify({ messages: this.messages, muted: Object.fromEntries(this.muted) })); | |
| 145 | writeFileSync(`${this.file}.tmp`, JSON.stringify({ messages: this.messages, muted: Object.fromEntries(this.mutes) })); | |
| 125 | 146 | renameSync(`${this.file}.tmp`, this.file); |
| 126 | 147 | } |
| 127 | 148 | } |
new above
offline