decentralised sync engine

feat: more wip setup flow

serenity 95e37b07 da18e1a4

Changed files
+74 -1
src
+74 -1
src/index.ts
···
import { OWNER_DID, SERVER_PORT, SERVICE_DID } from "@/lib/env";
import { setRegistrationState } from "@/lib/state";
-
import { getRecordFromAtUri } from "@/lib/utils/atproto";
+
import type { AtUri, Did } from "@/lib/types/atproto";
+
import { systemsGmstnDevelopmentChannelRecordSchema } from "@/lib/types/lexicon/systems.gmstn.development.channel";
+
import { getRecordFromAtUri, stringToAtUri } from "@/lib/utils/atproto";
+
import { getConstellationBacklink } from "@/lib/utils/constellation";
import { newErrorResponse } from "@/lib/utils/http/responses";
import { connectToPrism } from "@/lib/utils/prism";
import {
···
wantedCollections: ["systems.gmstn.development.*"],
});
+
// TODO: probably move this to an `attachListeners` hook that attaches the listeners we want.
attachLatticeRegistrationListener(prismWebsocket);
+
+
const constellationBacklinksResult = await getConstellationBacklink({
+
subject: SERVICE_DID,
+
source: {
+
nsid: "systems.gmstn.development.channel",
+
fieldName: "routeThrough.uri",
+
},
+
});
+
+
if (!constellationBacklinksResult.ok) {
+
throw new Error(
+
"Something went wrong fetching constellation backlinks to do Shard handshakes",
+
);
+
}
+
+
const { records: channelBacklinks } = constellationBacklinksResult.data;
+
+
// TODO: For private lattices, do permission check on owner's PDS
+
// and filter out records from unauthorised pdses.
+
+
const channelRecordsPromises = channelBacklinks.map(
+
async ({ did, collection, rkey }) =>
+
await getRecordFromAtUri({
+
// @ts-expect-error seriously i gotta do something about the template literals not converting properly SIGH
+
authority: did,
+
collection,
+
rKey: rkey,
+
}),
+
);
+
+
const channelRecordResults = await Promise.all(channelRecordsPromises);
+
+
// mapping of shard -> list of channels (all AtUris)
+
const channelsByShard = new Map<AtUri, Array<AtUri>>();
+
+
channelRecordResults.forEach((result, idx) => {
+
if (!result.ok) return;
+
const { success, data: channelRecord } =
+
systemsGmstnDevelopmentChannelRecordSchema.safeParse(result.data);
+
if (!success) return;
+
const { storeAt } = channelRecord;
+
+
const storeAtAtUriResult = stringToAtUri(storeAt.uri);
+
if (!storeAtAtUriResult.ok) return;
+
const storeAtAtUri = storeAtAtUriResult.data;
+
+
// this is fine because Promise.all() preserves the order of the arrays
+
const {
+
did: authority,
+
collection,
+
rkey: rKey,
+
} = channelBacklinks[idx];
+
+
const existingMapValue = channelsByShard.get(storeAtAtUri);
+
+
const currentChannelUri: Required<AtUri> = {
+
// @ts-expect-error seriously i gotta do something about the template literals not converting properly SIGH
+
authority,
+
collection,
+
rKey,
+
};
+
+
if (!existingMapValue) {
+
channelsByShard.set(storeAtAtUri, [currentChannelUri]);
+
} else {
+
const prevUris = existingMapValue;
+
channelsByShard.set(storeAtAtUri, [...prevUris, currentChannelUri]);
+
}
+
});
const server = await setupServer();
for (const [url, route] of Object.entries(routes)) {