Files
gerbeur/shared/mention.ts
khannurien 134d1a0b97 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>
2026-10-09 12:54:09 +00:00

51 lines
1.8 KiB
TypeScript

/**
* Makes the server notify the same users the chat highlights and the editor
* suggests, by defining once what counts as an @mention.
*
* @module
*/
/**
* Matches an @username, with the name as its first group. The `@` must not
* follow a word character, so an email address mentions no one. Nor may it
* follow `[` or `(`, which leaves out a Markdown link's text or URL starting
* with `@`, and also a plain `(@name)`. The editor's suggestions match its
* source at the caret; the rest goes through {@link mentionedUsernames}.
* `Markdown` links mentions with a looser pattern of its own, on text already
* parsed, where code and URLs are out of the way.
*/
export const MENTION_RE: RegExp = /(?<![[(\w])@(\w+)/g;
/**
* Returns the names `body` mentions, in lower case, each once. Code, fenced
* or inline, and URLs mention no one, as `Markdown` shows them. The server
* notifies by it, and the chat highlights and chimes by it.
*/
export function mentionedUsernames(body: string): string[] {
const prose = body
// A fence left open runs to the end, as Markdown renders it.
.replace(/```[\s\S]*?(?:```|$)/g, " ")
.replace(/`[^`\n]*`/g, " ")
.replace(/https?:\/\/\S+/g, " ");
return [
...new Set([...prose.matchAll(MENTION_RE)].map((m) => m[1].toLowerCase())),
];
}
/**
* The name `@everyone` reaches. Said by a role with `chat:everyone` in the
* chat, it notifies every user, so no account may take it
* (`assertUsernameAvailable`).
*/
export const EVERYONE = "everyone";
/**
* Checks whether `body` says `@everyone`, in any case, by the rule of
* {@link mentionedUsernames}. Only the chat service reads it, to set a
* message's `mentionsEveryone` when its author may; the client reads that
* flag, not the text.
*/
export function mentionsEveryone(body: string): boolean {
return mentionedUsernames(body).includes(EVERYONE);
}