v4: a handle with this instance's own host names the local user — another instance writes bob from here as bob@gerbeur.example, so a mention or a link copied from there named no one here: /@bob@gerbeur.example was a 404, and the mention notified nobody. localHandle (shared/validation.ts) drops this instance's host, its API's or its pages', from a handle; resolveUsername reads names by it, so the page, the API and the Open Graph 301 reach bob at /@bob, and so do the mention notifications and the chat's highlight and chime.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
khannurien
2026-10-09 22:11:39 +00:00
parent 6dfba07341
commit 9608520675
11 changed files with 79 additions and 12 deletions

View File

@@ -4,7 +4,7 @@ import {
isFederationEvent,
parseInstance,
} from "../federation.ts";
import { HANDLE_RE, splitHandle } from "../validation.ts";
import { HANDLE_RE, localHandle, splitHandle } from "../validation.ts";
Deno.test("parseInstance: a host or a URL names an instance's origin", () => {
assertEquals(parseInstance("music.example"), "https://music.example");
@@ -21,6 +21,13 @@ Deno.test("parseInstance: a host or a URL names an instance's origin", () => {
assertEquals(instanceHost("http://localhost:8001"), "localhost:8001");
});
Deno.test("handles: this instance's host is dropped, another's kept", () => {
const self = (host: string) => host === "gerbeur.example";
assertEquals(localHandle("bob@gerbeur.example", self), "bob");
assertEquals(localHandle("bob@music.example", self), "bob@music.example");
assertEquals(localHandle("bob", self), "bob");
});
Deno.test("handles: a name, and a host for a user from another instance", () => {
assertEquals(splitHandle("bob"), { name: "bob" });
assertEquals(splitHandle("alice@music.example"), {

View File

@@ -75,6 +75,22 @@ export function splitHandle(handle: string): { name: string; host?: string } {
: { name: handle.slice(0, at), host: handle.slice(at + 1) };
}
/**
* Returns `handle` as this instance writes it, `isSelfHost` telling its own
* host. Another instance writes a user from here with this one's host,
* `bob@gerbeur.example`, who is `bob` here: a mention or a link copied from
* there names them so. Any other handle is returned as it is. The server
* resolves names by it (`resolveUsername`, the mention notifications), and
* the chat's highlight (`mentionsUser`) reads mentions by it.
*/
export function localHandle(
handle: string,
isSelfHost: (host: string) => boolean,
): string {
const { name, host } = splitHandle(handle);
return host && isSelfHost(host) ? name : handle;
}
/**
* How often a dump's or a playlist's slug may change, counted from the aliases
* the changes left: 3 times a day. Moderators are exempt. slug-service refuses