Skip to content

Commit

Permalink
chore: save event as staging after validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo committed Dec 17, 2024
1 parent 524c698 commit 0780228
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
13 changes: 13 additions & 0 deletions packages/homeserver/src/plugins/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { InferContext } from "elysia";
import { type Db, MongoClient } from "mongodb";

import type { EventBase } from "@hs/core/src/events/eventBase";
import { generateId } from "../authentication";

export interface Server {
_id: string;
Expand Down Expand Up @@ -153,6 +154,17 @@ export const routerWithMongodb = (db: Db) =>
);
};

const createStagingEvent = async (event: EventBase) => {
const id = generateId(event);
await eventsCollection.insertOne({
_id: id,
event,
staged: true,
});

return id;
};

return {
serversCollection,
getValidPublicKeyFromLocal,
Expand All @@ -163,6 +175,7 @@ export const routerWithMongodb = (db: Db) =>
getMissingEventsByDeep,
getLastEvent,
getAuthEvents,
createStagingEvent,
};
})(),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ describe("/send/:txnId", () => {
return;
},
},
createStagingEvent: async () => {
return;
},
serversCollection: {
findOne: async () => {
return;
Expand Down Expand Up @@ -83,6 +86,7 @@ describe("/send/:txnId", () => {
}),
);

const data = await resp.json();
expect(resp.status).toBe(400);
});

Expand Down Expand Up @@ -180,9 +184,12 @@ describe("/send/:txnId", () => {
);

const data = await resp.json();
const id = generateId(signedPdu);
expect(resp.status).toBe(200);
expect(data).toHaveProperty("pdus");
expect(data.pdus).toBeEmptyObject();
expect(data.pdus).toStrictEqual({
[id]: {},
});
});
});
});
Expand Down Expand Up @@ -228,6 +235,9 @@ describe("/send/:txnId using real case", () => {
return;
},
},
createStagingEvent: async () => {
return;
},
serversCollection: {
findOne: async () => {
return;
Expand Down
25 changes: 7 additions & 18 deletions packages/homeserver/src/routes/federation/sendTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const sendTransactionRoute = new Elysia().put(

const {
config,
mongo: { eventsCollection },
mongo: { eventsCollection, createStagingEvent },
} = context;

const { pdus, edus = [] } = body as any;
Expand Down Expand Up @@ -167,28 +167,17 @@ export const sendTransactionRoute = new Elysia().put(
for (const [roomId, pdus] of pdusByRoomId) {
// const roomVersion = getRoomVersion
for (const pdu of pdus) {
if (
!(await validatePdu(pdu).catch((e) => {
console.error("error validating pdu", e);
return true;
}))
) {
try {
await validatePdu(pdu);
resultPDUs[`${generateId(pdu)}`] = {};
void createStagingEvent(pdu);
} catch (e) {
console.error("error validating pdu", e);
resultPDUs[`${generateId(pdu)}`] = e as any;
}
}
}

await eventsCollection
.insertMany(
pdus.map((event) => ({
_id: generateId(event),
event,
})),
)
.catch((e) => {
console.error("error saving event", e);
});

return {
pdus: resultPDUs,
};
Expand Down

0 comments on commit 0780228

Please sign in to comment.