/* ═══════════════════════════════════════════════════════════════ BLOG ROUTES: read-only, mounted under /api GET /blog/:id one published post, its body and authors GET /community-blog the Community page's blog section A post is public once published. Unlisted (is_listed = 0) keeps its page but leaves it out of the section's "newest", though it can still be pinned there: pinning is attaching it by hand. The section has two halves, each pinned first and then newest, up to its slot count (pinned posts always show, even past it): internal blog_posts external featured cards owned by the community page, then items from its RSS/Atom feeds (feeds.js), skipping any URL a pinned card already shows. Shaped as featured cards, so FeaturedCards.tsx draws both. Authors are published people, in order, and/or the post's guest name. An author who is unpublished is left off rather than named, the same as on every roster. ═══════════════════════════════════════════════════════════════ */ import { Hono } from "hono"; import { asBool, loadBlocks, loadFeaturedCards, paragraphs } from "../shape.js"; import { readFeeds } from "../feeds.js"; const blog = new Hono(); const CACHE = "public, max-age=60, stale-while-revalidate=300"; const json = (c, body) => c.json(body, 200, { "Cache-Control": CACHE }); const marks = (n) => Array(n).fill("?").join(","); const PAGE_ID = "community"; const splitTags = (tags) => (tags ?? "") .split(",") .map((tag) => tag.trim()) .filter(Boolean); /* Published authors for a batch of posts, keyed by post id. */ function loadAuthors(db, ids) { const byPost = new Map(); if (ids.length === 0) return byPost; const rows = db .prepare( `SELECT a.post_id, p.id, p.display_name, p.pronouns, p.tagline, p.photo FROM blog_post_authors a JOIN people p ON p.id = a.person_id AND p.is_published = 1 WHERE a.post_id IN (${marks(ids.length)}) ORDER BY a.post_id, a.sort_order`, ) .all(...ids); for (const row of rows) { const list = byPost.get(row.post_id) ?? []; list.push({ id: row.id, name: row.display_name, pronouns: row.pronouns, tagline: row.tagline, photo: row.photo, }); byPost.set(row.post_id, list); } return byPost; } function shapeSummary(row, authors, body) { return { id: row.id, title: row.title, // The card's text: the excerpt, else the body's first paragraph. excerpt: row.excerpt || paragraphs(body)[0] || null, cover_image: row.cover_image, published_on: row.published_on, tags: splitTags(row.tags), authors: [ ...authors.map((a) => ({ id: a.id, name: a.name })), ...(row.guest_author ? [{ id: null, name: row.guest_author }] : []), ], }; } /* ── One post ──────────────────────────────────────────────── */ blog.get("/blog/:id", (c) => { const db = c.get("db"); const id = c.req.param("id"); const row = db.prepare(`SELECT * FROM blog_posts WHERE id = ? AND is_published = 1`).get(id); if (!row) return c.json({ error: "No such post" }, 404); const authors = loadAuthors(db, [id]).get(id) ?? []; const body = loadBlocks(db, "blog_post", [id], "body").get(id) ?? []; return json(c, { post: { id: row.id, title: row.title, excerpt: row.excerpt, cover_image: row.cover_image, published_on: row.published_on, tags: splitTags(row.tags), authors, guest: row.guest_author ? { name: row.guest_author, photo: row.guest_photo } : null, blocks: body, is_listed: asBool(row.is_listed), }, }); }); /* ── The Community page's section ──────────────────────────── */ blog.get("/community-blog", async (c) => { const db = c.get("db"); const page = db.prepare(`SELECT * FROM community_page WHERE id = ?`).get(PAGE_ID); if (!page) return c.json({ error: "The community page hasn't been set up." }, 500); // Ours: pinned, then newest. const pinned = db .prepare( `SELECT b.* FROM community_page_pinned_posts pp JOIN blog_posts b ON b.id = pp.post_id AND b.is_published = 1 WHERE pp.page_id = ? ORDER BY pp.sort_order`, ) .all(PAGE_ID); const room = Math.max(0, page.internal_slots - pinned.length); const pinnedIds = pinned.map((row) => row.id); const newest = room ? db .prepare( `SELECT * FROM blog_posts WHERE is_published = 1 AND is_listed = 1 ${pinnedIds.length ? `AND id NOT IN (${marks(pinnedIds.length)})` : ""} ORDER BY published_on IS NULL, published_on DESC, created_at DESC LIMIT ?`, ) .all(...pinnedIds, room) : []; const posts = [...pinned, ...newest]; const ids = posts.map((row) => row.id); const authors = loadAuthors(db, ids); const bodies = loadBlocks(db, "blog_post", ids, "body"); // Theirs: pinned cards, then the feeds' newest. const cards = loadFeaturedCards(db, "community_page", PAGE_ID); const feedRows = db .prepare(`SELECT url, label FROM community_page_feeds WHERE page_id = ? ORDER BY sort_order`) .all(PAGE_ID); const shown = new Set(cards.map((card) => card.url).filter(Boolean)); const spare = Math.max(0, page.external_slots - cards.length); const fromFeeds = spare && feedRows.length ? await readFeeds(feedRows) : []; const latest = fromFeeds .filter((item) => !shown.has(item.url)) .slice(0, spare) .map((item) => ({ kind: "link", ref: null, url: item.url, eyebrow: item.source, title: item.title, blurb: item.excerpt, image: item.image ? { file: item.image, kind: "featured" } : null, published: item.published, })); return json(c, { community_blog: { title: page.blog_title, blurb: page.blog_blurb, internal: { title: page.internal_title, posts: posts.map((row) => shapeSummary(row, authors.get(row.id) ?? [], bodies.get(row.id) ?? []), ), }, external: { title: page.external_title, url: page.external_url, label: page.external_label, cards: [...cards, ...latest], }, }, }); }); export default blog;