mirror of
https://github.com/moltbot/moltbot.git
synced 2026-04-26 16:06:16 +00:00
WhatsApp: lazy-load channel auth helpers
This commit is contained in:
@@ -1 +1,12 @@
|
||||
export { getActiveWebListener } from "./active-listener.js";
|
||||
export {
|
||||
getWebAuthAgeMs,
|
||||
logWebSelfId,
|
||||
logoutWeb,
|
||||
readWebSelfId,
|
||||
webAuthExists,
|
||||
} from "./auth-store.js";
|
||||
export { loginWeb } from "./login.js";
|
||||
export { startWebLoginWithQr, waitForWebLogin } from "./login-qr.js";
|
||||
export { whatsappSetupWizard } from "./setup-surface.js";
|
||||
export { monitorWebChannel } from "../../../src/channels/web/index.js";
|
||||
|
||||
@@ -223,7 +223,7 @@ export const whatsappPlugin: ChannelPlugin<ResolvedWhatsAppAccount> = {
|
||||
directory: {
|
||||
self: async ({ cfg, accountId }) => {
|
||||
const account = resolveWhatsAppAccount({ cfg, accountId });
|
||||
const { e164, jid } = getWhatsAppRuntime().channel.whatsapp.readWebSelfId(account.authDir);
|
||||
const { e164, jid } = (await loadWhatsAppChannelRuntime()).readWebSelfId(account.authDir);
|
||||
const id = e164 ?? jid;
|
||||
if (!id) {
|
||||
return null;
|
||||
@@ -298,12 +298,9 @@ export const whatsappPlugin: ChannelPlugin<ResolvedWhatsAppAccount> = {
|
||||
auth: {
|
||||
login: async ({ cfg, accountId, runtime, verbose }) => {
|
||||
const resolvedAccountId = accountId?.trim() || resolveDefaultWhatsAppAccountId(cfg);
|
||||
await getWhatsAppRuntime().channel.whatsapp.loginWeb(
|
||||
Boolean(verbose),
|
||||
undefined,
|
||||
runtime,
|
||||
resolvedAccountId,
|
||||
);
|
||||
await (
|
||||
await loadWhatsAppChannelRuntime()
|
||||
).loginWeb(Boolean(verbose), undefined, runtime, resolvedAccountId);
|
||||
},
|
||||
},
|
||||
heartbeat: {
|
||||
@@ -313,14 +310,14 @@ export const whatsappPlugin: ChannelPlugin<ResolvedWhatsAppAccount> = {
|
||||
}
|
||||
const account = resolveWhatsAppAccount({ cfg, accountId });
|
||||
const authExists = await (
|
||||
deps?.webAuthExists ?? getWhatsAppRuntime().channel.whatsapp.webAuthExists
|
||||
deps?.webAuthExists ?? (await loadWhatsAppChannelRuntime()).webAuthExists
|
||||
)(account.authDir);
|
||||
if (!authExists) {
|
||||
return { ok: false, reason: "whatsapp-not-linked" };
|
||||
}
|
||||
const listenerActive = deps?.hasActiveWebListener
|
||||
? deps.hasActiveWebListener()
|
||||
: Boolean(getWhatsAppRuntime().channel.whatsapp.getActiveWebListener());
|
||||
: Boolean((await loadWhatsAppChannelRuntime()).getActiveWebListener());
|
||||
if (!listenerActive) {
|
||||
return { ok: false, reason: "whatsapp-not-running" };
|
||||
}
|
||||
@@ -347,13 +344,13 @@ export const whatsappPlugin: ChannelPlugin<ResolvedWhatsAppAccount> = {
|
||||
typeof snapshot.linked === "boolean"
|
||||
? snapshot.linked
|
||||
: authDir
|
||||
? await getWhatsAppRuntime().channel.whatsapp.webAuthExists(authDir)
|
||||
? await (await loadWhatsAppChannelRuntime()).webAuthExists(authDir)
|
||||
: false;
|
||||
const authAgeMs =
|
||||
linked && authDir ? getWhatsAppRuntime().channel.whatsapp.getWebAuthAgeMs(authDir) : null;
|
||||
linked && authDir ? (await loadWhatsAppChannelRuntime()).getWebAuthAgeMs(authDir) : null;
|
||||
const self =
|
||||
linked && authDir
|
||||
? getWhatsAppRuntime().channel.whatsapp.readWebSelfId(authDir)
|
||||
? (await loadWhatsAppChannelRuntime()).readWebSelfId(authDir)
|
||||
: { e164: null, jid: null };
|
||||
return {
|
||||
configured: linked,
|
||||
@@ -371,7 +368,7 @@ export const whatsappPlugin: ChannelPlugin<ResolvedWhatsAppAccount> = {
|
||||
};
|
||||
},
|
||||
buildAccountSnapshot: async ({ account, runtime }) => {
|
||||
const linked = await getWhatsAppRuntime().channel.whatsapp.webAuthExists(account.authDir);
|
||||
const linked = await (await loadWhatsAppChannelRuntime()).webAuthExists(account.authDir);
|
||||
return {
|
||||
accountId: account.accountId,
|
||||
name: account.name,
|
||||
@@ -392,20 +389,18 @@ export const whatsappPlugin: ChannelPlugin<ResolvedWhatsAppAccount> = {
|
||||
},
|
||||
resolveAccountState: ({ configured }) => (configured ? "linked" : "not linked"),
|
||||
logSelfId: ({ account, runtime, includeChannelPrefix }) => {
|
||||
getWhatsAppRuntime().channel.whatsapp.logWebSelfId(
|
||||
account.authDir,
|
||||
runtime,
|
||||
includeChannelPrefix,
|
||||
void loadWhatsAppChannelRuntime().then((runtimeExports) =>
|
||||
runtimeExports.logWebSelfId(account.authDir, runtime, includeChannelPrefix),
|
||||
);
|
||||
},
|
||||
},
|
||||
gateway: {
|
||||
startAccount: async (ctx) => {
|
||||
const account = ctx.account;
|
||||
const { e164, jid } = getWhatsAppRuntime().channel.whatsapp.readWebSelfId(account.authDir);
|
||||
const { e164, jid } = (await loadWhatsAppChannelRuntime()).readWebSelfId(account.authDir);
|
||||
const identity = e164 ? e164 : jid ? `jid ${jid}` : "unknown";
|
||||
ctx.log?.info(`[${account.accountId}] starting provider (${identity})`);
|
||||
return getWhatsAppRuntime().channel.whatsapp.monitorWebChannel(
|
||||
return (await loadWhatsAppChannelRuntime()).monitorWebChannel(
|
||||
getWhatsAppRuntime().logging.shouldLogVerbose(),
|
||||
undefined,
|
||||
true,
|
||||
@@ -419,16 +414,20 @@ export const whatsappPlugin: ChannelPlugin<ResolvedWhatsAppAccount> = {
|
||||
);
|
||||
},
|
||||
loginWithQrStart: async ({ accountId, force, timeoutMs, verbose }) =>
|
||||
await getWhatsAppRuntime().channel.whatsapp.startWebLoginWithQr({
|
||||
await (
|
||||
await loadWhatsAppChannelRuntime()
|
||||
).startWebLoginWithQr({
|
||||
accountId,
|
||||
force,
|
||||
timeoutMs,
|
||||
verbose,
|
||||
}),
|
||||
loginWithQrWait: async ({ accountId, timeoutMs }) =>
|
||||
await getWhatsAppRuntime().channel.whatsapp.waitForWebLogin({ accountId, timeoutMs }),
|
||||
await (await loadWhatsAppChannelRuntime()).waitForWebLogin({ accountId, timeoutMs }),
|
||||
logoutAccount: async ({ account, runtime }) => {
|
||||
const cleared = await getWhatsAppRuntime().channel.whatsapp.logoutWeb({
|
||||
const cleared = await (
|
||||
await loadWhatsAppChannelRuntime()
|
||||
).logoutWeb({
|
||||
authDir: account.authDir,
|
||||
isLegacyAuthDir: account.isLegacyAuthDir,
|
||||
runtime,
|
||||
|
||||
Reference in New Issue
Block a user