2023-02-07 22:56:07 +01:00
|
|
|
import Router from "@koa/router";
|
2023-02-09 23:21:50 +01:00
|
|
|
import megalodon, { MegalodonInterface } from '@cutls/megalodon';
|
|
|
|
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';
|
2023-02-10 20:45:29 +01:00
|
|
|
import { apiNotificationMastodon } from './endpoints/notifications.js';
|
2023-02-09 23:21:50 +01:00
|
|
|
import { apiSearchMastodon } from './endpoints/search.js';
|
|
|
|
import { getInstance } from './endpoints/meta.js';
|
2023-02-07 22:56:07 +01:00
|
|
|
|
2023-02-09 23:21:50 +01:00
|
|
|
export function getClient(BASE_URL: string, authorization: string | undefined): MegalodonInterface {
|
2023-02-07 22:56:07 +01:00
|
|
|
const accessTokenArr = authorization?.split(' ') ?? [null];
|
|
|
|
const accessToken = accessTokenArr[accessTokenArr.length - 1];
|
|
|
|
const generator = (megalodon as any).default
|
|
|
|
const client = generator('misskey', BASE_URL, accessToken) as MegalodonInterface;
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
2023-02-09 23:21:50 +01:00
|
|
|
export function apiMastodonCompatible(router: Router): void {
|
|
|
|
apiAuthMastodon(router)
|
|
|
|
apiAccountMastodon(router)
|
|
|
|
apiStatusMastodon(router)
|
|
|
|
apiFilterMastodon(router)
|
|
|
|
apiTimelineMastodon(router)
|
2023-02-10 20:45:29 +01:00
|
|
|
apiNotificationMastodon(router)
|
2023-02-09 23:21:50 +01:00
|
|
|
apiSearchMastodon(router)
|
2023-02-07 22:56:07 +01:00
|
|
|
|
2023-02-09 23:21:50 +01:00
|
|
|
router.get('/v1/custom_emojis', async (ctx) => {
|
2023-02-07 22:56:07 +01:00
|
|
|
const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`;
|
|
|
|
const accessTokens = ctx.request.headers.authorization;
|
|
|
|
const client = getClient(BASE_URL, accessTokens);
|
|
|
|
try {
|
2023-02-09 23:21:50 +01:00
|
|
|
const data = await client.getInstanceCustomEmojis();
|
2023-02-07 22:56:07 +01:00
|
|
|
ctx.body = data.data;
|
|
|
|
} catch (e: any) {
|
|
|
|
console.error(e)
|
2023-02-07 23:05:26 +01:00
|
|
|
ctx.status = 401;
|
2023-02-09 23:21:50 +01:00
|
|
|
ctx.body = e.response.data;
|
2023-02-07 22:56:07 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-02-09 23:21:50 +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-09 23:21:50 +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
|
2023-02-07 22:56:07 +01:00
|
|
|
try {
|
2023-02-09 23:21:50 +01:00
|
|
|
const data = await client.getInstance();
|
|
|
|
ctx.body = getInstance(data.data);
|
2023-02-07 22:56:07 +01:00
|
|
|
} catch (e: any) {
|
|
|
|
console.error(e)
|
2023-02-07 23:05:26 +01:00
|
|
|
ctx.status = 401;
|
|
|
|
ctx.body = e.response.data;
|
2023-02-07 22:56:07 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|