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
This commit is contained in:
palashb01 2023-08-19 04:22:25 +05:30 committed by Tim Abbott
parent 3a4cb692fb
commit 574a87b87d
4 changed files with 47 additions and 3 deletions

View File

@ -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":

View File

@ -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;
}

View File

@ -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,
},
];

View File

@ -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 = {