File size: 1,399 Bytes
87dab07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Shared first-owner bootstrap for DM pairing approval surfaces.
import {
  formatCommandOwnerFromChannelSender,
  hasConfiguredCommandOwners,
} from "../commands/doctor-command-owner.js";
import { readConfigFileSnapshotForWrite, replaceConfigFile } from "../config/config.js";
import type { PairingChannel } from "./pairing-store.types.js";

type PairingCommandOwnerBootstrapResult = {
  ownerEntry: string | null;
  status: "configured" | "already-configured" | "unavailable";
};

/** Adds the approved sender as command owner only when no owner exists yet. */
export async function bootstrapCommandOwnerFromPairing(params: {
  channel: PairingChannel;
  id: string;
}): Promise<PairingCommandOwnerBootstrapResult> {
  const ownerEntry = formatCommandOwnerFromChannelSender(params);
  if (!ownerEntry) {
    return { ownerEntry: null, status: "unavailable" };
  }

  const { snapshot, writeOptions } = await readConfigFileSnapshotForWrite();
  if (hasConfiguredCommandOwners(snapshot.sourceConfig)) {
    return { ownerEntry, status: "already-configured" };
  }

  const nextConfig = structuredClone(snapshot.sourceConfig);
  nextConfig.commands = {
    ...nextConfig.commands,
    ownerAllowFrom: [ownerEntry],
  };
  await replaceConfigFile({
    sourceConfig: nextConfig,
    snapshot,
    writeOptions,
    afterWrite: { mode: "auto" },
  });
  return { ownerEntry, status: "configured" };
}