Skip to content

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

UX: Find groups also by their full_name when mentioning (#38998)

Gregor Sondermeier468fab3dfa3c
diff --git a/frontend/discourse/app/lib/user-search.js b/frontend/discourse/app/lib/user-search.js
index a79955690de..48227e743ea 100644
--- a/frontend/discourse/app/lib/user-search.js
+++ b/frontend/discourse/app/lib/user-search.js
@@ -172,6 +172,7 @@ function organizeResults(r, options) {
if (!exclude.includes(user.username)) {
user.isUser = true;
user.isMetadataMatch =
+ term &&
!lowerCaseIncludes(user.username, term) &&
!lowerCaseIncludes(user.name, term);
users.push(user);
@@ -193,85 +194,123 @@ function organizeResults(r, options) {
}
// Build prioritized list
- const exactUsernameSet = new Set();
- const partialUsernameSet = new Set();
- const exactGroupSet = new Set();
- const exactNameSet = new Set();
+ // These Sets are used to prevent duplicate entries in the autocomplete suggestions
+ const userUsernameSet = new Set();
+ const groupNameSet = new Set();
// 1. Exact username matches
- const exactUsernameMatches = users.filter((u) => {
+ const exactUserUsernameMatches = users.filter((u) => {
if (u.username.toLowerCase() !== term?.toLowerCase()) {
return false;
}
- exactUsernameSet.add(u.username);
+ userUsernameSet.add(u.username);
return true;
});
// 2. Exact group name matches
- const exactGroupMatches = groups.filter((g) => {
+ const exactGroupNameMatches = groups.filter((g) => {
if (g.name.toLowerCase() !== term?.toLowerCase()) {
return false;
}
- exactGroupSet.add(g.name);
+ groupNameSet.add(g.name);
return true;
});
// 3. Partial username matches (not already in exact matches)
- const partialUsernameMatches = users.filter((u) => {
+ const partialUserUsernameMatches = users.filter((u) => {
if (
- exactUsernameSet.has(u.username) ||
+ userUsernameSet.has(u.username) ||
u.isMetadataMatch ||
- !lowerCaseIncludes(u.username, term)
+ (term && !lowerCaseIncludes(u.username, term))
) {
return false;
}
- partialUsernameSet.add(u.username);
+ userUsernameSet.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))
- );
+ const partialGroupNameMatches = groups.filter((g) => {
+ if (
+ groupNameSet.has(g.name) ||
+ (term && !lowerCaseIncludes(g.name, term))
+ ) {
+ return false;
+ }
+
+ groupNameSet.add(g.name);
+ return true;
+ });
// 5. Exact name matches (not already included via username)
- const exactNameMatches = users.filter((u) => {
+ const exactUserNameMatches = users.filter((u) => {
if (
- exactUsernameSet.has(u.username) ||
- partialUsernameSet.has(u.username) ||
+ userUsernameSet.has(u.username) ||
u.name?.toLowerCase() !== term?.toLowerCase() ||
u.isMetadataMatch
) {
return false;
}
- exactNameSet.add(u.username);
+ userUsernameSet.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
- );
+ const partialUserNameMatches = users.filter((u) => {
+ if (
+ userUsernameSet.has(u.username) ||
+ !lowerCaseIncludes(u.name, term) ||
+ u.isMetadataMatch
+ ) {
+ return false;
+ }
+
+ userUsernameSet.add(u.username);
+ return true;
+ });
+
+ // 7. Exact group full_name matches
+ const exactGroupFullNameMatches = groups.filter((g) => {
+ if (
+ groupNameSet.has(g.name) ||
+ !g.full_name ||
+ g.full_name?.toLowerCase() !== term?.toLowerCase()
+ ) {
+ return false;
+ }
+ groupNameSet.add(g.name);
+ return true;
+ });
+
+ // 8. Partial group full_name matches (not already in exact matches)
+ const partialGroupFullNameMatches = groups.filter((g) => {
+ if (
+ groupNameSet.has(g.name) ||
+ !g.full_name ||
+ !lowerCaseIncludes(g.full_name, term)
+ ) {
+ return false;
+ }
+
+ groupNameSet.add(g.name);
+ return true;
+ });
- // 7. Users with metadata match
- const metadataMatchUsers = users.filter((u) => u.isMetadataMatch);
+ // 9. Users with metadata match
+ const metadataUserMatches = users.filter((u) => u.isMetadataMatch);
// Build the prioritized results in the new order
const prioritizedResults = [
- ...exactUsernameMatches,
- ...exactGroupMatches,
+ ...exactUserUsernameMatches,
+ ...exactGroupNameMatches,
...emails,
- ...partialUsernameMatches,
- ...partialGroupMatches,
- ...exactNameMatches,
- ...partialNameMatches,
- ...metadataMatchUsers,
+ ...partialUserUsernameMatches,
+ ...partialGroupNameMatches,
+ ...exactUserNameMatches,
+ ...partialUserNameMatches,
+ ...exactGroupFullNameMatches,
+ ...partialGroupFullNameMatches,
+ ...metadataUserMatches,
];
// Truncate to limit
diff --git a/frontend/discourse/tests/acceptance/composer-editor-mentions-test.js b/frontend/discourse/tests/acceptance/composer-editor-mentions-test.js
index b46e9d011ce..79efb04992e 100644
--- a/frontend/discourse/tests/acceptance/composer-editor-mentions-test.js
+++ b/frontend/discourse/tests/acceptance/composer-editor-mentions-test.js
@@ -143,7 +143,7 @@ acceptance("Composer - editor mentions", function (needs) {
);
});
- test("shows users immediately when @ is typed in a reply", async function (assert) {
+ test("shows users and groups immediately when @ is typed in a reply", async function (assert) {
await visit("/");
await click(".topic-list-item .title");
await click(".btn-primary.create");
@@ -154,7 +154,7 @@ acceptance("Composer - editor mentions", function (needs) {
[...document.querySelectorAll(".ac-user .username")].map(
(e) => e.innerText
),
- ["user_group", "user", "user2", "johndoe", "foo"]
+ ["user", "user2", "johndoe", "foo", "user_group"]
);
});
diff --git a/frontend/discourse/tests/unit/lib/user-search-test.js b/frontend/discourse/tests/unit/lib/user-search-test.js
index 34496319204..05b676acc4d 100644
--- a/frontend/discourse/tests/unit/lib/user-search-test.js
+++ b/frontend/discourse/tests/unit/lib/user-search-test.js
@@ -198,6 +198,8 @@ module("Unit | Utility | user-search", function (hooks) {
groups: [
{ name: "testers", usernames: [] }, // partial group match
{ name: "test", usernames: [] }, // exact group match
+ { name: "group_2", full_name: "Testers", usernames: [] }, // partial group match
+ { name: "group_4", full_name: "Test", usernames: [] }, // exact group match
],
});
}
@@ -210,30 +212,56 @@ module("Unit | Utility | user-search", function (hooks) {
limit: 10,
});
- // Expected order: exact username, exact group, partial username, partial group, exact name, partial name
+ // Expected order:
+ // exact user username, exact group name,
+ // partial user username, partial group name,
+ // exact user name, partial user name
+ // exact group full_name, partial group full_name
assert.strictEqual(
results[0].username,
"test",
- "Exact username match first"
+ "First: Exact user username match"
+ );
+ assert.strictEqual(
+ results[1].name,
+ "test",
+ "Second: Exact group name match"
);
- assert.strictEqual(results[1].name, "test", "Exact group match second");
assert.strictEqual(
results[2].username,
"testing",
- "Partial username match third"
+ "Third: Partial user username match"
);
assert.strictEqual(
results[3].name,
"testers",
- "Partial group match fourth"
+ "Fourth: Partial group name match"
+ );
+ assert.strictEqual(
+ results[4].username,
+ "player",
+ "Fifth: Exact user name match"
);
- assert.strictEqual(results[4].username, "player", "Exact name match fifth");
assert.strictEqual(
results[5].username,
"gamer",
- "Partial name match sixth"
+ "Sixth: Partial user name match"
+ );
+ assert.strictEqual(
+ results[6].full_name,
+ "Test",
+ "Seventh: Exact group full name match"
+ );
+ assert.strictEqual(
+ results[7].full_name,
+ "Testers",
+ "Eighth: Partial group full name match"
+ );
+ assert.strictEqual(
+ results[8].username,
+ "user123",
+ "Ninth/Last: Metadata match"
);
- assert.strictEqual(results[6].username, "user123", "Metadata match last");
});
test("it includes all matching groups when limit allows", async function (assert) {