import { isQuietByClock, previousLocalHour, QUIET_START_HOUR } from './clock.ts' import { DISCORD_POST_MESSAGE_CODE, executeExport } from './cross-package.ts' import { buildDigest, recordDelivery } from './digest.ts' import { getState, LAST_DIGEST_KEY, SETTINGS_KEY, type LastDigest, type Settings } from './store.ts' import { parseNow } from './status-core.ts' /** * Kent's personal Discord #reports channel (Sentry, Kody, Feedback, and EDM * reports already post here). Override with the `settings.fallbackChannelId` * state row or the `channelId` input. */ export const DEFAULT_FALLBACK_CHANNEL_ID = '1530233790516039881' /** Discord SUPPRESS_EMBEDS so digest links stay one line each. */ const SUPPRESS_EMBEDS = 1 << 2 export type FallbackInput = { /** Build the message but do not post or mark delivered. */ dryRun?: boolean /** Run even if still inside quiet hours (manual runs). */ force?: boolean /** Discord channel override. */ channelId?: string /** Optional ISO instant to treat as now (tests). */ now?: string } /** Pure: was a digest delivered since this quiet window began (last 10 PM MT)? */ export function deliveredThisWindow(last: LastDigest | null, now: Date): boolean { if (!last?.at) return false const windowStart = previousLocalHour(now, QUIET_START_HOUR) return Date.parse(last.at) >= windowStart.getTime() } /** * 8:15 AM MT safety net (package job `fallback-digest`, DISABLED by default). * If no digest was marked delivered since last night's 10 PM, post a plain * version to Discord via @kentcdodds/discord post-message, then mark those * items delivered. If the post fails nothing is marked, so the Kody routine * can still deliver later. * * @param input.dryRun - Build only; do not post or mark delivered * @param input.force - Allow a run before 8 AM MT * @param input.channelId - Discord channel override (default #reports) * @returns `{ ok, skipped? | posted?, channelId, content }` * * @example * import fallbackDigest from 'kody:@kentcdodds/quiet-hours/fallback-digest' * const preview = await fallbackDigest({ dryRun: true, force: true }) */ export default async function fallbackDigest(input: FallbackInput = {}) { const now = parseNow(input?.now) if (isQuietByClock(now) && input?.force !== true) { return { ok: true as const, skipped: 'still quiet hours; fallback runs after 8 AM MT' } } const last = await getState(LAST_DIGEST_KEY) if (deliveredThisWindow(last, now) && input?.force !== true) { return { ok: true as const, skipped: 'digest already delivered', lastDigest: last } } const settings = (await getState(SETTINGS_KEY)) ?? {} const channelId = (typeof input?.channelId === 'string' && input.channelId.trim()) || settings.fallbackChannelId || DEFAULT_FALLBACK_CHANNEL_ID const built = await buildDigest(now) const content = `${built.text}\n-# Fallback: the 8 AM summary was not marked delivered by 8:15.` if (input?.dryRun === true) { return { ok: true as const, dryRun: true as const, channelId, content, count: built.items.length } } const posted = (await executeExport(DISCORD_POST_MESSAGE_CODE, { channelId, content, flags: SUPPRESS_EMBEDS, })) as { id?: string } | null const record = await recordDelivery(built.items, 'discord-fallback', now) return { ok: true as const, posted: true as const, messageId: posted?.id ?? null, channelId, content, digestId: record.id, count: built.items.length, } }