decentralised sync engine
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: attach listener helper

serenity 63879865 0a968f67

+38 -1
+38 -1
src/lib/utils/registration.ts
··· 1 - import { getRegistrationState } from "@/lib/state"; 1 + import { OWNER_DID, SERVICE_DID } from "@/lib/env"; 2 + import { getRegistrationState, setRegistrationState } from "@/lib/state"; 3 + import type { AtUri } from "@/lib/types/atproto"; 4 + import { 5 + systemsGmstnDevelopmentChannelRecordSchema, 6 + systemsGmstnDevelopmentChannelRecordSchema, 7 + } from "@/lib/types/lexicon/systems.gmstn.development.channel"; 8 + import { prismCommitSchema } from "@/lib/types/prism"; 2 9 import type { RouteHandler, WsRouteHandler } from "@/lib/types/routes"; 10 + import { getRecordFromAtUri } from "@/lib/utils/atproto"; 3 11 import { newErrorResponse } from "@/lib/utils/http/responses"; 12 + import { rawDataToString } from "@/lib/utils/ws"; 13 + import type { RawData } from "ws"; 14 + import type WebSocket from "ws"; 4 15 5 16 export const wrapHttpRegistrationCheck = ( 6 17 routeHandler: RouteHandler, ··· 38 49 39 50 return wrappedFunction; 40 51 } 52 + 53 + export const attachLatticeRegistrationListener = (socket: WebSocket) => { 54 + socket.on("message", (rawData: RawData) => { 55 + const data = rawDataToString(rawData); 56 + const jsonData: unknown = JSON.parse(data); 57 + 58 + const { success: prismCommitParseSuccess, data: prismCommit } = 59 + prismCommitSchema.safeParse(jsonData); 60 + if (!prismCommitParseSuccess) return; 61 + 62 + const { did, commit } = prismCommit; 63 + if (did !== OWNER_DID) return; 64 + 65 + const { rkey } = commit; 66 + 67 + // TODO: replace empty string with call to resolve did doc and the endpoint and yadda yadda etc. etc. you get it. 68 + // if you don't, then the tl;dr is you need to resolve the did:plc document to get the service endpoint describing this lattice and ensure 69 + // that the domain/origin/whatever matches with the rkey (or record value if we decide to transition to that) 70 + const latticeDomain = SERVICE_DID.startsWith("did:web:") 71 + ? SERVICE_DID.slice(8) 72 + : ""; 73 + if (rkey !== latticeDomain) return; 74 + 75 + setRegistrationState(true); 76 + }); 77 + };