attestationsBy

tcmts.cli.attest.attestationsBy · #4yloyz3q · term · runs anywhere

The attestations an account has written about any of namespaces, as its repository signs them (signedRecord): one its PDS lists that doesn't check out is left out.

import { getJson, NotTheirs, signedRecord } from "#codebase/tcmts/atproto";
import type { Identity } from "#codebase/tcmts/atproto";
import type { FoundAttestation } from "#codebase/tcmts/cli/attest";
import { ATTESTATION_KINDS, parseAttestation } from "#codebase/tcmts/records";

export async function attestationsBy(
  id: Identity,
  namespaces: Set<string>,
): Promise<FoundAttestation[]> {
  const out: FoundAttestation[] = [];
  for (const collection of Object.values(ATTESTATION_KINDS)) {
    let cursor: string | undefined;
    // (so many pages of 100, however many a PDS says there are)
    let pages = 0;
    do {
      const q = new URLSearchParams({
        repo: id.did,
        collection,
        limit: "100",
        ...(cursor ? { cursor } : {}),
      });
      const page = await getJson<{ records: { uri: string; value: unknown }[]; cursor?: string }>(
        `${id.pds}/xrpc/com.atproto.repo.listRecords?${q}`,
      );
      for (const r of page.records) {
        const listed = parseAttestation(collection, r.value);
        if (!listed || !namespaces.has(listed.subject.namespace)) continue;
        const rkey = r.uri.slice(r.uri.lastIndexOf("/") + 1);
        const a = await signedRecord(id.did, collection, rkey).then(
          (s) => parseAttestation(collection, s.record),
          (e: unknown) => {
            if (e instanceof NotTheirs) return undefined;
            throw e;
          },
        );
        if (a && namespaces.has(a.subject.namespace)) out.push({ ...a, uri: r.uri, did: id.did });
      }
      cursor = page.records.length && ++pages < 50 ? page.cursor : undefined;
    } while (cursor);
  }
  return out;
}

uses

used by