de-duplicate mastodon API logging

This commit is contained in:
Hazelnoot 2025-03-21 20:38:28 -04:00
parent 03edc33424
commit da25595ba3
10 changed files with 827 additions and 1229 deletions

View file

@ -9,7 +9,7 @@ import { Inject, Injectable } from '@nestjs/common';
import { DI } from '@/di-symbols.js'; import { DI } from '@/di-symbols.js';
import { bindThis } from '@/decorators.js'; import { bindThis } from '@/decorators.js';
import type { Config } from '@/config.js'; import type { Config } from '@/config.js';
import { getErrorData, MastodonLogger } from '@/server/api/mastodon/MastodonLogger.js'; import { getErrorData, getErrorStatus, MastodonLogger } from '@/server/api/mastodon/MastodonLogger.js';
import { MastodonClientService } from '@/server/api/mastodon/MastodonClientService.js'; import { MastodonClientService } from '@/server/api/mastodon/MastodonClientService.js';
import { ApiAccountMastodon } from '@/server/api/mastodon/endpoints/account.js'; import { ApiAccountMastodon } from '@/server/api/mastodon/endpoints/account.js';
import { ApiAppsMastodon } from '@/server/api/mastodon/endpoints/apps.js'; import { ApiAppsMastodon } from '@/server/api/mastodon/endpoints/apps.js';
@ -74,6 +74,15 @@ export class MastodonApiServerService {
payload.on('error', done); payload.on('error', done);
}); });
fastify.setErrorHandler((error, request, reply) => {
const data = getErrorData(error);
const status = getErrorStatus(error);
this.logger.error(request, data, status);
reply.code(status).send(data);
});
fastify.register(multer.contentParser); fastify.register(multer.contentParser);
// External endpoints // External endpoints
@ -87,44 +96,25 @@ export class MastodonApiServerService {
this.apiTimelineMastodon.register(fastify); this.apiTimelineMastodon.register(fastify);
fastify.get('/v1/custom_emojis', async (_request, reply) => { fastify.get('/v1/custom_emojis', async (_request, reply) => {
try {
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
const data = await client.getInstanceCustomEmojis(); const data = await client.getInstanceCustomEmojis();
reply.send(data.data); reply.send(data.data);
} catch (e) {
const data = getErrorData(e);
this.logger.error('GET /v1/custom_emojis', data);
reply.code(401).send(data);
}
}); });
fastify.get('/v1/announcements', async (_request, reply) => { fastify.get('/v1/announcements', async (_request, reply) => {
try {
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
const data = await client.getInstanceAnnouncements(); const data = await client.getInstanceAnnouncements();
reply.send(data.data.map((announcement) => convertAnnouncement(announcement))); reply.send(data.data.map((announcement) => convertAnnouncement(announcement)));
} catch (e) {
const data = getErrorData(e);
this.logger.error('GET /v1/announcements', data);
reply.code(401).send(data);
}
}); });
fastify.post<{ Body: { id?: string } }>('/v1/announcements/:id/dismiss', async (_request, reply) => { fastify.post<{ Body: { id?: string } }>('/v1/announcements/:id/dismiss', async (_request, reply) => {
try {
if (!_request.body.id) return reply.code(400).send({ error: 'Missing required payload "id"' }); if (!_request.body.id) return reply.code(400).send({ error: 'Missing required payload "id"' });
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
const data = await client.dismissInstanceAnnouncement(_request.body['id']); const data = await client.dismissInstanceAnnouncement(_request.body['id']);
reply.send(data.data); reply.send(data.data);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`POST /v1/announcements/${_request.body.id}/dismiss`, data);
reply.code(401).send(data);
}
}); });
fastify.post('/v1/media', { preHandler: upload.single('file') }, async (_request, reply) => { fastify.post('/v1/media', { preHandler: upload.single('file') }, async (_request, reply) => {
try {
const multipartData = await _request.file(); const multipartData = await _request.file();
if (!multipartData) { if (!multipartData) {
reply.code(401).send({ error: 'No image' }); reply.code(401).send({ error: 'No image' });
@ -133,15 +123,9 @@ export class MastodonApiServerService {
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
const data = await client.uploadMedia(multipartData); const data = await client.uploadMedia(multipartData);
reply.send(convertAttachment(data.data as Entity.Attachment)); reply.send(convertAttachment(data.data as Entity.Attachment));
} catch (e) {
const data = getErrorData(e);
this.logger.error('POST /v1/media', data);
reply.code(401).send(data);
}
}); });
fastify.post<{ Body: { description?: string; focus?: string }}>('/v2/media', { preHandler: upload.single('file') }, async (_request, reply) => { fastify.post<{ Body: { description?: string; focus?: string }}>('/v2/media', { preHandler: upload.single('file') }, async (_request, reply) => {
try {
const multipartData = await _request.file(); const multipartData = await _request.file();
if (!multipartData) { if (!multipartData) {
reply.code(401).send({ error: 'No image' }); reply.code(401).send({ error: 'No image' });
@ -150,35 +134,18 @@ export class MastodonApiServerService {
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
const data = await client.uploadMedia(multipartData, _request.body); const data = await client.uploadMedia(multipartData, _request.body);
reply.send(convertAttachment(data.data as Entity.Attachment)); reply.send(convertAttachment(data.data as Entity.Attachment));
} catch (e) {
const data = getErrorData(e);
this.logger.error('POST /v2/media', data);
reply.code(401).send(data);
}
}); });
fastify.get('/v1/trends', async (_request, reply) => { fastify.get('/v1/trends', async (_request, reply) => {
try {
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
const data = await client.getInstanceTrends(); const data = await client.getInstanceTrends();
reply.send(data.data); reply.send(data.data);
} catch (e) {
const data = getErrorData(e);
this.logger.error('GET /v1/trends', data);
reply.code(401).send(data);
}
}); });
fastify.get('/v1/trends/tags', async (_request, reply) => { fastify.get('/v1/trends/tags', async (_request, reply) => {
try {
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
const data = await client.getInstanceTrends(); const data = await client.getInstanceTrends();
reply.send(data.data); reply.send(data.data);
} catch (e) {
const data = getErrorData(e);
this.logger.error('GET /v1/trends/tags', data);
reply.code(401).send(data);
}
}); });
fastify.get('/v1/trends/links', async (_request, reply) => { fastify.get('/v1/trends/links', async (_request, reply) => {
@ -187,104 +154,64 @@ export class MastodonApiServerService {
}); });
fastify.get('/v1/preferences', async (_request, reply) => { fastify.get('/v1/preferences', async (_request, reply) => {
try {
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
const data = await client.getPreferences(); const data = await client.getPreferences();
reply.send(data.data); reply.send(data.data);
} catch (e) {
const data = getErrorData(e);
this.logger.error('GET /v1/preferences', data);
reply.code(401).send(data);
}
}); });
fastify.get('/v1/followed_tags', async (_request, reply) => { fastify.get('/v1/followed_tags', async (_request, reply) => {
try {
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
const data = await client.getFollowedTags(); const data = await client.getFollowedTags();
reply.send(data.data); reply.send(data.data);
} catch (e) {
const data = getErrorData(e);
this.logger.error('GET /v1/followed_tags', data);
reply.code(401).send(data);
}
}); });
fastify.get<{ Querystring: TimelineArgs }>('/v1/bookmarks', async (_request, reply) => { fastify.get<{ Querystring: TimelineArgs }>('/v1/bookmarks', async (_request, reply) => {
try {
const { client, me } = await this.clientService.getAuthClient(_request); const { client, me } = await this.clientService.getAuthClient(_request);
const data = await client.getBookmarks(parseTimelineArgs(_request.query)); const data = await client.getBookmarks(parseTimelineArgs(_request.query));
const response = await Promise.all(data.data.map((status) => this.mastoConverters.convertStatus(status, me))); const response = await Promise.all(data.data.map((status) => this.mastoConverters.convertStatus(status, me)));
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error('GET /v1/bookmarks', data);
reply.code(401).send(data);
}
}); });
fastify.get<{ Querystring: TimelineArgs }>('/v1/favourites', async (_request, reply) => { fastify.get<{ Querystring: TimelineArgs }>('/v1/favourites', async (_request, reply) => {
try {
const { client, me } = await this.clientService.getAuthClient(_request); const { client, me } = await this.clientService.getAuthClient(_request);
const data = await client.getFavourites(parseTimelineArgs(_request.query)); const data = await client.getFavourites(parseTimelineArgs(_request.query));
const response = Promise.all(data.data.map((status) => this.mastoConverters.convertStatus(status, me))); const response = Promise.all(data.data.map((status) => this.mastoConverters.convertStatus(status, me)));
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error('GET /v1/favourites', data);
reply.code(401).send(data);
}
}); });
fastify.get<{ Querystring: TimelineArgs }>('/v1/mutes', async (_request, reply) => { fastify.get<{ Querystring: TimelineArgs }>('/v1/mutes', async (_request, reply) => {
try {
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
const data = await client.getMutes(parseTimelineArgs(_request.query)); const data = await client.getMutes(parseTimelineArgs(_request.query));
const response = Promise.all(data.data.map((account) => this.mastoConverters.convertAccount(account))); const response = Promise.all(data.data.map((account) => this.mastoConverters.convertAccount(account)));
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error('GET /v1/mutes', data);
reply.code(401).send(data);
}
}); });
fastify.get<{ Querystring: TimelineArgs }>('/v1/blocks', async (_request, reply) => { fastify.get<{ Querystring: TimelineArgs }>('/v1/blocks', async (_request, reply) => {
try {
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
const data = await client.getBlocks(parseTimelineArgs(_request.query)); const data = await client.getBlocks(parseTimelineArgs(_request.query));
const response = Promise.all(data.data.map((account) => this.mastoConverters.convertAccount(account))); const response = Promise.all(data.data.map((account) => this.mastoConverters.convertAccount(account)));
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error('GET /v1/blocks', data);
reply.code(401).send(data);
}
}); });
fastify.get<{ Querystring: { limit?: string }}>('/v1/follow_requests', async (_request, reply) => { fastify.get<{ Querystring: { limit?: string }}>('/v1/follow_requests', async (_request, reply) => {
try {
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
const limit = _request.query.limit ? parseInt(_request.query.limit) : 20; const limit = _request.query.limit ? parseInt(_request.query.limit) : 20;
const data = await client.getFollowRequests(limit); const data = await client.getFollowRequests(limit);
reply.send(await Promise.all(data.data.map(async (account) => await this.mastoConverters.convertAccount(account as Entity.Account)))); const response = await Promise.all(data.data.map((account) => this.mastoConverters.convertAccount(account as Entity.Account)));
} catch (e) {
const data = getErrorData(e); reply.send(response);
this.logger.error('GET /v1/follow_requests', data);
reply.code(401).send(data);
}
}); });
fastify.post<{ Querystring: TimelineArgs, Params: { id?: string } }>('/v1/follow_requests/:id/authorize', { preHandler: upload.single('none') }, async (_request, reply) => { fastify.post<{ Querystring: TimelineArgs, Params: { id?: string } }>('/v1/follow_requests/:id/authorize', { preHandler: upload.single('none') }, async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
@ -292,15 +219,9 @@ export class MastodonApiServerService {
const response = convertRelationship(data.data); const response = convertRelationship(data.data);
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`POST /v1/follow_requests/${_request.params.id}/authorize`, data);
reply.code(401).send(data);
}
}); });
fastify.post<{ Querystring: TimelineArgs, Params: { id?: string } }>('/v1/follow_requests/:id/reject', { preHandler: upload.single('none') }, async (_request, reply) => { fastify.post<{ Querystring: TimelineArgs, Params: { id?: string } }>('/v1/follow_requests/:id/reject', { preHandler: upload.single('none') }, async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
@ -308,11 +229,6 @@ export class MastodonApiServerService {
const response = convertRelationship(data.data); const response = convertRelationship(data.data);
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`POST /v1/follow_requests/${_request.params.id}/reject`, data);
reply.code(401).send(data);
}
}); });
//#endregion //#endregion
@ -327,7 +243,6 @@ export class MastodonApiServerService {
is_sensitive?: string, is_sensitive?: string,
}, },
}>('/v1/media/:id', { preHandler: upload.none() }, async (_request, reply) => { }>('/v1/media/:id', { preHandler: upload.none() }, async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const options = { const options = {
@ -339,11 +254,6 @@ export class MastodonApiServerService {
const response = convertAttachment(data.data); const response = convertAttachment(data.data);
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`PUT /v1/media/${_request.params.id}`, data);
reply.code(401).send(data);
}
}); });
done(); done();

View file

@ -3,37 +3,137 @@
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
import { Injectable } from '@nestjs/common'; import { Inject, Injectable } from '@nestjs/common';
import Logger, { Data } from '@/logger.js'; import Logger from '@/logger.js';
import { LoggerService } from '@/core/LoggerService.js'; import { LoggerService } from '@/core/LoggerService.js';
import { ApiError } from '@/server/api/error.js';
import { EnvService } from '@/core/EnvService.js';
import { FastifyRequest } from 'fastify';
@Injectable() @Injectable()
export class MastodonLogger { export class MastodonLogger {
public readonly logger: Logger; public readonly logger: Logger;
constructor(loggerService: LoggerService) { constructor(
@Inject(EnvService)
private readonly envService: EnvService,
loggerService: LoggerService,
) {
this.logger = loggerService.getLogger('masto-api'); this.logger = loggerService.getLogger('masto-api');
} }
public error(endpoint: string, error: Data): void { public error(request: FastifyRequest, error: MastodonError, status: number): void {
this.logger.error(`Error in mastodon API endpoint ${endpoint}:`, error); if ((status < 400 && status > 499) || this.envService.env.NODE_ENV === 'development') {
this.logger.error(`Error in mastodon endpoint ${request.method} ${request.url}:`, error);
}
} }
} }
export function getErrorData(error: unknown): Data { // TODO move elsewhere
if (error == null) return {}; export interface MastodonError {
if (typeof(error) === 'string') return error; error: string;
if (typeof(error) === 'object') { error_description: string;
}
export function getErrorData(error: unknown): MastodonError {
if (error && typeof(error) === 'object') {
// AxiosError, comes from the backend
if ('response' in error) { if ('response' in error) {
if (typeof(error.response) === 'object' && error.response) { if (typeof(error.response) === 'object' && error.response) {
if ('data' in error.response) { if ('data' in error.response) {
if (typeof(error.response.data) === 'object' && error.response.data) { if (typeof(error.response.data) === 'object' && error.response.data) {
return error.response.data as Record<string, unknown>; if ('error' in error.response.data) {
if (typeof(error.response.data.error) === 'object' && error.response.data.error) {
if ('code' in error.response.data.error) {
if (typeof(error.response.data.error.code) === 'string') {
return convertApiError(error.response.data.error as ApiError);
}
}
return convertUnknownError(error.response.data.error);
}
}
return convertUnknownError(error.response.data);
} }
} }
} }
// No data - this is a fallback to avoid leaking request/response details in the error
return convertUnknownError();
} }
return error as Record<string, unknown>;
if (error instanceof ApiError) {
return convertApiError(error);
} }
return { error };
if (error instanceof Error) {
return convertGenericError(error);
}
return convertUnknownError(error);
}
return {
error: 'UNKNOWN_ERROR',
error_description: String(error),
};
}
function convertApiError(apiError: ApiError): MastodonError {
const mastoError: MastodonError & Partial<ApiError> = {
error: apiError.code,
error_description: apiError.message,
...apiError,
};
delete mastoError.code;
delete mastoError.message;
return mastoError;
}
function convertUnknownError(data: object = {}): MastodonError {
return Object.assign({}, data, {
error: 'INTERNAL_ERROR',
error_description: 'Internal error occurred. Please contact us if the error persists.',
id: '5d37dbcb-891e-41ca-a3d6-e690c97775ac',
kind: 'server',
});
}
function convertGenericError(error: Error): MastodonError {
const mastoError: MastodonError & Partial<Error> = {
error: 'INTERNAL_ERROR',
error_description: String(error),
...error,
};
delete mastoError.name;
delete mastoError.message;
delete mastoError.stack;
return mastoError;
}
export function getErrorStatus(error: unknown): number {
// AxiosError, comes from the backend
if (typeof(error) === 'object' && error) {
if ('response' in error) {
if (typeof (error.response) === 'object' && error.response) {
if ('status' in error.response) {
if (typeof(error.response.status) === 'number') {
return error.response.status;
}
}
}
}
}
if (error instanceof ApiError && error.httpStatusCode) {
return error.httpStatusCode;
}
return 500;
} }

View file

