message-fetch: Use canonical operator for ID based API updates.

Updates handle_operators_supporting_id_based_api to use the
canonical operator when updating the narrow parameter to use
object IDs.
This commit is contained in:
Lauryn Menard
2024-10-11 17:25:13 +02:00
committed by Tim Abbott
parent 62c2f35995
commit 3405e95b6b

View File

@@ -8,6 +8,7 @@ import * as channel from "./channel";
import * as compose_closed_ui from "./compose_closed_ui"; import * as compose_closed_ui from "./compose_closed_ui";
import * as compose_recipient from "./compose_recipient"; import * as compose_recipient from "./compose_recipient";
import * as direct_message_group_data from "./direct_message_group_data"; import * as direct_message_group_data from "./direct_message_group_data";
import {Filter} from "./filter";
import * as message_feed_loading from "./message_feed_loading"; import * as message_feed_loading from "./message_feed_loading";
import * as message_feed_top_notices from "./message_feed_top_notices"; import * as message_feed_top_notices from "./message_feed_top_notices";
import * as message_helper from "./message_helper"; import * as message_helper from "./message_helper";
@@ -219,15 +220,10 @@ function get_messages_success(data: MessageFetchResponse, opts: MessageFetchOpti
// doing so breaks the app in various modules that expect a string of // doing so breaks the app in various modules that expect a string of
// user emails. // user emails.
function handle_operators_supporting_id_based_api(narrow_parameter: string): string { function handle_operators_supporting_id_based_api(narrow_parameter: string): string {
const operators_supporting_ids = new Set(["dm", "pm-with"]); // We use the canonical operator when checking these sets, so legacy
const operators_supporting_id = new Set([ // operators, such as "pm-with" and "stream", are not included here.
"id", const operators_supporting_ids = new Set(["dm"]);
"stream", const operators_supporting_id = new Set(["id", "channel", "sender", "dm-including"]);
"sender",
"group-pm-with",
"dm-including",
]);
const parsed_narrow_data = z.array(narrow_term_schema).parse(JSON.parse(narrow_parameter)); const parsed_narrow_data = z.array(narrow_term_schema).parse(JSON.parse(narrow_parameter));
const narrow_terms: { const narrow_terms: {
@@ -242,21 +238,23 @@ function handle_operators_supporting_id_based_api(narrow_parameter: string): str
negated?: boolean | undefined; negated?: boolean | undefined;
} = raw_term; } = raw_term;
if (operators_supporting_ids.has(raw_term.operator)) { const canonical_operator = Filter.canonicalize_operator(raw_term.operator);
if (operators_supporting_ids.has(canonical_operator)) {
const user_ids_array = people.emails_strings_to_user_ids_array(raw_term.operand); const user_ids_array = people.emails_strings_to_user_ids_array(raw_term.operand);
assert(user_ids_array !== undefined); assert(user_ids_array !== undefined);
narrow_term.operand = user_ids_array; narrow_term.operand = user_ids_array;
} }
if (operators_supporting_id.has(raw_term.operator)) { if (operators_supporting_id.has(canonical_operator)) {
if (raw_term.operator === "id") { if (canonical_operator === "id") {
// The message ID may not exist locally, // The message ID may not exist locally,
// so send the term to the server as is. // so send the term to the server as is.
narrow_terms.push(narrow_term); narrow_terms.push(narrow_term);
continue; continue;
} }
if (raw_term.operator === "stream") { if (canonical_operator === "channel") {
const stream_id = stream_data.get_stream_id(raw_term.operand); const stream_id = stream_data.get_stream_id(raw_term.operand);
if (stream_id !== undefined) { if (stream_id !== undefined) {
narrow_term.operand = stream_id; narrow_term.operand = stream_id;
@@ -266,7 +264,8 @@ function handle_operators_supporting_id_based_api(narrow_parameter: string): str
continue; continue;
} }
// The other operands supporting object IDs all work with user objects. // The other operands supporting integer IDs all work with
// a single user object.
const person = people.get_by_email(raw_term.operand); const person = people.get_by_email(raw_term.operand);
if (person !== undefined) { if (person !== undefined) {
narrow_term.operand = person.user_id; narrow_term.operand = person.user_id;