fix: honor explicit Synology Chat rate-limit env values

Landed from contributor PR #39197 by @scoootscooob.

Co-authored-by: scoootscooob <zhentongfan@gmail.com>
This commit is contained in:
Peter Steinberger
2026-03-08 02:33:52 +00:00
parent 6cb889da8c
commit af9d76b79a
3 changed files with 28 additions and 4 deletions

View File

@@ -130,4 +130,18 @@ describe("resolveAccount", () => {
const account = resolveAccount(cfg);
expect(account.allowedUserIds).toEqual(["u1", "u2"]);
});
it("respects SYNOLOGY_RATE_LIMIT=0 instead of defaulting to 30", () => {
process.env.SYNOLOGY_RATE_LIMIT = "0";
const cfg = { channels: { "synology-chat": {} } };
const account = resolveAccount(cfg);
expect(account.rateLimitPerMinute).toBe(0);
});
it("falls back to 30 for malformed SYNOLOGY_RATE_LIMIT values", () => {
process.env.SYNOLOGY_RATE_LIMIT = "0abc";
const cfg = { channels: { "synology-chat": {} } };
const account = resolveAccount(cfg);
expect(account.rateLimitPerMinute).toBe(30);
});
});

View File

@@ -20,6 +20,17 @@ function parseAllowedUserIds(raw: string | string[] | undefined): string[] {
.filter(Boolean);
}
function parseRateLimitPerMinute(raw: string | undefined): number {
if (raw == null) {
return 30;
}
const trimmed = raw.trim();
if (!/^-?\d+$/.test(trimmed)) {
return 30;
}
return Number.parseInt(trimmed, 10);
}
/**
* List all configured account IDs for this channel.
* Returns ["default"] if there's a base config, plus any named accounts.
@@ -62,7 +73,7 @@ export function resolveAccount(cfg: any, accountId?: string | null): ResolvedSyn
const envIncomingUrl = process.env.SYNOLOGY_CHAT_INCOMING_URL ?? "";
const envNasHost = process.env.SYNOLOGY_NAS_HOST ?? "localhost";
const envAllowedUserIds = process.env.SYNOLOGY_ALLOWED_USER_IDS ?? "";
const envRateLimit = process.env.SYNOLOGY_RATE_LIMIT;
const envRateLimitValue = parseRateLimitPerMinute(process.env.SYNOLOGY_RATE_LIMIT);
const envBotName = process.env.OPENCLAW_BOT_NAME ?? "OpenClaw";
// Merge: account override > base channel config > env var
@@ -78,9 +89,7 @@ export function resolveAccount(cfg: any, accountId?: string | null): ResolvedSyn
accountOverride.allowedUserIds ?? channelCfg.allowedUserIds ?? envAllowedUserIds,
),
rateLimitPerMinute:
accountOverride.rateLimitPerMinute ??
channelCfg.rateLimitPerMinute ??
(envRateLimit ? parseInt(envRateLimit, 10) || 30 : 30),
accountOverride.rateLimitPerMinute ?? channelCfg.rateLimitPerMinute ?? envRateLimitValue,
botName: accountOverride.botName ?? channelCfg.botName ?? envBotName,
allowInsecureSsl: accountOverride.allowInsecureSsl ?? channelCfg.allowInsecureSsl ?? false,
};