summaryrefslogtreecommitdiff
path: root/src/pages/outbox.json.ts
blob: 9e2505f2ca721710c0d4eea5a5e6032b5ace1f55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import type { APIRoute } from "astro";
import { env } from "../lib/environment.ts";
import { fromPosts, isOriginal } from "../lib/collection/helpers.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 {
          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 ?? "",
          },
        };
      }),
  );
  const outbox = {
    "@context": "https://www.w3.org/ns/activitystreams",
    id: new URL("outbox.json", site).href,
    type: "OrderedCollection",
    totalItems: 1,
    orderedItems,
  };
  return new Response(JSON.stringify(outbox), {
    headers: { "Content-Type": "application/activity+json" },
  });
};