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_store: File message links under the message's current location.

Aman Agrawal430a48976e39
diff --git a/web/src/message_store.ts b/web/src/message_store.ts
index 307a9276aca..90ebed33dc9 100644
--- a/web/src/message_store.ts
+++ b/web/src/message_store.ts
@@ -705,7 +705,13 @@ function update_or_remove_links_from_message(
// If there's no specified `to_message_id`, it was a link to a topic
// but not a specific message. That uses `NO_MESSAGE_ID`.
const to_message_id = link.message_id ?? NO_MESSAGE_ID;
- const link_map = get_link_map_for_narrow(topic_links_by_to, link.stream_id, link.topic);
+ const to_message = link.message_id === undefined ? undefined : get(link.message_id);
+ const to_narrow = to_message?.type === "stream" ? to_message : link;
+ const link_map = get_link_map_for_narrow(
+ topic_links_by_to,
+ to_narrow.stream_id,
+ to_narrow.topic,
+ );
if (link_map?.has(to_message_id)) {
const links_to_message = link_map.get(to_message_id)!;
if (new_message_id) {
@@ -776,8 +782,8 @@ export function save_topic_links(message: Message): void {
continue;
}
- const to_stream_id = link_data.stream_id;
- const to_topic = link_data.topic_name;
+ let to_stream_id = link_data.stream_id;
+ let to_topic = link_data.topic_name;
const to_message_id = link_data.message_id
? Number.parseInt(link_data.message_id, 10)
: undefined;
@@ -790,14 +796,20 @@ export function save_topic_links(message: Message): void {
// If we don't have access to this stream and/or message, or it's a buggy link,
// or it's a message from a muted user, just ignore it.
- if (!stream_data.get_sub_by_id(to_stream_id)) {
- continue;
- }
if (to_message_id !== undefined) {
const to_message = get(to_message_id);
- if (to_message === undefined || muted_users.is_user_muted(to_message.sender_id)) {
+ if (
+ to_message === undefined ||
+ muted_users.is_user_muted(to_message.sender_id) ||
+ to_message.type !== "stream"
+ ) {
continue;
}
+ to_stream_id = to_message.stream_id;
+ to_topic = to_message.topic;
+ }
+ if (!stream_data.get_sub_by_id(to_stream_id)) {
+ continue;
}
// (1) Save link in topic_links_by_from
diff --git a/web/tests/message_store.test.cjs b/web/tests/message_store.test.cjs
index 3ed7b0f79ea..b7af359e994 100644
--- a/web/tests/message_store.test.cjs
+++ b/web/tests/message_store.test.cjs
@@ -1582,6 +1582,103 @@ test("editing a message keeps links to it", () => {
]);
});
+test("message links follow the linked message's current location", () => {
+ 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);
+
+ function add_message(message) {
+ message_store.update_message_cache({type: "server_message", message});
+ return message;
+ }
+ const linked_message = add_message({
+ id: 621,
+ sender_id: alice.user_id,
+ type: "stream",
+ stream_id: design.stream_id,
+ topic: "Logo",
+ content: "<ignore>",
+ });
+ const stale_topic_message = add_message({
+ id: 622,
+ sender_id: bob.user_id,
+ type: "stream",
+ stream_id: sales.stream_id,
+ topic: "Renewals",
+ content: `<a href="/#narrow/channel/10-design/topic/old.20logo/near/${linked_message.id}">link</a>`,
+ });
+ const stale_channel_message = add_message({
+ id: 623,
+ sender_id: bob.user_id,
+ type: "stream",
+ stream_id: sales.stream_id,
+ topic: "Pricing",
+ content: `<a href="/#narrow/channel/12-sales/topic/Logo/near/${linked_message.id}">link</a>`,
+ });
+ assert.deepEqual(message_store.topic_links_to_narrow(design.stream_id, "Logo"), [
+ {stream_id: sales.stream_id, topic: "Renewals", message_id: stale_topic_message.id},
+ {stream_id: sales.stream_id, topic: "Pricing", message_id: stale_channel_message.id},
+ ]);
+
+ message_store.process_topic_edit({
+ message_ids: [linked_message.id],
+ new_stream_id: design.stream_id,
+ new_topic: "Branding",
+ });
+ linked_message.topic = "Branding";
+ message_store.remove([stale_topic_message.id]);
+ assert.deepEqual(message_store.topic_links_to_narrow(design.stream_id, "Branding"), [
+ {stream_id: sales.stream_id, topic: "Pricing", message_id: stale_channel_message.id},
+ ]);
+
+ message_store.remove([linked_message.id]);
+ assert.deepEqual(message_store.topic_links_from_narrow(sales.stream_id, "Pricing"), []);
+
+ const direct_message = add_message({
+ id: 624,
+ sender_id: alice.user_id,
+ type: "private",
+ display_recipient: [{id: alice.user_id}, {id: me.user_id}],
+ content: "<ignore>",
+ });
+ add_message({
+ id: 625,
+ sender_id: bob.user_id,
+ type: "stream",
+ stream_id: sales.stream_id,
+ topic: "Pricing",
+ content: `<a href="/#narrow/channel/10-design/topic/Logo/near/${direct_message.id}">link</a>`,
+ });
+ assert.deepEqual(message_store.topic_links_from_narrow(sales.stream_id, "Pricing"), []);
+});
+
+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};
+ stream_data.add_sub_for_tests(design);
+ const message = {
+ id: 631,
+ sender_id: alice.user_id,
+ type: "stream",
+ stream_id: design.stream_id,
+ topic: "Logo",
+ content: `<a href="/#narrow/channel/10-design/topic/Logo/near/631">link</a>`,
+ };
+ message_store.update_message_cache({type: "server_message", message});
+
+ message_store.reify_message_id({old_id: 631, new_id: 632});
+ message_store.remove([632]);
+
+ for (const link_map of [
+ message_store.topic_links_by_from_for_testing(),
+ message_store.topic_links_by_to_for_testing(),
+ ]) {
+ assert.equal(link_map.get(design.stream_id).get("logo").size, 0);
+ }
+});
+
test("process_topic_edit ignores messages missing from the local cache", () => {
message_store.clear_topic_links_for_testing();