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.

filter: Add helper for reading message ID operands.

Aman Agrawalf1466a8ce802
diff --git a/web/src/filter.ts b/web/src/filter.ts
index ac9f8ee8795..a3bc5140dfa 100644
--- a/web/src/filter.ts
+++ b/web/src/filter.ts
@@ -1183,6 +1183,16 @@ export class Filter {
);
}
+ // Returns the message ID named by an operator whose operand is a
+ // message ID, or undefined if the filter has no such term.
+ message_id_operand(operator: "id" | "near" | "with"): number | undefined {
+ const term = this.terms_with_operator(operator)[0];
+ if (term === undefined) {
+ return undefined;
+ }
+ return Number.parseInt(term.operand, 10);
+ }
+
has_negated_operand(operator: string, operand: string): boolean {
return this._terms.some(
(term) => term.negated && term.operator === operator && term.operand === operand,
@@ -2062,8 +2072,8 @@ export class Filter {
}
if (!message) {
- const message_id = Number.parseInt(this.terms_with_operator("with")[0]!.operand, 10);
- message = message_store.get(message_id);
+ const with_message_id = this.message_id_operand("with")!;
+ message = message_store.get(with_message_id);
}
if (!message) {
diff --git a/web/src/message_events.ts b/web/src/message_events.ts
index cb2353b05b6..abbceeca0f0 100644
--- a/web/src/message_events.ts
+++ b/web/src/message_events.ts
@@ -786,10 +786,7 @@ export function update_messages(events: UpdateMessageEvent[]): void {
//
// If the `with` message was moved, we need to update the URL to
// use a message from the old topic.
- const message_id = Number.parseInt(
- current_filter.terms_with_operator("with")[0]!.operand,
- 10,
- );
+ const message_id = current_filter.message_id_operand("with")!;
if (event.message_ids.includes(message_id)) {
// At this point, we know that the `with` message was moved.
if (!is_old_topic_empty_locally) {
diff --git a/web/src/message_view.ts b/web/src/message_view.ts
index 9caad43ac99..9ac1aa1de6b 100644
--- a/web/src/message_view.ts
+++ b/web/src/message_view.ts
@@ -370,12 +370,13 @@ export function try_rendering_locally_for_same_narrow(
return false;
}
+ const near_message_id = filter.message_id_operand("near");
let target_id;
if (opts.then_select_id !== undefined) {
target_id = opts.then_select_id;
target_scroll_offset = opts.then_select_offset;
- } else if (filter.has_operator("near")) {
- target_id = Number.parseInt(filter.terms_with_operator("near")[0]!.operand, 10);
+ } else if (near_message_id !== undefined) {
+ target_id = near_message_id;
} else if (filter.equals(current_filter)) {
// The caller doesn't want to force rerender and the filter is the same.
// Also, we don't have a specific message id we want to select, so we
@@ -558,13 +559,9 @@ export let show = (raw_terms: NarrowTerm[], show_opts: ShowMessageViewOpts): voi
const terms = filter.terms();
// These two narrowing operators specify what message should be
- // selected and should be the center of the narrow.
- if (filter.has_operator("near")) {
- id_info.target_id = Number.parseInt(filter.terms_with_operator("near")[0]!.operand, 10);
- }
- if (filter.has_operator("id")) {
- id_info.target_id = Number.parseInt(filter.terms_with_operator("id")[0]!.operand, 10);
- }
+ // selected and should be the center of the narrow; `id` wins
+ // if a filter somehow has both.
+ id_info.target_id = filter.message_id_operand("id") ?? filter.message_id_operand("near");
if (
// Filter has `with` operator but we don't have message locally.
@@ -577,7 +574,7 @@ export let show = (raw_terms: NarrowTerm[], show_opts: ShowMessageViewOpts): voi
// from server first and then try to select `then_select_id` message.
// There is no risk of this hack causing any issues since the `id_info`
// will be reset after we fetch the `with` operator message.
- id_info.target_id = Number.parseInt(filter.terms_with_operator("with")[0]!.operand, 10);
+ id_info.target_id = filter.message_id_operand("with")!;
}
// Narrow with near / id operator. There are two possibilities: