readCredits

tcmts.registry.readCredits · #ekblqpau · function · runs on the server

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

Read the credits in a listed codebase's main branch, from its server, up to head: only the entries since before (the head they were last read at), if head's history holds it; else all of them again, in place of those kept (a branch pushed over, or another server). Only merge entries credit anyone, and their contributors become people the registry knows.

import { contributedBy } from "#codebase/tcmts/cli/contributions";
import type { Remote } from "#codebase/tcmts/cli/sync";
import { credits, knowPerson, REGISTRY_CREDITS } from "#codebase/tcmts/registry";

export async function readCredits(
  uri: string,
  remote: Remote,
  head: string,
  before: string | undefined,
): Promise<void> {
  const all = await remote.ancestors(head);
  const known =
    before && all.includes(before) ? new Set(await remote.ancestors(before)) : undefined;
  if (!known) for (const x of await credits.find({ codebase: uri })) await credits.delete(x.id);
  const fresh = all.filter((h) => !known?.has(h)).slice(0, REGISTRY_CREDITS.entries);
  for (let i = 0; i < fresh.length; i += REGISTRY_CREDITS.batch) {
    for (const causal of await remote.getCausals(fresh.slice(i, i + REGISTRY_CREDITS.batch))) {
      const who = causal.parents.length > 1 ? contributedBy(causal.command) : undefined;
      if (!who) continue;
      const key = `${uri} ${causal.hash}`;
      if (await credits.findOne({ key })) continue;
      // (the entry's text is its command, then its message: its title is the message's first line)
      const [command, title] = causal.command.split("\n");
      try {
        await credits.insert({
          key,
          codebase: uri,
          did: who.did,
          entry: causal.hash,
          title: (title || command || "").slice(0, 200),
          at: new Date(causal.ts),
        });
      } catch {
        // (kept meanwhile, by another read of the same head)
      }
      await knowPerson(who.did);
    }
  }
}

uses

used by