import type { APIRoute } from "astro"; import { env } from "../lib/environment.ts"; import { fromPosts, isOriginal, isTranslation, sortFirstCreated, } from "../lib/collection/helpers.ts"; import { getCollection } from "astro:content"; import { Original, Translation } from "../lib/collection/schemas.ts"; import { defined } from "../utils/anonymous.ts"; const { PUBLIC_SITE_URL } = env; const site = new URL(PUBLIC_SITE_URL); export const GET: APIRoute = async (): Promise => { const collection = await getCollection("blog"); const orderedItems = collection.map(({ id, data, rendered }) => { const original = Original.safeParse(data); if (original.success) { const { dateCreated, title } = original.data; return { id: new URL(`blog/read/${id}`, site).href, type: "Create", actor: new URL("actor.json", site).href, published: dateCreated.toISOString(), to: ["https://www.w3.org/ns/activitystreams#Public"], object: { id: new URL(`blog/read/${id}`, site).href, type: "Article", name: title, url: new URL(`blog/read/${id}`, site).href, content: rendered?.html ?? "", alternate: collection.filter(isTranslation).map(({ id, data }) => { const { lang } = data; return { "type": "Link", href: new URL(`blog/read/${id}`, site).href, "hreflang": lang, }; }), }, }; } const translation = Translation.safeParse(data); if (translation.success) { const { dateCreated, title, translationOf } = translation.data; return { id: new URL(`blog/read/${id}`, site).href, type: "Create", actor: new URL("actor.json", site).href, published: dateCreated.toISOString(), to: ["https://www.w3.org/ns/activitystreams#Public"], object: { id: new URL(`blog/read/${id}`, site).href, type: "Article", name: title, url: new URL(`blog/read/${id}`, site).href, content: rendered?.html ?? "", inReplyTo: new URL(`blog/read/${translationOf.id}`, site).href, }, }; } const micro = Translation.safeParse(data); if (micro.success) { const { dateCreated, title } = micro.data; return { id: new URL(`blog/read/${id}`, site).href, type: "Create", actor: new URL("actor.json", site).href, published: dateCreated.toISOString(), to: ["https://www.w3.org/ns/activitystreams#Public"], object: { id: new URL(`blog/read/${id}`, site).href, type: "Note", name: title, url: new URL(`blog/read/${id}`, site).href, content: rendered?.html ?? "", }, }; } return undefined; }).filter(defined).sort((a, b) => new Date(a.published).getTime() - new Date(b.published).getTime() ); const outbox = { "@context": "https://www.w3.org/ns/activitystreams", id: new URL("outbox.json", site).href, type: "OrderedCollection", totalItems: orderedItems.length, orderedItems, }; return new Response(JSON.stringify(outbox), { headers: { "Content-Type": "application/activity+json" }, }); };