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 #208

Parked opened this
Closed without merging

smith/arm:smith/191-a-chat-mute-can-last-10-min-1-h-24-h-or → main 1 commit fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
2 files changed +57 −8
Software/so101/src/chat.test.ts +29 −1
@@ -1,5 +1,5 @@
11 import { describe, expect, test } from "bun:test";
2 import { mkdtempSync } from "node:fs";
2 import { mkdtempSync, writeFileSync } from "node:fs";
33 import { tmpdir } from "node:os";
44 import { join } from "node:path";
55 import { Chat, KEEP, MAX_CHARS } from "./chat.ts";
@@ -53,4 +53,32 @@ describe("chat", () => {
5353 expect(chat.delete(line.id)).toBe(false);
5454 expect(chat.messages).toEqual([]);
5555 });
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 });
5684 });
Software/so101/src/chat.ts +28 −7
@@ -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 → 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 }>();
4747 private next = 1;
4848 private readonly lastPost = new Map<string, number>();
4949 private readonly file: string | undefined;
@@ -57,6 +57,17 @@ 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 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
6071 post(who: Person, text: unknown): Message {
6172 if (this.muted.has(who.id)) throw new ChatError("you are muted");
6273 const clean = cleanText(text);
@@ -88,9 +99,10 @@ export class Chat {
8899 this.save();
89100 }
90101
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 });
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 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 }
118138 this.next = Math.max(0, ...this.messages.map((m) => m.id)) + 1;
119139 }
120140
121141 private save(): void {
122142 if (!this.file) return;
143 void this.muted; // drops the expired
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) }));
125146 renameSync(`${this.file}.tmp`, this.file);
126147 }
127148 }

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs