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

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