···
import { OWNER_DID, SERVER_PORT, SERVICE_DID } from "@/lib/env";
import { setRegistrationState } from "@/lib/state";
3
-
import { getRecordFromAtUri } from "@/lib/utils/atproto";
3
+
import type { AtUri, Did } from "@/lib/types/atproto";
4
+
import { systemsGmstnDevelopmentChannelRecordSchema } from "@/lib/types/lexicon/systems.gmstn.development.channel";
5
+
import { getRecordFromAtUri, stringToAtUri } from "@/lib/utils/atproto";
6
+
import { getConstellationBacklink } from "@/lib/utils/constellation";
import { newErrorResponse } from "@/lib/utils/http/responses";
import { connectToPrism } from "@/lib/utils/prism";
···
wantedCollections: ["systems.gmstn.development.*"],
44
+
// TODO: probably move this to an `attachListeners` hook that attaches the listeners we want.
attachLatticeRegistrationListener(prismWebsocket);
47
+
const constellationBacklinksResult = await getConstellationBacklink({
48
+
subject: SERVICE_DID,
50
+
nsid: "systems.gmstn.development.channel",
51
+
fieldName: "routeThrough.uri",
55
+
if (!constellationBacklinksResult.ok) {
57
+
"Something went wrong fetching constellation backlinks to do Shard handshakes",
61
+
const { records: channelBacklinks } = constellationBacklinksResult.data;
63
+
// TODO: For private lattices, do permission check on owner's PDS
64
+
// and filter out records from unauthorised pdses.
66
+
const channelRecordsPromises = channelBacklinks.map(
67
+
async ({ did, collection, rkey }) =>
68
+
await getRecordFromAtUri({
69
+
// @ts-expect-error seriously i gotta do something about the template literals not converting properly SIGH
76
+
const channelRecordResults = await Promise.all(channelRecordsPromises);
78
+
// mapping of shard -> list of channels (all AtUris)
79
+
const channelsByShard = new Map<AtUri, Array<AtUri>>();
81
+
channelRecordResults.forEach((result, idx) => {
82
+
if (!result.ok) return;
83
+
const { success, data: channelRecord } =
84
+
systemsGmstnDevelopmentChannelRecordSchema.safeParse(result.data);
85
+
if (!success) return;
86
+
const { storeAt } = channelRecord;
88
+
const storeAtAtUriResult = stringToAtUri(storeAt.uri);
89
+
if (!storeAtAtUriResult.ok) return;
90
+
const storeAtAtUri = storeAtAtUriResult.data;
92
+
// this is fine because Promise.all() preserves the order of the arrays
97
+
} = channelBacklinks[idx];
99
+
const existingMapValue = channelsByShard.get(storeAtAtUri);
101
+
const currentChannelUri: Required<AtUri> = {
102
+
// @ts-expect-error seriously i gotta do something about the template literals not converting properly SIGH
108
+
if (!existingMapValue) {
109
+
channelsByShard.set(storeAtAtUri, [currentChannelUri]);
111
+
const prevUris = existingMapValue;
112
+
channelsByShard.set(storeAtAtUri, [...prevUris, currentChannelUri]);
const server = await setupServer();
for (const [url, route] of Object.entries(routes)) {