From 574a87b87db60c494ad79bf60fa49d8164159ec3 Mon Sep 17 00:00:00 2001 From: palashb01 Date: Sat, 19 Aug 2023 04:22:25 +0530 Subject: [PATCH] search: Update search bar to display title when used sender:. This commit introduces a new behavior for the search bar. Now, if the search keyword is 'sender:', it displays the title of the user/me. for example: sender:me title: Messages sent by you sender: Palash title: Messages sent by Palash for the implementation, we already have the check for 'dm:', which is similar to this so just added a new if check for 'sender:' and return the title accordingly. Fixes: #18690 --- web/src/filter.js | 20 ++++++++++++++++++++ web/src/narrow.js | 16 ++++++++++++++++ web/tests/filter.test.js | 12 ++++++++++-- web/tests/narrow.test.js | 2 +- 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/web/src/filter.js b/web/src/filter.js index 47eabad331..a9ebd7662d 100644 --- a/web/src/filter.js +++ b/web/src/filter.js @@ -575,6 +575,9 @@ export class Filter { if (_.isEqual(term_types, ["streams-public"])) { return true; } + if (_.isEqual(term_types, ["sender"])) { + return true; + } return false; } @@ -714,6 +717,23 @@ export class Filter { // can have the same return type as other cases. return names.join(", "); } + if (term_types.length === 1 && _.isEqual(term_types, ["sender"])) { + const email = this.operands("sender")[0]; + const user = people.get_by_email(email); + let sender = email; + if (user) { + if (people.is_my_user_id(user.user_id)) { + return $t({defaultMessage: "Messages sent by you"}); + } + sender = user.full_name; + } + return $t( + {defaultMessage: "Messages sent by {sender}"}, + { + sender, + }, + ); + } if (term_types.length === 1) { switch (term_types[0]) { case "in-home": diff --git a/web/src/narrow.js b/web/src/narrow.js index ee50213338..7dba6791fa 100644 --- a/web/src/narrow.js +++ b/web/src/narrow.js @@ -120,6 +120,22 @@ export function compute_narrow_title(filter) { return $t({defaultMessage: "Invalid user"}); } + if (filter.has_operator("sender")) { + const user = people.get_by_email(filter.operands("sender")[0]); + if (user) { + if (people.is_my_user_id(user.user_id)) { + return $t({defaultMessage: "Messages sent by you"}); + } + return $t( + {defaultMessage: "Messages sent by {sender}"}, + { + sender: user.full_name, + }, + ); + } + return $t({defaultMessage: "Invalid user"}); + } + return filter_title; } diff --git a/web/tests/filter.test.js b/web/tests/filter.test.js index d8aaa149f0..48a4b96196 100644 --- a/web/tests/filter.test.js +++ b/web/tests/filter.test.js @@ -1522,6 +1522,7 @@ test("navbar_helpers", () => { test_redirect_url_with_search(test_case); } + const sender = [{operator: "sender", operand: joe.email}]; const in_home = [{operator: "in", operand: "home"}]; const in_all = [{operator: "in", operand: "all"}]; const is_starred = [{operator: "is", operand: "starred"}]; @@ -1563,6 +1564,13 @@ test("navbar_helpers", () => { ]; const test_cases = [ + { + operator: sender, + is_common_narrow: true, + icon: undefined, + title: "translated: Messages sent by " + joe.full_name, + redirect_url_with_search: "/#narrow/sender/" + joe.user_id + "-joe", + }, { operator: is_starred, is_common_narrow: true, @@ -1722,12 +1730,12 @@ test("navbar_helpers", () => { { operator: sender_me, redirect_url_with_search: "/#narrow/sender/" + me.user_id + "-Me-Myself", - is_common_narrow: false, + is_common_narrow: true, }, { operator: sender_joe, redirect_url_with_search: "/#narrow/sender/" + joe.user_id + "-joe", - is_common_narrow: false, + is_common_narrow: true, }, ]; diff --git a/web/tests/narrow.test.js b/web/tests/narrow.test.js index ebf41285a9..fb232797e7 100644 --- a/web/tests/narrow.test.js +++ b/web/tests/narrow.test.js @@ -786,7 +786,7 @@ run_test("narrow_compute_title", ({override}) => { assert.equal(narrow.compute_narrow_title(filter), "translated: Search results"); filter = new Filter([{operator: "sender", operand: "me"}]); - assert.equal(narrow.compute_narrow_title(filter), "translated: Search results"); + assert.equal(narrow.compute_narrow_title(filter), "translated: Messages sent by you"); // Stream narrows const sub = {