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 #248
smith/arm:smith/236-a-chat-mute-can-last-10-min-1-h-24-h-or → main 2 commits fork
3 files
changed +61
−11
Software/so101/src/chat.test.ts +30 −2
| @@ -35,7 +35,7 @@ describe("chat", () => { | ||
| 35 | 35 | let clock = 0; |
| 36 | 36 | const chat = new Chat({ file, now: () => (clock += 5000) }); |
| 37 | 37 | for (let i = 0; i < KEEP + 5; i++) chat.post(alice, `line ${i}`); |
| 38 | chat.mute("b", "bob", true); | |
| 38 | chat.mute("b", "bob", null); | |
| 39 | 39 | expect(chat.messages.length).toBe(KEEP); |
| 40 | 40 | const again = new Chat({ file }); |
| 41 | 41 | expect(again.messages.length).toBe(KEEP); |
| @@ -47,10 +47,38 @@ describe("chat", () => { | ||
| 47 | 47 | test("a muted person cannot post; a deleted line is gone", () => { |
| 48 | 48 | const chat = new Chat(); |
| 49 | 49 | const line = chat.post(alice, "spam"); |
| 50 | chat.mute("a", "alice", true); | |
| 50 | chat.mute("a", "alice", null); | |
| 51 | 51 | expect(() => chat.post(alice, "more")).toThrow(/muted/); |
| 52 | 52 | expect(chat.delete(line.id)).toBe(true); |
| 53 | 53 | expect(chat.delete(line.id)).toBe(false); |
| 54 | 54 | expect(chat.messages).toEqual([]); |
| 55 | 55 | }); |
| 56 | ||
| 57 | test("a timed mute ends on its own; a permanent one survives a reload; the old file format loads", () => { | |
| 58 | const min = 60_000; | |
| 59 | let clock = 0; | |
| 60 | const file = join(mkdtempSync(join(tmpdir(), "chat-")), "chat.json"); | |
| 61 | const chat = new Chat({ file, now: () => clock }); | |
| 62 | chat.mute("a", "alice", 10 * min); | |
| 63 | chat.mute("b", "bob", null); | |
| 64 | clock = 10 * min - 1000; | |
| 65 | expect(() => chat.post(alice, "hi")).toThrow(/muted/); | |
| 66 | expect(chat.muted.has("a")).toBe(true); | |
| 67 | clock = 10 * min + 1000; | |
| 68 | expect(chat.post(alice, "hi").text).toBe("hi"); | |
| 69 | expect(chat.muted.has("a")).toBe(false); | |
| 70 | clock = 1000 * min; | |
| 71 | const again = new Chat({ file, now: () => clock }); | |
| 72 | expect(again.muted.get("b")).toBe("bob"); | |
| 73 | expect(() => again.post(bob, "hi")).toThrow(/muted/); | |
| 74 | again.unmute("b"); | |
| 75 | expect(again.post(bob, "hi").text).toBe("hi"); | |
| 76 | }); | |
| 77 | ||
| 78 | test("an old-format file loads as a mute for good", async () => { | |
| 79 | const old = join(mkdtempSync(join(tmpdir(), "chat-")), "chat.json"); | |
| 80 | await Bun.write(old, JSON.stringify({ messages: [], muted: { c: "carol" } })); | |
| 81 | const chat = new Chat({ file: old, now: () => 1e12 }); | |
| 82 | expect(chat.muted.get("c")).toBe("carol"); | |
| 83 | }); | |
| 56 | 84 | }); |
Software/so101/src/chat.ts +29 −8
| @@ -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 → mute; `until` null is for good. */ | |
| 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,8 +57,15 @@ 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 gone. */ | |
| 61 | get muted(): Map<string, string> { | |
| 62 | const now = this.now(); | |
| 63 | return new Map([...this.mutes].filter(([, m]) => m.until === null || now < m.until).map(([id, m]) => [id, m.name])); | |
| 64 | } | |
| 65 | ||
| 60 | 66 | post(who: Person, text: unknown): Message { |
| 61 | if (this.muted.has(who.id)) throw new ChatError("you are muted"); | |
| 67 | const mute = this.mutes.get(who.id); | |
| 68 | if (mute && (mute.until === null || this.now() < mute.until)) throw new ChatError("you are muted"); | |
| 62 | 69 | const clean = cleanText(text); |
| 63 | 70 | const now = this.now(); |
| 64 | 71 | const last = this.lastPost.get(who.id); |
| @@ -88,9 +95,14 @@ export class Chat { | ||
| 88 | 95 | this.save(); |
| 89 | 96 | } |
| 90 | 97 | |
| 91 | mute(id: string, name: string, on: boolean): void { | |
| 92 | if (on) this.muted.set(id, name); | |
| 93 | else this.muted.delete(id); | |
| 98 | /** Mutes until the time `until` (ms), or for good when null. */ | |
| 99 | mute(id: string, name: string, until: number | null): void { | |
| 100 | this.mutes.set(id, { name, until }); | |
| 101 | this.save(); | |
| 102 | } | |
| 103 | ||
| 104 | unmute(id: string): void { | |
| 105 | this.mutes.delete(id); | |
| 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 | // a name string is the old format: muted for good | |
| 131 | for (const [id, v] of Object.entries(stored.muted as Record<string, unknown>)) { | |
| 132 | if (typeof v === "string") this.mutes.set(id, { name: v, until: null }); | |
| 133 | else if (v && typeof v === "object") { | |
| 134 | const { name, until } = v as { name?: unknown; until?: unknown }; | |
| 135 | if (typeof name === "string" && (until === null || typeof until === "number")) this.mutes.set(id, { name, until }); | |
| 136 | } | |
| 137 | } | |
| 138 | } | |
| 118 | 139 | this.next = Math.max(0, ...this.messages.map((m) => m.id)) + 1; |
| 119 | 140 | } |
| 120 | 141 | |
| 121 | 142 | private save(): void { |
| 122 | 143 | if (!this.file) return; |
| 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].map(([id, m]) => [id, m.until === null ? m.name : m])) })); | |
| 125 | 146 | renameSync(`${this.file}.tmp`, this.file); |
| 126 | 147 | } |
| 127 | 148 | } |
Software/so101/src/lobby.ts +2 −1
| @@ -342,7 +342,8 @@ export class Lobby { | ||
| 342 | 342 | if (typeof m.id !== "string" || !USER_ID.test(m.id) || typeof m.on !== "boolean") return this.send(seat, { type: "error", error: "mute whom?" }); |
| 343 | 343 | const id = m.id; |
| 344 | 344 | const name = [...this.seats].find((s) => s.who?.id === id)?.who?.name ?? [...this.chat.messages].reverse().find((x) => x.from?.id === id)?.from?.name ?? this.chat.muted.get(id) ?? id; |
| 345 | this.chat.mute(id, name, m.on); | |
| 345 | if (m.on) this.chat.mute(id, name, null); | |
| 346 | else this.chat.unmute(id); | |
| 346 | 347 | for (const s of this.seats) { |
| 347 | 348 | if (s.who?.id === id) this.send(s, this.hello(s)); |
| 348 | 349 | if (s.who?.owner) this.send(s, this.mutedList()); |
new above
offline