Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

message_fetch: Extract function to modify narrow before server query.

Aman Agrawal673281aae876
diff --git a/web/src/message_fetch.ts b/web/src/message_fetch.ts
index 01727ff4261..b2af4d33710 100644
--- a/web/src/message_fetch.ts
+++ b/web/src/message_fetch.ts
@@ -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;
}