Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
search_suggestion: Remove unused is_people logic.
Evy Kassirer0f4ba4fec1b0
diff --git a/web/src/search_suggestion.ts b/web/src/search_suggestion.tsindex 41ea1c35f71..49ea8d6d414 100644--- a/web/src/search_suggestion.ts+++ b/web/src/search_suggestion.ts@@ -1,12 +1,10 @@import Handlebars from "handlebars/runtime.js";import assert from "minimalistic-assert";-import render_user_pill from "../templates/user_pill.hbs";-import {MAX_ITEMS} from "./bootstrap_typeahead.ts";import * as common from "./common.ts";import * as direct_message_group_data from "./direct_message_group_data.ts";-import {Filter, create_user_pill_context} from "./filter.ts";+import {Filter} from "./filter.ts";import * as narrow_state from "./narrow_state.ts";import {page_params} from "./page_params.ts";import * as people from "./people.ts";@@ -29,19 +27,13 @@ export type UserPillItem = {type TermPattern = Omit<NarrowTerm, "operand"> & Partial<Pick<NarrowTerm, "operand">>;export type Suggestion = {- description_html: string;+ // When there's a single pill on a suggestion line, we have space to provide+ // help text (description_html) explaining what the operator does. If a+ // suggestion will be parsed into multiple pills, then we don't include+ // `description_html`.+ description_html: string | undefined;search_string: string;-} & (- | {- is_people: false;- }- | {- is_people: true;- users: {- user_pill_context: UserPillItem;- }[];- }-);+};export let max_num_of_search_results = MAX_ITEMS;export function rewire_max_num_of_search_results(value: typeof max_num_of_search_results): void {@@ -52,16 +44,6 @@ function channel_matches_query(channel_name: string, q: string): boolean {return common.phrase_match(q, channel_name);}-function user_pill_item(person: User): UserPillItem {- return {- id: person.user_id,- display_value: person.full_name,- has_image: true,- img_src: people.small_avatar_url_for_person(person),- should_add_guest_user_indicator: people.should_add_guest_user_indicator(person.user_id),- };-}-function match_criteria(terms: NarrowTerm[], criteria: TermPattern[]): boolean {const filter = new Filter(terms);return criteria.some((cr) => {@@ -93,10 +75,6 @@ function format_as_suggestion(terms: NarrowTerm[], is_operator_suggestion = falsreturn {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`- // and show user pills for those.- is_people: false,};}@@ -168,7 +146,7 @@ function get_channel_suggestions(last: NarrowTerm, terms: NarrowTerm[]): Suggestnegated: last.negated,};const search_string = Filter.unparse([term]);- return {description_html, search_string, is_people: false};+ return {description_html, search_string};});}@@ -247,9 +225,6 @@ function get_group_suggestions(last: NarrowTerm, terms: NarrowTerm[]): Suggestio}all_users_but_last_part.push(user);}- const user_pill_contexts = all_users_but_last_part.map((person) =>- create_user_pill_context(person),- );const person_matcher = people.build_person_matcher(last_part);let persons = people.filter_all_persons((person) => {@@ -278,13 +253,9 @@ function get_group_suggestions(last: NarrowTerm, terms: NarrowTerm[]): Suggestioterms = [{operator: "is", operand: "dm"}, term];}- const all_user_pill_contexts = [...user_pill_contexts, user_pill_item(person)];-return {description_html: prefix,search_string: Filter.unparse(terms),- is_people: true,- users: all_user_pill_contexts.map((user_pill_context) => ({user_pill_context})),};});}@@ -386,19 +357,13 @@ function get_person_suggestions(return {description_html: prefix,search_string: Filter.unparse(terms),- is_people: true,- users: [- {- user_pill_context: user_pill_item(person),- },- ],};});}function get_default_suggestion_line(terms: NarrowTerm[]): SuggestionLine {if (terms.length === 0) {- return [{description_html: "", search_string: "", is_people: false}];+ return [{description_html: "", search_string: ""}];}const suggestion_line = [];const suggestion_strings = new Set();@@ -578,6 +543,7 @@ function get_special_filter_suggestions(// suggesting negated terms.if (last.negated === true || is_search_operand_negated) {suggestions = suggestions+ .filter((suggestion) => suggestion.search_string !== "-is:resolved").map((suggestion) => {// If the search_string is "is:resolved", we want to suggest "Unresolved topics"// instead of "Exclude resolved topics".@@ -587,19 +553,13 @@ function get_special_filter_suggestions(search_string: "-" + suggestion.search_string,description_html: "unresolved topics",};- } else if (suggestion.search_string === "-is:resolved") {- return null;}return {...suggestion,search_string: "-" + suggestion.search_string,description_html: "exclude " + suggestion.description_html,};- })- .filter(- (suggestion): suggestion is SuggestionAndIncompatiblePatterns =>- suggestion !== null,- );+ });}const last_string = Filter.unparse([last]).toLowerCase();@@ -619,7 +579,7 @@ function get_special_filter_suggestions(return (s.search_string.toLowerCase().startsWith(last_string) ||show_operator_suggestions ||- s.description_html.toLowerCase().startsWith(last_string)+ s.description_html?.toLowerCase().startsWith(last_string));});const filtered_suggestions = suggestions.map(({incompatible_patterns, ...s}) => s);@@ -640,7 +600,6 @@ function get_channels_filter_suggestions(last: NarrowTerm, terms: NarrowTerm[]):{search_string,description_html,- is_people: false,incompatible_patterns: [{operator: "is", operand: "dm"},{operator: "channel"},@@ -660,7 +619,6 @@ function get_is_filter_suggestions(last: NarrowTerm, terms: NarrowTerm[]): Sugge{search_string: "is:resolved",description_html: "resolved topics",- is_people: false,incompatible_patterns: [{operator: "is", operand: "resolved"},{operator: "is", operand: "dm"},@@ -671,7 +629,6 @@ function get_is_filter_suggestions(last: NarrowTerm, terms: NarrowTerm[]): Sugge{search_string: "-is:resolved",description_html: "unresolved topics",- is_people: false,incompatible_patterns: [{operator: "is", operand: "resolved"},{operator: "is", operand: "dm"},@@ -685,7 +642,6 @@ function get_is_filter_suggestions(last: NarrowTerm, terms: NarrowTerm[]): Sugge{search_string: "is:dm",description_html: "direct messages",- is_people: false,incompatible_patterns: [{operator: "is", operand: "dm"},{operator: "is", operand: "resolved"},@@ -698,19 +654,16 @@ function get_is_filter_suggestions(last: NarrowTerm, terms: NarrowTerm[]): Sugge{search_string: "is:starred",description_html: "starred messages",- is_people: false,incompatible_patterns: [{operator: "is", operand: "starred"}],},{search_string: "is:mentioned",description_html: "messages that mention you",- is_people: false,incompatible_patterns: [{operator: "is", operand: "mentioned"}],},{search_string: "is:followed",description_html: "followed topics",- is_people: false,incompatible_patterns: [{operator: "is", operand: "followed"},{operator: "is", operand: "dm"},@@ -721,19 +674,16 @@ function get_is_filter_suggestions(last: NarrowTerm, terms: NarrowTerm[]): Sugge{search_string: "is:alerted",description_html: "alerted messages",- is_people: false,incompatible_patterns: [{operator: "is", operand: "alerted"}],},{search_string: "is:unread",description_html: "unread messages",- is_people: false,incompatible_patterns: [{operator: "is", operand: "unread"}],},{search_string: "is:muted",description_html: "muted messages",- is_people: false,incompatible_patterns: [{operator: "is", operand: "muted"},{operator: "in", operand: "home"},@@ -742,7 +692,6 @@ function get_is_filter_suggestions(last: NarrowTerm, terms: NarrowTerm[]): Sugge{search_string: "is:resolved",description_html: "resolved topics",- is_people: false,incompatible_patterns: [{operator: "is", operand: "resolved"},{operator: "is", operand: "dm"},@@ -753,7 +702,6 @@ function get_is_filter_suggestions(last: NarrowTerm, terms: NarrowTerm[]): Sugge{search_string: "-is:resolved",description_html: "unresolved topics",- is_people: false,incompatible_patterns: [{operator: "is", operand: "resolved"},{operator: "is", operand: "dm"},@@ -785,25 +733,21 @@ function get_has_filter_suggestions(last: NarrowTerm, terms: NarrowTerm[]): Sugg{search_string: "has:link",description_html: "messages with links",- is_people: false,incompatible_patterns: [{operator: "has", operand: "link"}],},{search_string: "has:image",description_html: "messages with images",- is_people: false,incompatible_patterns: [{operator: "has", operand: "image"}],},{search_string: "has:attachment",description_html: "messages with attachments",- is_people: false,incompatible_patterns: [{operator: "has", operand: "attachment"}],},{search_string: "has:reaction",description_html: "messages with reactions",- is_people: false,incompatible_patterns: [{operator: "has", operand: "reaction"}],},];@@ -840,7 +784,6 @@ function get_sent_by_me_suggestions(last: NarrowTerm, terms: NarrowTerm[]): Sugg{search_string: sender_query,description_html,- is_people: false,},];}@@ -996,33 +939,19 @@ class Attacher {const description_htmls = [];const search_strings = [];for (const suggestion of suggestion_line) {- if (suggestion.description_html !== "") {- // To be able to render multiple user pills per suggestion,- // we generate the user pill html here and concatenate it- // together with other parts of the suggestion for the- // Suggestion html.- if (suggestion.is_people) {- const user_pills_html = suggestion.users- .map((user) => render_user_pill(user))- .join(" ");- description_htmls.push(suggestion.description_html + user_pills_html);- } else {- description_htmls.push(suggestion.description_html);- }+ if (suggestion.description_html && suggestion.description_html !== "") {+ description_htmls.push(suggestion.description_html);}-if (suggestion.search_string !== "") {search_strings.push(suggestion.search_string);}}return {- description_html: description_htmls.join(", "),+ // We only display description_html for suggestion lines+ // with only one suggestion.+ description_html:+ description_htmls.length === 1 ? util.the(description_htmls) : undefined,search_string: search_strings.join(" "),- // This is a full suggestion line of multiple suggestions,- // some of which might be people, but people can be suggested- // alongside non-people, so we do the html conversion for user- // suggestions already by this point.- is_people: false,};});}@@ -1087,7 +1016,6 @@ export function get_search_result({search_string: last.operand,description_html: search_term_description_html(last),- is_people: false,},];attacher.push([...attacher.base, ...suggestion_line]);@@ -1180,8 +1108,10 @@ export function finalize_search_result(result: Suggestion[]): {lookup_table: Map<string, Suggestion>;} {for (const sug of result) {- const first = sug.description_html.charAt(0).toUpperCase();- sug.description_html = first + sug.description_html.slice(1);+ if (sug.description_html) {+ const first = sug.description_html.charAt(0).toUpperCase();+ sug.description_html = first + sug.description_html.slice(1);+ }}// Typeahead expects us to give it strings, not objects,diff --git a/web/tests/search.test.cjs b/web/tests/search.test.cjsindex 7c56d2930dd..c7856cda5b4 100644--- a/web/tests/search.test.cjs+++ b/web/tests/search.test.cjs@@ -46,17 +46,7 @@ run_test("initialize", ({override, override_rewire, mock_template}) => {const $searchbox_form = $("#searchbox_form");stub_pills();- mock_template("search_list_item.hbs", true, (data, html) => {- if (data.is_people) {- for (const user of data.users) {- assert.equal(typeof user.user_pill_context.id, "number");- assert.equal(typeof user.user_pill_context.display_value, "string");- assert.equal(typeof user.user_pill_context.has_image, "boolean");- assert.equal(typeof user.user_pill_context.img_src, "string");- }- }- return html;- });+ mock_template("search_list_item.hbs", true, (_data, html) => html);let expected_pill_display_value = "";let input_pill_displayed = false;@@ -144,57 +134,21 @@ run_test("initialize", ({override, override_rewire, mock_template}) => {"dm-including:zo",{description_html: "group direct messages including",- is_people: true,search_string: "dm-including:[email protected]",- users: [- {- user_pill_context: {- display_value: "Zoe",- has_image: true,- id: 7,- img_src:- "https://secure.gravatar.com/avatar/0f030c97ab51312c7bbffd3966198ced?d=identicon&version=1",- },- },- ],},],["dm:zo",{description_html: "direct messages with",- is_people: true,search_string: "dm:[email protected]",- users: [- {- user_pill_context: {- display_value: "Zoe",- has_image: true,- id: 7,- img_src:- "https://secure.gravatar.com/avatar/0f030c97ab51312c7bbffd3966198ced?d=identicon&version=1",- },- },- ],},],["sender:zo",{description_html: "sent by",- is_people: true,search_string: "sender:[email protected]",- users: [- {- user_pill_context: {- display_value: "Zoe",- has_image: true,- id: 7,- img_src:- "https://secure.gravatar.com/avatar/0f030c97ab51312c7bbffd3966198ced?d=identicon&version=1",- },- },- ],},],[diff --git a/web/tests/search_suggestion.test.cjs b/web/tests/search_suggestion.test.cjsindex d32b616995b..0d69d5c440f 100644--- a/web/tests/search_suggestion.test.cjs+++ b/web/tests/search_suggestion.test.cjs@@ -160,7 +160,6 @@ test("subset_suggestions", ({mock_template}) => {test("dm_suggestions", ({override, mock_template}) => {mock_template("search_description.hbs", true, (_data, html) => html);- mock_template("user_pill.hbs", true, (_data, html) => html);let query = "is:dm";let suggestions = get_suggestions(query);@@ -300,7 +299,6 @@ test("dm_suggestions", ({override, mock_template}) => {test("group_suggestions", ({mock_template}) => {mock_template("search_description.hbs", true, (_data, html) => html);- mock_template("user_pill.hbs", true, (_data, html) => html);// If there's an existing completed user pill right before// the input string, we suggest a user group as one of the@@ -489,7 +487,6 @@ test("has_suggestions", ({override, mock_template}) => {test("check_is_suggestions", ({override, mock_template}) => {mock_template("search_description.hbs", true, (_data, html) => html);- mock_template("user_pill.hbs", true, (_data, html) => html);override(narrow_state, "stream_id", noop);@@ -662,7 +659,6 @@ test("sent_by_me_suggestions", ({override, mock_template}) => {test("topic_suggestions", ({override, mock_template}) => {mock_template("search_description.hbs", true, (_data, html) => html);- mock_template("user_pill.hbs", true, (_data, html) => html);let suggestions;let expected;@@ -856,7 +852,6 @@ test("channel_completion", ({override}) => {test("people_suggestions", ({override, mock_template}) => {mock_template("search_description.hbs", true, (_data, html) => html);- mock_template("user_pill.hbs", true, (_data, html) => html);let query = "te";@@ -933,20 +928,6 @@ test("people_suggestions", ({override, mock_template}) => {];assert.deepEqual(suggestions.strings, expected);- function is_person(q) {- return suggestions.lookup_table.get(q).description_html.includes(`class="user-pill`);- }- assert.equal(is_person("dm:[email protected]"), true);- assert.equal(is_person("sender:[email protected]"), true);- assert.equal(is_person("dm-including:[email protected]"), true);-- function has_image(q) {- return suggestions.lookup_table.get(q).description_html.includes(`class="pill-image"`);- }- assert.equal(has_image("dm:[email protected]"), true);- assert.equal(has_image("sender:[email protected]"), true);- assert.equal(has_image("dm-including:[email protected]"), true);-function test_describe(q, description_html_start) {assert.ok(suggestions.lookup_table.get(q).description_html.startsWith(description_html_start),@@ -974,37 +955,6 @@ test("people_suggestions", ({override, mock_template}) => {test_avatar_url("sender:[email protected]", expectedString);test_avatar_url("dm-including:[email protected]", expectedString);- const guest_html = "<i>(guest)</i>";- function test_guest_user_indicator(q, should_have_guest_indicator) {- const description_html = suggestions.lookup_table.get(q).description_html;- if (should_have_guest_indicator) {- assert.ok(description_html.includes(guest_html));- } else {- assert.ok(!description_html.includes(guest_html));- }- }-- override(realm, "realm_enable_guest_user_indicator", true);- suggestions = get_suggestions(query);-- test_guest_user_indicator("dm:[email protected]", false);- test_guest_user_indicator("sender:[email protected]", false);- test_guest_user_indicator("dm-including:[email protected]", false);-- bob.is_guest = true;- suggestions = get_suggestions(query);-- test_guest_user_indicator("dm:[email protected]", true);- test_guest_user_indicator("sender:[email protected]", true);- test_guest_user_indicator("dm-including:[email protected]", true);-- override(realm, "realm_enable_guest_user_indicator", false);- suggestions = get_suggestions(query);-- test_guest_user_indicator("dm:[email protected]", false);- test_guest_user_indicator("sender:[email protected]", false);- test_guest_user_indicator("dm-including:[email protected]", false);-suggestions = get_suggestions("Ted "); // note spaceexpected = ["Ted", "dm:[email protected]", "sender:[email protected]", "dm-including:[email protected]"];