Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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',
},
},
},
);
);
};
19 changes: 19 additions & 0 deletions packages/homeserver/src/dtos/federation/transactions.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof SendTransactionParamsDto>;
export type SendTransactionBody = Static<typeof SendTransactionBodyDto>;
export type SendTransactionResponse = Static<typeof SendTransactionResponseDto>;
export type GetEventParams = Static<typeof GetEventParamsDto>;
export type GetEventResponse = Static<typeof GetEventResponseDto>;