|
| 1 | +import { |
| 2 | + DEFAULT_ACCOUNT_ID, |
| 3 | + hasConfiguredAccountValue, |
| 4 | + mergeAccountConfig, |
| 5 | +} from "openclaw/plugin-sdk/account-core"; |
| 6 | +import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts"; |
| 7 | +import { hasSlackAccountCredentials } from "./src/account-configured.js"; |
| 8 | + |
| 9 | +type SlackAccount = NonNullable<NonNullable<OpenClawConfig["channels"]>["slack"]>; |
| 10 | + |
| 11 | +function hasConfiguredSlackAccount(account: SlackAccount | undefined, env: NodeJS.ProcessEnv) { |
| 12 | + const userIdentity = account?.postAs === "user"; |
| 13 | + return hasSlackAccountCredentials({ |
| 14 | + config: account ?? {}, |
| 15 | + identityTokenConfigured: |
| 16 | + hasConfiguredAccountValue(userIdentity ? account?.userToken : account?.botToken) || |
| 17 | + hasConfiguredAccountValue(userIdentity ? env.SLACK_USER_TOKEN : env.SLACK_BOT_TOKEN), |
| 18 | + appTokenConfigured: |
| 19 | + hasConfiguredAccountValue(account?.appToken) || |
| 20 | + hasConfiguredAccountValue(env.SLACK_APP_TOKEN), |
| 21 | + }); |
| 22 | +} |
| 23 | + |
| 24 | +/** Resolve Slack activation through its account owner's real transport credential contract. */ |
| 25 | +export function hasConfiguredSlackChannelState(params: { |
| 26 | + cfg: OpenClawConfig; |
| 27 | + env?: NodeJS.ProcessEnv; |
| 28 | +}): boolean { |
| 29 | + const channel = params.cfg.channels?.slack; |
| 30 | + if (channel?.enabled === false) { |
| 31 | + return false; |
| 32 | + } |
| 33 | + const defaultAccount = channel?.accounts?.[DEFAULT_ACCOUNT_ID]; |
| 34 | + if (defaultAccount?.enabled !== false) { |
| 35 | + const account = defaultAccount |
| 36 | + ? mergeAccountConfig({ |
| 37 | + channelConfig: channel, |
| 38 | + accountConfig: defaultAccount, |
| 39 | + nestedObjectKeys: ["relay"], |
| 40 | + }) |
| 41 | + : channel; |
| 42 | + if (hasConfiguredSlackAccount(account, params.env ?? process.env)) { |
| 43 | + return true; |
| 44 | + } |
| 45 | + } |
| 46 | + return Object.entries(channel?.accounts ?? {}).some(([accountId, account]) => { |
| 47 | + if (accountId === DEFAULT_ACCOUNT_ID || account.enabled === false) { |
| 48 | + return false; |
| 49 | + } |
| 50 | + // Ambient credentials belong only to the default account, never a named tenant. |
| 51 | + return hasConfiguredSlackAccount( |
| 52 | + mergeAccountConfig({ |
| 53 | + channelConfig: channel, |
| 54 | + accountConfig: account, |
| 55 | + nestedObjectKeys: ["relay"], |
| 56 | + }), |
| 57 | + {}, |
| 58 | + ); |
| 59 | + }); |
| 60 | +} |
0 commit comments