summaryrefslogtreecommitdiff
path: root/src/routes/api/mastodon.json
diff options
context:
space:
mode:
authornetop://ウィビ <paul@webb.page>2026-04-11 14:42:43 -0700
committernetop://ウィビ <paul@webb.page>2026-04-11 14:42:43 -0700
commitb052f741d935abd2f51423abf3fcda9157844b5c (patch)
treed01d9db0e4c4f4f9093662a049db366b8b2301af /src/routes/api/mastodon.json
initial commitHEADprimary
Diffstat (limited to 'src/routes/api/mastodon.json')
-rw-r--r--src/routes/api/mastodon.json/+server.ts55
1 files changed, 55 insertions, 0 deletions
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<string> {
+ 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;
+}