mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 18:24:35 +00:00
refactor(matrix): dedupe directory/target match helpers
This commit is contained in:
@@ -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";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user