diff --git a/packages/homeserver/src/routes/federation/index.ts b/packages/homeserver/src/routes/federation/index.ts index a8925717..092b9caa 100644 --- a/packages/homeserver/src/routes/federation/index.ts +++ b/packages/homeserver/src/routes/federation/index.ts @@ -8,6 +8,8 @@ import { makeJoinEndpoint } from "./makeJoin"; import { sendJoinEndpoint } from "./sendJoin"; import { getMissingEventsRoute } from "./getMissingEvents"; import validateHeaderSignature from "../../plugins/validateHeaderSignature"; +import { isMongodbContext } from "../../plugins/isMongodbContext"; +import { generateId } from "../../authentication"; const federationV1Endpoints = new Elysia({ prefix: "/_matrix/federation/v1", @@ -18,10 +20,29 @@ const federationV1Endpoints = new Elysia({ .use(queryEndpoints) .use(makeJoinEndpoint) .use(getMissingEventsRoute) - .put("/send/:txnId", ({ params, body }) => { + .put("/send/:txnId", async ({ params, body, ...context }) => { console.log("receive send ->", params); console.log("body ->", body); + if (!isMongodbContext(context)) { + throw new Error("No mongodb context"); + } + + const { + mongo: { eventsCollection }, + } = context; + + const { pdus } = body as any; + + if (pdus) { + await eventsCollection.insertMany( + pdus.map((event: any) => ({ + _id: generateId(event), + event, + })) + ); + } + return { [params.txnId]: {}, };