From 34e6717dabe485537d3d1d4f5440266de7474dac Mon Sep 17 00:00:00 2001 From: Mar0xy Date: Sun, 24 Sep 2023 05:35:50 +0200 Subject: [PATCH] add: filter endpoint to masto api --- .../api/mastodon/MastodonApiServerService.ts | 59 +++++++++++++++++ .../server/api/mastodon/endpoints/filter.ts | 66 +++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 packages/backend/src/server/api/mastodon/endpoints/filter.ts diff --git a/packages/backend/src/server/api/mastodon/MastodonApiServerService.ts b/packages/backend/src/server/api/mastodon/MastodonApiServerService.ts index 467ce2b9e..9212a1fcf 100644 --- a/packages/backend/src/server/api/mastodon/MastodonApiServerService.ts +++ b/packages/backend/src/server/api/mastodon/MastodonApiServerService.ts @@ -15,6 +15,7 @@ import { apiAuthMastodon } from './endpoints/auth.js'; import { apiAccountMastodon } from './endpoints/account.js'; import { apiSearchMastodon } from './endpoints/search.js'; import { apiNotifyMastodon } from './endpoints/notifications.js'; +import { apiFilterMastodon } from './endpoints/filter.js'; const staticAssets = fileURLToPath(new URL('../../../../assets/', import.meta.url)); @@ -667,6 +668,64 @@ export class MastodonApiServerService { reply.code(401).send(e.response.data); } }); + //#endregion + + //#region Filters + fastify.get<{ Params: { id: string } }>("/v1/filters/:id", async (_request, reply) => { + const BASE_URL = `${_request.protocol}://${_request.hostname}`; + const accessTokens = _request.headers.authorization; + const client = getClient(BASE_URL, accessTokens); + try { + const filter = new apiFilterMastodon(_request, client); + !_request.params.id ? reply.send(await filter.getFilters()) : reply.send(await filter.getFilter()); + } catch (e: any) { + console.error(e); + console.error(e.response.data); + reply.code(401).send(e.response.data); + } + }); + + fastify.post("/v1/filters", async (_request, reply) => { + const BASE_URL = `${_request.protocol}://${_request.hostname}`; + const accessTokens = _request.headers.authorization; + const client = getClient(BASE_URL, accessTokens); + try { + const filter = new apiFilterMastodon(_request, client); + reply.send(await filter.createFilter()); + } catch (e: any) { + console.error(e); + console.error(e.response.data); + reply.code(401).send(e.response.data); + } + }); + + fastify.post<{ Params: { id: string } }>("/v1/filters/:id", async (_request, reply) => { + const BASE_URL = `${_request.protocol}://${_request.hostname}`; + const accessTokens = _request.headers.authorization; + const client = getClient(BASE_URL, accessTokens); + try { + const filter = new apiFilterMastodon(_request, client); + reply.send(await filter.updateFilter()); + } catch (e: any) { + console.error(e); + console.error(e.response.data); + reply.code(401).send(e.response.data); + } + }); + + fastify.delete<{ Params: { id: string } }>("/v1/filters/:id", async (_request, reply) => { + const BASE_URL = `${_request.protocol}://${_request.hostname}`; + const accessTokens = _request.headers.authorization; + const client = getClient(BASE_URL, accessTokens); + try { + const filter = new apiFilterMastodon(_request, client); + reply.send(await filter.rmFilter()); + } catch (e: any) { + console.error(e); + console.error(e.response.data); + reply.code(401).send(e.response.data); + } + }); //#endregion done(); } diff --git a/packages/backend/src/server/api/mastodon/endpoints/filter.ts b/packages/backend/src/server/api/mastodon/endpoints/filter.ts new file mode 100644 index 000000000..221090f28 --- /dev/null +++ b/packages/backend/src/server/api/mastodon/endpoints/filter.ts @@ -0,0 +1,66 @@ +import type { MegalodonInterface } from "megalodon"; +import type { FastifyRequest } from 'fastify'; +import { convertTimelinesArgsId } from "./timeline.js"; +import { IdConvertType as IdType, convertId, convertFilter } from '../converters.js'; + +export class apiFilterMastodon { + private request: FastifyRequest; + private client: MegalodonInterface; + + constructor(request: FastifyRequest, client: MegalodonInterface) { + this.request = request; + this.client = client; + } + + public async getFilters() { + try { + const data = await this.client.getFilters(); + return data.data.map((filter) => convertFilter(filter)); + } catch (e: any) { + console.error(e); + return e.response.data; + } + } + + public async getFilter() { + try { + const data = await this.client.getFilter( convertId((this.request.params as any).id, IdType.SharkeyId) ); + return convertFilter(data.data); + } catch (e: any) { + console.error(e); + return e.response.data; + } + } + + public async createFilter() { + try { + const body: any = this.request.body; + const data = await this.client.createFilter(body.pharse, body.context, body); + return convertFilter(data.data); + } catch (e: any) { + console.error(e); + return e.response.data; + } + } + + public async updateFilter() { + try { + const body: any = this.request.body; + const data = await this.client.updateFilter(convertId((this.request.params as any).id, IdType.SharkeyId), body.pharse, body.context); + return convertFilter(data.data); + } catch (e: any) { + console.error(e); + return e.response.data; + } + } + + public async rmFilter() { + try { + const data = await this.client.deleteFilter( convertId((this.request.params as any).id, IdType.SharkeyId) ); + return data.data; + } catch (e: any) { + console.error(e); + return e.response.data; + } + } +} \ No newline at end of file