Skip to content

Commit

Permalink
convert makeGetMissingEventsProcedure
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo committed Dec 9, 2024
1 parent ab70f07 commit c7db17f
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 52 deletions.
48 changes: 48 additions & 0 deletions packages/homeserver/src/plugins/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,52 @@ export const routerWithMongodb = (db: Db) =>
);
};

const getDeepEarliestAndLatestEvents = async (
roomId: string,
earliest_events: string[],
latest_events: string[],
) => {
const depths = await eventsCollection
.find(
{
_id: { $in: [...earliest_events, ...latest_events] },
"event.room_id": roomId,
},
{ projection: { "event.depth": 1 } },
)
.toArray()
.then((events) => events.map((event) => event.event.depth));

if (depths.length === 0) {
return [];
}

const minDepth = Math.min(...depths);
const maxDepth = Math.max(...depths);

return [minDepth, maxDepth];
};

const getMissingEventsByDeep = async (
roomId: string,
minDepth: number,
maxDepth: number,
limit: number,
) => {
const events = await eventsCollection
.find(
{
"event.room_id": roomId,
"event.depth": { $gte: minDepth, $lte: maxDepth },
},
{ limit: limit, sort: { "event.depth": 1 } },
)
.map((event) => event.event)
.toArray();

return events;
};

const getAuthEvents = async (roomId: string) => {
return eventsCollection
.find(
Expand Down Expand Up @@ -51,6 +97,8 @@ export const routerWithMongodb = (db: Db) =>

return {
eventsCollection,
getDeepEarliestAndLatestEvents,
getMissingEventsByDeep,
getLastEvent,
getAuthEvents,
};
Expand Down
39 changes: 39 additions & 0 deletions packages/homeserver/src/procedures/getMissingEvents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { EventBase } from "../events/eventBase";

export const makeGetMissingEventsProcedure = (
getDeepEarliestAndLatestEvents: (
roomId: string,
earliest_events: string[],
latest_events: string[],
) => Promise<number[]>,
getMissingEventsByDeep: (
roomId: string,
minDepth: number,
maxDepth: number,
limit: number,
) => Promise<EventBase[]>,
) => {
return async (
roomId: string,
earliest_events: string[],
latest_events: string[],
limit: number,
) => {
const [minDepth, maxDepth] = await getDeepEarliestAndLatestEvents(
roomId,
earliest_events,
latest_events,
);

const events = await getMissingEventsByDeep(
roomId,
minDepth,
maxDepth,
limit,
);

return {
events,
};
};
};
66 changes: 16 additions & 50 deletions packages/homeserver/src/routes/federation/getMissingEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,37 @@ import { Elysia, t } from "elysia";
import "@hs/endpoints/src/query";
import "@hs/endpoints/src/server";
import { isMongodbContext } from "../../plugins/isMongodbContext";
import { makeGetMissingEventsProcedure } from "../../procedures/getMissingEvents";

//POST http://rc1:443/_matrix/federation/v1/get_missing_events/%21EiexWZWYPDXWLzPRCq%3Arc1

export const getMissingEvents = new Elysia().post(
export const getMissingEventsRoute = new Elysia().post(
"/get_missing_events/:roomId",
async ({ params, body, ...context }) => {
if (!isMongodbContext(context)) {
throw new Error("No mongodb context");
}
const {
mongo: { eventsCollection },
} = context;
const roomId = decodeURIComponent(params.roomId);

console.log("get_missing_events ->", { roomId });
console.log("get_missing_events ->", { body });

console.log({
_id: { $in: [...body.earliest_events, ...body.latest_events] },
"event.room_id": roomId,
});

const depths = await eventsCollection
.find(
{
_id: { $in: [...body.earliest_events, ...body.latest_events] },
"event.room_id": roomId,
},
{ projection: { "event.depth": 1 } },
)
.toArray()
.then((events) => events.map((event) => event.event.depth));

if (depths.length === 0) {
console.log("get_missing_events depths -> No events found");
return {
events: [],
};
}

console.log("get_missing_events depths ->", depths);

const minDepth = Math.min(...depths);
const maxDepth = Math.max(...depths);
const {
mongo: { getDeepEarliestAndLatestEvents, getMissingEventsByDeep },
} = context;

console.log("get_missing_events depths ->", { minDepth, maxDepth });
const getMissingEvents = makeGetMissingEventsProcedure(
getDeepEarliestAndLatestEvents,
getMissingEventsByDeep,
);

const events = await eventsCollection
.find(
{
"event.room_id": roomId,
"event.depth": { $gte: minDepth, $lte: maxDepth },
},
{ limit: body.limit, sort: { "event.depth": 1 } },
)
.toArray()
.then((events) => events.map((event) => event.event));
const events = await getMissingEvents(
roomId,
body.earliest_events,
body.latest_events,
body.limit,
);

const result = {
return {
events,
};

console.log("get_missing_events result ->", result);

return result;
},
{
params: t.Object(
Expand Down
4 changes: 2 additions & 2 deletions packages/homeserver/src/routes/federation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { usersEndpoints } from "./users";
import { versionEndpoints } from "./version";
import { makeJoinEndpoint } from "./makeJoin";
import { sendJoinEndpoint } from "./sendJoin";
import { getMissingEvents } from "./getMissingEvents";
import { getMissingEventsRoute } from "./getMissingEvents";

const federationV1Endpoints = new Elysia({
prefix: "/_matrix/federation/v1",
Expand All @@ -15,7 +15,7 @@ const federationV1Endpoints = new Elysia({
.use(usersEndpoints)
.use(queryEndpoints)
.use(makeJoinEndpoint)
.use(getMissingEvents)
.use(getMissingEventsRoute)
.put("/send/:txnId", ({ params, body }) => {
console.log("receive send ->", params);
console.log("body ->", body);
Expand Down

0 comments on commit c7db17f

Please sign in to comment.