diff options
Diffstat (limited to 'src/pages')
-rw-r--r-- | src/pages/outbox.json.ts | 98 |
1 files changed, 77 insertions, 21 deletions
diff --git a/src/pages/outbox.json.ts b/src/pages/outbox.json.ts index 9e2505f..64b0b18 100644 --- a/src/pages/outbox.json.ts +++ b/src/pages/outbox.json.ts @@ -1,37 +1,93 @@ import type { APIRoute } from "astro"; import { env } from "../lib/environment.ts"; -import { fromPosts, isOriginal } from "../lib/collection/helpers.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<Response> => { - const orderedItems = await fromPosts( - isOriginal, - (x) => - x.map(({ id, data, rendered }) => { - const { dateCreated, title } = data; - return { + 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: "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 ?? "", - }, - }; - }), + 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: 1, + totalItems: orderedItems.length, orderedItems, }; return new Response(JSON.stringify(outbox), { |