firefish/packages/backend/src/server/api/mastodon/endpoints/filter.ts
cutestnekoaqua 717aa899b1
feat: v1 Mastodon API
This commit adds (maybe unstable) support for Mastodons v1 api
also some v2 endpoints, maybe I miss stuff, I dont know.
We will need to test this but it should be kinda stable
and work like (old) butter.

Co-authored-by: Natty <natty.sh.git@gmail.com>
Co-authored-by: cutls <web-pro@cutls.com>
2023-02-09 23:21:50 +01:00

84 lines
2.6 KiB
TypeScript

import megalodon, { MegalodonInterface } from '@cutls/megalodon';
import Router from "@koa/router";
import { koaBody } from 'koa-body';
import { getClient } from '../ApiMastodonCompatibleService.js';
export function apiFilterMastodon(router: Router): void {
router.get('/v1/filters', koaBody(), async (ctx) => {
const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`;
const accessTokens = ctx.request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
const body: any = ctx.request.body;
try {
const data = await client.getFilters();
ctx.body = data.data;
} catch (e: any) {
console.error(e)
ctx.status = 401;
ctx.body = e.response.data;
}
});
router.get('/v1/filters/:id', koaBody(), async (ctx) => {
const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`;
const accessTokens = ctx.request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
const body: any = ctx.request.body;
try {
const data = await client.getFilter(ctx.params.id);
ctx.body = data.data;
} catch (e: any) {
console.error(e)
ctx.status = 401;
ctx.body = e.response.data;
}
});
router.post('/v1/filters', koaBody(), async (ctx) => {
const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`;
const accessTokens = ctx.request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
const body: any = ctx.request.body;
try {
const data = await client.createFilter(body.phrase, body.context, body);
ctx.body = data.data;
} catch (e: any) {
console.error(e)
ctx.status = 401;
ctx.body = e.response.data;
}
});
router.post('/v1/filters/:id', koaBody(), async (ctx) => {
const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`;
const accessTokens = ctx.request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
const body: any = ctx.request.body;
try {
const data = await client.updateFilter(ctx.params.id, body.phrase, body.context);
ctx.body = data.data;
} catch (e: any) {
console.error(e)
ctx.status = 401;
ctx.body = e.response.data;
}
});
router.delete('/v1/filters/:id', koaBody(), async (ctx) => {
const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`;
const accessTokens = ctx.request.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
const body: any = ctx.request.body;
try {
const data = await client.deleteFilter(ctx.params.id);
ctx.body = data.data;
} catch (e: any) {
console.error(e)
ctx.status = 401;
ctx.body = e.response.data;
}
});
}