hippofish/packages/backend/src/server/api/mastodon/ApiMastodonCompatibleService.ts

143 lines
4.8 KiB
TypeScript
Raw Normal View History

import Router from "@koa/router";
import megalodon, { MegalodonInterface } from "megalodon";
2023-02-11 00:41:19 +01:00
import { apiAuthMastodon } from "./endpoints/auth.js";
import { apiAccountMastodon } from "./endpoints/account.js";
import { apiStatusMastodon } from "./endpoints/status.js";
import { apiFilterMastodon } from "./endpoints/filter.js";
import { apiTimelineMastodon } from "./endpoints/timeline.js";
import { apiNotificationsMastodon } from "./endpoints/notifications.js";
import { apiSearchMastodon } from "./endpoints/search.js";
import { getInstance } from "./endpoints/meta.js";
import { convertAnnouncement, convertFilter } from "./converters.js";
import { convertId, IdType } from "../index.js";
2023-02-11 00:41:19 +01:00
export function getClient(
BASE_URL: string,
authorization: string | undefined,
): MegalodonInterface {
const accessTokenArr = authorization?.split(" ") ?? [null];
const accessToken = accessTokenArr[accessTokenArr.length - 1];
2023-02-11 00:41:19 +01:00
const generator = (megalodon as any).default;
2023-07-05 21:53:00 +02:00
const client = generator(BASE_URL, accessToken) as MegalodonInterface;
2023-02-11 00:41:19 +01:00
return client;
}
export function apiMastodonCompatible(router: Router): void {
2023-02-11 00:41:19 +01:00
apiAuthMastodon(router);
apiAccountMastodon(router);
apiStatusMastodon(router);
apiFilterMastodon(router);
apiTimelineMastodon(router);
apiNotificationsMastodon(router);
apiSearchMastodon(router);
2023-02-11 00:41:19 +01:00
router.get("/v1/custom_emojis", async (ctx) => {
const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`;
const accessTokens = ctx.request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = await client.getInstanceCustomEmojis();
ctx.body = data.data;
} catch (e: any) {
2023-02-11 00:41:19 +01:00
console.error(e);
2023-02-07 23:05:26 +01:00
ctx.status = 401;
ctx.body = e.response.data;
}
});
2023-02-11 00:41:19 +01:00
router.get("/v1/instance", async (ctx) => {
2023-02-07 23:05:26 +01:00
const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`;
const accessTokens = ctx.request.headers.authorization;
2023-02-11 00:41:19 +01:00
const client = getClient(BASE_URL, accessTokens); // we are using this here, because in private mode some info isnt
// displayed without being logged in
try {
const data = await client.getInstance();
2023-02-18 20:58:02 +01:00
ctx.body = await getInstance(data.data);
} catch (e: any) {
2023-02-11 00:41:19 +01:00
console.error(e);
2023-02-07 23:05:26 +01:00
ctx.status = 401;
ctx.body = e.response.data;
}
});
2023-03-22 17:43:05 +01:00
router.get("/v1/announcements", async (ctx) => {
const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`;
const accessTokens = ctx.request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = await client.getInstanceAnnouncements();
2023-05-02 05:32:18 +02:00
ctx.body = data.data.map((announcement) =>
convertAnnouncement(announcement),
);
} catch (e: any) {
console.error(e);
ctx.status = 401;
ctx.body = e.response.data;
}
});
2023-04-25 00:45:34 +02:00
router.post<{ Params: { id: string } }>(
"/v1/announcements/:id/dismiss",
async (ctx) => {
const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`;
const accessTokens = ctx.request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = await client.dismissInstanceAnnouncement(
2023-07-03 00:18:30 +02:00
convertId(ctx.params.id, IdType.FirefishId),
);
2023-04-25 00:45:34 +02:00
ctx.body = data.data;
} catch (e: any) {
console.error(e);
ctx.status = 401;
ctx.body = e.response.data;
}
},
);
2023-03-22 17:43:05 +01:00
router.get("/v1/filters", async (ctx) => {
const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`;
const accessTokens = ctx.request.headers.authorization;
const client = getClient(BASE_URL, accessTokens); // we are using this here, because in private mode some info isnt
// displayed without being logged in
try {
const data = await client.getFilters();
2023-05-02 05:32:18 +02:00
ctx.body = data.data.map((filter) => convertFilter(filter));
2023-03-22 17:43:05 +01:00
} catch (e: any) {
console.error(e);
ctx.status = 401;
ctx.body = e.response.data;
}
});
router.get("/v1/trends", async (ctx) => {
const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`;
const accessTokens = ctx.request.headers.authorization;
const client = getClient(BASE_URL, accessTokens); // we are using this here, because in private mode some info isnt
// displayed without being logged in
try {
const data = await client.getInstanceTrends();
ctx.body = data.data;
} catch (e: any) {
console.error(e);
ctx.status = 401;
ctx.body = e.response.data;
}
});
router.get("/v1/preferences", async (ctx) => {
const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`;
const accessTokens = ctx.request.headers.authorization;
const client = getClient(BASE_URL, accessTokens); // we are using this here, because in private mode some info isnt
// displayed without being logged in
try {
const data = await client.getPreferences();
ctx.body = data.data;
} catch (e: any) {
console.error(e);
ctx.status = 401;
ctx.body = e.response.data;
}
});
2023-02-11 00:41:19 +01:00
}