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: Key topic link maps by lowercased topic name.

Evy Kassirer61600b6dfa34
diff --git a/web/src/message_store.ts b/web/src/message_store.ts
index 356f144c338..45e5bad0906 100644
--- a/web/src/message_store.ts
+++ b/web/src/message_store.ts
@@ -478,7 +478,7 @@ function get_link_map_for_narrow<T>(
stream_id: number,
topic: string,
): Map<number, T[]> | undefined {
- return map.get(stream_id)?.get(topic);
+ return map.get(stream_id)?.get(topic.toLowerCase());
}
function get_or_create_link_map_for_narrow<T>(
@@ -486,16 +486,22 @@ function get_or_create_link_map_for_narrow<T>(
stream_id: number,
topic: string,
): Map<number, T[]> {
+ const narrow_map = get_link_map_for_narrow(map, stream_id, topic);
+ if (narrow_map !== undefined) {
+ return narrow_map;
+ }
if (!map.has(stream_id)) {
map.set(stream_id, new Map<string, Map<number, T[]>>());
}
- if (!map.get(stream_id)!.get(topic)) {
- map.get(stream_id)!.set(topic, new Map<number, T[]>());
- }
- return map.get(stream_id)!.get(topic)!;
+ const new_narrow_map = new Map<number, T[]>();
+ map.get(stream_id)!.set(topic.toLowerCase(), new_narrow_map);
+ return new_narrow_map;
}
-// from_stream_id -> from_topic -> from_message_id -> TopicLink[]
+// Topic names are case-insensitive, so both link maps are keyed by
+// lowercased topic name.
+//
+// from_stream_id -> lowercased from_topic -> from_message_id -> TopicLink[]
const topic_links_by_from = new Map<number, Map<string, Map<number, TopicLink[]>>>();
export function topic_links_by_from_for_testing(): Map<
number,
@@ -504,7 +510,7 @@ export function topic_links_by_from_for_testing(): Map<
return topic_links_by_from;
}
-// to_stream_id -> to_topic -> to_message_id -> from_message_id[]
+// to_stream_id -> lowercased to_topic -> to_message_id -> from_message_id[]
// Instead of storing a TopicLink, this only stores the from_message_id
// since we can get that message's narrow data from its id.
const topic_links_by_to = new Map<number, Map<string, Map<number, number[]>>>();
@@ -558,10 +564,10 @@ export function topic_links_from_narrow(stream_id: number, topic: string): Topic
}
// Hide links from this topic to the same topic, since those feel
// unnecessary to reference.
- if (target.stream_id === stream_id && target.topic === topic) {
+ if (target.stream_id === stream_id && util.lower_same(target.topic, topic)) {
continue;
}
- const link_string = JSON.stringify(target);
+ const link_string = JSON.stringify({...target, topic: target.topic.toLowerCase()});
if (added_links.has(link_string)) {
continue;
}
@@ -603,10 +609,13 @@ export function topic_links_to_narrow(stream_id: number, topic: string): TopicLi
assert(message?.type === "stream");
// Hide links to this topic from the same topic, since those feel
// unnecessary to reference.
- if (message.stream_id === stream_id && message.topic === topic) {
+ if (message.stream_id === stream_id && util.lower_same(message.topic, topic)) {
continue;
}
- const stream_topic = JSON.stringify({stream_id: message.stream_id, topic: message.topic});
+ const stream_topic = JSON.stringify({
+ stream_id: message.stream_id,
+ topic: message.topic.toLowerCase(),
+ });
if (added_narrows.has(stream_topic)) {
continue;
}
@@ -825,6 +834,11 @@ export function process_topic_edit(opts: {
}
const old_stream_id = message.stream_id;
const old_topic = message.topic;
+ if (old_stream_id === new_stream_id && util.lower_same(old_topic, new_topic)) {
+ // The narrow's key is unchanged, so its entries are already
+ // in the right place.
+ continue;
+ }
// Move any links from this message stored with the old topic
const links_from_old_narrow = get_link_map_for_narrow(
diff --git a/web/tests/message_events.test.cjs b/web/tests/message_events.test.cjs
index 724c836662c..52c9e38b7ba 100644
--- a/web/tests/message_events.test.cjs
+++ b/web/tests/message_events.test.cjs
@@ -29,6 +29,7 @@ const linkifiers = zrequire("linkifiers");
const markdown = zrequire("markdown");
const message_events = zrequire("message_events");
const message_helper = zrequire("message_helper");
+const message_store = zrequire("message_store");
const {set_realm} = zrequire("state_data");
const stream_data = zrequire("stream_data");
const settings_config = zrequire("settings_config");
@@ -252,3 +253,78 @@ run_test(
assert.deepEqual(rendered_msgs, [original_message]);
},
);
+
+run_test(
+ "update_messages case-only topic name change keeps topic links findable",
+ ({override_rewire}) => {
+ override_rewire(message_events, "update_views_filtered_on_message_property", () => {}, {
+ unused: false,
+ });
+ message_store.clear_topic_links_for_testing();
+ message_lists.current.view = {rerender_messages: noop};
+ // update_messages checks whether the edit history modal is open.
+ const $message_edit_history_modal = $.create("#message-edit-history");
+ $message_edit_history_modal.set_parents_result(".micromodal", $.create("micromodal"));
+
+ const old_topic = "weekend plans";
+ const new_topic = "Weekend plans";
+ const link = {stream_id: denmark.stream_id, topic: "dinner", message_id: undefined};
+ const raw_message = {
+ id: 333,
+ display_recipient: denmark.name,
+ flags: [],
+ sender_id: alice.user_id,
+ stream_id: denmark.stream_id,
+ topic: old_topic,
+ type: "stream",
+ content: `<a href="/#narrow/channel/${denmark.stream_id}-Denmark/topic/dinner">dinner</a>`,
+ reactions: [],
+ submessages: [],
+ avatar_url: `/avatar/${alice.user_id}`,
+ };
+ const message = message_helper.process_new_message({
+ type: "server_message",
+ raw_message,
+ }).message;
+ assert.deepEqual(message_store.topic_links_from_narrow(denmark.stream_id, old_topic), [
+ link,
+ ]);
+ assert.deepEqual(message_store.topic_links_to_narrow(denmark.stream_id, "dinner"), [
+ {stream_id: denmark.stream_id, topic: old_topic, message_id: message.id},
+ ]);
+
+ const events = [
+ {
+ message_id: message.id,
+ message_ids: [message.id],
+ user_id: alice.user_id,
+ flags: [],
+ edit_timestamp: 1700000000,
+ stream_id: denmark.stream_id,
+ orig_subject: old_topic,
+ topic: new_topic,
+ topic_links: [],
+ rendering_only: false,
+ },
+ ];
+
+ message_events.update_messages(events);
+
+ // The links are still found under either spelling of the topic.
+ assert.equal(message.topic, new_topic);
+ assert.deepEqual(message_store.topic_links_from_narrow(denmark.stream_id, old_topic), [
+ link,
+ ]);
+ assert.deepEqual(message_store.topic_links_from_narrow(denmark.stream_id, new_topic), [
+ link,
+ ]);
+ assert.deepEqual(message_store.topic_links_to_narrow(denmark.stream_id, "dinner"), [
+ {stream_id: denmark.stream_id, topic: new_topic, message_id: message.id},
+ ]);
+
+ // Deleting the message finds its links and removes them.
+ message_store.remove([message.id]);
+ assert.deepEqual(message_store.topic_links_from_narrow(denmark.stream_id, new_topic), []);
+ assert.deepEqual(message_store.topic_links_to_narrow(denmark.stream_id, "dinner"), []);
+ },
+);
diff --git a/web/tests/message_store.test.cjs b/web/tests/message_store.test.cjs
index 90b17d5fcdc..aa73c5ada3a 100644
--- a/web/tests/message_store.test.cjs
+++ b/web/tests/message_store.test.cjs
@@ -1457,6 +1457,92 @@ test("get and update topic links to/from narrow", () => {
assert.deepEqual(message_store.topic_links_to_narrow(design.stream_id, "spending"), [
{stream_id: development.stream_id, topic: "✔ budget", message_id: message9.id},
]);
+
+ // ------ Now let's test that topic names are matched case-insensitively
+ message_store.clear_topic_links_for_testing();
+ const message10 = add_message(600, sales.stream_id, "Pricing");
+ const message11 = add_message(601, design.stream_id, "Logo");
+ // A link written with a different case than the topic it points to.
+ save_message_link(
+ sales.stream_id,
+ "Pricing",
+ message10.id,
+ "/#narrow/channel/10-design/topic/LOGO/",
+ );
+ // Both narrows can be looked up with any casing.
+ assert.deepEqual(message_store.topic_links_from_narrow(sales.stream_id, "pricing"), [
+ {stream_id: design.stream_id, topic: "LOGO", message_id: undefined},
+ ]);
+ assert.deepEqual(message_store.topic_links_to_narrow(design.stream_id, "Logo"), [
+ {stream_id: sales.stream_id, topic: "Pricing", message_id: message10.id},
+ ]);
+ // A link from a topic to itself with a different case is still hidden.
+ save_message_link(
+ design.stream_id,
+ "Logo",
+ message11.id,
+ "/#narrow/channel/10-design/topic/logo/",
+ );
+ assert.deepEqual(message_store.topic_links_from_narrow(design.stream_id, "Logo"), []);
+ assert.deepEqual(message_store.topic_links_to_narrow(design.stream_id, "Logo"), [
+ {stream_id: sales.stream_id, topic: "Pricing", message_id: message10.id},
+ ]);
+ // Renaming a topic to change only its case keeps its links, and
+ // later removing the message deletes them.
+ message_store.process_topic_edit({
+ message_ids: [message10.id],
+ new_stream_id: sales.stream_id,
+ new_topic: "PRICING",
+ });
+ message10.topic = "PRICING";
+ assert.deepEqual(message_store.topic_links_from_narrow(sales.stream_id, "PRICING"), [
+ {stream_id: design.stream_id, topic: "LOGO", message_id: undefined},
+ ]);
+ message_store.remove([message10.id]);
+ assert.deepEqual(message_store.topic_links_from_narrow(sales.stream_id, "PRICING"), []);
+ assert.deepEqual(message_store.topic_links_to_narrow(design.stream_id, "Logo"), []);
+});
+
+test("message links survive a case-only topic rename of the linking message", () => {
+ 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: 601,
+ sender_id: alice.user_id,
+ type: "stream",
+ stream_id: design.stream_id,
+ topic: "Logo",
+ content: "<ignore>",
+ });
+ const linking_message = add_message({
+ id: 602,
+ 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>`,
+ });
+ assert.deepEqual(message_store.topic_links_to_narrow(design.stream_id, "Logo"), [
+ {stream_id: sales.stream_id, topic: "Renewals", message_id: linking_message.id},
+ ]);
+
+ // update_messages sets the new topic directly for a rename that only
+ // changes its case, without calling process_topic_edit.
+ linking_message.topic = "renewals";
+
+ // Deleting the linking message and then editing the message it linked
+ // to must still find the link and clean it up.
+ message_store.remove([linking_message.id]);
+ message_store.update_message_content(linked_message, "<p>edited</p>");
+ assert.deepEqual(message_store.topic_links_to_narrow(design.stream_id, "Logo"), []);
});
test("process_topic_edit ignores messages missing from the local cache", () => {