* Fix TS errors and warnings * Fix ESLint errors and warnings * Fix property typos in various places * Fix property data conversion * Add missing entity properties * Normalize logging and reduce spam * Check for missing request parameters * Allow mastodon API to work with local debugging * Safer error handling * Fix quote-post detection
This commit is contained in:
parent
2c2fb8a692
commit
16f483d273
13 changed files with 1275 additions and 1147 deletions
|
|
@ -3,270 +3,238 @@
|
|||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { ParsedUrlQuery } from 'querystring';
|
||||
import { getErrorData, MastodonLogger } from '@/server/api/mastodon/MastodonLogger.js';
|
||||
import { convertConversation, convertList, MastoConverters } from '../converters.js';
|
||||
import { getClient } from '../MastodonApiServerService.js';
|
||||
import { parseTimelineArgs, TimelineArgs, toBoolean } from '../timelineArgs.js';
|
||||
import type { Entity } from 'megalodon';
|
||||
import type { FastifyInstance } from 'fastify';
|
||||
import type { Config } from '@/config.js';
|
||||
import { NoteEditRepository, NotesRepository, UsersRepository } from '@/models/_.js';
|
||||
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
||||
|
||||
export function limitToInt(q: ParsedUrlQuery) {
|
||||
const object: any = q;
|
||||
if (q.limit) if (typeof q.limit === 'string') object.limit = parseInt(q.limit, 10);
|
||||
if (q.offset) if (typeof q.offset === 'string') object.offset = parseInt(q.offset, 10);
|
||||
return object;
|
||||
}
|
||||
|
||||
export function argsToBools(q: ParsedUrlQuery) {
|
||||
// Values taken from https://docs.joinmastodon.org/client/intro/#boolean
|
||||
const toBoolean = (value: string) =>
|
||||
!['0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF'].includes(value);
|
||||
|
||||
// Keys taken from:
|
||||
// - https://docs.joinmastodon.org/methods/accounts/#statuses
|
||||
// - https://docs.joinmastodon.org/methods/timelines/#public
|
||||
// - https://docs.joinmastodon.org/methods/timelines/#tag
|
||||
const object: any = q;
|
||||
if (q.only_media) if (typeof q.only_media === 'string') object.only_media = toBoolean(q.only_media);
|
||||
if (q.exclude_replies) if (typeof q.exclude_replies === 'string') object.exclude_replies = toBoolean(q.exclude_replies);
|
||||
if (q.exclude_reblogs) if (typeof q.exclude_reblogs === 'string') object.exclude_reblogs = toBoolean(q.exclude_reblogs);
|
||||
if (q.pinned) if (typeof q.pinned === 'string') object.pinned = toBoolean(q.pinned);
|
||||
if (q.local) if (typeof q.local === 'string') object.local = toBoolean(q.local);
|
||||
return q;
|
||||
}
|
||||
|
||||
export class ApiTimelineMastodon {
|
||||
private fastify: FastifyInstance;
|
||||
constructor(
|
||||
private readonly fastify: FastifyInstance,
|
||||
private readonly mastoConverters: MastoConverters,
|
||||
private readonly logger: MastodonLogger,
|
||||
) {}
|
||||
|
||||
constructor(fastify: FastifyInstance, config: Config, private mastoconverter: MastoConverters) {
|
||||
this.fastify = fastify;
|
||||
}
|
||||
|
||||
public async getTL() {
|
||||
this.fastify.get('/v1/timelines/public', async (_request, reply) => {
|
||||
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
|
||||
public getTL() {
|
||||
this.fastify.get<{ Querystring: TimelineArgs }>('/v1/timelines/public', async (_request, reply) => {
|
||||
const BASE_URL = `${_request.protocol}://${_request.host}`;
|
||||
const accessTokens = _request.headers.authorization;
|
||||
const client = getClient(BASE_URL, accessTokens);
|
||||
try {
|
||||
const query: any = _request.query;
|
||||
const data = query.local === 'true'
|
||||
? await client.getLocalTimeline(argsToBools(limitToInt(query)))
|
||||
: await client.getPublicTimeline(argsToBools(limitToInt(query)));
|
||||
reply.send(await Promise.all(data.data.map(async (status: Entity.Status) => await this.mastoconverter.convertStatus(status))));
|
||||
} catch (e: any) {
|
||||
console.error(e);
|
||||
console.error(e.response.data);
|
||||
reply.code(401).send(e.response.data);
|
||||
const data = toBoolean(_request.query.local)
|
||||
? await client.getLocalTimeline(parseTimelineArgs(_request.query))
|
||||
: await client.getPublicTimeline(parseTimelineArgs(_request.query));
|
||||
reply.send(await Promise.all(data.data.map(async (status: Entity.Status) => await this.mastoConverters.convertStatus(status))));
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error('GET /v1/timelines/public', data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public async getHomeTl() {
|
||||
this.fastify.get('/v1/timelines/home', async (_request, reply) => {
|
||||
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
|
||||
public getHomeTl() {
|
||||
this.fastify.get<{ Querystring: TimelineArgs }>('/v1/timelines/home', async (_request, reply) => {
|
||||
const BASE_URL = `${_request.protocol}://${_request.host}`;
|
||||
const accessTokens = _request.headers.authorization;
|
||||
const client = getClient(BASE_URL, accessTokens);
|
||||
try {
|
||||
const query: any = _request.query;
|
||||
const data = await client.getHomeTimeline(limitToInt(query));
|
||||
reply.send(await Promise.all(data.data.map(async (status: Entity.Status) => await this.mastoconverter.convertStatus(status))));
|
||||
} catch (e: any) {
|
||||
console.error(e);
|
||||
console.error(e.response.data);
|
||||
reply.code(401).send(e.response.data);
|
||||
const data = await client.getHomeTimeline(parseTimelineArgs(_request.query));
|
||||
reply.send(await Promise.all(data.data.map(async (status: Entity.Status) => await this.mastoConverters.convertStatus(status))));
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error('GET /v1/timelines/home', data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public async getTagTl() {
|
||||
this.fastify.get<{ Params: { hashtag: string } }>('/v1/timelines/tag/:hashtag', async (_request, reply) => {
|
||||
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
|
||||
public getTagTl() {
|
||||
this.fastify.get<{ Params: { hashtag?: string }, Querystring: TimelineArgs }>('/v1/timelines/tag/:hashtag', async (_request, reply) => {
|
||||
const BASE_URL = `${_request.protocol}://${_request.host}`;
|
||||
const accessTokens = _request.headers.authorization;
|
||||
const client = getClient(BASE_URL, accessTokens);
|
||||
try {
|
||||
const query: any = _request.query;
|
||||
const params: any = _request.params;
|
||||
const data = await client.getTagTimeline(params.hashtag, limitToInt(query));
|
||||
reply.send(await Promise.all(data.data.map(async (status: Entity.Status) => await this.mastoconverter.convertStatus(status))));
|
||||
} catch (e: any) {
|
||||
console.error(e);
|
||||
console.error(e.response.data);
|
||||
reply.code(401).send(e.response.data);
|
||||
if (!_request.params.hashtag) return reply.code(400).send({ error: 'Missing required parameter "hashtag"' });
|
||||
const data = await client.getTagTimeline(_request.params.hashtag, parseTimelineArgs(_request.query));
|
||||
reply.send(await Promise.all(data.data.map(async (status: Entity.Status) => await this.mastoConverters.convertStatus(status))));
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`GET /v1/timelines/tag/${_request.params.hashtag}`, data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public async getListTL() {
|
||||
this.fastify.get<{ Params: { id: string } }>('/v1/timelines/list/:id', async (_request, reply) => {
|
||||
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
|
||||
public getListTL() {
|
||||
this.fastify.get<{ Params: { id?: string }, Querystring: TimelineArgs }>('/v1/timelines/list/:id', async (_request, reply) => {
|
||||
const BASE_URL = `${_request.protocol}://${_request.host}`;
|
||||
const accessTokens = _request.headers.authorization;
|
||||
const client = getClient(BASE_URL, accessTokens);
|
||||
try {
|
||||
const query: any = _request.query;
|
||||
const params: any = _request.params;
|
||||
const data = await client.getListTimeline(params.id, limitToInt(query));
|
||||
reply.send(await Promise.all(data.data.map(async (status: Entity.Status) => await this.mastoconverter.convertStatus(status))));
|
||||
} catch (e: any) {
|
||||
console.error(e);
|
||||
console.error(e.response.data);
|
||||
reply.code(401).send(e.response.data);
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
const data = await client.getListTimeline(_request.params.id, parseTimelineArgs(_request.query));
|
||||
reply.send(await Promise.all(data.data.map(async (status: Entity.Status) => await this.mastoConverters.convertStatus(status))));
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`GET /v1/timelines/list/${_request.params.id}`, data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public async getConversations() {
|
||||
this.fastify.get('/v1/conversations', async (_request, reply) => {
|
||||
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
|
||||
public getConversations() {
|
||||
this.fastify.get<{ Querystring: TimelineArgs }>('/v1/conversations', async (_request, reply) => {
|
||||
const BASE_URL = `${_request.protocol}://${_request.host}`;
|
||||
const accessTokens = _request.headers.authorization;
|
||||
const client = getClient(BASE_URL, accessTokens);
|
||||
try {
|
||||
const query: any = _request.query;
|
||||
const data = await client.getConversationTimeline(limitToInt(query));
|
||||
const data = await client.getConversationTimeline(parseTimelineArgs(_request.query));
|
||||
reply.send(data.data.map((conversation: Entity.Conversation) => convertConversation(conversation)));
|
||||
} catch (e: any) {
|
||||
console.error(e);
|
||||
console.error(e.response.data);
|
||||
reply.code(401).send(e.response.data);
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error('GET /v1/conversations', data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public async getList() {
|
||||
this.fastify.get<{ Params: { id: string } }>('/v1/lists/:id', async (_request, reply) => {
|
||||
public getList() {
|
||||
this.fastify.get<{ Params: { id?: string } }>('/v1/lists/:id', async (_request, reply) => {
|
||||
try {
|
||||
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
const BASE_URL = `${_request.protocol}://${_request.host}`;
|
||||
const accessTokens = _request.headers.authorization;
|
||||
const client = getClient(BASE_URL, accessTokens);
|
||||
const params: any = _request.params;
|
||||
const data = await client.getList(params.id);
|
||||
const data = await client.getList(_request.params.id);
|
||||
reply.send(convertList(data.data));
|
||||
} catch (e: any) {
|
||||
console.error(e);
|
||||
console.error(e.response.data);
|
||||
reply.code(401).send(e.response.data);
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`GET /v1/lists/${_request.params.id}`, data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public async getLists() {
|
||||
public getLists() {
|
||||
this.fastify.get('/v1/lists', async (_request, reply) => {
|
||||
try {
|
||||
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
|
||||
const BASE_URL = `${_request.protocol}://${_request.host}`;
|
||||
const accessTokens = _request.headers.authorization;
|
||||
const client = getClient(BASE_URL, accessTokens);
|
||||
const data = await client.getLists();
|
||||
reply.send(data.data.map((list: Entity.List) => convertList(list)));
|
||||
} catch (e: any) {
|
||||
console.error(e);
|
||||
return e.response.data;
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error('GET /v1/lists', data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public async getListAccounts() {
|
||||
this.fastify.get<{ Params: { id: string } }>('/v1/lists/:id/accounts', async (_request, reply) => {
|
||||
public getListAccounts() {
|
||||
this.fastify.get<{ Params: { id?: string }, Querystring: { limit?: number, max_id?: string, since_id?: string } }>('/v1/lists/:id/accounts', async (_request, reply) => {
|
||||
try {
|
||||
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
const BASE_URL = `${_request.protocol}://${_request.host}`;
|
||||
const accessTokens = _request.headers.authorization;
|
||||
const client = getClient(BASE_URL, accessTokens);
|
||||
const params: any = _request.params;
|
||||
const query: any = _request.query;
|
||||
const data = await client.getAccountsInList(params.id, query);
|
||||
reply.send(data.data.map((account: Entity.Account) => this.mastoconverter.convertAccount(account)));
|
||||
} catch (e: any) {
|
||||
console.error(e);
|
||||
console.error(e.response.data);
|
||||
reply.code(401).send(e.response.data);
|
||||
const data = await client.getAccountsInList(_request.params.id, _request.query);
|
||||
reply.send(data.data.map((account: Entity.Account) => this.mastoConverters.convertAccount(account)));
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`GET /v1/lists/${_request.params.id}/accounts`, data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public async addListAccount() {
|
||||
this.fastify.post<{ Params: { id: string } }>('/v1/lists/:id/accounts', async (_request, reply) => {
|
||||
public addListAccount() {
|
||||
this.fastify.post<{ Params: { id?: string }, Querystring: { accounts_id?: string[] } }>('/v1/lists/:id/accounts', async (_request, reply) => {
|
||||
try {
|
||||
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
if (!_request.query.accounts_id) return reply.code(400).send({ error: 'Missing required property "accounts_id"' });
|
||||
const BASE_URL = `${_request.protocol}://${_request.host}`;
|
||||
const accessTokens = _request.headers.authorization;
|
||||
const client = getClient(BASE_URL, accessTokens);
|
||||
const params: any = _request.params;
|
||||
const query: any = _request.query;
|
||||
const data = await client.addAccountsToList(params.id, query.accounts_id);
|
||||
const data = await client.addAccountsToList(_request.params.id, _request.query.accounts_id);
|
||||
reply.send(data.data);
|
||||
} catch (e: any) {
|
||||
console.error(e);
|
||||
console.error(e.response.data);
|
||||
reply.code(401).send(e.response.data);
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`POST /v1/lists/${_request.params.id}/accounts`, data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public async rmListAccount() {
|
||||
this.fastify.delete<{ Params: { id: string } }>('/v1/lists/:id/accounts', async (_request, reply) => {
|
||||
public rmListAccount() {
|
||||
this.fastify.delete<{ Params: { id?: string }, Querystring: { accounts_id?: string[] } }>('/v1/lists/:id/accounts', async (_request, reply) => {
|
||||
try {
|
||||
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
if (!_request.query.accounts_id) return reply.code(400).send({ error: 'Missing required property "accounts_id"' });
|
||||
const BASE_URL = `${_request.protocol}://${_request.host}`;
|
||||
const accessTokens = _request.headers.authorization;
|
||||
const client = getClient(BASE_URL, accessTokens);
|
||||
const params: any = _request.params;
|
||||
const query: any = _request.query;
|
||||
const data = await client.deleteAccountsFromList(params.id, query.accounts_id);
|
||||
const data = await client.deleteAccountsFromList(_request.params.id, _request.query.accounts_id);
|
||||
reply.send(data.data);
|
||||
} catch (e: any) {
|
||||
console.error(e);
|
||||
console.error(e.response.data);
|
||||
reply.code(401).send(e.response.data);
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`DELETE /v1/lists/${_request.params.id}/accounts`, data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public async createList() {
|
||||
this.fastify.post('/v1/lists', async (_request, reply) => {
|
||||
public createList() {
|
||||
this.fastify.post<{ Body: { title?: string } }>('/v1/lists', async (_request, reply) => {
|
||||
try {
|
||||
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
|
||||
if (!_request.body.title) return reply.code(400).send({ error: 'Missing required payload "title"' });
|
||||
const BASE_URL = `${_request.protocol}://${_request.host}`;
|
||||
const accessTokens = _request.headers.authorization;
|
||||
const client = getClient(BASE_URL, accessTokens);
|
||||
const body: any = _request.body;
|
||||
const data = await client.createList(body.title);
|
||||
const data = await client.createList(_request.body.title);
|
||||
reply.send(convertList(data.data));
|
||||
} catch (e: any) {
|
||||
console.error(e);
|
||||
console.error(e.response.data);
|
||||
reply.code(401).send(e.response.data);
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error('POST /v1/lists', data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public async updateList() {
|
||||
this.fastify.put<{ Params: { id: string } }>('/v1/lists/:id', async (_request, reply) => {
|
||||
public updateList() {
|
||||
this.fastify.put<{ Params: { id?: string }, Body: { title?: string } }>('/v1/lists/:id', async (_request, reply) => {
|
||||
try {
|
||||
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
if (!_request.body.title) return reply.code(400).send({ error: 'Missing required payload "title"' });
|
||||
const BASE_URL = `${_request.protocol}://${_request.host}`;
|
||||
const accessTokens = _request.headers.authorization;
|
||||
const client = getClient(BASE_URL, accessTokens);
|
||||
const body: any = _request.body;
|
||||
const params: any = _request.params;
|
||||
const data = await client.updateList(params.id, body.title);
|
||||
const data = await client.updateList(_request.params.id, _request.body.title);
|
||||
reply.send(convertList(data.data));
|
||||
} catch (e: any) {
|
||||
console.error(e);
|
||||
console.error(e.response.data);
|
||||
reply.code(401).send(e.response.data);
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`PUT /v1/lists/${_request.params.id}`, data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public async deleteList() {
|
||||
this.fastify.delete<{ Params: { id: string } }>('/v1/lists/:id', async (_request, reply) => {
|
||||
public deleteList() {
|
||||
this.fastify.delete<{ Params: { id?: string } }>('/v1/lists/:id', async (_request, reply) => {
|
||||
try {
|
||||
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
|
||||
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
|
||||
const BASE_URL = `${_request.protocol}://${_request.host}`;
|
||||
const accessTokens = _request.headers.authorization;
|
||||
const client = getClient(BASE_URL, accessTokens);
|
||||
const params: any = _request.params;
|
||||
const data = await client.deleteList(params.id);
|
||||
await client.deleteList(_request.params.id);
|
||||
reply.send({});
|
||||
} catch (e: any) {
|
||||
console.error(e);
|
||||
console.error(e.response.data);
|
||||
reply.code(401).send(e.response.data);
|
||||
} catch (e) {
|
||||
const data = getErrorData(e);
|
||||
this.logger.error(`DELETE /v1/lists/${_request.params.id}`, data);
|
||||
reply.code(401).send(data);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue