// Form parsing for every post-write action: composing a new thread or reply, // editing one, and naming a moderation subject. Each parser returns the same // {ok:true,...} | {ok:false,status,message} shape so a route action is a // `if (!parsed.ok) return fail(parsed.status, ...)` away from its record // assembly, and the field rules live here rather than per route. import { AtUri } from '@atproto/syntax' import type { ModSubject } from '#lib/server/ozone-client.js' import { parse_facets, type FacetRecord } from '#lib/richtext/facets.js' import { too_long } from '#lib/richtext/grapheme.js' // The fields the composer form carries. A subject line is thread-only, so it // is read only when the caller declares a limit for it; the video side channel // (poster/duration/presentation) is reply-only and simply comes back undefined // for a form that doesn't send it. export type ComposerInput = | { ok: true name?: string body?: string facets?: FacetRecord[] subject?: string spoiler: boolean sage: boolean file?: File poster?: File duration?: number presentation?: string } | { ok: false; status: number; message: string } export function parse_composer_input( data: FormData, limits: { name: number; body: number; subject?: number } ): ComposerInput { const name = data.get('name')?.toString() || undefined // formatting markup is parsed out here: `body` becomes clean text and the // styles it carried are embedded as facets on the record const raw_body = data.get('body')?.toString() || undefined const parsed_body = raw_body !== undefined ? parse_facets(raw_body) : undefined const body = parsed_body?.text || undefined const facets = parsed_body?.facets.length ? parsed_body.facets : undefined // the subject line is optional even where it is allowed: an image and a body // are enough of a post const subject = limits.subject === undefined ? undefined : data.get('subject')?.toString()?.trim() || undefined const over = (limits.subject !== undefined ? too_long('subject', subject, limits.subject) : undefined) ?? too_long('name', name, limits.name) ?? too_long('body', body, limits.body) if (over) return { ok: false, status: 400, message: over } if (name?.trim()?.startsWith('@')) { return { ok: false, status: 400, message: 'name may not start with @' } } const entry = data.get('file') const poster_entry = data.get('poster') const duration_raw = data.get('duration')?.toString() return { ok: true, name, body, facets, subject, spoiler: data.get('spoiler') != null, sage: data.get('sage') != null, file: entry instanceof File && entry.size > 0 ? entry : undefined, // video-only side channel: a client-captured poster frame plus hints the // server can't derive without decoding the video poster: poster_entry instanceof File && poster_entry.size > 0 ? poster_entry : undefined, duration: duration_raw ? Number(duration_raw) : undefined, presentation: data.get('presentation')?.toString() || undefined } } // Shared validation for the thread page's edit actions: pull the fields an // edit may change (name, body) off the form, reject impersonating names, and // confirm the caller owns the record. Image embeds are intentionally not // editable. export type EditInput = | { ok: true; target: AtUri; name?: string; body: string } | { ok: false; status: number; message: string } export function parse_edit_input( data: FormData, session_did: string ): EditInput { const uri = data.get('uri')?.toString() if (!uri) return { ok: false, status: 400, message: 'uri required' } const name = data.get('name')?.toString()?.trim() || undefined const body = data.get('body')?.toString() ?? '' if (name?.startsWith('@')) { return { ok: false, status: 400, message: 'name may not start with @' } } let target: AtUri try { target = new AtUri(uri) } catch { return { ok: false, status: 400, message: 'invalid uri' } } // put only works against your own repo anyway, but fail loudly and early if (target.did !== session_did) { return { ok: false, status: 403, message: 'not your post' } } return { ok: true, target, name, body } } // Shared subject parsing for the thread page's moderate/takedown actions: an // account subject (hide/takedown user) carries only a did; a post subject // carries a strongRef. `hide` is the only effect that applies to an account. export type ModSubjectInput = | { ok: true; subject: ModSubject } | { ok: false; status: number; message: string } export function parse_mod_subject(data: FormData): ModSubjectInput { const did = data.get('did')?.toString() if (did) return { ok: true, subject: { did } } const uri = data.get('uri')?.toString() const cid = data.get('cid')?.toString() if (!uri || !cid) { return { ok: false, status: 400, message: 'uri and cid required' } } return { ok: true, subject: { uri, cid } } }