filter: Only parse "dm" `operand` emails once.

This commit is contained in:
Aman Agrawal 2024-08-05 12:16:45 +00:00
parent c8924a75b5
commit 71a77eb9c5
1 changed files with 18 additions and 8 deletions

View File

@ -23,6 +23,10 @@ import * as unread from "./unread";
import * as user_topics from "./user_topics"; import * as user_topics from "./user_topics";
import * as util from "./util"; import * as util from "./util";
type MessageMatchesSearchTermOpts = {
operand_ids?: number[];
};
type IconData = { type IconData = {
title: string; title: string;
is_spectator: boolean; is_spectator: boolean;
@ -148,7 +152,12 @@ function message_in_home(message: Message): boolean {
); );
} }
function message_matches_search_term(message: Message, operator: string, operand: string): boolean { function message_matches_search_term(
message: Message,
operator: string,
operand: string,
opts: MessageMatchesSearchTermOpts,
): boolean {
switch (operator) { switch (operator) {
case "has": case "has":
switch (operand) { switch (operand) {
@ -233,20 +242,16 @@ function message_matches_search_term(message: Message, operator: string, operand
return people.id_matches_email_operand(message.sender_id, operand); return people.id_matches_email_operand(message.sender_id, operand);
case "dm": { case "dm": {
// TODO: use user_ids, not emails here
if (message.type !== "private") { if (message.type !== "private") {
return false; return false;
} }
const operand_ids = people.pm_with_operand_ids(operand);
if (!operand_ids) {
return false;
}
const user_ids = people.pm_with_user_ids(message); const user_ids = people.pm_with_user_ids(message);
if (!user_ids) { if (!user_ids) {
return false; return false;
} }
return _.isEqual(operand_ids, user_ids); return _.isEqual(opts.operand_ids, user_ids);
} }
case "dm-including": { case "dm-including": {
@ -1562,17 +1567,22 @@ export class Filter {
// Make a shallow copy to avoid modifying the original terms. // Make a shallow copy to avoid modifying the original terms.
const terms = this._terms.map((term) => ({...term})); const terms = this._terms.map((term) => ({...term}));
const opts: MessageMatchesSearchTermOpts = {};
// Since we are doing a case-insensitive match, we need to // Since we are doing a case-insensitive match, we need to
// ensure that the operand is in lowercase. // ensure that the operand is in lowercase.
for (const term of terms) { for (const term of terms) {
if (term.operand !== undefined) { if (term.operand !== undefined) {
term.operand = term.operand.toLowerCase(); term.operand = term.operand.toLowerCase();
} }
if (term.operator === "dm") {
opts.operand_ids = people.pm_with_operand_ids(term.operand) ?? [];
}
} }
return (message: Message) => return (message: Message) =>
terms.every((term) => { terms.every((term) => {
let ok = message_matches_search_term(message, term.operator, term.operand); let ok = message_matches_search_term(message, term.operator, term.operand, opts);
if (term.negated) { if (term.negated) {
ok = !ok; ok = !ok;
} }