import { createHash, timingSafeEqual } from 'node:crypto' import type { Context, Hono } from 'hono' import { deleteCookie, getCookie, setCookie } from 'hono/cookie' import { appview, bskyProfile } from './appview.ts' import { globalHides, reports } from './db.ts' /** * Admin dashboard at /admin: sign in with ADMIN_TOKEN, triage reported events * (dismiss, or hide for everyone), and undo global hides. Off entirely when ADMIN_TOKEN is unset. */ const TOKEN = process.env.ADMIN_TOKEN ?? '' const COOKIE = 'juno_admin' const secure = (process.env.PUBLIC_URL ?? '').startsWith('https://') const digest = (s: string) => createHash('sha256').update(s).digest() const matches = (given?: string) => !!TOKEN && !!given && timingSafeEqual(digest(given), digest(TOKEN)) const authed = (c: Context) => matches(getCookie(c, COOKIE)) async function eventView(uri: string) { return appview.event(uri, '').catch(() => null) } export function mountAdmin(app: Hono) { if (!TOKEN) return app.get('/admin', (c) => c.html(authed(c) ? dashboard : login())) app.post('/admin/login', async (c) => { const form = await c.req.parseBody() const code = typeof form.code === 'string' ? form.code.trim() : '' if (!matches(code)) { await new Promise((r) => setTimeout(r, 1000)) // slow down guessing return c.html(login('That code didn’t work.'), 401) } setCookie(c, COOKIE, code, { httpOnly: true, secure, sameSite: 'Strict', path: '/admin', maxAge: 60 * 60 * 24 * 30 }) return c.redirect('/admin') }) app.post('/admin/logout', (c) => { deleteCookie(c, COOKIE, { path: '/admin' }) return c.redirect('/admin') }) // JSON API for the page. Everything below needs the cookie. app.use('/admin/api/*', async (c, next) => { if (!authed(c)) return c.json({ error: 'signed out' }, 401) await next() }) // Open reports, one entry per event, most recently reported first. app.get('/admin/api/reports', async (c) => { const open = reports.open() const byEvent = new Map() for (const r of open) byEvent.set(r.subject_uri, [...(byEvent.get(r.subject_uri) ?? []), r]) const reporterDids = [...new Set(open.map((r) => r.reporter))] const profiles = new Map(await Promise.all(reporterDids.map(async (d) => [d, await bskyProfile(d)] as const))) const items = await Promise.all( [...byEvent].map(async ([uri, rs]) => ({ uri, event: await eventView(uri), reports: rs.map((r) => ({ id: r.id, reason: r.reason, details: r.details, createdAt: r.created_at, reporter: profiles.get(r.reporter) ?? { did: r.reporter } })), })), ) return c.json({ items }) }) app.get('/admin/api/hidden', async (c) => { const uris = globalHides.list() return c.json({ items: await Promise.all(uris.map(async (uri) => ({ uri, event: await eventView(uri) }))) }) }) const uriFrom = async (c: Context) => { const { uri } = await c.req.json<{ uri?: string }>().catch(() => ({}) as { uri?: string }) return typeof uri === 'string' && uri.startsWith('at://') ? uri : undefined } app.post('/admin/api/dismiss', async (c) => { const uri = await uriFrom(c) if (!uri) return c.json({ error: 'uri required' }, 400) reports.resolve(uri, 'dismissed') return c.json({ ok: true }) }) app.post('/admin/api/hide', async (c) => { const uri = await uriFrom(c) if (!uri) return c.json({ error: 'uri required' }, 400) globalHides.add(uri) reports.resolve(uri, 'hidden') return c.json({ ok: true }) }) app.post('/admin/api/unhide', async (c) => { const uri = await uriFrom(c) if (!uri) return c.json({ error: 'uri required' }, 400) globalHides.remove(uri) return c.json({ ok: true }) }) } // ---------- pages ---------- const head = (title: string) => ` ${title} · Juno admin ` const login = (error?: string) => `${head('Sign in')}

Juno admin ✦

Enter the admin code.

${error ? `

${error}

` : ''}
` const dashboard = `${head('Reports')}

Juno admin ✦

Loading…

`