@ -5,7 +5,6 @@
import { Inject, Injectable } from '@nestjs/common'; import { Inject, Injectable } from '@nestjs/common';
import { parseTimelineArgs, TimelineArgs, toBoolean } from '@/server/api/mastodon/argsUtils.js'; import { parseTimelineArgs, TimelineArgs, toBoolean } from '@/server/api/mastodon/argsUtils.js';
import { getErrorData, MastodonLogger } from '@/server/api/mastodon/MastodonLogger.js';
import { MastodonClientService } from '@/server/api/mastodon/MastodonClientService.js'; import { MastodonClientService } from '@/server/api/mastodon/MastodonClientService.js';
import { DriveService } from '@/core/DriveService.js'; import { DriveService } from '@/core/DriveService.js';
import { DI } from '@/di-symbols.js'; import { DI } from '@/di-symbols.js';
@ -31,13 +30,11 @@ export class ApiAccountMastodon {
private readonly clientService: MastodonClientService, private readonly clientService: MastodonClientService,
private readonly mastoConverters: MastoConverters, private readonly mastoConverters: MastoConverters,
private readonly logger: MastodonLogger,
private readonly driveService: DriveService, private readonly driveService: DriveService,
) {} ) {}
public register(fastify: FastifyInstance, upload: ReturnType<typeof multer>): void { public register(fastify: FastifyInstance, upload: ReturnType<typeof multer>): void {
fastify.get<ApiAccountMastodonRoute>('/v1/accounts/verify_credentials', async (_request, reply) => { fastify.get<ApiAccountMastodonRoute>('/v1/accounts/verify_credentials', async (_request, reply) => {
try {
const client = await this.clientService.getClient(_request); const client = await this.clientService.getClient(_request);
const data = await client.verifyAccountCredentials(); const data = await client.verifyAccountCredentials();
const acct = await this.mastoConverters.convertAccount(data.data); const acct = await this.mastoConverters.convertAccount(data.data);
@ -52,11 +49,6 @@ export class ApiAccountMastodon {
}, },
}); });
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error('GET /v1/accounts/verify_credentials', data);
reply.code(401).send(data);
}
}); });
fastify.patch<{ fastify.patch<{
@ -80,7 +72,6 @@ export class ApiAccountMastodon {
}, },
}>('/v1/accounts/update_credentials', { preHandler: upload.any() }, async (_request, reply) => { }>('/v1/accounts/update_credentials', { preHandler: upload.any() }, async (_request, reply) => {
const accessTokens = _request.headers.authorization; const accessTokens = _request.headers.authorization;
try {
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
// Check if there is a Header or Avatar being uploaded, if there is proceed to upload it to the drive of the user and then set it. // Check if there is a Header or Avatar being uploaded, if there is proceed to upload it to the drive of the user and then set it.
if (_request.files.length > 0 && accessTokens) { if (_request.files.length > 0 && accessTokens) {
@ -146,16 +137,12 @@ export class ApiAccountMastodon {
} : undefined, } : undefined,
}; };
const data = await client.updateCredentials(options); const data = await client.updateCredentials(options);
reply.send(await this.mastoConverters.convertAccount(data.data)); const response = await this.mastoConverters.convertAccount(data.data);
} catch (e) {
const data = getErrorData(e); reply.send(response);
this.logger.error('PATCH /v1/accounts/update_credentials', data);
reply.code(401).send(data);
}
}); });
fastify.get<{ Querystring: { acct?: string }}>('/v1/accounts/lookup', async (_request, reply) => { fastify.get<{ Querystring: { acct?: string }}>('/v1/accounts/lookup', async (_request, reply) => {
try {
if (!_request.query.acct) return reply.code(400).send({ error: 'Missing required property "acct"' }); if (!_request.query.acct) return reply.code(400).send({ error: 'Missing required property "acct"' });
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
@ -165,15 +152,9 @@ export class ApiAccountMastodon {
const response = await this.mastoConverters.convertAccount(data.data.accounts[0]); const response = await this.mastoConverters.convertAccount(data.data.accounts[0]);
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error('GET /v1/accounts/lookup', data);
reply.code(401).send(data);
}
}); });
fastify.get<ApiAccountMastodonRoute & { Querystring: { id?: string | string[], 'id[]'?: string | string[] }}>('/v1/accounts/relationships', async (_request, reply) => { fastify.get<ApiAccountMastodonRoute & { Querystring: { id?: string | string[], 'id[]'?: string | string[] }}>('/v1/accounts/relationships', async (_request, reply) => {
try {
let ids = _request.query['id[]'] ?? _request.query['id'] ?? []; let ids = _request.query['id[]'] ?? _request.query['id'] ?? [];
if (typeof ids === 'string') { if (typeof ids === 'string') {
ids = [ids]; ids = [ids];
@ -184,15 +165,9 @@ export class ApiAccountMastodon {
const response = data.data.map(relationship => convertRelationship(relationship)); const response = data.data.map(relationship => convertRelationship(relationship));
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error('GET /v1/accounts/relationships', data);
reply.code(401).send(data);
}
}); });
fastify.get<{ Params: { id?: string } }>('/v1/accounts/:id', async (_request, reply) => { fastify.get<{ Params: { id?: string } }>('/v1/accounts/:id', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
@ -200,15 +175,9 @@ export class ApiAccountMastodon {
const account = await this.mastoConverters.convertAccount(data.data); const account = await this.mastoConverters.convertAccount(data.data);
reply.send(account); reply.send(account);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`GET /v1/accounts/${_request.params.id}`, data);
reply.code(401).send(data);
}
}); });
fastify.get<ApiAccountMastodonRoute & { Params: { id?: string } }>('/v1/accounts/:id/statuses', async (_request, reply) => { fastify.get<ApiAccountMastodonRoute & { Params: { id?: string } }>('/v1/accounts/:id/statuses', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const { client, me } = await this.clientService.getAuthClient(_request); const { client, me } = await this.clientService.getAuthClient(_request);
@ -216,15 +185,9 @@ export class ApiAccountMastodon {
const response = await Promise.all(data.data.map(async (status) => await this.mastoConverters.convertStatus(status, me))); const response = await Promise.all(data.data.map(async (status) => await this.mastoConverters.convertStatus(status, me)));
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`GET /v1/accounts/${_request.params.id}/statuses`, data);
reply.code(401).send(data);
}
}); });
fastify.get<{ Params: { id?: string } }>('/v1/accounts/:id/featured_tags', async (_request, reply) => { fastify.get<{ Params: { id?: string } }>('/v1/accounts/:id/featured_tags', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
@ -232,15 +195,9 @@ export class ApiAccountMastodon {
const response = data.data.map((tag) => convertFeaturedTag(tag)); const response = data.data.map((tag) => convertFeaturedTag(tag));
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`GET /v1/accounts/${_request.params.id}/featured_tags`, data);
reply.code(401).send(data);
}
}); });
fastify.get<ApiAccountMastodonRoute & { Params: { id?: string } }>('/v1/accounts/:id/followers', async (_request, reply) => { fastify.get<ApiAccountMastodonRoute & { Params: { id?: string } }>('/v1/accounts/:id/followers', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
@ -251,15 +208,9 @@ export class ApiAccountMastodon {
const response = await Promise.all(data.data.map(async (account) => await this.mastoConverters.convertAccount(account))); const response = await Promise.all(data.data.map(async (account) => await this.mastoConverters.convertAccount(account)));
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`GET /v1/accounts/${_request.params.id}/followers`, data);
reply.code(401).send(data);
}
}); });
fastify.get<ApiAccountMastodonRoute & { Params: { id?: string } }>('/v1/accounts/:id/following', async (_request, reply) => { fastify.get<ApiAccountMastodonRoute & { Params: { id?: string } }>('/v1/accounts/:id/following', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
@ -270,15 +221,9 @@ export class ApiAccountMastodon {
const response = await Promise.all(data.data.map(async (account) => await this.mastoConverters.convertAccount(account))); const response = await Promise.all(data.data.map(async (account) => await this.mastoConverters.convertAccount(account)));
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`GET /v1/accounts/${_request.params.id}/following`, data);
reply.code(401).send(data);
}
}); });
fastify.get<{ Params: { id?: string } }>('/v1/accounts/:id/lists', async (_request, reply) => { fastify.get<{ Params: { id?: string } }>('/v1/accounts/:id/lists', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
@ -286,15 +231,9 @@ export class ApiAccountMastodon {
const response = data.data.map((list) => convertList(list)); const response = data.data.map((list) => convertList(list));
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`GET /v1/accounts/${_request.params.id}/lists`, data);
reply.code(401).send(data);
}
}); });
fastify.post<ApiAccountMastodonRoute & { Params: { id?: string } }>('/v1/accounts/:id/follow', { preHandler: upload.single('none') }, async (_request, reply) => { fastify.post<ApiAccountMastodonRoute & { Params: { id?: string } }>('/v1/accounts/:id/follow', { preHandler: upload.single('none') }, async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
@ -303,15 +242,9 @@ export class ApiAccountMastodon {
acct.following = true; acct.following = true;
reply.send(acct); reply.send(acct);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`POST /v1/accounts/${_request.params.id}/follow`, data);
reply.code(401).send(data);
}
}); });
fastify.post<ApiAccountMastodonRoute & { Params: { id?: string } }>('/v1/accounts/:id/unfollow', { preHandler: upload.single('none') }, async (_request, reply) => { fastify.post<ApiAccountMastodonRoute & { Params: { id?: string } }>('/v1/accounts/:id/unfollow', { preHandler: upload.single('none') }, async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
@ -320,15 +253,9 @@ export class ApiAccountMastodon {
acct.following = false; acct.following = false;
reply.send(acct); reply.send(acct);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`POST /v1/accounts/${_request.params.id}/unfollow`, data);
reply.code(401).send(data);
}
}); });
fastify.post<ApiAccountMastodonRoute & { Params: { id?: string } }>('/v1/accounts/:id/block', { preHandler: upload.single('none') }, async (_request, reply) => { fastify.post<ApiAccountMastodonRoute & { Params: { id?: string } }>('/v1/accounts/:id/block', { preHandler: upload.single('none') }, async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
@ -336,15 +263,9 @@ export class ApiAccountMastodon {
const response = convertRelationship(data.data); const response = convertRelationship(data.data);
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`POST /v1/accounts/${_request.params.id}/block`, data);
reply.code(401).send(data);
}
}); });
fastify.post<ApiAccountMastodonRoute & { Params: { id?: string } }>('/v1/accounts/:id/unblock', { preHandler: upload.single('none') }, async (_request, reply) => { fastify.post<ApiAccountMastodonRoute & { Params: { id?: string } }>('/v1/accounts/:id/unblock', { preHandler: upload.single('none') }, async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
@ -352,15 +273,9 @@ export class ApiAccountMastodon {
const response = convertRelationship(data.data); const response = convertRelationship(data.data);
return reply.send(response); return reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`POST /v1/accounts/${_request.params.id}/unblock`, data);
reply.code(401).send(data);
}
}); });
fastify.post<ApiAccountMastodonRoute & { Params: { id?: string } }>('/v1/accounts/:id/mute', { preHandler: upload.single('none') }, async (_request, reply) => { fastify.post<ApiAccountMastodonRoute & { Params: { id?: string } }>('/v1/accounts/:id/mute', { preHandler: upload.single('none') }, async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
@ -371,15 +286,9 @@ export class ApiAccountMastodon {
const response = convertRelationship(data.data); const response = convertRelationship(data.data);
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`POST /v1/accounts/${_request.params.id}/mute`, data);
reply.code(401).send(data);
}
}); });
fastify.post<ApiAccountMastodonRoute & { Params: { id?: string } }>('/v1/accounts/:id/unmute', { preHandler: upload.single('none') }, async (_request, reply) => { fastify.post<ApiAccountMastodonRoute & { Params: { id?: string } }>('/v1/accounts/:id/unmute', { preHandler: upload.single('none') }, async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
@ -387,11 +296,6 @@ export class ApiAccountMastodon {
const response = convertRelationship(data.data); const response = convertRelationship(data.data);
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`POST /v1/accounts/${_request.params.id}/unmute`, data);
reply.code(401).send(data);
}
}); });
} }
} }

