| 1 | /** |
| 2 | * `@handle` mentions in a message: people by username, agents by handle. |
| 3 | * Pure, so they are tested apart from the service. |
| 4 | * |
| 5 | * A message keeps the handles it mentions as ` a b ` (spaces around each), |
| 6 | * so the sidebar finds a viewer's mentions with `LIKE '% name %'` without |
| 7 | * matching `@bobby` for `@bob`. |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * `@` after the start or a character that cannot be part of an address |
| 12 | * or a word (so `me@example.com` is not a mention), then the handle. |
| 13 | */ |
| 14 | const MENTION = /(^|[^a-z0-9_.@-])@([a-z0-9](?:[a-z0-9_-]{0,38}[a-z0-9_])?)/gi; |
| 15 | |
| 16 | /** The handles `body` mentions, lowercased, each once, in order. */ |
| 17 | export function mentionedHandles(body: string): string[] { |
| 18 | const found = new Set<string>(); |
| 19 | for (const match of String(body ?? "").matchAll(MENTION)) found.add(match[2].toLowerCase()); |
| 20 | return [...found]; |
| 21 | } |
| 22 | |
| 23 | /** How a message's mentions are kept: `` (none) or ` a b `. */ |
| 24 | export function mentionsColumn(handles: string[]): string { |
| 25 | return handles.length ? ` ${handles.join(" ")} ` : ""; |
| 26 | } |
| 27 | |
| 28 | /** Whether a kept mentions column names `handle`. */ |
| 29 | export function mentions(column: string | null | undefined, handle: string): boolean { |
| 30 | return !!column && column.includes(` ${handle.toLowerCase()} `); |
| 31 | } |