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

Parked opened this
Closed without merging

smith/arm:smith/236-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 +61 −11
Software/so101/src/chat.test.ts +30 −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,38 @@ 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 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 });
5684 });
Software/so101/src/chat.ts +29 −8
@@ -42,8 +42,8 @@ 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 /** id → mute; `until` null is for good. */
46 private readonly mutes = new Map<string, { name: string; until: number | null }>();
4747 private next = 1;
4848 private readonly lastPost = new Map<string, number>();
4949 private readonly file: string | undefined;
@@ -57,8 +57,15 @@ export class Chat {
5757 if (this.file) this.load(this.file);
5858 }
5959
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
6066 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");
6269 const clean = cleanText(text);
6370 const now = this.now();
6471 const last = this.lastPost.get(who.id);
@@ -88,9 +95,14 @@ export class Chat {
8895 this.save();
8996 }
9097
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);
94106 this.save();
95107 }
96108
@@ -114,14 +126,23 @@ export class Chat {
114126 .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")))
115127 .slice(-KEEP);
116128 }
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 }
118139 this.next = Math.max(0, ...this.messages.map((m) => m.id)) + 1;
119140 }
120141
121142 private save(): void {
122143 if (!this.file) return;
123144 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])) }));
125146 renameSync(`${this.file}.tmp`, this.file);
126147 }
127148 }
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