refactor(federation): extract extractDomain helper from resolveOrCreateReplicatedUser

This commit is contained in:
Jannis Braun
2026-03-31 04:41:14 +02:00
parent a3a7527c9e
commit 77f02c805e
+16 -10
View File
@@ -867,6 +867,20 @@ export async function federationRoutes(app: FastifyInstance): Promise<void> {
// ─── Relay Event Processors ────────────────────────────────────────────────── // ─── Relay Event Processors ──────────────────────────────────────────────────
/**
* Extract bare domain from a homeInstance value.
* Handles both full URLs ("https://nova.ddns.net") and bare domains ("nova.ddns.net").
* Used to normalize homeInstance to a canonical format for identity matching.
*/
export function extractDomain(homeInstance: string): string {
try {
return new URL(homeInstance).hostname;
} catch {
// Already a bare domain or malformed — strip protocol manually
return homeInstance.replace(/^https?:\/\//, '').split('/')[0] ?? homeInstance;
}
}
/** /**
* Resolve a home user ID to a local user. * Resolve a home user ID to a local user.
* Matches users where home_user_id = homeUserId, or where * Matches users where home_user_id = homeUserId, or where
@@ -914,16 +928,8 @@ export function resolveOrCreateReplicatedUser(
const existing = resolveLocalUser(homeUserId, db); const existing = resolveLocalUser(homeUserId, db);
if (existing) return existing; if (existing) return existing;
// Build a username@domain identifier. Extract the domain from the // Extract bare domain from homeInstance for username construction
// homeInstance URL (strip protocol) so it matches the convention used const domain = extractDomain(homeInstance);
// by the normal replicated-user registration path.
let domain: string;
try {
domain = new URL(homeInstance).hostname;
} catch {
// Fallback: strip protocol manually
domain = homeInstance.replace(/^https?:\/\//, '').split('/')[0] ?? homeInstance;
}
// Use the snowflake-style homeUserId as the local part; append the // Use the snowflake-style homeUserId as the local part; append the
// domain so the username is globally unique and human-readable. // domain so the username is globally unique and human-readable.