From b052f741d935abd2f51423abf3fcda9157844b5c Mon Sep 17 00:00:00 2001 From: "netop://ウィビ" Date: Sat, 11 Apr 2026 14:42:43 -0700 Subject: initial commit --- src/routes/api/mastodon.json/+server.ts | 55 +++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/routes/api/mastodon.json/+server.ts (limited to 'src/routes/api/mastodon.json') diff --git a/src/routes/api/mastodon.json/+server.ts b/src/routes/api/mastodon.json/+server.ts new file mode 100644 index 0000000..72cbd38 --- /dev/null +++ b/src/routes/api/mastodon.json/+server.ts @@ -0,0 +1,55 @@ + + + +/*** IMPORT ------------------------------------------- ***/ + +import { error, json } from "@sveltejs/kit"; + +/*** EXPORT ------------------------------------------- ***/ + +export const POST = async({ fetch }) => { + try { + const response = await fetch("https://social.coop/users/netopwibby/outbox?page=true", { + headers: { "Content-Type": "application/json" }, + method: "GET" + }); + + const { orderedItems } = await response.json(); + const { object } = findCreateType(orderedItems); + + return json({ + created: object.published, + content: object.content, + link: object.url, + media: processAttachments(object.attachment) + }); + } catch(welp) { + console.error(`Error fetching latest Mastodon post: ${String(welp)}`); + return error(500); + } +}; + +/*** HELPER ------------------------------------------- ***/ + +function findCreateType(arr: Array<{ [key: string]: any }>): { [key: string]: any } | undefined { + return arr.find(obj => obj.type === "Create"); +} + +function processAttachments(attachments: Array<{ [key: string]: any }>): Array { + const media = []; + + if (attachments) { + for (const attachment of attachments) { + // TODO + // : use blurhash given to us by mastodon + // : https://github.com/woltapp/blurhash/tree/master/TypeScript#example + // : `attachment` comes with blurhash, height, mediaType, width + // if (attachment && attachment.url) + + // @ts-ignore | Argument of type "any" is not assignable to parameter of type "never". + media.push(attachment.url); + } + } + + return media; +} -- cgit v1.2.3