checkCodebase

tcmts.registry.checkCodebase · #5t2crjso · term · runs on the server

uses tcmts.registry.codebases, which uses tcmts.server.table, which uses tcmts.server.Table, which uses #tcm/host (database)

Check a listed codebase against its author's signed repository (a job, queued as Jetstream sends its record, or says it changed or went): kept as the repository signs it, and its main branch mirrored (#vu73aeue); or gone, its names with it, if the repository doesn't hold it (they stopped listing it) or holds something that isn't a listing. Its author is then someone the registry knows, by the handle their DID document names.

import { isDid, NotTheirs, resolveIdentity, signedRecord } from "#codebase/tcmts/atproto";
import { CODEBASE_COLLECTION, parseCodebaseRecord } from "#codebase/tcmts/records";
import { codebases, mirrorCodebase, people, unmountCodebase } from "#codebase/tcmts/registry";
import { enqueue } from "#codebase/tcmts/server";

export async function checkCodebase(uri: string): Promise<void> {
  const m = /^at:\/\/([^/]+)\/codes\.tcm\.codebase\/([^/]+)$/.exec(uri);
  if (!m || !isDid(m[1]!)) return;
  const [, did, rkey] = m as unknown as [string, string, string];
  const had = await codebases.findOne({ uri });
  const drop = async () => {
    if (!had) return;
    if (had.value.mount) unmountCodebase(had.value.mount);
    await codebases.delete(had.id);
  };
  try {
    const got = await signedRecord(did, CODEBASE_COLLECTION, rkey);
    const r = parseCodebaseRecord(rkey, got.record);
    if (!r) return void (await drop());
    const said = {
      uri,
      did,
      name: r.name,
      url: r.url,
      description: r.description,
      createdAt: r.createdAt,
      state: "verified" as const,
    };
    if (had) await codebases.update(had.id, (v) => ({ ...v, ...said }));
    else
      try {
        await codebases.insert(said);
      } catch {
        // (kept meanwhile, by another event of the same record: one row per record)
        const now = await codebases.findOne({ uri });
        if (now) await codebases.update(now.id, (v) => ({ ...v, ...said }));
      }
    enqueue(mirrorCodebase, uri);
    const handle = await resolveIdentity(did).then(
      (i) => i.handle,
      () => undefined,
    );
    const person = await people.findOne({ did });
    if (!person)
      try {
        await people.insert({ did, seq: 0, ...(handle ? { handle } : {}) });
      } catch {
        // (kept meanwhile: one row per DID)
      }
    else if (handle && person.value.handle !== handle)
      await people.put(person.id, { ...person.value, handle });
  } catch (e) {
    if (!(e instanceof NotTheirs)) throw e;
    await drop();
  }
}

uses

used by