View file

@ -4,7 +4,6 @@
*/ */
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { getErrorData, MastodonLogger } from '@/server/api/mastodon/MastodonLogger.js';
import { MastodonClientService } from '@/server/api/mastodon/MastodonClientService.js'; import { MastodonClientService } from '@/server/api/mastodon/MastodonClientService.js';
import type { FastifyInstance } from 'fastify'; import type { FastifyInstance } from 'fastify';
import type multer from 'fastify-multer'; import type multer from 'fastify-multer';
@ -61,12 +60,10 @@ type AuthMastodonRoute = { Body?: AuthPayload, Querystring: AuthPayload };
export class ApiAppsMastodon { export class ApiAppsMastodon {
constructor( constructor(
private readonly clientService: MastodonClientService, private readonly clientService: MastodonClientService,
private readonly logger: MastodonLogger,
) {} ) {}
public register(fastify: FastifyInstance, upload: ReturnType<typeof multer>): void { public register(fastify: FastifyInstance, upload: ReturnType<typeof multer>): void {
fastify.post<AuthMastodonRoute>('/v1/apps', { preHandler: upload.single('none') }, async (_request, reply) => { fastify.post<AuthMastodonRoute>('/v1/apps', { preHandler: upload.single('none') }, async (_request, reply) => {
try {
const body = _request.body ?? _request.query; const body = _request.body ?? _request.query;
if (!body.scopes) return reply.code(400).send({ error: 'Missing required payload "scopes"' }); if (!body.scopes) return reply.code(400).send({ error: 'Missing required payload "scopes"' });
if (!body.redirect_uris) return reply.code(400).send({ error: 'Missing required payload "redirect_uris"' }); if (!body.redirect_uris) return reply.code(400).send({ error: 'Missing required payload "redirect_uris"' });
@ -110,11 +107,6 @@ export class ApiAppsMastodon {
}; };
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error('GET /v1/apps', data);
reply.code(401).send(data);
}
}); });
} }
} }

View file

@ -6,7 +6,6 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { toBoolean } from '@/server/api/mastodon/argsUtils.js'; import { toBoolean } from '@/server/api/mastodon/argsUtils.js';
import { MastodonClientService } from '@/server/api/mastodon/MastodonClientService.js'; import { MastodonClientService } from '@/server/api/mastodon/MastodonClientService.js';
import { getErrorData, MastodonLogger } from '@/server/api/mastodon/MastodonLogger.js';
import { convertFilter } from '../converters.js'; import { convertFilter } from '../converters.js';
import type { FastifyInstance } from 'fastify'; import type { FastifyInstance } from 'fastify';
import type multer from 'fastify-multer'; import type multer from 'fastify-multer';
@ -28,27 +27,19 @@ interface ApiFilterMastodonRoute {
export class ApiFilterMastodon { export class ApiFilterMastodon {
constructor( constructor(
private readonly clientService: MastodonClientService, private readonly clientService: MastodonClientService,
private readonly logger: MastodonLogger,
) {} ) {}
public register(fastify: FastifyInstance, upload: ReturnType<typeof multer>): void { public register(fastify: FastifyInstance, upload: ReturnType<typeof multer>): void {
fastify.get('/v1/filters', async (_request, reply) => { fastify.get('/v1/filters', async (_request, reply) => {
try {
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
const data = await client.getFilters(); const data = await client.getFilters();
const response = data.data.map((filter) => convertFilter(filter)); const response = data.data.map((filter) => convertFilter(filter));
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error('GET /v1/filters', data);
reply.code(401).send(data);
}
}); });
fastify.get<ApiFilterMastodonRoute & { Params: { id?: string } }>('/v1/filters/:id', async (_request, reply) => { fastify.get<ApiFilterMastodonRoute & { Params: { id?: string } }>('/v1/filters/:id', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
@ -56,15 +47,9 @@ export class ApiFilterMastodon {
const response = convertFilter(data.data); const response = convertFilter(data.data);
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`GET /v1/filters/${_request.params.id}`, data);
reply.code(401).send(data);
}
}); });
fastify.post<ApiFilterMastodonRoute>('/v1/filters', { preHandler: upload.single('none') }, async (_request, reply) => { fastify.post<ApiFilterMastodonRoute>('/v1/filters', { preHandler: upload.single('none') }, async (_request, reply) => {
try {
if (!_request.body.phrase) return reply.code(400).send({ error: 'Missing required payload "phrase"' }); if (!_request.body.phrase) return reply.code(400).send({ error: 'Missing required payload "phrase"' });
if (!_request.body.context) return reply.code(400).send({ error: 'Missing required payload "context"' }); if (!_request.body.context) return reply.code(400).send({ error: 'Missing required payload "context"' });
@ -81,15 +66,9 @@ export class ApiFilterMastodon {
const response = convertFilter(data.data); const response = convertFilter(data.data);
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error('POST /v1/filters', data);
reply.code(401).send(data);
}
}); });
fastify.post<ApiFilterMastodonRoute & { Params: { id?: string } }>('/v1/filters/:id', { preHandler: upload.single('none') }, async (_request, reply) => { fastify.post<ApiFilterMastodonRoute & { Params: { id?: string } }>('/v1/filters/:id', { preHandler: upload.single('none') }, async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
if (!_request.body.phrase) return reply.code(400).send({ error: 'Missing required payload "phrase"' }); if (!_request.body.phrase) return reply.code(400).send({ error: 'Missing required payload "phrase"' });
if (!_request.body.context) return reply.code(400).send({ error: 'Missing required payload "context"' }); if (!_request.body.context) return reply.code(400).send({ error: 'Missing required payload "context"' });
@ -107,26 +86,15 @@ export class ApiFilterMastodon {
const response = convertFilter(data.data); const response = convertFilter(data.data);
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`POST /v1/filters/${_request.params.id}`, data);
reply.code(401).send(data);
}
}); });
fastify.delete<ApiFilterMastodonRoute & { Params: { id?: string } }>('/v1/filters/:id', async (_request, reply) => { fastify.delete<ApiFilterMastodonRoute & { Params: { id?: string } }>('/v1/filters/:id', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
const data = await client.deleteFilter(_request.params.id); const data = await client.deleteFilter(_request.params.id);
reply.send(data.data); reply.send(data.data);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`DELETE /v1/filters/${_request.params.id}`, data);
reply.code(401).send(data);
}
}); });
} }
} }

View file

