add: filter endpoint to masto api

This commit is contained in:
Mar0xy 2023-09-24 05:35:50 +02:00
parent 4e375f303f
commit 34e6717dab
No known key found for this signature in database
GPG key ID: 56569BBE47D2C828
2 changed files with 125 additions and 0 deletions

View file

@ -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();
}

View file

@ -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;
}
}
}