v4: a moderator's or an admin's @everyone in the chat notifies every user — there was no way to reach everyone at once. The new chat:everyone permission, held by moderators and admins, lets a chat message saying @everyone notify every user but its author and those reading the chat, through notifyEveryone, after notifyMentions, so a user also named is told once, by name; the notification is a user_mentioned with everyone set, which the notifications page and the digest word as "mentioned everyone in the chat". chat_messages.mentions_everyone, sent as ChatMessage.mentionsEveryone, is set from the author's role each time the body is written, so a user's @everyone reaches no one, a moderator's edit of a user's message can't make it reach everyone, and a demoted author's next edit loses it. Only an edit that adds @everyone notifies, so fixing an announcement's typo reaches no one new. The client highlights and chimes by the flag, never by the text; Markdown shows @everyone as a mention only in a flagged message, and never links it to a profile. The chat composer offers @everyone first to those who may say it. No account may be named everyone, in any case: migration 0023 adds the column, flags the moderators' and admins' past @everyone messages, renames a user named everyone to everyone_N and drops an alias everyone. 265 backend tests and 681 frontend tests pass, and deno check, tsc, eslint, doc:lint and i18n:compile --strict are clean; checked in screenshots of the chat at 1280 and 390px, with a moderator's and a user's @everyone and the suggestions open.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -28,14 +28,14 @@ Deno.test("runMigrations: a fresh database is left as schema.sql made it", () =>
|
||||
.get() as {
|
||||
n: number;
|
||||
};
|
||||
assertEquals(applied.n, 22);
|
||||
assertEquals(applied.n, 23);
|
||||
// Running again applies nothing.
|
||||
runMigrations(db);
|
||||
assertEquals(
|
||||
(db.prepare(`SELECT COUNT(*) as n FROM schema_migrations;`).get() as {
|
||||
n: number;
|
||||
}).n,
|
||||
22,
|
||||
23,
|
||||
);
|
||||
db.close();
|
||||
});
|
||||
|
||||
39
api/test/db/migrations/0023_mention_everyone.test.ts
Normal file
39
api/test/db/migrations/0023_mention_everyone.test.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import "../../setup.ts";
|
||||
import { assertEquals } from "@std/assert";
|
||||
import { DatabaseSync } from "node:sqlite";
|
||||
import { up as up0023 } from "../../../db/migrations/0023_mention_everyone.ts";
|
||||
|
||||
Deno.test("migration 0023 flags past @everyone and frees the name", () => {
|
||||
const db = new DatabaseSync(":memory:");
|
||||
db.exec(`
|
||||
CREATE TABLE users (id TEXT PRIMARY KEY, username TEXT, role TEXT);
|
||||
CREATE TABLE user_aliases (username TEXT PRIMARY KEY, user_id TEXT);
|
||||
CREATE TABLE chat_messages (id TEXT PRIMARY KEY, user_id TEXT, body TEXT);
|
||||
INSERT INTO users VALUES
|
||||
('m', 'mod', 'moderator'), ('u', 'Everyone', 'user'),
|
||||
('t', 'everyone_2', 'user'), ('a', 'ann', 'user');
|
||||
INSERT INTO user_aliases VALUES ('everyone_3', 't'), ('EVERYONE', 'a');
|
||||
INSERT INTO chat_messages VALUES
|
||||
('1', 'm', 'hi @everyone'), ('3', 'u', 'hi @everyone'),
|
||||
('4', 'm', 'hi all');
|
||||
`);
|
||||
// In code, it says nothing.
|
||||
db.prepare(`INSERT INTO chat_messages VALUES ('2', 'm', ?);`).run(
|
||||
"hi `@everyone`",
|
||||
);
|
||||
up0023(db);
|
||||
up0023(db); // idempotent
|
||||
|
||||
const flagged = db.prepare(
|
||||
`SELECT id FROM chat_messages WHERE mentions_everyone = 1;`,
|
||||
).all() as { id: string }[];
|
||||
assertEquals(flagged.map((r) => r.id), ["1"]);
|
||||
const names = db.prepare(`SELECT id, username FROM users ORDER BY id;`)
|
||||
.all() as { id: string; username: string }[];
|
||||
assertEquals(names.find((r) => r.id === "u")?.username, "everyone_4");
|
||||
const aliases = db.prepare(`SELECT username FROM user_aliases;`).all() as {
|
||||
username: string;
|
||||
}[];
|
||||
assertEquals(aliases.map((r) => r.username), ["everyone_3"]);
|
||||
db.close();
|
||||
});
|
||||
@@ -669,6 +669,19 @@ test("renaming: the old name redirects for good, and nobody else can take it", a
|
||||
assertEquals((await rename(other.id, old)).status, 409);
|
||||
// Nor to a current one.
|
||||
assertEquals((await rename(other.id, renamed)).status, 409);
|
||||
// Nor to everyone, in any case, which the chat's @everyone takes.
|
||||
assertEquals((await rename(other.id, "Everyone")).status, 409);
|
||||
assertEquals(
|
||||
(await post("/api/users", {
|
||||
json: {
|
||||
username: "everyone",
|
||||
password: PASSWORD,
|
||||
email: `${uniqueName()}@example.test`,
|
||||
inviteToken: invite(other.id),
|
||||
},
|
||||
})).status,
|
||||
409,
|
||||
);
|
||||
// Signing in takes the current name only.
|
||||
assertEquals(
|
||||
(await post("/api/sessions", {
|
||||
|
||||
@@ -448,6 +448,73 @@ wsTest(
|
||||
},
|
||||
);
|
||||
|
||||
wsTest(
|
||||
"chat: @everyone notifies all who aren't reading, when its author may say it",
|
||||
async (connect) => {
|
||||
const mod = await createUser({ role: "moderator" });
|
||||
const user = await createUser();
|
||||
const named = await createUser();
|
||||
const reader = await createUser();
|
||||
const m = await connect(mod);
|
||||
const u = await connect(user);
|
||||
const r = await connect(reader);
|
||||
r.send({ type: "chat_focus", open: true });
|
||||
await r.sync();
|
||||
const mentioned = () =>
|
||||
Object.fromEntries(
|
||||
(db.prepare(
|
||||
`SELECT user_id, data FROM notifications WHERE type = 'user_mentioned';`,
|
||||
).all() as { user_id: string; data: string }[])
|
||||
.map((row) => [row.user_id, !!JSON.parse(row.data).everyone]),
|
||||
);
|
||||
|
||||
// A user's @everyone reaches no one.
|
||||
u.send({ type: "chat_send", body: "@everyone hi" });
|
||||
const plain = (await m.next("chat_message")).message;
|
||||
assertEquals(plain.mentionsEveryone, undefined);
|
||||
assertEquals(mentioned(), {});
|
||||
|
||||
// A moderator's reaches all but its author and the reader, and a user
|
||||
// named too is told by name.
|
||||
m.send({
|
||||
type: "chat_send",
|
||||
body: `@Everyone, and @${named.username}`,
|
||||
});
|
||||
const byMod = (x: { message: { userId: string } }) =>
|
||||
x.message.userId === mod.id;
|
||||
const sent = (await u.next("chat_message", byMod)).message;
|
||||
assertEquals(sent.mentionsEveryone, true);
|
||||
const expected = { [user.id]: true, [named.id]: false };
|
||||
assertEquals(mentioned(), expected);
|
||||
|
||||
// Fixing a typo reaches no one new, not even a newcomer.
|
||||
const late = await createUser();
|
||||
m.send({ type: "chat_edit", id: sent.id, body: "@everyone, and all" });
|
||||
await u.next("chat_message_updated");
|
||||
assertEquals(mentioned(), expected);
|
||||
|
||||
// A moderator's edit can't make a user's message reach everyone.
|
||||
m.send({ type: "chat_edit", id: plain.id, body: "@everyone edited" });
|
||||
const edited = (await u.next("chat_message_updated")).message;
|
||||
assertEquals(edited.mentionsEveryone, undefined);
|
||||
assertEquals(mentioned(), expected);
|
||||
|
||||
// An edit that adds it notifies, the newcomer included.
|
||||
m.send({ type: "chat_send", body: "soon" });
|
||||
const draft = (await u.next("chat_message", byMod)).message;
|
||||
m.send({ type: "chat_edit", id: draft.id, body: "now, @everyone" });
|
||||
await u.next("chat_message_updated");
|
||||
assertEquals(
|
||||
count(
|
||||
`SELECT COUNT(*) AS n FROM notifications WHERE data LIKE ?;`,
|
||||
`%${draft.id}%`,
|
||||
),
|
||||
3,
|
||||
);
|
||||
assertEquals(mentioned()[late.id], true);
|
||||
},
|
||||
);
|
||||
|
||||
wsTest("chat: typing and read markers", async (connect) => {
|
||||
const alice = await createUser();
|
||||
const bob = await createUser();
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { createUser, db, test } from "../helpers.ts";
|
||||
import { assertEquals, assertThrows } from "@std/assert";
|
||||
import {
|
||||
chatMessageMentionsEveryone,
|
||||
createChatMessage,
|
||||
deleteChatMessage,
|
||||
getChatMessagesAfter,
|
||||
@@ -161,3 +162,26 @@ test("read state: with no messages at all the marker can't be moved", async () =
|
||||
insertMessage(other.id, "later", T(5));
|
||||
assertEquals(getChatReadState(me.id).unread, 1);
|
||||
});
|
||||
|
||||
test("mentionsEveryone: set from the author's role when the body is written", async () => {
|
||||
const mod = await createUser({ role: "moderator" });
|
||||
const user = await createUser();
|
||||
const flagged = createChatMessage(mod.id, "hi @everyone");
|
||||
assertEquals(flagged.mentionsEveryone, true);
|
||||
assertEquals(chatMessageMentionsEveryone(flagged.id), true);
|
||||
assertEquals(
|
||||
createChatMessage(user.id, "hi @everyone").mentionsEveryone,
|
||||
undefined,
|
||||
);
|
||||
assertEquals(
|
||||
createChatMessage(mod.id, "`@everyone`").mentionsEveryone,
|
||||
undefined,
|
||||
);
|
||||
assertEquals(chatMessageMentionsEveryone(crypto.randomUUID()), false);
|
||||
|
||||
// Demoted, the author's next edit loses it.
|
||||
db.prepare(`UPDATE users SET role = 'user' WHERE id = ?;`).run(mod.id);
|
||||
const edited = updateChatMessage(flagged.id, "hi @everyone!", mod.id, false);
|
||||
assertEquals(edited.mentionsEveryone, undefined);
|
||||
assertEquals(chatMessageMentionsEveryone(flagged.id), false);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user