message_fetch: Avoid variable reuse in narrow transformation logic.

This commit is contained in:
afeefuddin 2024-07-02 20:45:43 +05:30 committed by Tim Abbott
parent b41a17a1d0
commit afe405e0ab
1 changed files with 22 additions and 18 deletions

View File

@ -194,39 +194,43 @@ function handle_operators_supporting_id_based_api(data) {
return data; return data;
} }
data.narrow = JSON.parse(data.narrow); const parsed_narrow_data = JSON.parse(data.narrow);
data.narrow = data.narrow.map((filter) => {
if (operators_supporting_ids.has(filter.operator)) { const narrow_terms = [];
filter.operand = people.emails_strings_to_user_ids_array(filter.operand); for (const raw_term of parsed_narrow_data) {
const narrow_term = raw_term;
if (operators_supporting_ids.has(raw_term.operator)) {
narrow_term.operand = people.emails_strings_to_user_ids_array(raw_term.operand);
} }
if (operators_supporting_id.has(filter.operator)) { if (operators_supporting_id.has(raw_term.operator)) {
if (filter.operator === "id") { if (raw_term.operator === "id") {
// The message ID may not exist locally, // The message ID may not exist locally,
// so send the filter to the server as is. // so send the term to the server as is.
return filter; narrow_terms.push(narrow_term);
continue;
} }
if (filter.operator === "stream") { if (raw_term.operator === "stream") {
const stream_id = stream_data.get_stream_id(filter.operand); const stream_id = stream_data.get_stream_id(raw_term.operand);
if (stream_id !== undefined) { if (stream_id !== undefined) {
filter.operand = stream_id; narrow_term.operand = stream_id;
} }
return filter; narrow_terms.push(narrow_term);
continue;
} }
// The other operands supporting object IDs all work with user objects. // The other operands supporting object IDs all work with user objects.
const person = people.get_by_email(filter.operand); const person = people.get_by_email(raw_term.operand);
if (person !== undefined) { if (person !== undefined) {
filter.operand = person.user_id; narrow_term.operand = person.user_id;
} }
} }
narrow_terms.push(narrow_term);
}
return filter; data.narrow = JSON.stringify(narrow_terms);
});
data.narrow = JSON.stringify(data.narrow);
return data; return data;
} }