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) #222

Parked opened this
Closed without merging

smith/arm:smith/213-a-chat-mute-can-last-10-min-1-h-24-h-or → main 2 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
3 files changed +55 −10
Software/so101/src/chat.test.ts +26 −2
@@ -35,7 +35,7 @@ describe("chat", () => {
3535 let clock = 0;
3636 const chat = new Chat({ file, now: () => (clock += 5000) });
3737 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);
3939 expect(chat.messages.length).toBe(KEEP);
4040 const again = new Chat({ file });
4141 expect(again.messages.length).toBe(KEEP);
@@ -47,10 +47,34 @@ describe("chat", () => {
4747 test("a muted person cannot post; a deleted line is gone", () => {
4848 const chat = new Chat();
4949 const line = chat.post(alice, "spam");
50 chat.mute("a", "alice", true);
50 chat.mute("a", "alice", null);
5151 expect(() => chat.post(alice, "more")).toThrow(/muted/);
5252 expect(chat.delete(line.id)).toBe(true);
5353 expect(chat.delete(line.id)).toBe(false);
5454 expect(chat.messages).toEqual([]);
5555 });
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 });
5680 });
Software/so101/src/chat.ts +27 −7
@@ -42,8 +42,7 @@ export function cleanText(text: unknown): string {
4242
4343 export class Chat {
4444 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 }>();
4746 private next = 1;
4847 private readonly lastPost = new Map<string, number>();
4948 private readonly file: string | undefined;
@@ -88,9 +87,25 @@ export class Chat {
8887 this.save();
8988 }
9089
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);
94109 this.save();
95110 }
96111
@@ -114,14 +129,19 @@ export class Chat {
114129 .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")))
115130 .slice(-KEEP);
116131 }
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 }
118138 this.next = Math.max(0, ...this.messages.map((m) => m.id)) + 1;
119139 }
120140
121141 private save(): void {
122142 if (!this.file) return;
123143 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) }));
125145 renameSync(`${this.file}.tmp`, this.file);
126146 }
127147 }
Software/so101/src/lobby.ts +2 −1
@@ -342,7 +342,8 @@ export class Lobby {
342342 if (typeof m.id !== "string" || !USER_ID.test(m.id) || typeof m.on !== "boolean") return this.send(seat, { type: "error", error: "mute whom?" });
343343 const id = m.id;
344344 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);
346347 for (const s of this.seats) {
347348 if (s.who?.id === id) this.send(s, this.hello(s));
348349 if (s.who?.owner) this.send(s, this.mutedList());

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs