|
| 1 | +import {BLOG_FROM, BLOG_FULL_COLUMNS, BlogQueryRow, toAdminBlogPost} from 'api/helpers/blog' |
| 2 | +import {APIErrors, APIHandler} from 'api/helpers/endpoint' |
| 3 | +import {MAX_BLOG_CONTENT_LENGTH} from 'common/blog/blog' |
| 4 | +import {parseJsonContentToText} from 'common/util/parse' |
| 5 | +import {throwErrorIfNotAdmin} from 'shared/helpers/auth' |
| 6 | +import {createSupabaseDirectClient} from 'shared/supabase/init' |
| 7 | + |
| 8 | +/** |
| 9 | + * Create a post. Always as a `draft`, never published, and never broadcast. |
| 10 | + * |
| 11 | + * Publishing is a second, separate call to `update-blog-post`, for the same reason creating a |
| 12 | + * spotlight cannot publish one: the thing that makes a post public also mails every member, and an |
| 13 | + * endpoint that can do that as a side effect of "save what I just typed" is one mis-click away from |
| 14 | + * sending an unfinished draft to everybody. |
| 15 | + * |
| 16 | + * Admins only, not mods — publishing under the Compass name is an editorial act, not a moderation |
| 17 | + * one. Same split as `create-spotlight`. |
| 18 | + */ |
| 19 | +export const createBlogPost: APIHandler<'create-blog-post'> = async (props, auth) => { |
| 20 | + await throwErrorIfNotAdmin(auth.uid) |
| 21 | + |
| 22 | + const {slug, title, excerpt, content, coverImageUrl} = props |
| 23 | + |
| 24 | + // Flattened once, here, so that every consumer of `content_text` — reading time today, search |
| 25 | + // later — reads the same string, and none of them has to know how to walk a ProseMirror tree. |
| 26 | + const contentText = parseJsonContentToText(content ?? null) |
| 27 | + if (contentText.length > MAX_BLOG_CONTENT_LENGTH) { |
| 28 | + throw APIErrors.badRequest( |
| 29 | + `That post is ${contentText.length} characters; the limit is ${MAX_BLOG_CONTENT_LENGTH}`, |
| 30 | + ) |
| 31 | + } |
| 32 | + |
| 33 | + const pg = createSupabaseDirectClient() |
| 34 | + |
| 35 | + // The unique index on `slug` is the real guard — two admins creating the same slug at once is not |
| 36 | + // a race we can win with a pre-check — so this catches it after the fact and reports it in terms of |
| 37 | + // what the admin did rather than as a constraint name. |
| 38 | + const existing = await pg.oneOrNone<{status: string}>( |
| 39 | + `select status from blog_posts where slug = $(slug)`, |
| 40 | + {slug}, |
| 41 | + ) |
| 42 | + if (existing) { |
| 43 | + throw APIErrors.badRequest( |
| 44 | + `The slug “${slug}” is already taken by a ${existing.status} post — edit that one, or pick another slug`, |
| 45 | + ) |
| 46 | + } |
| 47 | + |
| 48 | + const inserted = await pg.one<{id: string}>( |
| 49 | + `insert into blog_posts (slug, title, excerpt, content, content_text, cover_image_url, author_id) |
| 50 | + values ($(slug), $(title), $(excerpt), $(content), $(contentText), $(coverImageUrl), $(authorId)) |
| 51 | + returning id`, |
| 52 | + { |
| 53 | + slug, |
| 54 | + title, |
| 55 | + excerpt: excerpt?.trim() || null, |
| 56 | + content: content ?? {}, |
| 57 | + contentText, |
| 58 | + coverImageUrl: coverImageUrl || null, |
| 59 | + authorId: auth.uid, |
| 60 | + }, |
| 61 | + ) |
| 62 | + |
| 63 | + // Re-read through the byline join rather than returning the inserted row, so the admin page gets |
| 64 | + // exactly the same shape as `get-blog-posts-admin` and does not need a second code path for the |
| 65 | + // row it just created. |
| 66 | + const row = await pg.one<BlogQueryRow>( |
| 67 | + `select ${BLOG_FULL_COLUMNS} ${BLOG_FROM} where b.id = $(id)`, |
| 68 | + {id: inserted.id}, |
| 69 | + ) |
| 70 | + |
| 71 | + return {post: toAdminBlogPost(row)} |
| 72 | +} |
0 commit comments