/* ═══════════════════════════════════════════════════════════════ 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, json, loadBlocks, loadFeaturedCards, marks } from "../shape.js"; import { readFeeds } from "../feeds.js"; const blog = new Hono(); 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; } /* The first paragraph of each post with no excerpt of its own, the card's fallback text, by post id. Only that one row per post: a card doesn't need the rest of the body. */ function firstParagraphs(db, rows) { const ids = rows.filter((row) => !row.excerpt).map((row) => row.id); if (ids.length === 0) return new Map(); const found = db .prepare( `SELECT owner_id, text FROM ( SELECT owner_id, text, ROW_NUMBER() OVER (PARTITION BY owner_id ORDER BY sort_order) AS n FROM content_blocks WHERE owner_kind = 'blog_post' AND slot = 'body' AND type = 'paragraph' AND text IS NOT NULL AND text <> '' AND owner_id IN (${marks(ids.length)}) ) WHERE n = 1`, ) .all(...ids); return new Map(found.map((row) => [row.owner_id, row.text])); } function shapeSummary(row, authors, firstParagraph) { return { id: row.id, title: row.title, // The card's text: the excerpt, else the body's first paragraph. excerpt: row.excerpt || firstParagraph || 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 }] : []), ], }; } /* A person's page: the published, listed posts they're an author of, newest first, as the same summaries the blog section shows. routes/people.js adds them to /people/:id. */ export function postsBy(db, personId) { const rows = db .prepare( `SELECT b.* FROM blog_posts b JOIN blog_post_authors a ON a.post_id = b.id AND a.person_id = ? WHERE b.is_published = 1 AND b.is_listed = 1 ORDER BY b.published_on IS NULL, b.published_on DESC, b.created_at DESC`, ) .all(personId); const ids = rows.map((row) => row.id); const authors = loadAuthors(db, ids); const first = firstParagraphs(db, rows); return rows.map((row) => shapeSummary(row, authors.get(row.id) ?? [], first.get(row.id))); } /* ── 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, cover_alt: row.cover_alt, show_contents: row.show_contents === 1, 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 first = firstParagraphs(db, posts); // 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: { internal: { title: page.internal_title, posts: posts.map((row) => shapeSummary(row, authors.get(row.id) ?? [], first.get(row.id)), ), }, external: { title: page.external_title, url: page.external_url, label: page.external_label, cards: [...cards, ...latest], }, }, }); }); export default blog;