mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-18 21:40:53 +00:00
82 lines
2.6 KiB
TypeScript
82 lines
2.6 KiB
TypeScript
import {
|
|
applyAccountNameToChannelSection,
|
|
applySetupAccountConfigPatch,
|
|
DEFAULT_ACCOUNT_ID,
|
|
hasConfiguredSecretInput,
|
|
migrateBaseNameToDefaultAccount,
|
|
normalizeAccountId,
|
|
type OpenClawConfig,
|
|
} from "openclaw/plugin-sdk/mattermost";
|
|
import type { ChannelSetupAdapter } from "../../../src/channels/plugins/types.adapters.js";
|
|
import { resolveMattermostAccount, type ResolvedMattermostAccount } from "./mattermost/accounts.js";
|
|
import { normalizeMattermostBaseUrl } from "./mattermost/client.js";
|
|
|
|
const channel = "mattermost" as const;
|
|
|
|
export function isMattermostConfigured(account: ResolvedMattermostAccount): boolean {
|
|
const tokenConfigured =
|
|
Boolean(account.botToken?.trim()) || hasConfiguredSecretInput(account.config.botToken);
|
|
return tokenConfigured && Boolean(account.baseUrl);
|
|
}
|
|
|
|
export function resolveMattermostAccountWithSecrets(cfg: OpenClawConfig, accountId: string) {
|
|
return resolveMattermostAccount({
|
|
cfg,
|
|
accountId,
|
|
allowUnresolvedSecretRef: true,
|
|
});
|
|
}
|
|
|
|
export const mattermostSetupAdapter: ChannelSetupAdapter = {
|
|
resolveAccountId: ({ accountId }) => normalizeAccountId(accountId),
|
|
applyAccountName: ({ cfg, accountId, name }) =>
|
|
applyAccountNameToChannelSection({
|
|
cfg,
|
|
channelKey: channel,
|
|
accountId,
|
|
name,
|
|
}),
|
|
validateInput: ({ accountId, input }) => {
|
|
const token = input.botToken ?? input.token;
|
|
const baseUrl = normalizeMattermostBaseUrl(input.httpUrl);
|
|
if (input.useEnv && accountId !== DEFAULT_ACCOUNT_ID) {
|
|
return "Mattermost env vars can only be used for the default account.";
|
|
}
|
|
if (!input.useEnv && (!token || !baseUrl)) {
|
|
return "Mattermost requires --bot-token and --http-url (or --use-env).";
|
|
}
|
|
if (input.httpUrl && !baseUrl) {
|
|
return "Mattermost --http-url must include a valid base URL.";
|
|
}
|
|
return null;
|
|
},
|
|
applyAccountConfig: ({ cfg, accountId, input }) => {
|
|
const token = input.botToken ?? input.token;
|
|
const baseUrl = normalizeMattermostBaseUrl(input.httpUrl);
|
|
const namedConfig = applyAccountNameToChannelSection({
|
|
cfg,
|
|
channelKey: channel,
|
|
accountId,
|
|
name: input.name,
|
|
});
|
|
const next =
|
|
accountId !== DEFAULT_ACCOUNT_ID
|
|
? migrateBaseNameToDefaultAccount({
|
|
cfg: namedConfig,
|
|
channelKey: channel,
|
|
})
|
|
: namedConfig;
|
|
return applySetupAccountConfigPatch({
|
|
cfg: next,
|
|
channelKey: channel,
|
|
accountId,
|
|
patch: input.useEnv
|
|
? {}
|
|
: {
|
|
...(token ? { botToken: token } : {}),
|
|
...(baseUrl ? { baseUrl } : {}),
|
|
},
|
|
});
|
|
},
|
|
};
|