From 628c7b239819738ab70e7287d23593631817e6d2 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 15 Feb 2026 05:57:11 +0000 Subject: [PATCH] refactor(slack): dedupe allowlist match selection --- src/slack/resolve-users.ts | 51 +++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/src/slack/resolve-users.ts b/src/slack/resolve-users.ts index 66f101d3221..53d2e4c9a74 100644 --- a/src/slack/resolve-users.ts +++ b/src/slack/resolve-users.ts @@ -115,6 +115,27 @@ function scoreSlackUser(user: SlackUserLookup, match: { name?: string; email?: s return score; } +function resolveSlackUserFromMatches( + input: string, + matches: SlackUserLookup[], + parsed: { name?: string; email?: string }, +): SlackUserResolution { + const scored = matches + .map((user) => ({ user, score: scoreSlackUser(user, parsed) })) + .toSorted((a, b) => b.score - a.score); + const best = scored[0]?.user ?? matches[0]; + return { + input, + resolved: true, + id: best.id, + name: best.displayName ?? best.realName ?? best.name, + email: best.email, + deleted: best.deleted, + isBot: best.isBot, + note: matches.length > 1 ? "multiple matches; chose best" : undefined, + }; +} + export async function resolveSlackUserAllowlist(params: { token: string; entries: string[]; @@ -142,20 +163,7 @@ export async function resolveSlackUserAllowlist(params: { if (parsed.email) { const matches = users.filter((user) => user.email === parsed.email); if (matches.length > 0) { - const scored = matches - .map((user) => ({ user, score: scoreSlackUser(user, parsed) })) - .toSorted((a, b) => b.score - a.score); - const best = scored[0]?.user ?? matches[0]; - results.push({ - input, - resolved: true, - id: best.id, - name: best.displayName ?? best.realName ?? best.name, - email: best.email, - deleted: best.deleted, - isBot: best.isBot, - note: matches.length > 1 ? "multiple matches; chose best" : undefined, - }); + results.push(resolveSlackUserFromMatches(input, matches, parsed)); continue; } } @@ -168,20 +176,7 @@ export async function resolveSlackUserAllowlist(params: { return candidates.includes(target); }); if (matches.length > 0) { - const scored = matches - .map((user) => ({ user, score: scoreSlackUser(user, parsed) })) - .toSorted((a, b) => b.score - a.score); - const best = scored[0]?.user ?? matches[0]; - results.push({ - input, - resolved: true, - id: best.id, - name: best.displayName ?? best.realName ?? best.name, - email: best.email, - deleted: best.deleted, - isBot: best.isBot, - note: matches.length > 1 ? "multiple matches; chose best" : undefined, - }); + results.push(resolveSlackUserFromMatches(input, matches, parsed)); continue; } }