mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 14:14:32 +00:00
refactor(slack): dedupe allowlist match selection
This commit is contained in:
@@ -115,6 +115,27 @@ function scoreSlackUser(user: SlackUserLookup, match: { name?: string; email?: s
|
|||||||
return score;
|
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: {
|
export async function resolveSlackUserAllowlist(params: {
|
||||||
token: string;
|
token: string;
|
||||||
entries: string[];
|
entries: string[];
|
||||||
@@ -142,20 +163,7 @@ export async function resolveSlackUserAllowlist(params: {
|
|||||||
if (parsed.email) {
|
if (parsed.email) {
|
||||||
const matches = users.filter((user) => user.email === parsed.email);
|
const matches = users.filter((user) => user.email === parsed.email);
|
||||||
if (matches.length > 0) {
|
if (matches.length > 0) {
|
||||||
const scored = matches
|
results.push(resolveSlackUserFromMatches(input, matches, parsed));
|
||||||
.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,
|
|
||||||
});
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -168,20 +176,7 @@ export async function resolveSlackUserAllowlist(params: {
|
|||||||
return candidates.includes(target);
|
return candidates.includes(target);
|
||||||
});
|
});
|
||||||
if (matches.length > 0) {
|
if (matches.length > 0) {
|
||||||
const scored = matches
|
results.push(resolveSlackUserFromMatches(input, matches, parsed));
|
||||||
.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,
|
|
||||||
});
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user