/** * What every part of the feed and of Linked products needs to know about this site: its Hub id, * its address, its timezone and whether campuses and publishing are on. */ import type { FeedContext } from './build'; /* eslint-disable @typescript-eslint/no-require-imports -- CommonJS */ const prisma = require('../config/db'); const { getSetting } = require('../utils/settingsCache') as { getSetting(key: string, fallback?: string): Promise }; const { campusesEnabled } = require('../utils/campuses') as { campusesEnabled(): Promise }; /* eslint-enable @typescript-eslint/no-require-imports */ /** Setting: "Publish my events" (on unless set to "false"). */ export const PUBLISH_KEY = 'feed_publish'; export async function publishOn(): Promise { return String(await getSetting(PUBLISH_KEY, 'true')).toLowerCase() !== 'false'; } /** The site's address, as it tells the Hub (hub/report.js installInfo). */ export async function siteUrl(): Promise { const url = await getSetting('app_base_url', process.env.FRONTEND_URL || ''); return String(url || process.env.FRONTEND_URL || '').split(',')[0]!.trim().replace(/\/+$/, ''); } export async function hubInstallId(): Promise { const row = await prisma.appSetting.findUnique({ where: { key: 'hub_install_id' } }); return row?.value || null; } /** Placeholder id for a site not connected to the Hub (its JSON-LD still works; it has no feed). */ export const NO_INSTALL_ID = '00000000-0000-4000-8000-000000000000'; export async function feedContext(now = new Date()): Promise { const [installId, baseUrl, campusesOn] = await Promise.all([hubInstallId(), siteUrl(), campusesEnabled().catch(() => false)]); return { installId: installId || NO_INSTALL_ID, connected: !!installId, baseUrl, timezone: process.env.APP_TIMEZONE || 'Africa/Johannesburg', campusesOn, now, }; }