Skip to content

Commit 6513749

Browse files
committed
Mattermost: split setup adapter helpers
1 parent c8576ec commit 6513749

File tree

3 files changed

+90
-83
lines changed

3 files changed

+90
-83
lines changed

extensions/mattermost/src/channel.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ import { sendMessageMattermost } from "./mattermost/send.js";
3838
import { resolveMattermostOpaqueTarget } from "./mattermost/target-resolution.js";
3939
import { looksLikeMattermostTargetId, normalizeMattermostMessagingTarget } from "./normalize.js";
4040
import { getMattermostRuntime } from "./runtime.js";
41-
import { mattermostSetupAdapter, mattermostSetupWizard } from "./setup-surface.js";
41+
import { mattermostSetupAdapter } from "./setup-core.js";
42+
import { mattermostSetupWizard } from "./setup-surface.js";
4243

4344
const mattermostMessageActions: ChannelMessageActionAdapter = {
4445
listActions: ({ cfg }) => {
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import {
2+
applyAccountNameToChannelSection,
3+
applySetupAccountConfigPatch,
4+
DEFAULT_ACCOUNT_ID,
5+
hasConfiguredSecretInput,
6+
migrateBaseNameToDefaultAccount,
7+
normalizeAccountId,
8+
type OpenClawConfig,
9+
} from "openclaw/plugin-sdk/mattermost";
10+
import type { ChannelSetupAdapter } from "../../../src/channels/plugins/types.adapters.js";
11+
import { resolveMattermostAccount, type ResolvedMattermostAccount } from "./mattermost/accounts.js";
12+
import { normalizeMattermostBaseUrl } from "./mattermost/client.js";
13+
14+
const channel = "mattermost" as const;
15+
16+
export function isMattermostConfigured(account: ResolvedMattermostAccount): boolean {
17+
const tokenConfigured =
18+
Boolean(account.botToken?.trim()) || hasConfiguredSecretInput(account.config.botToken);
19+
return tokenConfigured && Boolean(account.baseUrl);
20+
}
21+
22+
export function resolveMattermostAccountWithSecrets(cfg: OpenClawConfig, accountId: string) {
23+
return resolveMattermostAccount({
24+
cfg,
25+
accountId,
26+
allowUnresolvedSecretRef: true,
27+
});
28+
}
29+
30+
export const mattermostSetupAdapter: ChannelSetupAdapter = {
31+
resolveAccountId: ({ accountId }) => normalizeAccountId(accountId),
32+
applyAccountName: ({ cfg, accountId, name }) =>
33+
applyAccountNameToChannelSection({
34+
cfg,
35+
channelKey: channel,
36+
accountId,
37+
name,
38+
}),
39+
validateInput: ({ accountId, input }) => {
40+
const token = input.botToken ?? input.token;
41+
const baseUrl = normalizeMattermostBaseUrl(input.httpUrl);
42+
if (input.useEnv && accountId !== DEFAULT_ACCOUNT_ID) {
43+
return "Mattermost env vars can only be used for the default account.";
44+
}
45+
if (!input.useEnv && (!token || !baseUrl)) {
46+
return "Mattermost requires --bot-token and --http-url (or --use-env).";
47+
}
48+
if (input.httpUrl && !baseUrl) {
49+
return "Mattermost --http-url must include a valid base URL.";
50+
}
51+
return null;
52+
},
53+
applyAccountConfig: ({ cfg, accountId, input }) => {
54+
const token = input.botToken ?? input.token;
55+
const baseUrl = normalizeMattermostBaseUrl(input.httpUrl);
56+
const namedConfig = applyAccountNameToChannelSection({
57+
cfg,
58+
channelKey: channel,
59+
accountId,
60+
name: input.name,
61+
});
62+
const next =
63+
accountId !== DEFAULT_ACCOUNT_ID
64+
? migrateBaseNameToDefaultAccount({
65+
cfg: namedConfig,
66+
channelKey: channel,
67+
})
68+
: namedConfig;
69+
return applySetupAccountConfigPatch({
70+
cfg: next,
71+
channelKey: channel,
72+
accountId,
73+
patch: input.useEnv
74+
? {}
75+
: {
76+
...(token ? { botToken: token } : {}),
77+
...(baseUrl ? { baseUrl } : {}),
78+
},
79+
});
80+
},
81+
};

extensions/mattermost/src/setup-surface.ts

Lines changed: 7 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,15 @@
1-
import {
2-
applyAccountNameToChannelSection,
3-
applySetupAccountConfigPatch,
4-
DEFAULT_ACCOUNT_ID,
5-
hasConfiguredSecretInput,
6-
migrateBaseNameToDefaultAccount,
7-
normalizeAccountId,
8-
type OpenClawConfig,
9-
} from "openclaw/plugin-sdk/mattermost";
1+
import { DEFAULT_ACCOUNT_ID, hasConfiguredSecretInput } from "openclaw/plugin-sdk/mattermost";
102
import { type ChannelSetupWizard } from "../../../src/channels/plugins/setup-wizard.js";
11-
import type { ChannelSetupAdapter } from "../../../src/channels/plugins/types.adapters.js";
123
import { formatDocsLink } from "../../../src/terminal/links.js";
4+
import { listMattermostAccountIds } from "./mattermost/accounts.js";
135
import {
14-
listMattermostAccountIds,
15-
resolveMattermostAccount,
16-
type ResolvedMattermostAccount,
17-
} from "./mattermost/accounts.js";
18-
import { normalizeMattermostBaseUrl } from "./mattermost/client.js";
6+
isMattermostConfigured,
7+
mattermostSetupAdapter,
8+
resolveMattermostAccountWithSecrets,
9+
} from "./setup-core.js";
1910

2011
const channel = "mattermost" as const;
21-
22-
function isMattermostConfigured(account: ResolvedMattermostAccount): boolean {
23-
const tokenConfigured =
24-
Boolean(account.botToken?.trim()) || hasConfiguredSecretInput(account.config.botToken);
25-
return tokenConfigured && Boolean(account.baseUrl);
26-
}
27-
28-
function resolveMattermostAccountWithSecrets(cfg: OpenClawConfig, accountId: string) {
29-
return resolveMattermostAccount({
30-
cfg,
31-
accountId,
32-
allowUnresolvedSecretRef: true,
33-
});
34-
}
35-
36-
export const mattermostSetupAdapter: ChannelSetupAdapter = {
37-
resolveAccountId: ({ accountId }) => normalizeAccountId(accountId),
38-
applyAccountName: ({ cfg, accountId, name }) =>
39-
applyAccountNameToChannelSection({
40-
cfg,
41-
channelKey: channel,
42-
accountId,
43-
name,
44-
}),
45-
validateInput: ({ accountId, input }) => {
46-
const token = input.botToken ?? input.token;
47-
const baseUrl = normalizeMattermostBaseUrl(input.httpUrl);
48-
if (input.useEnv && accountId !== DEFAULT_ACCOUNT_ID) {
49-
return "Mattermost env vars can only be used for the default account.";
50-
}
51-
if (!input.useEnv && (!token || !baseUrl)) {
52-
return "Mattermost requires --bot-token and --http-url (or --use-env).";
53-
}
54-
if (input.httpUrl && !baseUrl) {
55-
return "Mattermost --http-url must include a valid base URL.";
56-
}
57-
return null;
58-
},
59-
applyAccountConfig: ({ cfg, accountId, input }) => {
60-
const token = input.botToken ?? input.token;
61-
const baseUrl = normalizeMattermostBaseUrl(input.httpUrl);
62-
const namedConfig = applyAccountNameToChannelSection({
63-
cfg,
64-
channelKey: channel,
65-
accountId,
66-
name: input.name,
67-
});
68-
const next =
69-
accountId !== DEFAULT_ACCOUNT_ID
70-
? migrateBaseNameToDefaultAccount({
71-
cfg: namedConfig,
72-
channelKey: channel,
73-
})
74-
: namedConfig;
75-
return applySetupAccountConfigPatch({
76-
cfg: next,
77-
channelKey: channel,
78-
accountId,
79-
patch: input.useEnv
80-
? {}
81-
: {
82-
...(token ? { botToken: token } : {}),
83-
...(baseUrl ? { baseUrl } : {}),
84-
},
85-
});
86-
},
87-
};
12+
export { mattermostSetupAdapter } from "./setup-core.js";
8813

8914
export const mattermostSetupWizard: ChannelSetupWizard = {
9015
channel,

0 commit comments

Comments
 (0)