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 (after #211 makes main's tests green) #231
smith/arm:smith/213-a-chat-mute-can-last-10-min-1-h-24-h-or → main 2 commits fork
3 files
changed +55
−10
Software/so101/src/chat.test.ts +26 −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,34 @@ 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 blocks until it ends and then drops out", () => { | |
| 58 | let clock = 0; | |
| 59 | const chat = new Chat({ 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, and the old file format loads", () => { | |
| 70 | const file = join(mkdtempSync(join(tmpdir(), "chat-")), "chat.json"); | |
| 71 | new Chat({ file }).mute("a", "alice", null); | |
| 72 | expect(new Chat({ file, now: () => 1e15 }).muted.get("a")).toBe("alice"); | |
| 73 | const timed = new Chat({ file }); | |
| 74 | timed.mute("b", "bob", 5000); | |
| 75 | expect(new Chat({ file, now: () => 0 }).muted.has("b")).toBe(true); | |
| 76 | expect(new Chat({ file, now: () => 6000 }).muted.has("b")).toBe(false); | |
| 77 | Bun.write(file, JSON.stringify({ messages: [], muted: { c: "carol" } })); | |
| 78 | return Bun.sleep(20).then(() => expect(new Chat({ file }).muted.get("c")).toBe("carol")); | |
| 79 | }); | |
| 56 | 80 | }); |
Software/so101/src/chat.ts +27 −7
| @@ -42,8 +42,7 @@ 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 | private readonly mutes = new Map<string, { name: string; until: number | null }>(); | |
| 47 | 46 | private next = 1; |
| 48 | 47 | private readonly lastPost = new Map<string, number>(); |
| 49 | 48 | private readonly file: string | undefined; |
| @@ -88,9 +87,25 @@ export class Chat { | ||
| 88 | 87 | this.save(); |
| 89 | 88 | } |
| 90 | 89 | |
| 91 | mute(id: string, name: string, on: boolean): void { | |
| 92 | if (on) this.muted.set(id, name); | |
| 93 | else this.muted.delete(id); | |
| 90 | /** id → name of everyone muted now; a mute that has run out is dropped. */ | |
| 91 | get muted(): Map<string, string> { | |
| 92 | const now = this.now(); | |
| 93 | const out = new Map<string, string>(); | |
| 94 | for (const [id, m] of this.mutes) { | |
| 95 | if (m.until !== null && now >= m.until) this.mutes.delete(id); | |
| 96 | else out.set(id, m.name); | |
| 97 | } | |
| 98 | return out; | |
| 99 | } | |
| 100 | ||
| 101 | /** Mutes until the epoch-ms `until`, or for good when null. */ | |
| 102 | mute(id: string, name: string, until: number | null): void { | |
| 103 | this.mutes.set(id, { name, until }); | |
| 104 | this.save(); | |
| 105 | } | |
| 106 | ||
| 107 | unmute(id: string): void { | |
| 108 | this.mutes.delete(id); | |
| 94 | 109 | this.save(); |
| 95 | 110 | } |
| 96 | 111 | |
| @@ -114,14 +129,19 @@ export class Chat { | ||
| 114 | 129 | .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 | 130 | .slice(-KEEP); |
| 116 | 131 | } |
| 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); | |
| 132 | if (stored.muted && typeof stored.muted === "object") { | |
| 133 | for (const [id, v] of Object.entries(stored.muted)) { | |
| 134 | if (typeof v === "string") this.mutes.set(id, { name: v, until: null }); // the old format: for good | |
| 135 | else if (v && typeof (v as any).name === "string" && (typeof (v as any).until === "number" || (v as any).until === null)) this.mutes.set(id, { name: (v as any).name, until: (v as any).until }); | |
| 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; |
| 123 | 143 | mkdirSync(dirname(this.file), { recursive: true }); |
| 124 | writeFileSync(`${this.file}.tmp`, JSON.stringify({ messages: this.messages, muted: Object.fromEntries(this.muted) })); | |
| 144 | writeFileSync(`${this.file}.tmp`, JSON.stringify({ messages: this.messages, muted: Object.fromEntries(this.mutes) })); | |
| 125 | 145 | renameSync(`${this.file}.tmp`, this.file); |
| 126 | 146 | } |
| 127 | 147 | } |
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