@ -10,12 +10,9 @@ import type { Config } from '@/config.js';
import { DI } from '@/di-symbols.js'; import { DI } from '@/di-symbols.js';
import type { MiMeta, UsersRepository } from '@/models/_.js'; import type { MiMeta, UsersRepository } from '@/models/_.js';
import { MastoConverters } from '@/server/api/mastodon/converters.js'; import { MastoConverters } from '@/server/api/mastodon/converters.js';
import { getErrorData, MastodonLogger } from '@/server/api/mastodon/MastodonLogger.js';
import { MastodonClientService } from '@/server/api/mastodon/MastodonClientService.js'; import { MastodonClientService } from '@/server/api/mastodon/MastodonClientService.js';
import type { FastifyInstance } from 'fastify'; import type { FastifyInstance } from 'fastify';
// TODO rename to ApiInstanceMastodon
@Injectable() @Injectable()
export class ApiInstanceMastodon { export class ApiInstanceMastodon {
constructor( constructor(
@ -29,13 +26,11 @@ export class ApiInstanceMastodon {
private readonly config: Config, private readonly config: Config,
private readonly mastoConverters: MastoConverters, private readonly mastoConverters: MastoConverters,
private readonly logger: MastodonLogger,
private readonly clientService: MastodonClientService, private readonly clientService: MastodonClientService,
) {} ) {}
public register(fastify: FastifyInstance): void { public register(fastify: FastifyInstance): void {
fastify.get('/v1/instance', async (_request, reply) => { fastify.get('/v1/instance', async (_request, reply) => {
try {
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
const data = await client.getInstance(); const data = await client.getInstance();
const instance = data.data; const instance = data.data;
@ -100,11 +95,6 @@ export class ApiInstanceMastodon {
}; };
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error('GET /v1/instance', data);
reply.code(401).send(data);
}
}); });
} }
} }

View file

@ -6,7 +6,6 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { parseTimelineArgs, TimelineArgs } from '@/server/api/mastodon/argsUtils.js'; import { parseTimelineArgs, TimelineArgs } from '@/server/api/mastodon/argsUtils.js';
import { MastoConverters } from '@/server/api/mastodon/converters.js'; import { MastoConverters } from '@/server/api/mastodon/converters.js';
import { getErrorData, MastodonLogger } from '@/server/api/mastodon/MastodonLogger.js';
import { MastodonClientService } from '../MastodonClientService.js'; import { MastodonClientService } from '../MastodonClientService.js';
import type { FastifyInstance } from 'fastify'; import type { FastifyInstance } from 'fastify';
import type multer from 'fastify-multer'; import type multer from 'fastify-multer';
@ -23,12 +22,10 @@ export class ApiNotificationsMastodon {
constructor( constructor(
private readonly mastoConverters: MastoConverters, private readonly mastoConverters: MastoConverters,
private readonly clientService: MastodonClientService, private readonly clientService: MastodonClientService,
private readonly logger: MastodonLogger,
) {} ) {}
public register(fastify: FastifyInstance, upload: ReturnType<typeof multer>): void { public register(fastify: FastifyInstance, upload: ReturnType<typeof multer>): void {
fastify.get<ApiNotifyMastodonRoute>('/v1/notifications', async (_request, reply) => { fastify.get<ApiNotifyMastodonRoute>('/v1/notifications', async (_request, reply) => {
try {
const { client, me } = await this.clientService.getAuthClient(_request); const { client, me } = await this.clientService.getAuthClient(_request);
const data = await client.getNotifications(parseTimelineArgs(_request.query)); const data = await client.getNotifications(parseTimelineArgs(_request.query));
const response = Promise.all(data.data.map(async n => { const response = Promise.all(data.data.map(async n => {
@ -40,15 +37,9 @@ export class ApiNotificationsMastodon {
})); }));
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error('GET /v1/notifications', data);
reply.code(401).send(data);
}
}); });
fastify.get<ApiNotifyMastodonRoute & { Params: { id?: string } }>('/v1/notification/:id', async (_request, reply) => { fastify.get<ApiNotifyMastodonRoute & { Params: { id?: string } }>('/v1/notification/:id', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const { client, me } = await this.clientService.getAuthClient(_request); const { client, me } = await this.clientService.getAuthClient(_request);
@ -59,39 +50,22 @@ export class ApiNotificationsMastodon {
} }
reply.send(converted); reply.send(converted);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`GET /v1/notification/${_request.params.id}`, data);
reply.code(401).send(data);
}
}); });
fastify.post<ApiNotifyMastodonRoute & { Params: { id?: string } }>('/v1/notification/:id/dismiss', { preHandler: upload.single('none') }, async (_request, reply) => { fastify.post<ApiNotifyMastodonRoute & { Params: { id?: string } }>('/v1/notification/:id/dismiss', { preHandler: upload.single('none') }, async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
const data = await client.dismissNotification(_request.params.id); const data = await client.dismissNotification(_request.params.id);
reply.send(data.data); reply.send(data.data);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`POST /v1/notification/${_request.params.id}/dismiss`, data);
reply.code(401).send(data);
}
}); });
fastify.post<ApiNotifyMastodonRoute>('/v1/notifications/clear', { preHandler: upload.single('none') }, async (_request, reply) => { fastify.post<ApiNotifyMastodonRoute>('/v1/notifications/clear', { preHandler: upload.single('none') }, async (_request, reply) => {
try {
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
const data = await client.dismissNotifications(); const data = await client.dismissNotifications();
reply.send(data.data); reply.send(data.data);
} catch (e) {
const data = getErrorData(e);
this.logger.error('POST /v1/notifications/clear', data);
reply.code(401).send(data);
}
}); });
} }
} }

View file

