Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

search: Add support for empty string topic.

Prakhar Pratyush033c706a031c
diff --git a/web/src/filter.ts b/web/src/filter.ts
index 0ae7081ed1b..254827bb20c 100644
--- a/web/src/filter.ts
+++ b/web/src/filter.ts
@@ -41,7 +41,8 @@ type Part =
| {
type: "channel_topic";
channel: string;
- topic: string;
+ topic_display_name: string;
+ is_empty_string_topic: boolean;
}
| {
type: "is_operator";
@@ -56,6 +57,7 @@ type Part =
type: "prefix_for_operator";
prefix_for_operator: string;
operand: string;
+ is_empty_string_topic?: boolean;
}
| {
type: "user_pill";
@@ -468,11 +470,11 @@ export class Filter {
}
// Match all operands that either have no spaces, or are surrounded by
- // quotes, preceded by an optional operator that may have a space after it.
+ // quotes, preceded by an optional operator.
// TODO: rewrite this using `str.matchAll` to get out the match objects
// with individual capture groups, so we don’t need to write a separate
// parser with `.split`.
- const matches = str.match(/([^\s:]+: ?)?("[^"]+"?|\S+)/g);
+ const matches = str.match(/([^\s:]+:)?("[^"]+"?|\S+)/g);
if (matches === null) {
return terms;
}
@@ -716,7 +718,7 @@ export class Filter {
}
// Convert a list of terms to a human-readable description.
- static parts_for_describe(terms: NarrowTerm[]): Part[] {
+ static parts_for_describe(terms: NarrowTerm[], is_operator_suggestion: boolean): Part[] {
const parts: Part[] = [];
if (terms.length === 0) {
@@ -736,7 +738,8 @@ export class Filter {
parts.push({
type: "channel_topic",
channel,
- topic,
+ topic_display_name: util.get_final_topic_display_name(topic),
+ is_empty_string_topic: topic === "",
});
terms = terms.slice(2);
}
@@ -812,6 +815,14 @@ export class Filter {
// Assume the operand is a partially formed name and return
// the operator as the channel name in the next block.
}
+ if (canonicalized_operator === "topic" && !is_operator_suggestion) {
+ return {
+ type: "prefix_for_operator",
+ prefix_for_operator,
+ operand: util.get_final_topic_display_name(operand),
+ is_empty_string_topic: operand === "",
+ };
+ }
return {
type: "prefix_for_operator",
prefix_for_operator,
@@ -826,9 +837,12 @@ export class Filter {
return [...parts, ...more_parts];
}
- static search_description_as_html(terms: NarrowTerm[]): string {
+ static search_description_as_html(
+ terms: NarrowTerm[],
+ is_operator_suggestion: boolean,
+ ): string {
return render_search_description({
- parts: Filter.parts_for_describe(terms),
+ parts: Filter.parts_for_describe(terms, is_operator_suggestion),
});
}
diff --git a/web/src/search_pill.ts b/web/src/search_pill.ts
index 813ab0a93e7..5cbb3623cdb 100644
--- a/web/src/search_pill.ts
+++ b/web/src/search_pill.ts
@@ -120,6 +120,13 @@ export function create_pills($pill_container: JQuery): SearchPillWidget {
if (item.type === "search_user") {
return render_search_user_pill(item);
}
+ if (item.operator === "topic" && item.operand === "") {
+ return render_input_pill({
+ is_empty_string_topic: true,
+ sign: item.negated ? "-" : "",
+ topic_display_name: util.get_final_topic_display_name(""),
+ });
+ }
const display_value = get_search_string_from_item(item);
return render_input_pill({
display_value,
@@ -180,9 +187,20 @@ export function set_search_bar_contents(
// Instead, we keep the partial pill to the end of the
// search box as text input, which will update the
// typeahead to show operand suggestions.
+ // Note: We make a pill for `topic:` as it represents empty string topic
+ // except the case where it suggests `topic` operator.
if (input.at(-1) === ":" && term.operand === "" && term === search_terms.at(-1)) {
- partial_pill = input;
- continue;
+ const is_topic_operator_suggestion = (): boolean => {
+ const is_typeahead_visible = $("#searchbox_form .typeahead").is(":visible");
+ return (
+ is_typeahead_visible &&
+ $("#searchbox_form .typeahead-item.active .empty-topic-display").length === 0
+ );
+ };
+ if (term.operator !== "topic" || is_topic_operator_suggestion()) {
+ partial_pill = input;
+ continue;
+ }
}
if (!Filter.is_valid_search_term(term)) {
@@ -223,6 +241,9 @@ function get_search_operand(item: SearchPill, for_display: boolean): string {
if (for_display && item.operator === "channel") {
return stream_data.get_valid_sub_by_id_string(item.operand).name;
}
+ if (for_display && item.operator === "topic") {
+ return util.get_final_topic_display_name(item.operand);
+ }
return item.operand;
}
diff --git a/web/src/search_suggestion.ts b/web/src/search_suggestion.ts
index f10b6ffd84a..2b759bd93a6 100644
--- a/web/src/search_suggestion.ts
+++ b/web/src/search_suggestion.ts
@@ -16,6 +16,7 @@ import * as stream_data from "./stream_data.ts";
import * as stream_topic_history from "./stream_topic_history.ts";
import * as stream_topic_history_util from "./stream_topic_history_util.ts";
import * as typeahead_helper from "./typeahead_helper.ts";
+import * as util from "./util.ts";
export type UserPillItem = {
id: number;
@@ -96,9 +97,9 @@ function check_validity(
return true;
}
-function format_as_suggestion(terms: NarrowTerm[]): Suggestion {
+function format_as_suggestion(terms: NarrowTerm[], is_operator_suggestion = false): Suggestion {
return {
- description_html: Filter.search_description_as_html(terms),
+ description_html: Filter.search_description_as_html(terms, is_operator_suggestion),
search_string: Filter.unparse(terms),
// TODO: This isn't actually always false. We should
// treat terms with emails (dm, sender, etc) as `is_people`
@@ -450,7 +451,8 @@ export function get_topic_suggestions_from_candidates({
// soon as we find enough matches.
const topics: string[] = [];
for (const topic of candidate_topics) {
- if (common.phrase_match(guess, topic)) {
+ const topic_display_name = util.get_final_topic_display_name(topic);
+ if (common.phrase_match(guess, topic_display_name)) {
topics.push(topic);
if (topics.length >= max_num_topics) {
break;
@@ -828,7 +830,7 @@ function get_operator_suggestions(last: NarrowTerm): Suggestion[] {
choice = "channel";
}
const op = [{operator: choice, operand: "", negated}];
- return format_as_suggestion(op);
+ return format_as_suggestion(op, true);
});
}
@@ -851,7 +853,7 @@ function suggestion_search_string(suggestion_line: SuggestionLine): string {
}
function suggestions_for_current_filter(): SuggestionLine[] {
- if (narrow_state.stream_id() && narrow_state.topic() !== "") {
+ if (narrow_state.stream_id() && narrow_state.topic() !== undefined) {
return [
get_default_suggestion_line([
{
diff --git a/web/templates/input_pill.hbs b/web/templates/input_pill.hbs
index eabe41bf56c..c8432adfc6e 100644
--- a/web/templates/input_pill.hbs
+++ b/web/templates/input_pill.hbs
@@ -13,7 +13,11 @@
<i class="zulip-icon zulip-icon-hashtag stream-privacy-type-icon" aria-hidden="true"></i>
{{~/if~}}
{{/if}}
+ {{#if is_empty_string_topic}}
+ {{sign}}topic: <span class="empty-topic-display">{{topic_display_name}}</span>
+ {{else}}
{{ display_value }}
+ {{/if}}
</span>
{{~#if should_add_guest_user_indicator}}&nbsp;<i>({{t 'guest'}})</i>{{~/if~}}
{{~#if deactivated}}&nbsp;({{t 'deactivated'}}){{~/if~}}
diff --git a/web/templates/search_description.hbs b/web/templates/search_description.hbs
index 967b2855772..ab7a48f8f76 100644
--- a/web/templates/search_description.hbs
+++ b/web/templates/search_description.hbs
@@ -3,17 +3,21 @@
{{#if (eq this.type "plain_text")~}}
{{~this.content~}}
{{else if (eq this.type "channel_topic")}}
- {{~!-- squash whitespace --~}}
- channel {{this.channel}} > {{this.topic}}
- {{~!-- squash whitespace --~}}
+ {{~#if is_empty_string_topic~}}
+ channel {{this.channel}} > <span class="empty-topic-display">{{this.topic_display_name}}</span>
+ {{~else~}}
+ channel {{this.channel}} > {{this.topic_display_name}}
+ {{~/if~}}
{{else if (eq this.type "invalid_has")}}
{{~!-- squash whitespace --~}}
invalid {{this.operand}} operand for has operator
{{~!-- squash whitespace --~}}
{{else if (eq this.type "prefix_for_operator")}}
- {{~!-- squash whitespace --~}}
+ {{~#if is_empty_string_topic~}}
+ {{this.prefix_for_operator}} <span class="empty-topic-display">{{this.operand}}</span>
+ {{~ else ~}}
{{this.prefix_for_operator}} {{this.operand}}{{#if (or (eq this.operand "link") (eq this.operand "image") (eq this.operand "attachment") (eq this.operand "reaction"))}}s{{/if}}
- {{~!-- squash whitespace --~}}
+ {{~/if~}}
{{else if (eq this.type "user_pill")}}
{{~!-- squash whitespace --~}}
{{this.operator}}
diff --git a/web/tests/filter.test.cjs b/web/tests/filter.test.cjs
index b93762a2b54..233be8a9b6d 100644
--- a/web/tests/filter.test.cjs
+++ b/web/tests/filter.test.cjs
@@ -1414,8 +1414,17 @@ test("parse", () => {
string = `channel: ${foo_stream_id} topic: "separated with space"`;
terms = [
- {operator: "channel", operand: foo_stream_id.toString()},
- {operator: "topic", operand: "separated with space"},
+ {operator: "channel", operand: ""},
+ {operator: "search", operand: foo_stream_id.toString()},
+ {operator: "topic", operand: ""},
+ {operator: "search", operand: '"separated with space"'},
+ ];
+ _test();
+
+ string = `topic: is:starred`;
+ terms = [
+ {operator: "topic", operand: ""},
+ {operator: "is", operand: "starred"},
];
_test();
});
@@ -1478,18 +1487,18 @@ test("unparse", () => {
assert_same_terms(Filter.parse(string), terms);
});
-test("describe", ({mock_template}) => {
+test("describe", ({mock_template, override}) => {
let narrow;
let string;
mock_template("search_description.hbs", true, (_data, html) => html);
narrow = [{operator: "channels", operand: "public"}];
string = "channels public";
- assert.equal(Filter.search_description_as_html(narrow), string);
+ assert.equal(Filter.search_description_as_html(narrow, false), string);
narrow = [{operator: "channels", operand: "public", negated: true}];
string = "exclude channels public";
- assert.equal(Filter.search_description_as_html(narrow), string);
+ assert.equal(Filter.search_description_as_html(narrow, false), string);
const devel_id = new_stream_id();
make_sub("devel", devel_id);
@@ -1499,7 +1508,7 @@ test("describe", ({mock_template}) => {
{operator: "is", operand: "starred"},
];
string = "channel devel, starred messages";
- assert.equal(Filter.search_description_as_html(narrow), string);
+ assert.equal(Filter.search_description_as_html(narrow, false), string);
const river_id = new_stream_id();
make_sub("river", river_id);
@@ -1508,100 +1517,100 @@ test("describe", ({mock_template}) => {
{operator: "is", operand: "unread"},
];
string = "channel river, unread messages";
- assert.equal(Filter.search_description_as_html(narrow), string);
+ assert.equal(Filter.search_description_as_html(narrow, false), string);
narrow = [
{operator: "channel", operand: devel_id.toString()},
{operator: "topic", operand: "JS"},
];
string = "channel devel > JS";
- assert.equal(Filter.search_description_as_html(narrow), string);
+ assert.equal(Filter.search_description_as_html(narrow, false), string);
narrow = [
{operator: "is", operand: "dm"},
{operator: "search", operand: "lunch"},
];
string = "direct messages, search for lunch";
- assert.equal(Filter.search_description_as_html(narrow), string);
+ assert.equal(Filter.search_description_as_html(narrow, false), string);
narrow = [{operator: "id", operand: 99}];
string = "message ID 99";
- assert.equal(Filter.search_description_as_html(narrow), string);
+ assert.equal(Filter.search_description_as_html(narrow, false), string);
narrow = [{operator: "in", operand: "home"}];
string = "messages in home";
- assert.equal(Filter.search_description_as_html(narrow), string);
+ assert.equal(Filter.search_description_as_html(narrow, false), string);
narrow = [{operator: "is", operand: "mentioned"}];
string = "@-mentions";
- assert.equal(Filter.search_description_as_html(narrow), string);
+ assert.equal(Filter.search_description_as_html(narrow, false), string);
narrow = [{operator: "is", operand: "alerted"}];
string = "alerted messages";
- assert.equal(Filter.search_description_as_html(narrow), string);
+ assert.equal(Filter.search_description_as_html(narrow, false), string);
narrow = [{operator: "is", operand: "resolved"}];
string = "topics marked as resolved";
- assert.equal(Filter.search_description_as_html(narrow), string);
+ assert.equal(Filter.search_description_as_html(narrow, false), string);
narrow = [{operator: "is", operand: "followed"}];
string = "followed topics";
- assert.equal(Filter.search_description_as_html(narrow), string);
+ assert.equal(Filter.search_description_as_html(narrow, false), string);
narrow = [{operator: "is", operand: "something_we_do_not_support"}];
string = "invalid something_we_do_not_support operand for is operator";
- assert.equal(Filter.search_description_as_html(narrow), string);
+ assert.equal(Filter.search_description_as_html(narrow, false), string);
// this should be unreachable, but just in case
narrow = [{operator: "bogus", operand: "foo"}];
string = "unknown operator";
- assert.equal(Filter.search_description_as_html(narrow), string);
+ assert.equal(Filter.search_description_as_html(narrow, false), string);
narrow = [
{operator: "channel", operand: devel_id.toString()},
{operator: "topic", operand: "JS", negated: true},
];
string = "channel devel, exclude topic JS";
- assert.equal(Filter.search_description_as_html(narrow), string);
+ assert.equal(Filter.search_description_as_html(narrow, false), string);
narrow = [
{operator: "is", operand: "dm"},
{operator: "search", operand: "lunch", negated: true},
];
string = "direct messages, exclude lunch";
- assert.equal(Filter.search_description_as_html(narrow), string);
+ assert.equal(Filter.search_description_as_html(narrow, false), string);
narrow = [
{operator: "channel", operand: devel_id.toString()},
{operator: "is", operand: "starred", negated: true},
];
string = "channel devel, exclude starred messages";
- assert.equal(Filter.search_description_as_html(narrow), string);
+ assert.equal(Filter.search_description_as_html(narrow, false), string);
narrow = [
{operator: "channel", operand: devel_id.toString()},
{operator: "has", operand: "image", negated: true},
];
string = "channel devel, exclude messages with images";
- assert.equal(Filter.search_description_as_html(narrow), string);
+ assert.equal(Filter.search_description_as_html(narrow, false), string);
narrow = [
{operator: "has", operand: "abc", negated: true},
{operator: "channel", operand: devel_id.toString()},
];
string = "invalid abc operand for has operator, channel devel";
- assert.equal(Filter.search_description_as_html(narrow), string);
+ assert.equal(Filter.search_description_as_html(narrow, false), string);
narrow = [
{operator: "has", operand: "image", negated: true},
{operator: "channel", operand: devel_id.toString()},
];
string = "exclude messages with images, channel devel";
- assert.equal(Filter.search_description_as_html(narrow), string);
+ assert.equal(Filter.search_description_as_html(narrow, false), string);
narrow = [];
string = "combined feed";
- assert.equal(Filter.search_description_as_html(narrow), string);
+ assert.equal(Filter.search_description_as_html(narrow, false), string);
// canonical version of the operator is used in description
narrow = [
@@ -1609,7 +1618,32 @@ test("describe", ({mock_template}) => {
{operator: "subject", operand: "JS", negated: true},
];
string = "channel devel, exclude topic JS";
- assert.equal(Filter.search_description_as_html(narrow), string);
+ assert.equal(Filter.search_description_as_html(narrow, false), string);
+
+ // Empty string topic involved.
+ override(realm, "realm_empty_topic_display_name", "general chat");
+ narrow = [
+ {operator: "channel", operand: devel_id.toString()},
+ {operator: "topic", operand: ""},
+ ];
+ string = 'channel devel > <span class="empty-topic-display">translated: general chat</span>';
+ assert.equal(Filter.search_description_as_html(narrow, false), string);
+
+ narrow = [
+ {operator: "topic", operand: ""},
+ {operator: "is", operand: "starred"},
+ ];
+ string =
+ 'topic <span class="empty-topic-display">translated: general chat</span>, starred messages';
+ assert.equal(Filter.search_description_as_html(narrow, false), string);
+
+ narrow = [{operator: "topic", operand: ""}];
+ string = `topic <span class="empty-topic-display">translated: general chat</span>`;
+ assert.equal(Filter.search_description_as_html(narrow, false), string);
+
+ narrow = [{operator: "topic", operand: ""}];
+ string = "topic ";
+ assert.equal(Filter.search_description_as_html(narrow, true), string);
});
test("can_bucket_by", () => {
@@ -1807,23 +1841,23 @@ test("is_valid_search_term", () => {
stream_data.add_sub(denmark);
const test_data = [
- ["has: image", true],
- ["has: nonsense", false],
- ["is: unread", true],
- ["is: nonsense", false],
- ["in: home", true],
- ["in: nowhere", false],
- ["id: 4", true],
- ["near: home", false],
- ["channel: " + denmark.stream_id, true],
- [`channel: ${invalid_sub_id}`, false],
- ["channels: public", true],
- ["channels: private", false],
- ["topic: GhostTown", true],
- ["dm-including: [email protected]", true],
- ["sender: [email protected]", false],
+ ["has:image", true],
+ ["has:nonsense", false],
+ ["is:unread", true],
+ ["is:nonsense", false],
+ ["in:home", true],
+ ["in:nowhere", false],
+ ["id:4", true],
+ ["near:home", false],
+ ["channel:" + denmark.stream_id, true],
+ [`channel:${invalid_sub_id}`, false],
+ ["channels:public", true],
+ ["channels:private", false],
+ ["topic:GhostTown", true],
+ ["dm-including:[email protected]", true],
+ ["sender:[email protected]", false],
];
for (const [search_term_string, expected_is_valid] of test_data) {
assert.equal(
diff --git a/web/tests/search_suggestion.test.cjs b/web/tests/search_suggestion.test.cjs
index 1adf03961f8..e20839276b6 100644
--- a/web/tests/search_suggestion.test.cjs
+++ b/web/tests/search_suggestion.test.cjs
@@ -1037,10 +1037,4 @@ test("queries_with_spaces", () => {
suggestions = get_suggestions(query);
expected = [`channel:${dev_help_id}`];
assert.deepEqual(suggestions.strings, expected);
-
- // test extra space after operator still works
- query = "channel: offi";
- suggestions = get_suggestions(query);
- expected = [`channel:${office_id}`];
- assert.deepEqual(suggestions.strings, expected);
});