refactor: centralize whatsapp auth detection
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
@@ -10,12 +9,10 @@ import {
|
||||
readConfigFileSnapshot,
|
||||
writeConfigFile,
|
||||
} from "../config/config.js";
|
||||
import { resolveOAuthDir } from "../config/paths.js";
|
||||
import { DEFAULT_ACCOUNT_ID } from "../routing/session-key.js";
|
||||
import type { RuntimeEnv } from "../runtime.js";
|
||||
import { note } from "../terminal/note.js";
|
||||
import { resolveUserPath } from "../utils.js";
|
||||
import { resolveWebCredsPath } from "../web/auth-store.js";
|
||||
import { hasAnyWhatsAppAuth } from "../web/accounts.js";
|
||||
|
||||
function resolveLegacyConfigPath(env: NodeJS.ProcessEnv): string {
|
||||
const override = env.CLAWDIS_CONFIG_PATH?.trim();
|
||||
@@ -57,48 +54,6 @@ export function replaceModernName(value: string | undefined): string | undefined
|
||||
return value.replace(/clawdbot/g, "clawdis");
|
||||
}
|
||||
|
||||
function hasWebCreds(authDir: string): boolean {
|
||||
try {
|
||||
const credsPath = resolveWebCredsPath(authDir);
|
||||
const stats = fs.statSync(credsPath);
|
||||
return stats.isFile() && stats.size > 1;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function listWhatsAppAuthDirs(cfg: ClawdbotConfig): string[] {
|
||||
const oauthDir = resolveOAuthDir();
|
||||
const whatsappDir = path.join(oauthDir, "whatsapp");
|
||||
const authDirs = new Set<string>([oauthDir, path.join(whatsappDir, DEFAULT_ACCOUNT_ID)]);
|
||||
|
||||
const accounts = cfg.channels?.whatsapp?.accounts;
|
||||
if (accounts && typeof accounts === "object") {
|
||||
for (const [accountId, accountCfg] of Object.entries(accounts)) {
|
||||
const configured = accountCfg?.authDir?.trim();
|
||||
const authDir = configured ? resolveUserPath(configured) : path.join(whatsappDir, accountId);
|
||||
authDirs.add(authDir);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const entries = fs.readdirSync(whatsappDir, { withFileTypes: true });
|
||||
for (const entry of entries) {
|
||||
if (!entry.isDirectory()) continue;
|
||||
authDirs.add(path.join(whatsappDir, entry.name));
|
||||
}
|
||||
} catch {
|
||||
// ignore missing dirs
|
||||
}
|
||||
|
||||
return Array.from(authDirs);
|
||||
}
|
||||
|
||||
function hasWhatsAppAuthState(cfg: ClawdbotConfig): boolean {
|
||||
const authDirs = listWhatsAppAuthDirs(cfg);
|
||||
return authDirs.some((authDir) => hasWebCreds(authDir));
|
||||
}
|
||||
|
||||
export function normalizeLegacyConfigValues(cfg: ClawdbotConfig): {
|
||||
config: ClawdbotConfig;
|
||||
changes: string[];
|
||||
@@ -266,7 +221,7 @@ export function normalizeLegacyConfigValues(cfg: ClawdbotConfig): {
|
||||
|
||||
const legacyAckReaction = cfg.messages?.ackReaction?.trim();
|
||||
const hasWhatsAppConfig = cfg.channels?.whatsapp !== undefined;
|
||||
const hasWhatsAppAuth = hasWhatsAppAuthState(cfg);
|
||||
const hasWhatsAppAuth = hasAnyWhatsAppAuth(cfg);
|
||||
if (legacyAckReaction && (hasWhatsAppConfig || hasWhatsAppAuth)) {
|
||||
const hasWhatsAppAck = cfg.channels?.whatsapp?.ackReaction !== undefined;
|
||||
if (!hasWhatsAppAck) {
|
||||
|
||||
Reference in New Issue
Block a user