@ -5,7 +5,6 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { MastodonClientService } from '@/server/api/mastodon/MastodonClientService.js'; import { MastodonClientService } from '@/server/api/mastodon/MastodonClientService.js';
import { getErrorData, MastodonLogger } from '@/server/api/mastodon/MastodonLogger.js';
import { MastoConverters } from '../converters.js'; import { MastoConverters } from '../converters.js';
import { parseTimelineArgs, TimelineArgs } from '../argsUtils.js'; import { parseTimelineArgs, TimelineArgs } from '../argsUtils.js';
import Account = Entity.Account; import Account = Entity.Account;
@ -24,12 +23,10 @@ export class ApiSearchMastodon {
constructor( constructor(
private readonly mastoConverters: MastoConverters, private readonly mastoConverters: MastoConverters,
private readonly clientService: MastodonClientService, private readonly clientService: MastodonClientService,
private readonly logger: MastodonLogger,
) {} ) {}
public register(fastify: FastifyInstance): void { public register(fastify: FastifyInstance): void {
fastify.get<ApiSearchMastodonRoute>('/v1/search', async (_request, reply) => { fastify.get<ApiSearchMastodonRoute>('/v1/search', async (_request, reply) => {
try {
if (!_request.query.q) return reply.code(400).send({ error: 'Missing required property "q"' }); if (!_request.query.q) return reply.code(400).send({ error: 'Missing required property "q"' });
const query = parseTimelineArgs(_request.query); const query = parseTimelineArgs(_request.query);
@ -37,15 +34,9 @@ export class ApiSearchMastodon {
const data = await client.search(_request.query.q, { type: _request.query.type, ...query }); const data = await client.search(_request.query.q, { type: _request.query.type, ...query });
reply.send(data.data); reply.send(data.data);
} catch (e) {
const data = getErrorData(e);
this.logger.error('GET /v1/search', data);
reply.code(401).send(data);
}
}); });
fastify.get<ApiSearchMastodonRoute>('/v2/search', async (_request, reply) => { fastify.get<ApiSearchMastodonRoute>('/v2/search', async (_request, reply) => {
try {
if (!_request.query.q) return reply.code(400).send({ error: 'Missing required property "q"' }); if (!_request.query.q) return reply.code(400).send({ error: 'Missing required property "q"' });
const query = parseTimelineArgs(_request.query); const query = parseTimelineArgs(_request.query);
@ -61,15 +52,9 @@ export class ApiSearchMastodon {
}; };
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error('GET /v2/search', data);
reply.code(401).send(data);
}
}); });
fastify.get<ApiSearchMastodonRoute>('/v1/trends/statuses', async (_request, reply) => { fastify.get<ApiSearchMastodonRoute>('/v1/trends/statuses', async (_request, reply) => {
try {
const baseUrl = this.clientService.getBaseUrl(_request); const baseUrl = this.clientService.getBaseUrl(_request);
const res = await fetch(`${baseUrl}/api/notes/featured`, const res = await fetch(`${baseUrl}/api/notes/featured`,
{ {
@ -86,15 +71,9 @@ export class ApiSearchMastodon {
const response = await Promise.all(data.map(status => this.mastoConverters.convertStatus(status, me))); const response = await Promise.all(data.map(status => this.mastoConverters.convertStatus(status, me)));
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error('GET /v1/trends/statuses', data);
reply.code(401).send(data);
}
}); });
fastify.get<ApiSearchMastodonRoute>('/v2/suggestions', async (_request, reply) => { fastify.get<ApiSearchMastodonRoute>('/v2/suggestions', async (_request, reply) => {
try {
const baseUrl = this.clientService.getBaseUrl(_request); const baseUrl = this.clientService.getBaseUrl(_request);
const res = await fetch(`${baseUrl}/api/users`, const res = await fetch(`${baseUrl}/api/users`,
{ {
@ -120,11 +99,6 @@ export class ApiSearchMastodon {
})); }));
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error('GET /v2/suggestions', data);
reply.code(401).send(data);
}
}); });
} }
} }

View file

@ -6,7 +6,6 @@
import querystring, { ParsedUrlQueryInput } from 'querystring'; import querystring, { ParsedUrlQueryInput } from 'querystring';
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { emojiRegexAtStartToEnd } from '@/misc/emoji-regex.js'; import { emojiRegexAtStartToEnd } from '@/misc/emoji-regex.js';
import { getErrorData, MastodonLogger } from '@/server/api/mastodon/MastodonLogger.js';
import { parseTimelineArgs, TimelineArgs, toBoolean, toInt } from '@/server/api/mastodon/argsUtils.js'; import { parseTimelineArgs, TimelineArgs, toBoolean, toInt } from '@/server/api/mastodon/argsUtils.js';
import { MastodonClientService } from '@/server/api/mastodon/MastodonClientService.js'; import { MastodonClientService } from '@/server/api/mastodon/MastodonClientService.js';
import { convertAttachment, convertPoll, MastoConverters } from '../converters.js'; import { convertAttachment, convertPoll, MastoConverters } from '../converters.js';
@ -22,13 +21,11 @@ function normalizeQuery(data: Record<string, unknown>) {
export class ApiStatusMastodon { export class ApiStatusMastodon {
constructor( constructor(
private readonly mastoConverters: MastoConverters, private readonly mastoConverters: MastoConverters,
private readonly logger: MastodonLogger,
private readonly clientService: MastodonClientService, private readonly clientService: MastodonClientService,
) {} ) {}
public register(fastify: FastifyInstance): void { public register(fastify: FastifyInstance): void {
fastify.get<{ Params: { id?: string } }>('/v1/statuses/:id', async (_request, reply) => { fastify.get<{ Params: { id?: string } }>('/v1/statuses/:id', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const { client, me } = await this.clientService.getAuthClient(_request); const { client, me } = await this.clientService.getAuthClient(_request);
@ -36,30 +33,18 @@ export class ApiStatusMastodon {
const response = await this.mastoConverters.convertStatus(data.data, me); const response = await this.mastoConverters.convertStatus(data.data, me);
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`GET /v1/statuses/${_request.params.id}`, data);
reply.code(_request.is404 ? 404 : 401).send(data);
}
}); });
fastify.get<{ Params: { id?: string } }>('/v1/statuses/:id/source', async (_request, reply) => { fastify.get<{ Params: { id?: string } }>('/v1/statuses/:id/source', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
const data = await client.getStatusSource(_request.params.id); const data = await client.getStatusSource(_request.params.id);
reply.send(data.data); reply.send(data.data);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`GET /v1/statuses/${_request.params.id}/source`, data);
reply.code(_request.is404 ? 404 : 401).send(data);
}
}); });
fastify.get<{ Params: { id?: string }, Querystring: TimelineArgs }>('/v1/statuses/:id/context', async (_request, reply) => { fastify.get<{ Params: { id?: string }, Querystring: TimelineArgs }>('/v1/statuses/:id/context', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const { client, me } = await this.clientService.getAuthClient(_request); const { client, me } = await this.clientService.getAuthClient(_request);
@ -69,30 +54,18 @@ export class ApiStatusMastodon {
const response = { ancestors, descendants }; const response = { ancestors, descendants };
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`GET /v1/statuses/${_request.params.id}/context`, data);
reply.code(_request.is404 ? 404 : 401).send(data);
}
}); });
fastify.get<{ Params: { id?: string } }>('/v1/statuses/:id/history', async (_request, reply) => { fastify.get<{ Params: { id?: string } }>('/v1/statuses/:id/history', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const user = await this.clientService.getAuth(_request); const user = await this.clientService.getAuth(_request);
const edits = await this.mastoConverters.getEdits(_request.params.id, user); const edits = await this.mastoConverters.getEdits(_request.params.id, user);
reply.send(edits); reply.send(edits);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`GET /v1/statuses/${_request.params.id}/history`, data);
reply.code(401).send(data);
}
}); });
fastify.get<{ Params: { id?: string } }>('/v1/statuses/:id/reblogged_by', async (_request, reply) => { fastify.get<{ Params: { id?: string } }>('/v1/statuses/:id/reblogged_by', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
@ -100,15 +73,9 @@ export class ApiStatusMastodon {
const response = await Promise.all(data.data.map((account: Entity.Account) => this.mastoConverters.convertAccount(account))); const response = await Promise.all(data.data.map((account: Entity.Account) => this.mastoConverters.convertAccount(account)));
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`GET /v1/statuses/${_request.params.id}/reblogged_by`, data);
reply.code(401).send(data);
}
}); });
fastify.get<{ Params: { id?: string } }>('/v1/statuses/:id/favourited_by', async (_request, reply) => { fastify.get<{ Params: { id?: string } }>('/v1/statuses/:id/favourited_by', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
@ -116,15 +83,9 @@ export class ApiStatusMastodon {
const response = await Promise.all(data.data.map((account: Entity.Account) => this.mastoConverters.convertAccount(account))); const response = await Promise.all(data.data.map((account: Entity.Account) => this.mastoConverters.convertAccount(account)));
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`GET /v1/statuses/${_request.params.id}/favourited_by`, data);
reply.code(401).send(data);
}
}); });
fastify.get<{ Params: { id?: string } }>('/v1/media/:id', async (_request, reply) => { fastify.get<{ Params: { id?: string } }>('/v1/media/:id', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
@ -132,15 +93,9 @@ export class ApiStatusMastodon {
const response = convertAttachment(data.data); const response = convertAttachment(data.data);
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`GET /v1/media/${_request.params.id}`, data);
reply.code(401).send(data);
}
}); });
fastify.get<{ Params: { id?: string } }>('/v1/polls/:id', async (_request, reply) => { fastify.get<{ Params: { id?: string } }>('/v1/polls/:id', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
@ -148,15 +103,9 @@ export class ApiStatusMastodon {
const response = convertPoll(data.data); const response = convertPoll(data.data);
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`GET /v1/polls/${_request.params.id}`, data);
reply.code(401).send(data);
}
}); });
fastify.post<{ Params: { id?: string }, Body: { choices?: number[] } }>('/v1/polls/:id/votes', async (_request, reply) => { fastify.post<{ Params: { id?: string }, Body: { choices?: number[] } }>('/v1/polls/:id/votes', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
if (!_request.body.choices) return reply.code(400).send({ error: 'Missing required payload "choices"' }); if (!_request.body.choices) return reply.code(400).send({ error: 'Missing required payload "choices"' });
@ -165,11 +114,6 @@ export class ApiStatusMastodon {
const response = convertPoll(data.data); const response = convertPoll(data.data);
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`GET /v1/polls/${_request.params.id}/votes`, data);
reply.code(401).send(data);
}
}); });
fastify.post<{ fastify.post<{
@ -196,8 +140,6 @@ export class ApiStatusMastodon {
} }
}>('/v1/statuses', async (_request, reply) => { }>('/v1/statuses', async (_request, reply) => {
let body = _request.body; let body = _request.body;
try {
const { client, me } = await this.clientService.getAuthClient(_request);
if ((!body.poll && body['poll[options][]']) || (!body.media_ids && body['media_ids[]']) if ((!body.poll && body['poll[options][]']) || (!body.media_ids && body['media_ids[]'])
) { ) {
body = normalizeQuery(body); body = normalizeQuery(body);
@ -206,6 +148,8 @@ export class ApiStatusMastodon {
const removed = text.replace(/@\S+/g, '').replace(/\s|/g, ''); const removed = text.replace(/@\S+/g, '').replace(/\s|/g, '');
const isDefaultEmoji = emojiRegexAtStartToEnd.test(removed); const isDefaultEmoji = emojiRegexAtStartToEnd.test(removed);
const isCustomEmoji = /^:[a-zA-Z0-9@_]+:$/.test(removed); const isCustomEmoji = /^:[a-zA-Z0-9@_]+:$/.test(removed);
const { client, me } = await this.clientService.getAuthClient(_request);
if ((body.in_reply_to_id && isDefaultEmoji) || (body.in_reply_to_id && isCustomEmoji)) { if ((body.in_reply_to_id && isDefaultEmoji) || (body.in_reply_to_id && isCustomEmoji)) {
const a = await client.createEmojiReaction( const a = await client.createEmojiReaction(
body.in_reply_to_id, body.in_reply_to_id,
@ -245,11 +189,6 @@ export class ApiStatusMastodon {
const response = await this.mastoConverters.convertStatus(data.data as Entity.Status, me); const response = await this.mastoConverters.convertStatus(data.data as Entity.Status, me);
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error('POST /v1/statuses', data);
reply.code(401).send(data);
}
}); });
fastify.put<{ fastify.put<{
@ -267,7 +206,6 @@ export class ApiStatusMastodon {
}, },
} }
}>('/v1/statuses/:id', async (_request, reply) => { }>('/v1/statuses/:id', async (_request, reply) => {
try {
const { client, me } = await this.clientService.getAuthClient(_request); const { client, me } = await this.clientService.getAuthClient(_request);
const body = _request.body; const body = _request.body;
@ -290,15 +228,9 @@ export class ApiStatusMastodon {
const response = await this.mastoConverters.convertStatus(data.data, me); const response = await this.mastoConverters.convertStatus(data.data, me);
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`POST /v1/statuses/${_request.params.id}`, data);
reply.code(401).send(data);
}
}); });
fastify.post<{ Params: { id?: string } }>('/v1/statuses/:id/favourite', async (_request, reply) => { fastify.post<{ Params: { id?: string } }>('/v1/statuses/:id/favourite', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const { client, me } = await this.clientService.getAuthClient(_request); const { client, me } = await this.clientService.getAuthClient(_request);
@ -306,15 +238,9 @@ export class ApiStatusMastodon {
const response = await this.mastoConverters.convertStatus(data.data, me); const response = await this.mastoConverters.convertStatus(data.data, me);
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`POST /v1/statuses/${_request.params.id}/favorite`, data);
reply.code(401).send(data);
}
}); });
fastify.post<{ Params: { id?: string } }>('/v1/statuses/:id/unfavourite', async (_request, reply) => { fastify.post<{ Params: { id?: string } }>('/v1/statuses/:id/unfavourite', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const { client, me } = await this.clientService.getAuthClient(_request); const { client, me } = await this.clientService.getAuthClient(_request);
@ -322,15 +248,9 @@ export class ApiStatusMastodon {
const response = await this.mastoConverters.convertStatus(data.data, me); const response = await this.mastoConverters.convertStatus(data.data, me);
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`GET /v1/statuses/${_request.params.id}/unfavorite`, data);
reply.code(401).send(data);
}
}); });
fastify.post<{ Params: { id?: string } }>('/v1/statuses/:id/reblog', async (_request, reply) => { fastify.post<{ Params: { id?: string } }>('/v1/statuses/:id/reblog', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const { client, me } = await this.clientService.getAuthClient(_request); const { client, me } = await this.clientService.getAuthClient(_request);
@ -338,15 +258,9 @@ export class ApiStatusMastodon {
const response = await this.mastoConverters.convertStatus(data.data, me); const response = await this.mastoConverters.convertStatus(data.data, me);
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`POST /v1/statuses/${_request.params.id}/reblog`, data);
reply.code(401).send(data);
}
}); });
fastify.post<{ Params: { id?: string } }>('/v1/statuses/:id/unreblog', async (_request, reply) => { fastify.post<{ Params: { id?: string } }>('/v1/statuses/:id/unreblog', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const { client, me } = await this.clientService.getAuthClient(_request); const { client, me } = await this.clientService.getAuthClient(_request);
@ -354,15 +268,9 @@ export class ApiStatusMastodon {
const response = await this.mastoConverters.convertStatus(data.data, me); const response = await this.mastoConverters.convertStatus(data.data, me);
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`POST /v1/statuses/${_request.params.id}/unreblog`, data);
reply.code(401).send(data);
}
}); });
fastify.post<{ Params: { id?: string } }>('/v1/statuses/:id/bookmark', async (_request, reply) => { fastify.post<{ Params: { id?: string } }>('/v1/statuses/:id/bookmark', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const { client, me } = await this.clientService.getAuthClient(_request); const { client, me } = await this.clientService.getAuthClient(_request);
@ -370,15 +278,9 @@ export class ApiStatusMastodon {
const response = await this.mastoConverters.convertStatus(data.data, me); const response = await this.mastoConverters.convertStatus(data.data, me);
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`POST /v1/statuses/${_request.params.id}/bookmark`, data);
reply.code(401).send(data);
}
}); });
fastify.post<{ Params: { id?: string } }>('/v1/statuses/:id/unbookmark', async (_request, reply) => { fastify.post<{ Params: { id?: string } }>('/v1/statuses/:id/unbookmark', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const { client, me } = await this.clientService.getAuthClient(_request); const { client, me } = await this.clientService.getAuthClient(_request);
@ -386,14 +288,8 @@ export class ApiStatusMastodon {
const response = await this.mastoConverters.convertStatus(data.data, me); const response = await this.mastoConverters.convertStatus(data.data, me);
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`POST /v1/statuses/${_request.params.id}/unbookmark`, data);
reply.code(401).send(data);
}
}); });
fastify.post<{ Params: { id?: string } }>('/v1/statuses/:id/pin', async (_request, reply) => { fastify.post<{ Params: { id?: string } }>('/v1/statuses/:id/pin', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const { client, me } = await this.clientService.getAuthClient(_request); const { client, me } = await this.clientService.getAuthClient(_request);
@ -401,15 +297,9 @@ export class ApiStatusMastodon {
const response = await this.mastoConverters.convertStatus(data.data, me); const response = await this.mastoConverters.convertStatus(data.data, me);
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`POST /v1/statuses/${_request.params.id}/pin`, data);
reply.code(401).send(data);
}
}); });
fastify.post<{ Params: { id?: string } }>('/v1/statuses/:id/unpin', async (_request, reply) => { fastify.post<{ Params: { id?: string } }>('/v1/statuses/:id/unpin', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const { client, me } = await this.clientService.getAuthClient(_request); const { client, me } = await this.clientService.getAuthClient(_request);
@ -417,15 +307,9 @@ export class ApiStatusMastodon {
const response = await this.mastoConverters.convertStatus(data.data, me); const response = await this.mastoConverters.convertStatus(data.data, me);
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`POST /v1/statuses/${_request.params.id}/unpin`, data);
reply.code(401).send(data);
}
}); });
fastify.post<{ Params: { id?: string, name?: string } }>('/v1/statuses/:id/react/:name', async (_request, reply) => { fastify.post<{ Params: { id?: string, name?: string } }>('/v1/statuses/:id/react/:name', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
if (!_request.params.name) return reply.code(400).send({ error: 'Missing required parameter "name"' }); if (!_request.params.name) return reply.code(400).send({ error: 'Missing required parameter "name"' });
@ -434,15 +318,9 @@ export class ApiStatusMastodon {
const response = await this.mastoConverters.convertStatus(data.data, me); const response = await this.mastoConverters.convertStatus(data.data, me);
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`POST /v1/statuses/${_request.params.id}/react/${_request.params.name}`, data);
reply.code(401).send(data);
}
}); });
fastify.post<{ Params: { id?: string, name?: string } }>('/v1/statuses/:id/unreact/:name', async (_request, reply) => { fastify.post<{ Params: { id?: string, name?: string } }>('/v1/statuses/:id/unreact/:name', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
if (!_request.params.name) return reply.code(400).send({ error: 'Missing required parameter "name"' }); if (!_request.params.name) return reply.code(400).send({ error: 'Missing required parameter "name"' });
@ -451,26 +329,15 @@ export class ApiStatusMastodon {
const response = await this.mastoConverters.convertStatus(data.data, me); const response = await this.mastoConverters.convertStatus(data.data, me);
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`POST /v1/statuses/${_request.params.id}/unreact/${_request.params.name}`, data);
reply.code(401).send(data);
}
}); });
fastify.delete<{ Params: { id?: string } }>('/v1/statuses/:id', async (_request, reply) => { fastify.delete<{ Params: { id?: string } }>('/v1/statuses/:id', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
const data = await client.deleteStatus(_request.params.id); const data = await client.deleteStatus(_request.params.id);
reply.send(data.data); reply.send(data.data);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`DELETE /v1/statuses/${_request.params.id}`, data);
reply.code(401).send(data);
}
}); });
} }
} }

