|
| 1 | +import {DEFAULT_BRAND_CONFIG} from '#/lib/community/BrandContext' |
| 2 | +import {type ComputedBrandConfig} from '#/lib/community/types' |
| 3 | +import {LATINSKY_BRAND_SLUG, LATINSKY_HANDLE_SUFFIXES} from '#/lib/constants' |
| 4 | +import {BRAND_SERVICE_URL} from '#/env' |
| 5 | + |
| 6 | +/** |
| 7 | + * Minimal shape of a stored account needed to resolve its community brand. |
| 8 | + * Kept local (rather than importing SessionAccount) so this module stays a pure, |
| 9 | + * testable leaf with no session dependency. |
| 10 | + */ |
| 11 | +export interface ResolvableAccount { |
| 12 | + /** The account's PDS URL, e.g. `https://blacksky.app`. */ |
| 13 | + pdsUrl?: string |
| 14 | + /** The account's handle, e.g. `alice.latinsky.app`. */ |
| 15 | + handle?: string |
| 16 | + /** Community slug stamped on the account at signup (deterministic fast path). */ |
| 17 | + communitySlug?: string |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Normalize a host or URL for equality comparison: lowercase, drop the scheme, |
| 22 | + * any path/query, the port, and a trailing dot. Mirrors the server-side |
| 23 | + * `normalize_host` in acorn/brand so both sides agree on what "same host" means. |
| 24 | + */ |
| 25 | +export function normalizeHost(input: string): string { |
| 26 | + let s = input.trim() |
| 27 | + const scheme = s.indexOf('://') |
| 28 | + if (scheme !== -1) s = s.slice(scheme + 3) |
| 29 | + s = s.split('/')[0] |
| 30 | + s = s.split('?')[0] |
| 31 | + s = s.split(':')[0] |
| 32 | + return s.replace(/\.+$/, '').toLowerCase() |
| 33 | +} |
| 34 | + |
| 35 | +async function fetchResolvedConfig( |
| 36 | + query: string, |
| 37 | +): Promise<ComputedBrandConfig | null> { |
| 38 | + try { |
| 39 | + const res = await fetch(`${BRAND_SERVICE_URL}/brands/resolve?${query}`, { |
| 40 | + headers: {accept: 'application/json'}, |
| 41 | + }) |
| 42 | + if (!res.ok) return null |
| 43 | + const data = (await res.json()) as {computed?: ComputedBrandConfig} |
| 44 | + return data.computed ?? null |
| 45 | + } catch { |
| 46 | + return null |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +/** Summary of a published brand, as returned by the brand service `GET /brands`. */ |
| 51 | +export interface BrandListEntry { |
| 52 | + slug: string |
| 53 | + name: string |
| 54 | + displayName: string |
| 55 | + themeColor: string |
| 56 | + logo: string |
| 57 | + description: string |
| 58 | + /** The community's PDS URL — signup points at this when the community is picked. */ |
| 59 | + pds: string |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * List published communities (for the signup community picker). Returns an empty |
| 64 | + * array when the service is unreachable so callers can fall back to a |
| 65 | + * Blacksky-only default. |
| 66 | + */ |
| 67 | +export async function fetchBrandList(): Promise<BrandListEntry[]> { |
| 68 | + try { |
| 69 | + const res = await fetch(`${BRAND_SERVICE_URL}/brands`, { |
| 70 | + headers: {accept: 'application/json'}, |
| 71 | + }) |
| 72 | + if (!res.ok) return [] |
| 73 | + const data = (await res.json()) as {brands?: BrandListEntry[]} |
| 74 | + return data.brands ?? [] |
| 75 | + } catch { |
| 76 | + return [] |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/** Resolve a brand's computed config by community slug. */ |
| 81 | +export function fetchBrandBySlug( |
| 82 | + slug: string, |
| 83 | +): Promise<ComputedBrandConfig | null> { |
| 84 | + return fetchResolvedConfig(`slug=${encodeURIComponent(slug)}`) |
| 85 | +} |
| 86 | + |
| 87 | +/** Resolve a brand's computed config by the account's PDS host. */ |
| 88 | +export function resolveBrandForPds( |
| 89 | + pdsUrl: string, |
| 90 | +): Promise<ComputedBrandConfig | null> { |
| 91 | + return fetchResolvedConfig(`pds=${encodeURIComponent(normalizeHost(pdsUrl))}`) |
| 92 | +} |
| 93 | + |
| 94 | +// The default community (Blacksky) owns/shares this PDS and its config is bundled |
| 95 | +// into the app, so the host is taken from the default brand config rather than a |
| 96 | +// separate constant — single source of truth for "the default community's PDS". |
| 97 | +function isBlackskyPds(pdsUrl: string | undefined): boolean { |
| 98 | + if (!pdsUrl) return false |
| 99 | + return ( |
| 100 | + normalizeHost(pdsUrl) === |
| 101 | + normalizeHost(DEFAULT_BRAND_CONFIG.services.pds.url) |
| 102 | + ) |
| 103 | +} |
| 104 | + |
| 105 | +function isLatinskyHandle(handle: string | undefined): boolean { |
| 106 | + if (!handle) return false |
| 107 | + const h = handle.toLowerCase() |
| 108 | + return LATINSKY_HANDLE_SUFFIXES.some(suffix => h.endsWith(suffix)) |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Resolve the community brand config for an account. Resolution order: |
| 113 | + * 1. `communitySlug` stamped at signup — deterministic, and required to |
| 114 | + * disambiguate communities that share a PDS. |
| 115 | + * 2. The Blacksky PDS is the default/shared PDS and is never resolved by PDS: |
| 116 | + * - a Latinsky handle on it → Latinsky (the handle carveout); |
| 117 | + * - anything else → null (the bundled Blacksky default). Blacksky itself |
| 118 | + * has no brand-service config by design. |
| 119 | + * 3. Any other PDS host → brand (the general case). |
| 120 | + * Returns `null` when nothing matches or the service is unreachable; callers |
| 121 | + * fall back to the bundled Blacksky default. |
| 122 | + */ |
| 123 | +export async function resolveBrandForAccount( |
| 124 | + account: ResolvableAccount, |
| 125 | +): Promise<ComputedBrandConfig | null> { |
| 126 | + if (account.communitySlug) { |
| 127 | + return fetchBrandBySlug(account.communitySlug) |
| 128 | + } |
| 129 | + if (isBlackskyPds(account.pdsUrl)) { |
| 130 | + // Shared/default PDS — not PDS-resolvable. Latinsky is identified by handle; |
| 131 | + // a plain Blacksky account falls through to the bundled default. |
| 132 | + if (isLatinskyHandle(account.handle)) { |
| 133 | + return fetchBrandBySlug(LATINSKY_BRAND_SLUG) |
| 134 | + } |
| 135 | + return null |
| 136 | + } |
| 137 | + if (account.pdsUrl) { |
| 138 | + return resolveBrandForPds(account.pdsUrl) |
| 139 | + } |
| 140 | + return null |
| 141 | +} |
0 commit comments