mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	filter: Validate and parse sender:me when turning into a pill.
Fixes #31315. We want to parse sender:me with the email when turning it into a pill, but not before then so that "Sent by me" is still the search string in the suggestions.
This commit is contained in:
		@@ -453,7 +453,7 @@ export class Filter {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Parse a string into a list of terms (see below).
 | 
			
		||||
    static parse(str: string): NarrowTerm[] {
 | 
			
		||||
    static parse(str: string, for_pills = false): NarrowTerm[] {
 | 
			
		||||
        const terms: NarrowTerm[] = [];
 | 
			
		||||
        let search_term: string[] = [];
 | 
			
		||||
        let negated;
 | 
			
		||||
@@ -511,6 +511,14 @@ export class Filter {
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                if (
 | 
			
		||||
                    for_pills &&
 | 
			
		||||
                    operator === "sender" &&
 | 
			
		||||
                    operand.toString().toLowerCase() === "me"
 | 
			
		||||
                ) {
 | 
			
		||||
                    operand = people.my_current_email();
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                // We use Filter.operator_to_prefix() to check if the
 | 
			
		||||
                // operator is known.  If it is not known, then we treat
 | 
			
		||||
                // it as a search for the given string (which may contain
 | 
			
		||||
@@ -575,6 +583,9 @@ export class Filter {
 | 
			
		||||
            case "pm-with":
 | 
			
		||||
            case "dm-including":
 | 
			
		||||
            case "pm-including":
 | 
			
		||||
                if (term.operand === "me") {
 | 
			
		||||
                    return true;
 | 
			
		||||
                }
 | 
			
		||||
                return term.operand
 | 
			
		||||
                    .split(",")
 | 
			
		||||
                    .every((email) => people.get_by_email(email) !== undefined);
 | 
			
		||||
 
 | 
			
		||||
@@ -53,7 +53,7 @@ function full_search_query_in_terms(): NarrowTerm[] {
 | 
			
		||||
    assert(search_pill_widget !== null);
 | 
			
		||||
    return [
 | 
			
		||||
        ...search_pill.get_current_search_pill_terms(search_pill_widget),
 | 
			
		||||
        ...Filter.parse(get_search_bar_text()),
 | 
			
		||||
        ...Filter.parse(get_search_bar_text(), true),
 | 
			
		||||
    ];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1405,8 +1405,8 @@ test("parse", () => {
 | 
			
		||||
    let string;
 | 
			
		||||
    let terms;
 | 
			
		||||
 | 
			
		||||
    function _test() {
 | 
			
		||||
        const result = Filter.parse(string);
 | 
			
		||||
    function _test(for_pills = false) {
 | 
			
		||||
        const result = Filter.parse(string, for_pills);
 | 
			
		||||
        assert_same_terms(result, terms);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -1444,6 +1444,14 @@ test("parse", () => {
 | 
			
		||||
    terms = [{operator: "sender", operand: "leo+test@zulip.com"}];
 | 
			
		||||
    _test();
 | 
			
		||||
 | 
			
		||||
    string = "sender:me";
 | 
			
		||||
    terms = [{operator: "sender", operand: `${me.email}`}];
 | 
			
		||||
    _test(true);
 | 
			
		||||
 | 
			
		||||
    string = "-sender:me";
 | 
			
		||||
    terms = [{operator: "sender", operand: `${me.email}`, negated: true}];
 | 
			
		||||
    _test(true);
 | 
			
		||||
 | 
			
		||||
    string = "https://www.google.com";
 | 
			
		||||
    terms = [{operator: "search", operand: "https://www.google.com"}];
 | 
			
		||||
    _test();
 | 
			
		||||
@@ -1964,6 +1972,7 @@ test("is_valid_search_term", () => {
 | 
			
		||||
        ["topic:GhostTown", true],
 | 
			
		||||
        ["dm-including:alice@example.com", true],
 | 
			
		||||
        ["sender:ghost@zulip.com", false],
 | 
			
		||||
        ["sender:me", true],
 | 
			
		||||
        ["dm:alice@example.com,ghost@example.com", false],
 | 
			
		||||
        ["dm:alice@example.com,joe@example.com", true],
 | 
			
		||||
    ];
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user