refactor(matrix): dedupe directory/target match helpers

This commit is contained in:
Peter Steinberger
2026-02-18 16:20:12 +00:00
parent e5f13db13d
commit eb4f1e765c
2 changed files with 71 additions and 62 deletions

View File

@@ -6,6 +6,22 @@ import type {
} from "openclaw/plugin-sdk";
import { listMatrixDirectoryGroupsLive, listMatrixDirectoryPeersLive } from "./directory-live.js";
function findExactDirectoryMatches(
matches: ChannelDirectoryEntry[],
query: string,
): ChannelDirectoryEntry[] {
const normalized = query.trim().toLowerCase();
if (!normalized) {
return [];
}
return matches.filter((match) => {
const id = match.id.trim().toLowerCase();
const name = match.name?.trim().toLowerCase();
const handle = match.handle?.trim().toLowerCase();
return normalized === id || normalized === name || normalized === handle;
});
}
function pickBestGroupMatch(
matches: ChannelDirectoryEntry[],
query: string,
@@ -13,19 +29,8 @@ function pickBestGroupMatch(
if (matches.length === 0) {
return undefined;
}
const normalized = query.trim().toLowerCase();
if (normalized) {
const exact = matches.find((match) => {
const name = match.name?.trim().toLowerCase();
const handle = match.handle?.trim().toLowerCase();
const id = match.id.trim().toLowerCase();
return name === normalized || handle === normalized || id === normalized;
});
if (exact) {
return exact;
}
}
return matches[0];
const [exact] = findExactDirectoryMatches(matches, query);
return exact ?? matches[0];
}
function pickBestUserMatch(
@@ -35,16 +40,7 @@ function pickBestUserMatch(
if (matches.length === 0) {
return undefined;
}
const normalized = query.trim().toLowerCase();
if (!normalized) {
return undefined;
}
const exact = matches.filter((match) => {
const id = match.id.trim().toLowerCase();
const name = match.name?.trim().toLowerCase();
const handle = match.handle?.trim().toLowerCase();
return normalized === id || normalized === name || normalized === handle;
});
const exact = findExactDirectoryMatches(matches, query);
if (exact.length === 1) {
return exact[0];
}
@@ -59,12 +55,7 @@ function describeUserMatchFailure(matches: ChannelDirectoryEntry[], query: strin
if (!normalized) {
return "empty input";
}
const exact = matches.filter((match) => {
const id = match.id.trim().toLowerCase();
const name = match.name?.trim().toLowerCase();
const handle = match.handle?.trim().toLowerCase();
return normalized === id || normalized === name || normalized === handle;
});
const exact = findExactDirectoryMatches(matches, normalized);
if (exact.length === 0) {
return "no exact match; use full Matrix ID";
}