Skip to content

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

DEV: Simplify filter suggestions and fix exact-match crash

Régis Hanolb56b3cdb208e
diff --git a/frontend/discourse/app/lib/filter-suggestions.js b/frontend/discourse/app/lib/filter-suggestions.js
index e5019208358..fa4fd2ab461 100644
--- a/frontend/discourse/app/lib/filter-suggestions.js
+++ b/frontend/discourse/app/lib/filter-suggestions.js
@@ -1,8 +1,17 @@
import { ajax } from "discourse/lib/ajax";
+import escapeRegExp from "discourse/lib/escape-regexp";
import { removeAccents } from "discourse/lib/utilities";
import { i18n } from "discourse-i18n";
const MAX_RESULTS = 20;
+const SEGMENT_PATTERN = /"[^"]*"|'[^']*'|\S+/g;
+const PREFIX_PATTERN = /^(?:-=|=-|-|=)/;
+const EMPTY_SEGMENT = Object.freeze({
+ word: "",
+ prefix: "",
+ filterName: null,
+ value: null,
+});
export default class FilterSuggestions {
/**
@@ -13,8 +22,7 @@ export default class FilterSuggestions {
* @returns {Object} { suggestions: Array, activeFilter: string|null }
*/
static async getSuggestions(text, tips = [], context = {}) {
- const parser = new FilterParser(text);
- const lastSegment = parser.getLastSegment();
+ const lastSegment = parseLastSegment(text);
if (!lastSegment.word) {
return {
@@ -23,7 +31,7 @@ export default class FilterSuggestions {
};
}
- if (lastSegment.filterName && lastSegment.hasColon) {
+ if (lastSegment.filterName) {
const tip = this.findTipForFilter(lastSegment.filterName, tips);
if (tip?.type) {
@@ -40,7 +48,6 @@ export default class FilterSuggestions {
}
}
- // Otherwise, filter the available tips based on what user typed
return {
suggestions: this.filterTips(tips, lastSegment.word, lastSegment.prefix),
activeFilter: null,
@@ -50,15 +57,7 @@ export default class FilterSuggestions {
static getTopLevelTips(tips) {
return tips
.filter((tip) => tip.priority === 1)
- .sort((a, b) => {
- // First by priority (descending)
- const priorityDiff = (b.priority || 0) - (a.priority || 0);
- if (priorityDiff !== 0) {
- return priorityDiff;
- }
- // Then alphabetically
- return a.name.localeCompare(b.name);
- })
+ .sort((a, b) => a.name.localeCompare(b.name))
.slice(0, MAX_RESULTS);
}
@@ -169,69 +168,31 @@ export default class FilterSuggestions {
}
}
-/**
- * Parses filter input text into structured segments
- */
-class FilterParser {
- constructor(text) {
- this.text = text || "";
- this.segments = this.parse();
- }
-
- parse() {
- // Split on whitespace, but preserve quoted strings
- const quotedPattern = /"[^"]*"|'[^']*'|[^\s]+/g;
- const words = (this.text.match(quotedPattern) || []).filter(Boolean);
- this.endsWithSpace = this.text.endsWith(" ");
- return words.map((word) => this.parseWord(word));
+function parseLastSegment(text) {
+ if (!text || text.endsWith(" ")) {
+ return EMPTY_SEGMENT;
}
- parseWord(word) {
- const prefix = this.extractPrefix(word);
- const withoutPrefix = word.substring(prefix.length);
- const colonIndex = withoutPrefix.indexOf(":");
-
- if (colonIndex > 0) {
- const filterName = withoutPrefix.substring(0, colonIndex);
- const value = withoutPrefix.substring(colonIndex + 1);
+ const word = text.match(SEGMENT_PATTERN)?.at(-1);
- return {
- word,
- prefix,
- filterName,
- value,
- hasColon: true,
- };
- }
-
- return {
- word,
- prefix,
- filterName: null,
- value: null,
- hasColon: false,
- };
+ if (!word) {
+ return EMPTY_SEGMENT;
}
- extractPrefix(word) {
- const match = word.match(/^(-=|=-|-|=)/);
- return match ? match[0] : "";
- }
-
- getLastSegment() {
- const empty = {
- word: "",
- prefix: "",
- filterName: null,
- value: null,
- hasColon: false,
- };
+ const prefix = word.match(PREFIX_PATTERN)?.[0] || "";
+ const withoutPrefix = word.substring(prefix.length);
+ const colonIndex = withoutPrefix.indexOf(":");
- if (this.endsWithSpace) {
- return empty;
- }
- return this.segments[this.segments.length - 1] || empty;
+ if (colonIndex <= 0) {
+ return { word, prefix, filterName: null, value: null };
}
+
+ return {
+ word,
+ prefix,
+ filterName: withoutPrefix.substring(0, colonIndex),
+ value: withoutPrefix.substring(colonIndex + 1),
+ };
}
class FilterTypeValueSuggester {
@@ -250,7 +211,7 @@ class FilterTypeValueSuggester {
if (this.tip.delimiters) {
const delimiterPattern = new RegExp(
- `[${this.tip.delimiters.map((d) => this.escapeRegex(d.name)).join("")}]`
+ `[${this.tip.delimiters.map((d) => escapeRegExp(d.name)).join("")}]`
);
const parts = value.split(delimiterPattern);
@@ -270,10 +231,6 @@ class FilterTypeValueSuggester {
}
}
- escapeRegex(str) {
- return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
- }
-
buildSuggestionName(term) {
return `${this.prefix}${this.filterName}:${this.valuePrefix}${term}`;
}
@@ -371,87 +328,33 @@ class FilterTypeValueSuggester {
}
quoteIfNeeded(name) {
- const needsQuoting = /[\s&\-()'""]/.test(name);
- if (!needsQuoting) {
+ if (!/[\s&\-()'"]/.test(name)) {
return name;
}
- const hasDoubleQuote = name.includes('"');
- const hasSingleQuote = name.includes("'");
-
- if (hasDoubleQuote && !hasSingleQuote) {
- return `'${name}'`;
- } else if (hasSingleQuote && !hasDoubleQuote) {
- return `"${name}"`;
- } else if (hasDoubleQuote && hasSingleQuote) {
- return `'${name.replace(/'/g, "\\'")}'`;
- } else {
- return `"${name}"`;
- }
+ return name.includes('"') ? `'${name}'` : `"${name}"`;
}
async getUserSuggestions() {
- try {
- const data = { limit: 10 };
- if (this.searchTerm) {
- data.term = this.searchTerm;
- } else {
- data.last_seen_users = true;
- }
-
- const response = await ajax("/u/search/users.json", { data });
-
- let results = response.users.map((user) => ({
- name: this.buildSuggestionName(user.username),
- description: user.name || "",
- term: user.username,
- isSuggestion: true,
- }));
-
- results = this.prepareDelimiterSuggestions(results);
- return results;
- } catch {
- return [];
- }
+ return this.prepareDelimiterSuggestions(await this.#fetchUsers());
}
async getGroupSuggestions() {
- try {
- const groupData = { limit: 10 };
- if (this.searchTerm) {
- groupData.term = this.searchTerm;
- }
-
- const groupResponse = await ajax("/groups/search.json", {
- data: groupData,
- });
-
- let results = groupResponse.map((group) => ({
- name: this.buildSuggestionName(group.name),
- description: group.full_name || group.name,
- term: group.name,
- isSuggestion: true,
- }));
-
- results = this.prepareDelimiterSuggestions(results);
- return results;
- } catch {
- return [];
- }
+ return this.prepareDelimiterSuggestions(await this.#fetchGroups());
}
async getUsernameGroupListSuggestions() {
- const usedTerms = new Set(this.previousValues.map((v) => v.toLowerCase()));
let suggestions = [];
- // Add special entries (*, nobody, etc.) if no values selected yet
- if (this.tip.extra_entries && usedTerms.size === 0) {
+ if (this.tip.extra_entries && this.previousValues.length === 0) {
suggestions = this.tip.extra_entries
.filter((entry) => {
if (!this.searchTerm) {
return true;
}
+
const searchLower = this.searchTerm.toLowerCase();
+
return (
entry.name.toLowerCase().includes(searchLower) ||
entry.description.toLowerCase().includes(searchLower)
@@ -465,65 +368,21 @@ class FilterTypeValueSuggester {
}));
}
- try {
- const userData = { limit: 10 };
- if (this.searchTerm) {
- userData.term = this.searchTerm;
- } else {
- userData.last_seen_users = true;
- }
-
- const userResponse = await ajax("/u/search/users.json", {
- data: userData,
- });
- const userSuggestions = userResponse.users
- .filter((user) => !usedTerms.has(user.username.toLowerCase()))
- .map((user) => ({
- name: this.buildSuggestionName(user.username),
- description: user.name || "",
- term: user.username,
- isSuggestion: true,
- }));
+ suggestions = this.prepareDelimiterSuggestions(
+ suggestions.concat(await this.#fetchUsers(), await this.#fetchGroups(5))
+ );
- suggestions = suggestions.concat(userSuggestions);
- } catch {
- // Continue without user suggestions
- }
-
- // Add group suggestions
- try {
- const groupData = { limit: 5 };
- if (this.searchTerm) {
- groupData.term = this.searchTerm;
- }
-
- const groupResponse = await ajax("/groups/search.json", {
- data: groupData,
- });
- const groupSuggestions = groupResponse
- .filter((group) => !usedTerms.has(group.name.toLowerCase()))
- .map((group) => ({
- name: this.buildSuggestionName(group.name),
- description: group.full_name || group.name,
- term: group.name,
- isSuggestion: true,
- }));
+ const searchLower = this.searchTerm?.toLowerCase();
- suggestions = suggestions.concat(groupSuggestions);
- } catch {
- // Continue without group suggestions
- }
-
- suggestions = this.prepareDelimiterSuggestions(suggestions);
return suggestions
.sort((a, b) => {
- const searchLower = this.searchTerm?.toLowerCase();
- const aExact = a.term.toLowerCase() === searchLower;
- const bExact = b.term.toLowerCase() === searchLower;
+ const aExact = (a.term || "").toLowerCase() === searchLower;
+ const bExact = (b.term || "").toLowerCase() === searchLower;
if (aExact && !bExact) {
return -1;
}
+
if (!aExact && bExact) {
return 1;
}
@@ -542,22 +401,18 @@ class FilterTypeValueSuggester {
];
return options
- .filter((opt) => {
- if (!this.searchTerm) {
- return true;
- }
- const desc = i18n(`filter.description.days.${opt.key}`);
- return (
- opt.value.includes(this.searchTerm) ||
- desc.toLowerCase().includes(this.searchTerm.toLowerCase())
- );
- })
.map((opt) => ({
name: this.buildSuggestionName(opt.value),
description: i18n(`filter.description.${opt.key}`),
term: opt.value,
isSuggestion: true,
- }));
+ }))
+ .filter(
+ (s) =>
+ !this.searchTerm ||
+ s.term.includes(this.searchTerm) ||
+ s.description.toLowerCase().includes(this.searchTerm.toLowerCase())
+ );
}
getNumberSuggestions() {
@@ -578,4 +433,48 @@ class FilterTypeValueSuggester {
isSuggestion: true,
}));
}
+
+ async #fetchGroups(limit = 10) {
+ const data = { limit };
+
+ if (this.searchTerm) {
+ data.term = this.searchTerm;
+ }
+
+ try {
+ const response = await ajax("/groups/search.json", { data });
+
+ return response.map((group) => ({
+ name: this.buildSuggestionName(group.name),
+ description: group.full_name || group.name,
+ term: group.name,
+ isSuggestion: true,
+ }));
+ } catch {
+ return [];
+ }
+ }
+
+ async #fetchUsers(limit = 10) {
+ const data = { limit };
+
+ if (this.searchTerm) {
+ data.term = this.searchTerm;
+ } else {
+ data.last_seen_users = true;
+ }
+
+ try {
+ const response = await ajax("/u/search/users.json", { data });
+
+ return response.users.map((user) => ({
+ name: this.buildSuggestionName(user.username),
+ description: user.name || "",
+ term: user.username,
+ isSuggestion: true,
+ }));
+ } catch {
+ return [];
+ }
+ }
}
diff --git a/frontend/discourse/tests/unit/lib/filter-suggestions-test.js b/frontend/discourse/tests/unit/lib/filter-suggestions-test.js
index 71cb7343ccd..95661965b52 100644
--- a/frontend/discourse/tests/unit/lib/filter-suggestions-test.js
+++ b/frontend/discourse/tests/unit/lib/filter-suggestions-test.js
@@ -26,6 +26,11 @@ function buildTips() {
],
},
{ name: "status:", description: "Pick a status" },
+ {
+ name: "order:views",
+ description: "Sort by views",
+ prefixes: [],
+ },
{
name: "status:solved",
description: "Solved topics",
@@ -106,16 +111,6 @@ module("Unit | Utility | FilterSuggestions", function (hooks) {
);
});
- test("-cat finds -categories", async function (assert) {
- const tips = buildTips();
- const { suggestions, activeFilter } =
- await FilterSuggestions.getSuggestions("-cat", tips, buildContext());
- const names = suggestions.map((s) => s.name);
-
- assert.deepEqual(names, ["-category:"], "returns only -category: for -cat");
- assert.strictEqual(activeFilter, null, "activeFilter is not set yet");
- });
-
test("trailing space switches back to top-level tips", async function (assert) {
const tips = buildTips();
const baseline = await FilterSuggestions.getSuggestions(
@@ -151,6 +146,28 @@ module("Unit | Utility | FilterSuggestions", function (hooks) {
assert.deepEqual(names, ["category:", "-category:", "=category:"]);
assert.strictEqual(activeFilter, null, "activeFilter is not set yet");
+
+ const negated = await FilterSuggestions.getSuggestions(
+ "-cat",
+ tips,
+ buildContext()
+ );
+ assert.deepEqual(
+ negated.suggestions.map((s) => s.name),
+ ["-category:"],
+ "-cat matches only the negated category tip"
+ );
+
+ const negatedOrder = await FilterSuggestions.getSuggestions(
+ "-order",
+ tips,
+ buildContext()
+ );
+ assert.deepEqual(
+ negatedOrder.suggestions.map((s) => s.name),
+ [],
+ "order tips advertise no prefixes"
+ );
});
test("does not show suggestions for already matched filters", async function (assert) {
@@ -205,6 +222,17 @@ module("Unit | Utility | FilterSuggestions", function (hooks) {
descFiltered.suggestions.length > 0,
"filters by description substring"
);
+
+ const placeholder = await FilterSuggestions.getSuggestions(
+ "after:descr",
+ tips,
+ buildContext()
+ );
+ assert.deepEqual(
+ placeholder.suggestions,
+ [],
+ "date descriptions resolve to real translations"
+ );
});
test("number suggestions include defaults and filter by partial", async function (assert) {
@@ -232,20 +260,6 @@ module("Unit | Utility | FilterSuggestions", function (hooks) {
);
});
- test("filtering tips by partial name returns matching tips", async function (assert) {
- const tips = buildTips();
-
- const { suggestions } = await FilterSuggestions.getSuggestions(
- "sta",
- tips,
- buildContext()
- );
- const names = suggestions.map((s) => s.name);
-
- assert.true(names.includes("status:solved"), "includes status:solved");
- assert.true(names.includes("status:unsolved"), "includes status:unsolved");
- });
-
test("number suggestions respect the minimum", async function (assert) {
const tips = [{ name: "max_results:", type: "number", min: 1 }];
@@ -355,6 +369,17 @@ module("Unit | Utility | FilterSuggestions", function (hooks) {
secondTerms.includes("cam"),
"cam is excluded after being used"
);
+
+ const exact = await FilterSuggestions.getSuggestions(
+ "assigned:sam",
+ tips,
+ buildContext()
+ );
+
+ assert.true(
+ exact.suggestions.map((s) => s.name).includes("assigned:sam,"),
+ "offers the delimiter continuation for an exact match"
+ );
});
test("tag_group suggestions properly quote names with special characters", async function (assert) {
@@ -399,7 +424,7 @@ module("Unit | Utility | FilterSuggestions", function (hooks) {
assert.strictEqual(
terms[3],
'"It\'s a Group"',
- "names with single quotes use double quotes and escape internal single quotes"
+ "names with single quotes use double quotes"
);
});
});