/** * The church profile (`crosscode.church/1`) from the church's Accounts (APP-1.0-PLAN §9): name, * short name, logo, icon, colours, brand style, timezone, contact, the church's other sites. * Accounts owns it; the App shows it and never stores it (P16: shared facts are edited in Accounts). * As the Website's church/profile.ts: kept in memory for 10 minutes, the last copy kept while * Accounts can't be reached (tried again after a minute). * * Before the App is linked to Accounts (and in local development) the profile saved in AppSetting * `church_profile_local` is used, if any (the demo saves the demo church's). */ import { CHURCH_PATHS, churchSchema, type Church } from '@crosscode/contracts'; import type { Ctx } from '../context.js'; import { accountsLink } from '../auth/accounts.js'; const FRESH_MS = 10 * 60e3; const RETRY_MS = 60e3; const TIMEOUT_MS = 5000; type Cached = { url: string; church: Church | null; at: number; ok: boolean }; const caches = new WeakMap | null }>(); async function fetchProfile(accountsUrl: string): Promise { const res = await fetch(`${accountsUrl.replace(/\/+$/, '')}${CHURCH_PATHS.profile}`, { headers: { accept: 'application/json' }, signal: AbortSignal.timeout(TIMEOUT_MS) }); if (!res.ok) throw new Error(`Accounts answered ${res.status}`); const parsed = churchSchema.safeParse(await res.json()); if (!parsed.success) throw new Error(`the profile isn't crosscode.church/1 (${parsed.error.issues[0]?.message ?? 'invalid'})`); return parsed.data; } async function localProfile(ctx: Ctx): Promise { const raw = await ctx.settings.json('church_profile_local', null); const parsed = raw ? churchSchema.safeParse(raw) : null; return parsed?.success ? parsed.data : null; } /** The church's profile, or null when there's none yet. Never throws. */ export async function churchProfile(ctx: Ctx): Promise { let link; try { link = await accountsLink(ctx); } catch { link = null; } if (!link) return localProfile(ctx).catch(() => null); let state = caches.get(ctx); if (!state) caches.set(ctx, (state = { cached: null, pending: null })); const { cached } = state; const url = link.url; if (cached && cached.url === url && ctx.now().getTime() - cached.at < (cached.ok ? FRESH_MS : RETRY_MS)) return cached.church; if (!state.pending) { const last = cached?.url === url ? cached.church : null; const s = state; s.pending = fetchProfile(url) .then((church) => { s.cached = { url, church, at: ctx.now().getTime(), ok: true }; return church; }, (e: Error) => { console.warn('[church] profile not fetched from Accounts:', e.message); s.cached = { url, church: last, at: ctx.now().getTime(), ok: false }; return last; }) .finally(() => { s.pending = null; }); } return state.pending; } /** `church.changed` from Accounts: fetched again next time (the last copy is shown meanwhile). */ export function churchChanged(ctx: Ctx): void { const state = caches.get(ctx); if (state?.cached) state.cached = { ...state.cached, at: 0 }; } /** The church's timezone: the profile's, else APP_TIMEZONE (§1). Days and prayer slots are in it. */ export async function churchTimezone(ctx: Ctx): Promise { return (await churchProfile(ctx))?.timezone ?? ctx.config.timezone; }