Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
message_store: Move links to unstored messages along with them.
Evy Kassirer3d483a56a7d8
diff --git a/web/src/message_store.ts b/web/src/message_store.tsindex 64511b4651d..bc523a76e43 100644--- a/web/src/message_store.ts+++ b/web/src/message_store.ts@@ -999,13 +999,20 @@ export function process_topic_edit(opts: {new_topic: string;}): void {const {message_ids, new_stream_id, new_topic} = opts;++ // A move can affect messages we haven't fetched locally, whose+ // only links are the ones saved to them while they aren't in the+ // store. Those are found through the linking messages' entries,+ // so they're re-filed before any of those entries move below.+ for (const message_id of message_ids) {+ if (get(message_id) === undefined) {+ refile_links_to_message(message_id, {stream_id: new_stream_id, topic: new_topic});+ }+ }+for (const message_id of message_ids) {const message = get(message_id);if (message?.type !== "stream") {- // A move can affect messages we haven't fetched locally.- // The link maps only track stored messages, so there are- // no links to move for those; update_messages refreshes- // the narrow, which rebuilds link data once they're fetched.continue;}const old_stream_id = message.stream_id;diff --git a/web/tests/message_store.test.cjs b/web/tests/message_store.test.cjsindex c8d9a013601..059bceabc0d 100644--- a/web/tests/message_store.test.cjs+++ b/web/tests/message_store.test.cjs@@ -1866,6 +1866,82 @@ test("message links to a message that isn't in the store yet", () => {]);});+test("links to a message that isn't in the store yet follow its moves", () => {+ message_store.clear_topic_links_for_testing();+ const design = {name: "design", subscribed: true, stream_id: 10};+ stream_data.add_sub_for_tests(design);+ const sales = {name: "sales", subscribed: true, stream_id: 12};+ stream_data.add_sub_for_tests(sales);++ const linked_message_id = 660;+ const linking_message = {+ id: 661,+ sender_id: bob.user_id,+ type: "stream",+ stream_id: sales.stream_id,+ topic: "Renewals",+ content: `<a href="/#narrow/channel/10-design/topic/Logo/near/${linked_message_id}">link</a>`,+ };+ message_store.update_message_cache({type: "server_message", message: linking_message});++ // Moving the linked message before it is fetched moves the link+ // along with it, and it stays pending for when it is.+ message_store.process_topic_edit({+ message_ids: [linked_message_id],+ new_stream_id: design.stream_id,+ new_topic: "Branding",+ });+ assert.deepEqual(message_store.topic_links_from_narrow(sales.stream_id, "Renewals"), [+ {stream_id: design.stream_id, topic: "Branding", message_id: linked_message_id},+ ]);+ assert.deepEqual(message_store.topic_links_to_narrow(design.stream_id, "Logo"), []);+ assert.deepEqual(message_store.topic_links_to_narrow(design.stream_id, "Branding"), [+ {stream_id: sales.stream_id, topic: "Renewals", message_id: linking_message.id},+ ]);+ assert.deepEqual(+ message_store.pending_message_link_targets_for_testing(),+ new Map([[linked_message_id, new Set([linking_message.id])]]),+ );++ message_store.update_message_cache({+ type: "server_message",+ message: {+ id: linked_message_id,+ sender_id: alice.user_id,+ type: "stream",+ stream_id: design.stream_id,+ topic: "Branding",+ content: "<ignore>",+ },+ });+ assert.deepEqual(message_store.topic_links_to_narrow(design.stream_id, "Branding"), [+ {stream_id: sales.stream_id, topic: "Renewals", message_id: linking_message.id},+ ]);+ assert.deepEqual(message_store.pending_message_link_targets_for_testing(), new Map());++ // A move of a topic containing both the linking message and its+ // unfetched target, which comes later in the event, moves the+ // link too, so it is still hidden as a link within the same topic.+ const later_linked_message_id = 663;+ const earlier_linking_message = {+ id: 662,+ sender_id: bob.user_id,+ type: "stream",+ stream_id: design.stream_id,+ topic: "Logo",+ content: `<a href="/#narrow/channel/10-design/topic/Logo/near/${later_linked_message_id}">link</a>`,+ };+ message_store.update_message_cache({type: "server_message", message: earlier_linking_message});+ message_store.process_topic_edit({+ message_ids: [earlier_linking_message.id, later_linked_message_id],+ new_stream_id: design.stream_id,+ new_topic: "Branding",+ });+ earlier_linking_message.topic = "Branding";+ assert.deepEqual(message_store.topic_links_from_narrow(design.stream_id, "Branding"), []);+ assert.deepEqual(message_store.topic_links_to_narrow(design.stream_id, "Logo"), []);+});+test("reify_message_id handles a message that links to itself", () => {message_store.clear_topic_links_for_testing();const design = {name: "design", subscribed: true, stream_id: 10};@@ -1895,8 +1971,8 @@ test("process_topic_edit ignores messages missing from the local cache", () => {message_store.clear_topic_links_for_testing();// A channel or topic move can include messages the current client- // hasn't fetched. The link maps only track stored messages, so such a- // move is a no-op for them and must not throw.+ // hasn't fetched. With no links saved to them, such a move is a+ // no-op for the link maps and must not throw.const missing_message_id = 987654;assert.equal(message_store.get(missing_message_id), undefined);