Skip to content

Commit 387d9fa

Browse files
committed
refactor(setup): reuse patched adapters in discord and signal
1 parent 4fd75e5 commit 387d9fa

File tree

2 files changed

+10
-119
lines changed

2 files changed

+10
-119
lines changed

extensions/discord/src/setup-core.ts

Lines changed: 5 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1+
import { createPatchedAccountSetupAdapter } from "../../../src/channels/plugins/setup-helpers.js";
12
import type { DiscordGuildEntry } from "../../../src/config/types.discord.js";
23
import {
3-
applyAccountNameToChannelSection,
44
DEFAULT_ACCOUNT_ID,
5-
migrateBaseNameToDefaultAccount,
6-
normalizeAccountId,
75
noteChannelLookupFailure,
86
noteChannelLookupSummary,
97
parseMentionOrPrefixedId,
@@ -72,15 +70,8 @@ export function parseDiscordAllowFromId(value: string): string | null {
7270
});
7371
}
7472

75-
export const discordSetupAdapter: ChannelSetupAdapter = {
76-
resolveAccountId: ({ accountId }) => normalizeAccountId(accountId),
77-
applyAccountName: ({ cfg, accountId, name }) =>
78-
applyAccountNameToChannelSection({
79-
cfg,
80-
channelKey: channel,
81-
accountId,
82-
name,
83-
}),
73+
export const discordSetupAdapter: ChannelSetupAdapter = createPatchedAccountSetupAdapter({
74+
channelKey: channel,
8475
validateInput: ({ accountId, input }) => {
8576
if (input.useEnv && accountId !== DEFAULT_ACCOUNT_ID) {
8677
return "DISCORD_BOT_TOKEN can only be used for the default account.";
@@ -90,53 +81,8 @@ export const discordSetupAdapter: ChannelSetupAdapter = {
9081
}
9182
return null;
9283
},
93-
applyAccountConfig: ({ cfg, accountId, input }) => {
94-
const namedConfig = applyAccountNameToChannelSection({
95-
cfg,
96-
channelKey: channel,
97-
accountId,
98-
name: input.name,
99-
});
100-
const next =
101-
accountId !== DEFAULT_ACCOUNT_ID
102-
? migrateBaseNameToDefaultAccount({
103-
cfg: namedConfig,
104-
channelKey: channel,
105-
})
106-
: namedConfig;
107-
if (accountId === DEFAULT_ACCOUNT_ID) {
108-
return {
109-
...next,
110-
channels: {
111-
...next.channels,
112-
discord: {
113-
...next.channels?.discord,
114-
enabled: true,
115-
...(input.useEnv ? {} : input.token ? { token: input.token } : {}),
116-
},
117-
},
118-
};
119-
}
120-
return {
121-
...next,
122-
channels: {
123-
...next.channels,
124-
discord: {
125-
...next.channels?.discord,
126-
enabled: true,
127-
accounts: {
128-
...next.channels?.discord?.accounts,
129-
[accountId]: {
130-
...next.channels?.discord?.accounts?.[accountId],
131-
enabled: true,
132-
...(input.token ? { token: input.token } : {}),
133-
},
134-
},
135-
},
136-
},
137-
};
138-
},
139-
};
84+
buildPatch: (input) => (input.useEnv ? {} : input.token ? { token: input.token } : {}),
85+
});
14086

14187
type DiscordAllowFromResolverParams = {
14288
cfg: OpenClawConfig;

extensions/signal/src/setup-core.ts

Lines changed: 5 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1+
import { createPatchedAccountSetupAdapter } from "../../../src/channels/plugins/setup-helpers.js";
12
import {
2-
applyAccountNameToChannelSection,
3-
DEFAULT_ACCOUNT_ID,
4-
migrateBaseNameToDefaultAccount,
5-
normalizeAccountId,
63
normalizeE164,
74
parseSetupEntriesAllowingWildcard,
85
promptParsedAllowFromForScopedChannel,
@@ -114,15 +111,8 @@ export async function promptSignalAllowFrom(params: {
114111
});
115112
}
116113

117-
export const signalSetupAdapter: ChannelSetupAdapter = {
118-
resolveAccountId: ({ accountId }) => normalizeAccountId(accountId),
119-
applyAccountName: ({ cfg, accountId, name }) =>
120-
applyAccountNameToChannelSection({
121-
cfg,
122-
channelKey: channel,
123-
accountId,
124-
name,
125-
}),
114+
export const signalSetupAdapter: ChannelSetupAdapter = createPatchedAccountSetupAdapter({
115+
channelKey: channel,
126116
validateInput: ({ input }) => {
127117
if (
128118
!input.signalNumber &&
@@ -135,53 +125,8 @@ export const signalSetupAdapter: ChannelSetupAdapter = {
135125
}
136126
return null;
137127
},
138-
applyAccountConfig: ({ cfg, accountId, input }) => {
139-
const namedConfig = applyAccountNameToChannelSection({
140-
cfg,
141-
channelKey: channel,
142-
accountId,
143-
name: input.name,
144-
});
145-
const next =
146-
accountId !== DEFAULT_ACCOUNT_ID
147-
? migrateBaseNameToDefaultAccount({
148-
cfg: namedConfig,
149-
channelKey: channel,
150-
})
151-
: namedConfig;
152-
if (accountId === DEFAULT_ACCOUNT_ID) {
153-
return {
154-
...next,
155-
channels: {
156-
...next.channels,
157-
signal: {
158-
...next.channels?.signal,
159-
enabled: true,
160-
...buildSignalSetupPatch(input),
161-
},
162-
},
163-
};
164-
}
165-
return {
166-
...next,
167-
channels: {
168-
...next.channels,
169-
signal: {
170-
...next.channels?.signal,
171-
enabled: true,
172-
accounts: {
173-
...next.channels?.signal?.accounts,
174-
[accountId]: {
175-
...next.channels?.signal?.accounts?.[accountId],
176-
enabled: true,
177-
...buildSignalSetupPatch(input),
178-
},
179-
},
180-
},
181-
},
182-
};
183-
},
184-
};
128+
buildPatch: (input) => buildSignalSetupPatch(input),
129+
});
185130

186131
type SignalSetupWizardHandlers = {
187132
resolveStatusLines: NonNullable<ChannelSetupWizard["status"]>["resolveStatusLines"];

0 commit comments

Comments
 (0)