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: Use search terms instead of query text for some helper code.

evykassirerc09dbcc781be
diff --git a/web/src/search.ts b/web/src/search.ts
index 57c2f1ede44..31e5c6253ed 100644
--- a/web/src/search.ts
+++ b/web/src/search.ts
@@ -39,14 +39,12 @@ type NarrowSearchOptions = {
type OnNarrowSearch = (terms: NarrowTerm[], options: NarrowSearchOptions) => void;
-function full_search_query_in_text(): string {
+function full_search_query_in_terms(): NarrowTerm[] {
assert(search_pill_widget !== null);
return [
- search_pill.get_current_search_string_for_widget(search_pill_widget),
- get_search_bar_text(),
- ]
- .join(" ")
- .trim();
+ ...search_pill.get_current_search_pill_terms(search_pill_widget),
+ ...Filter.parse(get_search_bar_text()),
+ ];
}
function narrow_or_search_for_term({on_narrow_search}: {on_narrow_search: OnNarrowSearch}): string {
@@ -57,12 +55,11 @@ function narrow_or_search_for_term({on_narrow_search}: {on_narrow_search: OnNarr
return get_search_bar_text();
}
- const search_query = full_search_query_in_text();
- if (search_query === "") {
+ const terms = full_search_query_in_terms();
+ if (terms.length === 0) {
exit_search({keep_search_narrow_open: true});
return "";
}
- const terms = Filter.parse(search_query);
// Reset the search bar to display as many pills as possible for `terms`.
// We do this in case some of these terms haven't been pillified yet
// because convert_to_pill_on_enter is false.
@@ -98,8 +95,7 @@ function narrow_to_search_contents_with_search_bar_open(): void {
if (!validate_text_terms()) {
return;
}
- const search_query = full_search_query_in_text();
- const terms = Filter.parse(search_query);
+ const terms = full_search_query_in_terms();
on_narrow_search(terms, {trigger: "search"});
// We want to keep the search bar open here, not show the
@@ -165,13 +161,12 @@ export function initialize(opts: {on_narrow_search: OnNarrowSearch}): void {
search_input_has_changed = true;
}
assert(search_pill_widget !== null);
- const query_from_pills =
- search_pill.get_current_search_string_for_widget(search_pill_widget);
+ const pill_terms = search_pill.get_current_search_pill_terms(search_pill_widget);
const add_current_filter =
- query_from_pills === "" && narrow_state.filter() !== undefined;
+ pill_terms.length === 0 && narrow_state.filter() !== undefined;
const suggestions = search_suggestion.get_suggestions(
- query_from_pills,
- query,
+ pill_terms,
+ Filter.parse(query),
add_current_filter,
);
// Update our global search_map hash
diff --git a/web/src/search_pill.ts b/web/src/search_pill.ts
index 483159002de..62829fece6a 100644
--- a/web/src/search_pill.ts
+++ b/web/src/search_pill.ts
@@ -1,4 +1,5 @@
import $ from "jquery";
+import assert from "minimalistic-assert";
import {Filter} from "./filter";
import * as input_pill from "./input_pill";
@@ -34,6 +35,9 @@ type SearchPill =
type: "search";
display_value: string;
description_html: string;
+ operator: string;
+ operand: string;
+ negated: boolean | undefined;
}
| SearchUserPill;
@@ -41,7 +45,9 @@ export type SearchPillWidget = InputPillContainer<SearchPill>;
export function create_item_from_search_string(search_string: string): SearchPill | undefined {
const search_terms = Filter.parse(search_string);
- if (!search_terms.every((term) => Filter.is_valid_search_term(term))) {
+ assert(search_terms.length === 1);
+ const search_term = search_terms[0]!;
+ if (!Filter.is_valid_search_term(search_term)) {
// This will cause pill validation to fail and trigger a shake animation.
return undefined;
}
@@ -50,6 +56,9 @@ export function create_item_from_search_string(search_string: string): SearchPil
display_value: search_string,
type: "search",
description_html,
+ operator: search_term.operator,
+ operand: search_term.operand,
+ negated: search_term.negated,
};
}
@@ -156,8 +165,16 @@ export function set_search_bar_contents(
}
}
-export function get_current_search_string_for_widget(pill_widget: SearchPillWidget): string {
- const items = pill_widget.items();
- const search_strings = items.map((item) => item.display_value);
- return search_strings.join(" ");
+function get_search_operand(item: SearchPill): string {
+ if (item.type === "search_user") {
+ return item.users.map((user) => user.email).join(",");
+ }
+ return item.operand;
+}
+
+export function get_current_search_pill_terms(pill_widget: SearchPillWidget): NarrowTerm[] {
+ return pill_widget.items().map((item) => ({
+ ...item,
+ operand: get_search_operand(item),
+ }));
}
diff --git a/web/src/search_suggestion.ts b/web/src/search_suggestion.ts
index 978d00d1e5a..e9e4a2ba46a 100644
--- a/web/src/search_suggestion.ts
+++ b/web/src/search_suggestion.ts
@@ -971,8 +971,8 @@ class Attacher {
}
export function get_search_result(
- query_from_pills: string,
- query_from_text: string,
+ pill_search_terms: NarrowTerm[],
+ text_search_terms: NarrowTerm[],
add_current_filter = false,
): Suggestion[] {
let suggestion_line: SuggestionLine;
@@ -980,8 +980,6 @@ export function get_search_result(
// search_terms correspond to the terms for the query in the input.
// This includes the entire query entered in the searchbox.
// terms correspond to the terms for the entire query entered in the searchbox.
- const pill_search_terms = Filter.parse(query_from_pills);
- const text_search_terms = Filter.parse(query_from_text);
let all_search_terms = [...pill_search_terms, ...text_search_terms];
// `last` will always be a text term, not a pill term. If there is no
@@ -1101,14 +1099,14 @@ export function get_search_result(
}
export function get_suggestions(
- query_from_pills: string,
- query_from_text: string,
+ pill_search_terms: NarrowTerm[],
+ text_search_terms: NarrowTerm[],
add_current_filter = false,
): {
strings: string[];
lookup_table: Map<string, Suggestion>;
} {
- const result = get_search_result(query_from_pills, query_from_text, add_current_filter);
+ const result = get_search_result(pill_search_terms, text_search_terms, add_current_filter);
return finalize_search_result(result);
}
diff --git a/web/tests/search_suggestion.test.js b/web/tests/search_suggestion.test.js
index 29d360126af..3a327325b39 100644
--- a/web/tests/search_suggestion.test.js
+++ b/web/tests/search_suggestion.test.js
@@ -11,6 +11,7 @@ const stream_topic_history_util = mock_esm("../src/stream_topic_history_util");
const direct_message_group_data = zrequire("direct_message_group_data");
+const {Filter} = zrequire("filter");
const stream_data = zrequire("stream_data");
const stream_topic_history = zrequire("stream_topic_history");
const people = zrequire("people");
@@ -67,7 +68,7 @@ function init() {
}
function get_suggestions(query, pill_query = "") {
- return search.get_suggestions(pill_query, query);
+ return search.get_suggestions(Filter.parse(pill_query), Filter.parse(query));
}
function test(label, f) {