Skip to content

A platform for community discussion. Free, open, simple.

FIX: make mentions autocomplete search prioritise exact matches for groups more (#33195)

Kelvb2f0dc7c7290
diff --git a/app/assets/javascripts/discourse/app/lib/user-search.js b/app/assets/javascripts/discourse/app/lib/user-search.js
index 38027b042a8..61532620b22 100644
--- a/app/assets/javascripts/discourse/app/lib/user-search.js
+++ b/app/assets/javascripts/discourse/app/lib/user-search.js
@@ -164,50 +164,121 @@ function organizeResults(r, options) {
const users = [],
emails = [],
groups = [];
- let resultsLength = 0;
if (r.users) {
r.users.forEach((user) => {
- if (resultsLength < options.limit && !exclude.includes(user.username)) {
+ if (!exclude.includes(user.username)) {
user.isUser = true;
user.isMetadataMatch =
!lowerCaseIncludes(user.username, term) &&
!lowerCaseIncludes(user.name, term);
users.push(user);
- resultsLength += 1;
}
});
}
if (options.allowEmails && emailValid(options.term)) {
emails.push({ username: options.term, isEmail: true });
- resultsLength += 1;
}
if (r.groups) {
r.groups.forEach((group) => {
- if (
- (options.term.toLowerCase() === group.name.toLowerCase() ||
- resultsLength < options.limit) &&
- !exclude.includes(group.name)
- ) {
+ if (!exclude.includes(group.name)) {
group.isGroup = true;
groups.push(group);
- resultsLength += 1;
}
});
}
- const results = [
- ...users.filter((u) => !u.isMetadataMatch),
+ // Build prioritized list
+ const exactUsernameSet = new Set();
+ const partialUsernameSet = new Set();
+ const exactGroupSet = new Set();
+ const exactNameSet = new Set();
+
+ // 1. Exact username matches
+ const exactUsernameMatches = users.filter((u) => {
+ if (u.username.toLowerCase() !== term?.toLowerCase()) {
+ return false;
+ }
+ exactUsernameSet.add(u.username);
+ return true;
+ });
+
+ // 2. Exact group name matches
+ const exactGroupMatches = groups.filter((g) => {
+ if (g.name.toLowerCase() !== term?.toLowerCase()) {
+ return false;
+ }
+ exactGroupSet.add(g.name);
+ return true;
+ });
+
+ // 3. Partial username matches (not already in exact matches)
+ const partialUsernameMatches = users.filter((u) => {
+ if (
+ exactUsernameSet.has(u.username) ||
+ u.isMetadataMatch ||
+ !lowerCaseIncludes(u.username, term)
+ ) {
+ return false;
+ }
+ partialUsernameSet.add(u.username);
+ return true;
+ });
+
+ // 4. Partial group name matches (not already in exact matches)
+ const partialGroupMatches = groups.filter(
+ (g) =>
+ !term || (!exactGroupSet.has(g.name) && lowerCaseIncludes(g.name, term))
+ );
+
+ // 5. Exact name matches (not already included via username)
+ const exactNameMatches = users.filter((u) => {
+ if (
+ exactUsernameSet.has(u.username) ||
+ partialUsernameSet.has(u.username) ||
+ u.name?.toLowerCase() !== term?.toLowerCase() ||
+ u.isMetadataMatch
+ ) {
+ return false;
+ }
+ exactNameSet.add(u.username);
+ return true;
+ });
+
+ // 6. Partial name matches (not already included)
+ const partialNameMatches = users.filter(
+ (u) =>
+ !exactUsernameSet.has(u.username) &&
+ !partialUsernameSet.has(u.username) &&
+ !exactNameSet.has(u.username) &&
+ lowerCaseIncludes(u.name, term) &&
+ !u.isMetadataMatch
+ );
+
+ // 7. Users with metadata match
+ const metadataMatchUsers = users.filter((u) => u.isMetadataMatch);
+
+ // Build the prioritized results in the new order
+ const prioritizedResults = [
+ ...exactUsernameMatches,
+ ...exactGroupMatches,
...emails,
- ...groups,
- ...users.filter((u) => u.isMetadataMatch),
+ ...partialUsernameMatches,
+ ...partialGroupMatches,
+ ...exactNameMatches,
+ ...partialNameMatches,
+ ...metadataMatchUsers,
];
- results.users = users;
- results.emails = emails;
- results.groups = groups;
+ // Truncate to limit
+ const results = prioritizedResults.slice(0, options.limit);
+
+ // Only include users, emails, and groups that are actually in the results
+ results.users = results.filter((item) => item.isUser);
+ results.emails = results.filter((item) => item.isEmail);
+ results.groups = results.filter((item) => item.isGroup);
return results;
}
diff --git a/app/assets/javascripts/discourse/tests/acceptance/composer-editor-mentions-test.js b/app/assets/javascripts/discourse/tests/acceptance/composer-editor-mentions-test.js
index c303a16ed3b..e0ad593a184 100644
--- a/app/assets/javascripts/discourse/tests/acceptance/composer-editor-mentions-test.js
+++ b/app/assets/javascripts/discourse/tests/acceptance/composer-editor-mentions-test.js
@@ -131,7 +131,7 @@ acceptance("Composer - editor mentions", function (needs) {
assert.deepEqual(
[...queryAll(".ac-user .username")].map((e) => e.innerText),
- ["foo", "user_group", "user", "user2"]
+ ["foo", "user", "user2"]
);
});
diff --git a/app/assets/javascripts/discourse/tests/unit/lib/user-search-test.js b/app/assets/javascripts/discourse/tests/unit/lib/user-search-test.js
index baca591574d..d12ef6167a6 100644
--- a/app/assets/javascripts/discourse/tests/unit/lib/user-search-test.js
+++ b/app/assets/javascripts/discourse/tests/unit/lib/user-search-test.js
@@ -122,14 +122,16 @@ module("Unit | Utility | user-search", function (hooks) {
}
});
- test("it places groups unconditionally for exact match", async function (assert) {
- let results = await userSearch({ term: "Team" });
- assert.strictEqual(results[results.length - 1]["name"], "team");
- });
-
test("it strips @ from the beginning", async function (assert) {
- let results = await userSearch({ term: "@Team" });
- assert.strictEqual(results[results.length - 1]["name"], "team");
+ const results = await userSearch({ term: "@Team" });
+
+ results.forEach((result) => {
+ const identifier = result.username || result.name;
+ assert.false(
+ identifier.startsWith("@"),
+ `Result '${identifier}' should not start with @`
+ );
+ });
});
test("it skips a search depending on punctuation", async function (assert) {
@@ -162,11 +164,10 @@ module("Unit | Utility | user-search", function (hooks) {
}
results = await userSearch({ term: "[email protected]", allowEmails: true });
- // 6 + email
- assert.strictEqual(results.length, 7);
+ assert.strictEqual(results.length, 6);
results = await userSearch({ term: "[email protected]", allowEmails: true });
- assert.strictEqual(results.length, 7);
+ assert.strictEqual(results.length, 6);
results = await userSearch({ term: "[email protected]" });
assert.strictEqual(results.length, 0);
@@ -182,4 +183,83 @@ module("Unit | Utility | user-search", function (hooks) {
const results = await userSearch({ term: "te", limit: 2 });
assert.strictEqual(results.length, 2);
});
+
+ test("it orders results by priority: exact username, exact group, partial username, partial group, exact name, partial name, metadata", async function (assert) {
+ pretender.get("/u/search/users", (request) => {
+ if (request.url.includes("term=test")) {
+ return response({
+ users: [
+ { username: "testing", name: "Testing User" }, // partial username match
+ { username: "test", name: "Test User" }, // exact username match
+ { username: "user123", name: "anon", title: "tester" }, // metadata match
+ { username: "player", name: "test" }, // exact name match
+ { username: "gamer", name: "testing games" }, // partial name match
+ ],
+ groups: [
+ { name: "testers", usernames: [] }, // partial group match
+ { name: "test", usernames: [] }, // exact group match
+ ],
+ });
+ }
+ return response({ users: [], groups: [] });
+ });
+
+ const results = await userSearch({
+ term: "test",
+ allowEmails: true,
+ limit: 10,
+ });
+
+ // Expected order: exact username, exact group, partial username, partial group, exact name, partial name
+ assert.strictEqual(
+ results[0].username,
+ "test",
+ "Exact username match first"
+ );
+ assert.strictEqual(results[1].name, "test", "Exact group match second");
+ assert.strictEqual(
+ results[2].username,
+ "testing",
+ "Partial username match third"
+ );
+ assert.strictEqual(
+ results[3].name,
+ "testers",
+ "Partial group match fourth"
+ );
+ assert.strictEqual(results[4].username, "player", "Exact name match fifth");
+ assert.strictEqual(
+ results[5].username,
+ "gamer",
+ "Partial name match sixth"
+ );
+ assert.strictEqual(results[6].username, "user123", "Metadata match last");
+ });
+
+ test("it includes all matching groups when limit allows", async function (assert) {
+ pretender.get("/u/search/users", (request) => {
+ if (request.url.includes("term=dev")) {
+ return response({
+ users: [{ username: "developer", name: "Developer" }],
+ groups: [
+ { name: "devs", usernames: [] },
+ { name: "development", usernames: [] },
+ { name: "dev-team", usernames: [] },
+ ],
+ });
+ }
+ return response({ users: [], groups: [] });
+ });
+
+ const results = await userSearch({ term: "dev", limit: 10 });
+ const groupNames = results.groups.map((g) => g.name);
+
+ // All groups should be included since limit allows
+ ["devs", "development", "dev-team"].forEach((groupName) => {
+ assert.true(
+ groupNames.includes(groupName),
+ `Should include '${groupName}' group`
+ );
+ });
+ });
});