diff --git a/package.json b/package.json index 2e9aad8a..8de55a1f 100644 --- a/package.json +++ b/package.json @@ -211,4 +211,4 @@ "minimumChangeThreshold": 0, "showDetails": true } -} +} \ No newline at end of file diff --git a/src/app/api/og/route.tsx b/src/app/api/og/route.tsx new file mode 100644 index 00000000..c1c54a9a --- /dev/null +++ b/src/app/api/og/route.tsx @@ -0,0 +1,182 @@ +import { ImageResponse, type NextRequest } from "next/server" +import uniqolor from "uniqolor" + +import { gql } from "@urql/core" + +import { toGateway } from "~/lib/ipfs-parser" +import { client } from "~/queries/graphql" + +const fontNormal = fetch( + "https://github.com/lxgw/LxgwWenKai/releases/download/v1.300/LXGWWenKai-Regular.ttf", +).then((res) => res.arrayBuffer()) + +export const runtime = "edge" + +export const revalidate = 60 * 60 * 24 // 24 hours +export const GET = async (req: NextRequest) => { + try { + const fontData = await fontNormal + + // api/og?characterId=52055¬eId=286&site=innei-4525 + const noteId = req.nextUrl.searchParams.get("noteId") + const characterId = req.nextUrl.searchParams.get("characterId") + + if (!noteId || !characterId) + return new Response(`Missing noteId or characterId`, { status: 400 }) + + const result = await client + .query( + gql` + query getNote($characterId: Int!, $noteId: Int!) { + note( + where: { + note_characterId_noteId_unique: { + characterId: $characterId + noteId: $noteId + } + } + ) { + metadata { + content + } + character { + handle + metadata { + content + } + } + } + } + `, + { + characterId: parseInt(characterId), + noteId: parseInt(noteId), + }, + ) + .toPromise() + const title = result.data.note.metadata?.content?.title + const subtitle = result.data.note.metadata?.content?.summary?.slice(0, 15) + const avatar = toGateway( + result.data.note.character.metadata?.content?.avatars?.[0], + ) + + const siteName = + result.data.note.character.metadata?.content?.site_name || + result.data.note.character.metadata?.content?.name + + const bgAccent = uniqolor(title, { + saturation: [30, 35], + lightness: [60, 70], + }).color + + const bgAccentLight = uniqolor(title, { + saturation: [30, 35], + lightness: [80, 90], + }).color + + const bgAccentUltraLight = uniqolor(title, { + saturation: [30, 35], + lightness: [95, 96], + }).color + + return new ImageResponse( + ( +