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_events: Fix moved messages not rendered if they are not cached.

Aman Agrawal7b9d10ae5ded
diff --git a/web/src/filter.ts b/web/src/filter.ts
index e226dbeb558..6ed96625d2a 100644
--- a/web/src/filter.ts
+++ b/web/src/filter.ts
@@ -917,6 +917,15 @@ export class Filter {
);
}
+ has_operand_case_insensitive(operator: string, operand: string): boolean {
+ return this._terms.some(
+ (term) =>
+ !term.negated &&
+ term.operator === operator &&
+ term.operand.toLowerCase() === operand.toLowerCase(),
+ );
+ }
+
has_operand(operator: string, operand: string): boolean {
return this._terms.some(
(term) => !term.negated && term.operator === operator && term.operand === operand,
@@ -1600,4 +1609,51 @@ export class Filter {
}
this.requires_adjustment_for_moved_with_target = false;
}
+
+ can_newly_match_moved_messages(new_channel: string, new_topic: string): boolean {
+ // Checks if any of the operators on this Filter object have
+ // the property that it's possible for their true value to
+ // change as a result of messages being moved into the
+ // channel/topic pair provided in the parameters.
+ if (this.has_operand_case_insensitive("channel", new_channel)) {
+ return true;
+ }
+
+ if (this.has_operand_case_insensitive("topic", new_topic)) {
+ return true;
+ }
+
+ const term_types = this.sorted_term_types();
+ const can_match_moved_msg_term_types = new Set([
+ // For some of these operators, we could return `false`
+ // with more analysis of either the pre-move location,
+ // user_topic metadata, etc.
+ //
+ // It might be worth the effort for the more common views,
+ // such as the Combined Feed, but some of these operators
+ // are very unlikely to be used in practice.
+ "not-channel",
+ "not-topic",
+ "is-followed",
+ "not-is-followed",
+ "is-resolved",
+ "not-is-resolved",
+ "channels-public",
+ "not-channels-public",
+ "is-muted",
+ "not-is-muted",
+ "in-home",
+ "not-in-home",
+ "in-all",
+ "not-in-all",
+ ]);
+
+ for (const term of term_types) {
+ if (can_match_moved_msg_term_types.has(term)) {
+ return true;
+ }
+ }
+
+ return false;
+ }
}
diff --git a/web/src/message_events.js b/web/src/message_events.js
index cd5b56964c3..edb47a4ff0f 100644
--- a/web/src/message_events.js
+++ b/web/src/message_events.js
@@ -178,9 +178,11 @@ export function update_messages(events) {
const messages_to_rerender = [];
let any_topic_edited = false;
let changed_narrow = false;
+ let refreshed_current_narrow = false;
let changed_compose = false;
let any_message_content_edited = false;
let any_stream_changed = false;
+ let local_cache_missing_messages = false;
for (const event of events) {
const anchor_message = message_store.get(event.message_id);
@@ -290,6 +292,11 @@ export function update_messages(events) {
const message = message_store.get(message_id);
if (message !== undefined) {
event_messages.push(message);
+ } else {
+ // If we don't have the message locally, we need to
+ // refresh the current narrow after the update to fetch
+ // the updated messages.
+ local_cache_missing_messages = true;
}
}
// The event.message_ids received from the server are not in sorted order.
@@ -434,6 +441,33 @@ export function update_messages(events) {
}
}
+ // If a message was moved to the current narrow and we don't have
+ // the message cached, we need to refresh the narrow to display the message.
+ if (!changed_narrow && local_cache_missing_messages && current_filter) {
+ let moved_message_stream = old_stream_name;
+ let moved_message_topic = orig_topic;
+ if (stream_changed) {
+ moved_message_stream = sub_store.get(new_stream_id).name;
+ }
+
+ if (topic_edited) {
+ moved_message_topic = new_topic;
+ }
+
+ if (
+ current_filter.can_newly_match_moved_messages(
+ moved_message_stream,
+ moved_message_topic,
+ )
+ ) {
+ refreshed_current_narrow = true;
+ message_view.show(current_filter.terms(), {
+ then_select_id: current_selected_id,
+ trigger: "stream/topic change",
+ });
+ }
+ }
+
// Ensure messages that are no longer part of this
// narrow are deleted and messages that are now part
// of this narrow are added to the message_list.
@@ -444,7 +478,7 @@ export function update_messages(events) {
// this should be a loop over all valid message_list_data
// objects, without the rerender (which will naturally
// happen in the following code).
- if (!changed_narrow && current_filter) {
+ if (!changed_narrow && !refreshed_current_narrow && current_filter) {
let message_ids_to_remove = [];
if (current_filter.can_apply_locally()) {
const predicate = current_filter.predicate();
@@ -544,7 +578,7 @@ export function update_messages(events) {
// large organizations.
for (const list of message_lists.all_rendered_message_lists()) {
- if (changed_narrow && list === message_lists.current) {
+ if ((changed_narrow || refreshed_current_narrow) && list === message_lists.current) {
// Avoid updating current message list if user switched to a different narrow and
// we don't want to preserver the rendered state for the current one.
continue;
diff --git a/web/tests/filter.test.js b/web/tests/filter.test.js
index 46b828be502..af443ff5850 100644
--- a/web/tests/filter.test.js
+++ b/web/tests/filter.test.js
@@ -2429,3 +2429,30 @@ run_test("adjusted_terms_if_moved", () => {
result = Filter.adjusted_terms_if_moved(raw_terms, message);
assert.deepStrictEqual(result, expected);
});
+
+run_test("can_newly_match_moved_messages", () => {
+ // Matches stream
+ let filter = new Filter([{operator: "channel", operand: "general"}]);
+ assert.deepEqual(filter.can_newly_match_moved_messages("general", "test"), true);
+ assert.deepEqual(filter.can_newly_match_moved_messages("General", "test"), true);
+ assert.deepEqual(filter.can_newly_match_moved_messages("random-stream", "test"), false);
+
+ // Matches topic
+ filter = new Filter([{operator: "topic", operand: "Test topic"}]);
+ assert.deepEqual(filter.can_newly_match_moved_messages("general", "Test topic"), true);
+ assert.deepEqual(filter.can_newly_match_moved_messages("general", "test topic"), true);
+ assert.deepEqual(filter.can_newly_match_moved_messages("general", "random topic"), false);
+
+ // Matches common narrows
+ filter = new Filter([{operator: "is", operand: "followed"}]);
+ assert.deepEqual(filter.can_newly_match_moved_messages("general", "test"), true);
+
+ filter = new Filter([{operator: "is", operand: "starred"}]);
+ assert.deepEqual(filter.can_newly_match_moved_messages("general", "test"), false);
+
+ filter = new Filter([{negated: true, operator: "channel", operand: "general"}]);
+ assert.deepEqual(filter.can_newly_match_moved_messages("something-else", "test"), true);
+
+ filter = new Filter([{negated: true, operator: "is", operand: "followed"}]);
+ assert.deepEqual(filter.can_newly_match_moved_messages("general", "test"), true);
+});