import { Client, simpleFetchHandler } from "@atcute/client"; import type { ShTangledRepo } from "@atcute/tangled"; import { query } from "@solidjs/router"; import { getUserPDS } from "./get_pds"; import type { DID } from "./types"; export const getUserRepos = query( async ( did: DID, ): Promise> => { const pds = await getUserPDS(did); if (!pds) return {}; const handler = simpleFetchHandler({ service: pds }); const rpc = new Client({ handler }); const repos: Record = {}; let cursor: string | undefined; while (true) { const { ok, data } = await rpc.get("com.atproto.repo.listRecords", { params: { repo: did, collection: "sh.tangled.repo", limit: 100, cursor, }, }); if (!ok) break; const length = data.records.length; const moreRepos = data.records.reduce( (acc, r) => { const record = Object.assign( { rkey: r.uri.slice(r.uri.lastIndexOf("/") + 1) }, r.value as ShTangledRepo.Main, ); acc[record.name] = record; return acc; }, new Object() as typeof repos, ); Object.assign(repos, moreRepos); cursor = data.cursor; if (length < 100) break; } return repos; }, "user_repos", ); export const getUserRepoByRkey = query( async (did: DID, rkey: string): Promise => { const pds = await getUserPDS(did); if (!pds) return; const handler = simpleFetchHandler({ service: pds }); const rpc = new Client({ handler }); const { ok, data } = await rpc.get("com.atproto.repo.getRecord", { params: { repo: did, collection: "sh.tangled.repo", rkey, }, }); if (!ok) return; return data.value as ShTangledRepo.Main; }, "user_repos", ); export async function getUserRepo( did: DID, repo: string, ): Promise { const repos = await getUserRepos(did); return repos[repo]; } export const getRepoKnot = async (did: DID, repo: string) => { const repos = await getUserRepos(did); return `https://${repos[repo].knot}`; };