fix(pairing): support legacy telegram allowFrom migration

This commit is contained in:
Peter Steinberger
2026-02-16 03:25:59 +00:00
parent 18c6f40d32
commit 6754a926ee
5 changed files with 175 additions and 8 deletions

View File

@@ -234,6 +234,31 @@ function normalizeAllowFromInput(channel: PairingChannel, entry: string | number
return normalizeAllowEntry(channel, normalizeId(entry));
}
function dedupePreserveOrder(entries: string[]): string[] {
const seen = new Set<string>();
const out: string[] = [];
for (const entry of entries) {
const normalized = String(entry).trim();
if (!normalized || seen.has(normalized)) {
continue;
}
seen.add(normalized);
out.push(normalized);
}
return out;
}
async function readAllowFromStateForPath(
channel: PairingChannel,
filePath: string,
): Promise<string[]> {
const { value } = await readJsonFile<AllowFromStore>(filePath, {
version: 1,
allowFrom: [],
});
return normalizeAllowFromList(channel, value);
}
async function readAllowFromState(params: {
channel: PairingChannel;
entry: string | number;
@@ -291,12 +316,19 @@ export async function readChannelAllowFromStore(
env: NodeJS.ProcessEnv = process.env,
accountId?: string,
): Promise<string[]> {
const filePath = resolveAllowFromPath(channel, env, accountId);
const { value } = await readJsonFile<AllowFromStore>(filePath, {
version: 1,
allowFrom: [],
});
return normalizeAllowFromList(channel, value);
const normalizedAccountId = accountId?.trim().toLowerCase() ?? "";
if (!normalizedAccountId) {
const filePath = resolveAllowFromPath(channel, env);
return await readAllowFromStateForPath(channel, filePath);
}
const scopedPath = resolveAllowFromPath(channel, env, accountId);
const scopedEntries = await readAllowFromStateForPath(channel, scopedPath);
// Backward compatibility: legacy channel-level allowFrom store was unscoped.
// Keep honoring it alongside account-scoped files to prevent re-pair prompts after upgrades.
const legacyPath = resolveAllowFromPath(channel, env);
const legacyEntries = await readAllowFromStateForPath(channel, legacyPath);
return dedupePreserveOrder([...scopedEntries, ...legacyEntries]);
}
export async function addChannelAllowFromStoreEntry(params: {