View file

@ -4,7 +4,6 @@
*/ */
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { getErrorData, MastodonLogger } from '@/server/api/mastodon/MastodonLogger.js';
import { MastodonClientService } from '@/server/api/mastodon/MastodonClientService.js'; import { MastodonClientService } from '@/server/api/mastodon/MastodonClientService.js';
import { convertList, MastoConverters } from '../converters.js'; import { convertList, MastoConverters } from '../converters.js';
import { parseTimelineArgs, TimelineArgs, toBoolean } from '../argsUtils.js'; import { parseTimelineArgs, TimelineArgs, toBoolean } from '../argsUtils.js';
@ -16,14 +15,11 @@ export class ApiTimelineMastodon {
constructor( constructor(
private readonly clientService: MastodonClientService, private readonly clientService: MastodonClientService,
private readonly mastoConverters: MastoConverters, private readonly mastoConverters: MastoConverters,
private readonly logger: MastodonLogger,
) {} ) {}
public register(fastify: FastifyInstance): void { public register(fastify: FastifyInstance): void {
fastify.get<{ Querystring: TimelineArgs }>('/v1/timelines/public', async (_request, reply) => { fastify.get<{ Querystring: TimelineArgs }>('/v1/timelines/public', async (_request, reply) => {
try {
const { client, me } = await this.clientService.getAuthClient(_request); const { client, me } = await this.clientService.getAuthClient(_request);
const query = parseTimelineArgs(_request.query); const query = parseTimelineArgs(_request.query);
const data = toBoolean(_request.query.local) const data = toBoolean(_request.query.local)
? await client.getLocalTimeline(query) ? await client.getLocalTimeline(query)
@ -31,30 +27,18 @@ export class ApiTimelineMastodon {
const response = await Promise.all(data.data.map((status: Entity.Status) => this.mastoConverters.convertStatus(status, me))); const response = await Promise.all(data.data.map((status: Entity.Status) => this.mastoConverters.convertStatus(status, me)));
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error('GET /v1/timelines/public', data);
reply.code(401).send(data);
}
}); });
fastify.get<{ Querystring: TimelineArgs }>('/v1/timelines/home', async (_request, reply) => { fastify.get<{ Querystring: TimelineArgs }>('/v1/timelines/home', async (_request, reply) => {
try {
const { client, me } = await this.clientService.getAuthClient(_request); const { client, me } = await this.clientService.getAuthClient(_request);
const query = parseTimelineArgs(_request.query); const query = parseTimelineArgs(_request.query);
const data = await client.getHomeTimeline(query); const data = await client.getHomeTimeline(query);
const response = await Promise.all(data.data.map((status: Entity.Status) => this.mastoConverters.convertStatus(status, me))); const response = await Promise.all(data.data.map((status: Entity.Status) => this.mastoConverters.convertStatus(status, me)));
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error('GET /v1/timelines/home', data);
reply.code(401).send(data);
}
}); });
fastify.get<{ Params: { hashtag?: string }, Querystring: TimelineArgs }>('/v1/timelines/tag/:hashtag', async (_request, reply) => { fastify.get<{ Params: { hashtag?: string }, Querystring: TimelineArgs }>('/v1/timelines/tag/:hashtag', async (_request, reply) => {
try {
if (!_request.params.hashtag) return reply.code(400).send({ error: 'Missing required parameter "hashtag"' }); if (!_request.params.hashtag) return reply.code(400).send({ error: 'Missing required parameter "hashtag"' });
const { client, me } = await this.clientService.getAuthClient(_request); const { client, me } = await this.clientService.getAuthClient(_request);
@ -63,15 +47,9 @@ export class ApiTimelineMastodon {
const response = await Promise.all(data.data.map((status: Entity.Status) => this.mastoConverters.convertStatus(status, me))); const response = await Promise.all(data.data.map((status: Entity.Status) => this.mastoConverters.convertStatus(status, me)));
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`GET /v1/timelines/tag/${_request.params.hashtag}`, data);
reply.code(401).send(data);
}
}); });
fastify.get<{ Params: { id?: string }, Querystring: TimelineArgs }>('/v1/timelines/list/:id', async (_request, reply) => { fastify.get<{ Params: { id?: string }, Querystring: TimelineArgs }>('/v1/timelines/list/:id', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const { client, me } = await this.clientService.getAuthClient(_request); const { client, me } = await this.clientService.getAuthClient(_request);
@ -80,30 +58,18 @@ export class ApiTimelineMastodon {
const response = await Promise.all(data.data.map(async (status: Entity.Status) => await this.mastoConverters.convertStatus(status, me))); const response = await Promise.all(data.data.map(async (status: Entity.Status) => await this.mastoConverters.convertStatus(status, me)));
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`GET /v1/timelines/list/${_request.params.id}`, data);
reply.code(401).send(data);
}
}); });
fastify.get<{ Querystring: TimelineArgs }>('/v1/conversations', async (_request, reply) => { fastify.get<{ Querystring: TimelineArgs }>('/v1/conversations', async (_request, reply) => {
try {
const { client, me } = await this.clientService.getAuthClient(_request); const { client, me } = await this.clientService.getAuthClient(_request);
const query = parseTimelineArgs(_request.query); const query = parseTimelineArgs(_request.query);
const data = await client.getConversationTimeline(query); const data = await client.getConversationTimeline(query);
const conversations = await Promise.all(data.data.map((conversation: Entity.Conversation) => this.mastoConverters.convertConversation(conversation, me))); const conversations = await Promise.all(data.data.map((conversation: Entity.Conversation) => this.mastoConverters.convertConversation(conversation, me)));
reply.send(conversations); reply.send(conversations);
} catch (e) {
const data = getErrorData(e);
this.logger.error('GET /v1/conversations', data);
reply.code(401).send(data);
}
}); });
fastify.get<{ Params: { id?: string } }>('/v1/lists/:id', async (_request, reply) => { fastify.get<{ Params: { id?: string } }>('/v1/lists/:id', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
@ -111,29 +77,17 @@ export class ApiTimelineMastodon {
const response = convertList(data.data); const response = convertList(data.data);
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`GET /v1/lists/${_request.params.id}`, data);
reply.code(401).send(data);
}
}); });
fastify.get('/v1/lists', async (_request, reply) => { fastify.get('/v1/lists', async (_request, reply) => {
try {
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
const data = await client.getLists(); const data = await client.getLists();
const response = data.data.map((list: Entity.List) => convertList(list)); const response = data.data.map((list: Entity.List) => convertList(list));
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error('GET /v1/lists', data);
reply.code(401).send(data);
}
}); });
fastify.get<{ Params: { id?: string }, Querystring: { limit?: number, max_id?: string, since_id?: string } }>('/v1/lists/:id/accounts', async (_request, reply) => { fastify.get<{ Params: { id?: string }, Querystring: { limit?: number, max_id?: string, since_id?: string } }>('/v1/lists/:id/accounts', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
@ -141,15 +95,9 @@ export class ApiTimelineMastodon {
const accounts = await Promise.all(data.data.map((account: Entity.Account) => this.mastoConverters.convertAccount(account))); const accounts = await Promise.all(data.data.map((account: Entity.Account) => this.mastoConverters.convertAccount(account)));
reply.send(accounts); reply.send(accounts);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`GET /v1/lists/${_request.params.id}/accounts`, data);
reply.code(401).send(data);
}
}); });
fastify.post<{ Params: { id?: string }, Querystring: { accounts_id?: string[] } }>('/v1/lists/:id/accounts', async (_request, reply) => { fastify.post<{ Params: { id?: string }, Querystring: { accounts_id?: string[] } }>('/v1/lists/:id/accounts', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); 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"' }); if (!_request.query.accounts_id) return reply.code(400).send({ error: 'Missing required property "accounts_id"' });
@ -157,15 +105,9 @@ export class ApiTimelineMastodon {
const data = await client.addAccountsToList(_request.params.id, _request.query.accounts_id); const data = await client.addAccountsToList(_request.params.id, _request.query.accounts_id);
reply.send(data.data); reply.send(data.data);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`POST /v1/lists/${_request.params.id}/accounts`, data);
reply.code(401).send(data);
}
}); });
fastify.delete<{ Params: { id?: string }, Querystring: { accounts_id?: string[] } }>('/v1/lists/:id/accounts', async (_request, reply) => { fastify.delete<{ Params: { id?: string }, Querystring: { accounts_id?: string[] } }>('/v1/lists/:id/accounts', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); 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"' }); if (!_request.query.accounts_id) return reply.code(400).send({ error: 'Missing required property "accounts_id"' });
@ -173,15 +115,9 @@ export class ApiTimelineMastodon {
const data = await client.deleteAccountsFromList(_request.params.id, _request.query.accounts_id); const data = await client.deleteAccountsFromList(_request.params.id, _request.query.accounts_id);
reply.send(data.data); reply.send(data.data);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`DELETE /v1/lists/${_request.params.id}/accounts`, data);
reply.code(401).send(data);
}
}); });
fastify.post<{ Body: { title?: string } }>('/v1/lists', async (_request, reply) => { fastify.post<{ Body: { title?: string } }>('/v1/lists', async (_request, reply) => {
try {
if (!_request.body.title) return reply.code(400).send({ error: 'Missing required payload "title"' }); if (!_request.body.title) return reply.code(400).send({ error: 'Missing required payload "title"' });
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
@ -189,15 +125,9 @@ export class ApiTimelineMastodon {
const response = convertList(data.data); const response = convertList(data.data);
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error('POST /v1/lists', data);
reply.code(401).send(data);
}
}); });
fastify.put<{ Params: { id?: string }, Body: { title?: string } }>('/v1/lists/:id', async (_request, reply) => { fastify.put<{ Params: { id?: string }, Body: { title?: string } }>('/v1/lists/:id', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); 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"' }); if (!_request.body.title) return reply.code(400).send({ error: 'Missing required payload "title"' });
@ -206,26 +136,15 @@ export class ApiTimelineMastodon {
const response = convertList(data.data); const response = convertList(data.data);
reply.send(response); reply.send(response);
} catch (e) {
const data = getErrorData(e);
this.logger.error(`PUT /v1/lists/${_request.params.id}`, data);
reply.code(401).send(data);
}
}); });
fastify.delete<{ Params: { id?: string } }>('/v1/lists/:id', async (_request, reply) => { fastify.delete<{ Params: { id?: string } }>('/v1/lists/:id', async (_request, reply) => {
try {
if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' }); if (!_request.params.id) return reply.code(400).send({ error: 'Missing required parameter "id"' });
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
await client.deleteList(_request.params.id); await client.deleteList(_request.params.id);
reply.send({}); reply.send({});
} catch (e) {
const data = getErrorData(e);
this.logger.error(`DELETE /v1/lists/${_request.params.id}`, data);
reply.code(401).send(data);
}
}); });
} }
} }