message_fetch: Extract function to modify narrow before server query.

This commit is contained in:
Aman Agrawal
2025-01-23 18:09:56 +05:30
committed by Tim Abbott
parent a9b7b0e692
commit 673281aae8

View File

@@ -317,6 +317,30 @@ function handle_operators_supporting_id_based_api(narrow_parameter: string): str
return JSON.stringify(narrow_terms);
}
function get_narrow_for_message_fetch(filter: Filter): string {
let narrow_data = filter.public_terms();
if (page_params.narrow !== undefined) {
narrow_data = [...narrow_data, ...page_params.narrow];
}
if (page_params.is_spectator) {
const web_public_narrow: NarrowTerm[] = [
{operator: "channels", operand: "web-public", negated: false},
];
// This logic is not ideal in that, in theory, an existing `channels`
// operator could be present, but not in a useful way. We don't attempt
// to validate the narrow is compatible with spectators here; the server
// will return an error if appropriate.
narrow_data = [...narrow_data, ...web_public_narrow];
}
let narrow_param_string = "";
if (narrow_data.length > 0) {
narrow_param_string = JSON.stringify(narrow_data);
narrow_param_string = handle_operators_supporting_id_based_api(narrow_param_string);
}
return narrow_param_string;
}
function get_parameters_for_message_fetch_api(opts: MessageFetchOptions): MessageFetchAPIParams {
if (typeof opts.anchor === "number") {
// Messages that have been locally echoed messages have
@@ -338,23 +362,9 @@ function get_parameters_for_message_fetch_api(opts: MessageFetchOptions): Messag
blueslip.error("Message list data is undefined!");
}
let narrow_data = msg_list_data.filter.public_terms();
if (page_params.narrow !== undefined) {
narrow_data = [...narrow_data, ...page_params.narrow];
}
if (page_params.is_spectator) {
const web_public_narrow: NarrowTerm[] = [
{operator: "channels", operand: "web-public", negated: false},
];
// This logic is not ideal in that, in theory, an existing `channels`
// operator could be present, but not in a useful way. We don't attempt
// to validate the narrow is compatible with spectators here; the server
// will return an error if appropriate.
narrow_data = [...narrow_data, ...web_public_narrow];
}
if (narrow_data.length > 0) {
const narrow_param_string = JSON.stringify(narrow_data);
data.narrow = handle_operators_supporting_id_based_api(narrow_param_string);
const narrow = get_narrow_for_message_fetch(msg_list_data.filter);
if (narrow !== "") {
data.narrow = narrow;
}
return data;
}