Files
gerbeur/shared/mention.ts
khannurien c0d03e9aaa v4: users sign in to one gerbeur instance with their account on another — topic instances, each with its own admins, domain and data, had no way to share accounts. Sign-in is federated over a subset of OpenID Connect, the authorization code flow with PKCE: each instance is an identity provider for its own users and a client for the others', and signs what it tells them with an Ed25519 key kept in federation_keys and published at /.well-known/jwks.json, beside its metadata at /.well-known/openid-configuration. Only sign-in is federated: an account from another instance is a local users row linked to (issuer, subject) in user_identities, with no password (password_hash ''), so sessions, roles, permissions, the socket and every foreign key work as before.
A remote user is name@host here, in users.username, in paths (/@alice@music.example) and in mentions, through one handle rule in shared/validation.ts that paths, MENTION_RE and Markdown all build on; Markdown undoes GFM's email autolink of a handle. Their name, email and avatar come from their home at each sign-in and by its signed events (federation_outbox, delivered by the federation job with backoff): a rename at home leaves the old handle an alias here, and a password change or a withdrawn grant ends their sessions here. Their password, email, avatar and name can't change here (assertLocalUser), nor can they sign in elsewhere from here.

An admin keeps the trusted and blocked instances on /manage (federation:manage): a trusted instance's users sign in from a button on the login page, a blocked one's can't and are signed out at once, and any other's join with an invite from the register page. The /authorize page asks the user once per instance, and /settings lists the instances their account signs in to, each disconnectable; /login?next= brings a signed-out visitor back to /authorize. Emails stay unique among local accounts only, and migration 0024 adds the tables, makes that index partial and reserves authorize and federation as category slugs. remote/gerbeur-instance.ts is the only way to other instances: https and public addresses only, no redirects, no certificate fallback, unless GERBEUR_FEDERATION_ALLOW_PRIVATE lets two instances on one machine talk.

298 backend tests and 702 frontend tests pass, and deno check, deno fmt, deno lint, tsc, eslint, doc:lint and i18n:compile --strict are clean; checked end to end with two built instances on one machine: sign-in through the other's login and consent pages, consent skipped once granted and asked again after disconnecting, a rename and a password change at home reaching the other, and blocking; screenshots of the login, consent, profile, settings and manage pages at 1280 and 390px.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 14:23:00 +00:00

57 lines
2.0 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
*/
import { HANDLE_SOURCE } from "./validation.ts";
/**
* Matches an @handle, a local `@name` or a remote user's `@name@host`
* (`HANDLE_SOURCE`), with the handle 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 = new RegExp(
`(?<![[(\\w])@(${HANDLE_SOURCE})`,
"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);
}