diff --git a/packages/homeserver/src/controllers/federation/transactions.controller.ts b/packages/homeserver/src/controllers/federation/transactions.controller.ts index 8f1b2b10c..9435f4e06 100644 --- a/packages/homeserver/src/controllers/federation/transactions.controller.ts +++ b/packages/homeserver/src/controllers/federation/transactions.controller.ts @@ -1,37 +1,77 @@ -import { EventService } from '@hs/federation-sdk'; +import { ConfigService, EventService } from '@hs/federation-sdk'; import { Elysia } from 'elysia'; import { container } from 'tsyringe'; import { ErrorResponseDto, + GetEventErrorResponseDto, + GetEventParamsDto, + GetEventResponseDto, SendTransactionBodyDto, SendTransactionResponseDto, } from '../../dtos'; export const transactionsPlugin = (app: Elysia) => { const eventService = container.resolve(EventService); - return app.put( - '/_matrix/federation/v1/send/:txnId', - async ({ body }) => { - // TODO need to validate better the payload - // biome-ignore lint/suspicious/noExplicitAny: - await eventService.processIncomingTransaction(body as any); + const configService = container.resolve(ConfigService); - return { - pdus: {}, - edus: {}, - }; - }, - { - body: SendTransactionBodyDto, - response: { - 200: SendTransactionResponseDto, - 400: ErrorResponseDto, + return app + .put( + '/_matrix/federation/v1/send/:txnId', + async ({ body }) => { + // TODO need to validate better the payload + // biome-ignore lint/suspicious/noExplicitAny: + await eventService.processIncomingTransaction(body as any); + + return { + pdus: {}, + edus: {}, + }; + }, + { + body: SendTransactionBodyDto, + response: { + 200: SendTransactionResponseDto, + 400: ErrorResponseDto, + }, + detail: { + tags: ['Federation'], + summary: 'Send transaction', + description: 'Send a transaction', + }, + }, + ) + .get( + '/_matrix/federation/v1/event/:eventId', + async ({ params, set }) => { + const eventData = await eventService.getEventById(params.eventId); + if (!eventData) { + set.status = 404; + return { + errcode: 'M_NOT_FOUND', + error: 'Event not found', + }; + } + + return { + origin_server_ts: eventData.event.origin_server_ts, + origin: configService.serverName, + pdus: [{ ...eventData.event, origin: configService.serverName }], + }; }, - detail: { - tags: ['Federation'], - summary: 'Send transaction', - description: 'Send a transaction', + { + params: GetEventParamsDto, + response: { + 200: GetEventResponseDto, + 401: GetEventErrorResponseDto, + 403: GetEventErrorResponseDto, + 404: GetEventErrorResponseDto, + 500: GetEventErrorResponseDto, + }, + detail: { + tags: ['Federation'], + summary: 'Get event', + description: 'Get an event', + }, }, - }, - ); + ); }; diff --git a/packages/homeserver/src/dtos/federation/transactions.dto.ts b/packages/homeserver/src/dtos/federation/transactions.dto.ts index 58e2e7a71..54849fe3a 100644 --- a/packages/homeserver/src/dtos/federation/transactions.dto.ts +++ b/packages/homeserver/src/dtos/federation/transactions.dto.ts @@ -29,6 +29,25 @@ export const SendTransactionResponseDto = t.Object({ }), }); +export const GetEventParamsDto = t.Object({ + eventId: t.String({ description: 'Event ID' }), +}); + +export const GetEventResponseDto = t.Object({ + origin_server_ts: t.Number({ description: 'Origin server timestamp' }), + origin: t.String({ description: 'Origin server' }), + pdus: t.Array(EventBaseDto, { + description: 'An array containing a single PDU', + }), +}); + +export const GetEventErrorResponseDto = t.Object({ + errcode: t.String({ description: 'Error code' }), + error: t.String({ description: 'Error message' }), +}); + export type SendTransactionParams = Static; export type SendTransactionBody = Static; export type SendTransactionResponse = Static; +export type GetEventParams = Static; +export type GetEventResponse = Static;