forked from mirrors/iceshrimp
91b8b7b76b
Co-authored-by: Luna <luna.dragon@suse.com> Reviewed-on: https://iceshrimp.dev/iceshrimp/iceshrimp/pulls/125 Co-authored-by: Luna D Dragon <lunarequest@nullrequest.com> Co-committed-by: Luna D Dragon <lunarequest@nullrequest.com>
91 lines
2.8 KiB
TypeScript
91 lines
2.8 KiB
TypeScript
import megalodon, { MegalodonInterface } from "megalodon";
|
|
import Router from "@koa/router";
|
|
import { getClient } from "../ApiMastodonCompatibleService.js";
|
|
import { IdType, convertId } from "../../index.js";
|
|
import { convertFilter } from "../converters.js";
|
|
|
|
export function apiFilterMastodon(router: Router): void {
|
|
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);
|
|
const body: any = ctx.request.body;
|
|
try {
|
|
const data = await client.getFilters();
|
|
ctx.body = data.data.map((filter) => convertFilter(filter));
|
|
} catch (e: any) {
|
|
console.error(e);
|
|
ctx.status = 401;
|
|
ctx.body = e.response.data;
|
|
}
|
|
});
|
|
|
|
router.get("/v1/filters/:id", 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(
|
|
convertId(ctx.params.id, IdType.IceshrimpId),
|
|
);
|
|
ctx.body = convertFilter(data.data);
|
|
} catch (e: any) {
|
|
console.error(e);
|
|
ctx.status = 401;
|
|
ctx.body = e.response.data;
|
|
}
|
|
});
|
|
|
|
router.post("/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);
|
|
const body: any = ctx.request.body;
|
|
try {
|
|
const data = await client.createFilter(body.phrase, body.context, body);
|
|
ctx.body = convertFilter(data.data);
|
|
} catch (e: any) {
|
|
console.error(e);
|
|
ctx.status = 401;
|
|
ctx.body = e.response.data;
|
|
}
|
|
});
|
|
|
|
router.post("/v1/filters/:id", 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(
|
|
convertId(ctx.params.id, IdType.IceshrimpId),
|
|
body.phrase,
|
|
body.context,
|
|
);
|
|
ctx.body = convertFilter(data.data);
|
|
} catch (e: any) {
|
|
console.error(e);
|
|
ctx.status = 401;
|
|
ctx.body = e.response.data;
|
|
}
|
|
});
|
|
|
|
router.delete("/v1/filters/:id", 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(
|
|
convertId(ctx.params.id, IdType.IceshrimpId),
|
|
);
|
|
ctx.body = data.data;
|
|
} catch (e: any) {
|
|
console.error(e);
|
|
ctx.status = 401;
|
|
ctx.body = e.response.data;
|
|
}
|
|
});
|
|
}
|