checkStar

tcmts.registry.checkStar · #bkeyofpk · term · runs on the server

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

Check a star against its writer's signed repository (a job, or when they've just starred): verified, or gone if their repository doesn't hold it (they took it back). Returns whether it's theirs now. Only a star at its own place counts: at://<DID>/codes.tcm.star/<its library's key>, so one person's star of a library is one row. Its writer is then someone the registry knows (their page lists their stars), by the handle their DID document names.

import { isDid, NotTheirs, resolveIdentity, signedRecord } from "#codebase/tcmts/atproto";
import { STAR_COLLECTION, starOf } from "#codebase/tcmts/records";
import { keepStar, people, stars } from "#codebase/tcmts/registry";
import type { RegistryStar } from "#codebase/tcmts/registry";

export async function checkStar(uri: string): Promise<boolean> {
  const m = /^at:\/\/([^/]+)\/codes\.tcm\.star\/([^/]+)$/.exec(uri);
  if (!m || !isDid(m[1]!)) return false;
  const [, did, rkey] = m as unknown as [string, string, string];
  const had = await stars.findOne({ uri });
  try {
    const got = await signedRecord(did, STAR_COLLECTION, rkey);
    const s = starOf(rkey, got.record);
    if (!s) {
      if (had) await stars.delete(had.id);
      return false;
    }
    const row: RegistryStar = { uri, did, ...s, state: "verified" };
    await keepStar(row);
    // (who starred, for their page: their handle as their DID document names it now; their profile is read daily)
    const handle = await resolveIdentity(did).then(
      (i) => i.handle,
      () => undefined,
    );
    const person = await people.findOne({ did });
    if (!person)
      try {
        // (no Jetstream position: they were met through a star checked when they made it)
        await people.insert({ did, seq: 0, ...(handle ? { handle } : {}) });
      } catch {
        // (kept meanwhile, by the same star's event: one row per DID)
      }
    else if (handle && person.value.handle !== handle)
      await people.put(person.id, { ...person.value, handle });
    return true;
  } catch (e) {
    if (!(e instanceof NotTheirs)) throw e;
    if (had) await stars.delete(had.id);
    return false;
  }
}

uses

used by