checkStar

tcmts.hub.checkStar · #oycwjfvt · term · runs on the server

uses tcmts.hub.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.

import { isDid, NotTheirs, signedRecord } from "#codebase/tcmts/atproto";
import { keepStar, stars } from "#codebase/tcmts/hub";
import type { HubStar } from "#codebase/tcmts/hub";
import { STAR_COLLECTION, starOf } from "#codebase/tcmts/records";

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: HubStar = { uri, did, ...s, state: "verified" };
    await keepStar(row);
    return true;
  } catch (e) {
    if (!(e instanceof NotTheirs)) throw e;
    if (had) await stars.delete(had.id);
    return false;
  }
}

uses

used by