Skip to article
Documentation

src/app/api/inbox/events/route.ts

Linked repository file from this build.

import { NextResponse } from "next/server";
import { apiRoute } from "@/lib/api-route";
import { getActor } from "@/lib/auth/server";
import { clearEvents, queryEvents } from "@/lib/db/inbox-store";
export const dynamic = "force-dynamic";
export const GET = apiRoute("Unable to load notifications.", async (request) => {
  const since = new URL(request.url).searchParams.get("since") ?? undefined;
  if (since && !Number.isFinite(Date.parse(since)))
    return NextResponse.json({ error: "Invalid event date." }, { status: 400 });
  return NextResponse.json(
    await queryEvents(await getActor(request), since ? new Date(since).toISOString() : undefined),
  );
});

export const POST = apiRoute("Unable to clear notifications.", async (request) => {
  const actor = await getActor(request);
  const input: unknown = await request.json().catch(() => null);
  const through =
    input && typeof input === "object" && "through" in input ? input.through : undefined;
  if (typeof through !== "string" || !Number.isFinite(Date.parse(through)))
    return NextResponse.json({ error: "Choose a valid notification date." }, { status: 400 });
  await clearEvents(actor, new Date(Math.min(Date.parse(through), Date.now())).toISOString());
  return NextResponse.json({ ok: true });
});