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: Anchor topic links on their permalink's message.

Evy Kassirerc803cca1ceac
diff --git a/web/src/hash_util.ts b/web/src/hash_util.ts
index bb285a9cd21..2ded3404258 100644
--- a/web/src/hash_util.ts
+++ b/web/src/hash_util.ts
@@ -136,6 +136,21 @@ export function by_stream_topic_url(stream_id: number, topic: string): string {
return internal_url.by_stream_topic_url(stream_id, topic, sub_store.maybe_get_stream_name);
}
+// A topic permalink, anchored on a message in the topic so that the
+// link follows the topic if it's moved or renamed.
+export function by_stream_topic_permalink(
+ stream_id: number,
+ topic: string,
+ message_id: number,
+): string {
+ return internal_url.by_stream_topic_url(
+ stream_id,
+ topic,
+ sub_store.maybe_get_stream_name,
+ message_id,
+ );
+}
+
// Encodes a term list into the
// corresponding hash: the # component
// of the narrow URL
@@ -533,9 +548,15 @@ export function decode_dm_recipient_user_ids_from_narrow_url(narrow_url: string)
}
}
-export function decode_stream_topic_from_url(
- url_str: string,
-): {stream_id: number; topic_name?: string; message_id?: string} | null {
+// Parses a channel, topic, or message narrow URL. For a topic permalink,
+// with_message_id is the message the link is anchored on, which the
+// link follows if the topic is moved or renamed.
+export function decode_stream_topic_from_url(url_str: string): {
+ stream_id: number;
+ topic_name?: string;
+ message_id?: string;
+ with_message_id?: string;
+} | null {
try {
const url = new URL(url_str, window.location.origin);
if (url.origin !== window.location.origin || !url.hash.startsWith("#narrow")) {
@@ -568,12 +589,7 @@ export function decode_stream_topic_from_url(
if (terms.length === 2) {
return {stream_id, topic_name: terms[1].operand};
}
- if (terms[2]?.operator === "with") {
- // For with operators, we currently discard the message ID unless
- // otherwise specified.
- return {stream_id, topic_name: terms[1].operand};
- }
- if (terms[2]?.operator !== "near") {
+ if (terms[2]?.operator !== "near" && terms[2]?.operator !== "with") {
return null;
}
// Callers parse the message id as a number, so a URL with anything
@@ -581,6 +597,9 @@ export function decode_stream_topic_from_url(
if (!/^\d+$/.test(terms[2].operand)) {
return null;
}
+ if (terms[2].operator === "with") {
+ return {stream_id, topic_name: terms[1].operand, with_message_id: terms[2].operand};
+ }
return {stream_id, topic_name: terms[1].operand, message_id: terms[2].operand};
} catch {
return null;
diff --git a/web/src/message_store.ts b/web/src/message_store.ts
index 29babe691a8..60bf3501e4a 100644
--- a/web/src/message_store.ts
+++ b/web/src/message_store.ts
@@ -462,17 +462,34 @@ export function reify_message_id({old_id, new_id}: {old_id: number; new_id: numb
// A cross-conversation link found in a message, to a stream>topic or to
// a specific message in a stream>topic. Links to a whole channel (with
// no topic) aren't tracked.
+//
+// message_id is the linked message for a message link. A topic link
+// can have one too: a topic permalink names a message in the topic
+// (the `with` operator in its URL), and the link goes wherever that
+// message is now, so it keeps working after the topic is moved or
+// renamed. kind tells the two apart.
type TopicLinkMessage =
- {kind: "message"; message_id: number} | {kind: "topic"; message_id: undefined};
+ {kind: "message"; message_id: number} | {kind: "topic"; message_id: number | undefined};
export type TopicLink = {stream_id: number; topic: string} & TopicLinkMessage;
// The message a link is tracked by: the linked message for a message
-// link, and none for a topic link.
-function linked_message_from_url({message_id}: {message_id?: string}): TopicLinkMessage {
+// link, or for a topic permalink, the message it's anchored on, which
+// the link follows if the topic is moved.
+function linked_message_from_url({
+ message_id,
+ with_message_id,
+}: {
+ message_id?: string;
+ with_message_id?: string;
+}): TopicLinkMessage {
if (message_id !== undefined) {
return {kind: "message", message_id: Number.parseInt(message_id, 10)};
}
- return {kind: "topic", message_id: undefined};
+ return {
+ kind: "topic",
+ message_id:
+ with_message_id === undefined ? undefined : Number.parseInt(with_message_id, 10),
+ };
}
const NO_MESSAGE_ID = 0;
@@ -525,7 +542,7 @@ export function topic_links_incoming_for_testing(): Map<
return topic_links_incoming;
}
-// to_message_id -> from_message_id[], for message links whose target
+// to_message_id -> from_message_id[], for links to a message that
// isn't in the store yet. Such links are saved with the narrow data
// in their URL until the target arrives, at which point we can ensure
// up-to-date narrow data for the message.
@@ -625,62 +642,100 @@ function remove_links_from_message_to(
// Links to a message that wasn't yet in the store are stored with
// narrow data that might become stale. This function updates the
-// narrow data for link(s) to `to_message_id`, or drops the link(s)
-// if to_narrow is undefined.
+// narrow data for link(s) to `to_message_id`. Message links are
+// dropped if message_link_narrow is undefined, while topic links
+// anchored on the message lose their anchor and stay where they are
+// if topic_link_narrow is undefined.
function refile_pending_link_from_message(
from_message_id: number,
to_message_id: number,
- to_narrow: util.StreamTopic | undefined,
+ message_link_narrow: util.StreamTopic | undefined,
+ topic_link_narrow: util.StreamTopic | undefined,
): void {
const removed_links = remove_links_from_message_to(from_message_id, to_message_id);
if (removed_links === undefined) {
return;
}
- // Pushing onto updated_links updates the array that's already stored
+ // Adding to updated_links updates the array that's already stored
// in topic_links_outgoing.
- const {updated_links} = removed_links;
-
- // If there's a new narrow, add the link back to the two maps with the
- // correct narrow. There might have been multiple stale links between
- // these messages, if they had different outdated data, but now that
- // we know the up-to-date narrow we only store one link.
- if (to_narrow !== undefined) {
- add_incoming_record(to_narrow, to_message_id, from_message_id);
- updated_links.push({
- stream_id: to_narrow.stream_id,
- topic: to_narrow.topic,
+ const {stale_links, updated_links} = removed_links;
+
+ // Add the links back to the two maps with the correct narrow. The
+ // message might have had several stale message links or topic
+ // links to the target, with different outdated narrows, but now
+ // that the up-to-date narrow is known, at most one of each is kept.
+ if (message_link_narrow !== undefined && stale_links.some((link) => link.kind === "message")) {
+ add_link_from_message(from_message_id, updated_links, {
+ stream_id: message_link_narrow.stream_id,
+ topic: message_link_narrow.topic,
message_id: to_message_id,
kind: "message",
});
}
+ const stale_topic_links = stale_links.filter((link) => link.kind === "topic");
+ for (const link of stale_topic_links) {
+ if (topic_link_narrow !== undefined) {
+ add_link_from_message(from_message_id, updated_links, {
+ stream_id: topic_link_narrow.stream_id,
+ topic: topic_link_narrow.topic,
+ message_id: to_message_id,
+ kind: "topic",
+ });
+ } else {
+ add_link_from_message(from_message_id, updated_links, {
+ stream_id: link.stream_id,
+ topic: link.topic,
+ message_id: undefined,
+ kind: "topic",
+ });
+ }
+ }
}
// Moves the links to a message that isn't in the store to the given
-// narrow data, or removes them if to_narrow is undefined.
+// narrow data, or, when a narrow is undefined, stops the links of that
+// kind from following the message.
function refile_pending_links_to_message(
to_message_id: number,
- to_narrow: util.StreamTopic | undefined,
+ message_link_narrow: util.StreamTopic | undefined,
+ topic_link_narrow: util.StreamTopic | undefined,
): void {
const linking_message_ids = pending_message_link_targets.get(to_message_id);
if (linking_message_ids === undefined) {
return;
}
for (const linking_message_id of linking_message_ids) {
- refile_pending_link_from_message(linking_message_id, to_message_id, to_narrow);
+ refile_pending_link_from_message(
+ linking_message_id,
+ to_message_id,
+ message_link_narrow,
+ topic_link_narrow,
+ );
}
}
// Files the links to a message that just arrived in the store under
-// its current narrow data, or drops them if it turns out to be from a
-// muted user or not a channel message.
+// its current narrow data.
function resolve_pending_links_to_message(message: Message): void {
- if (message.type === "stream" && !muted_users.is_user_muted(message.sender_id)) {
- refile_pending_links_to_message(message.id, {
- stream_id: message.stream_id,
- topic: message.topic,
- });
+ if (message.type !== "stream") {
+ // Links to a message id that isn't in a channel are buggy, so
+ // they're dropped. refile_pending_links_to_message with undefined
+ // narrows wouldn't do that, since it keeps topic links (only
+ // removing the message id) and here we want to fully remove the
+ // link.
+ const linking_message_ids = pending_message_link_targets.get(message.id) ?? [];
+ for (const linking_message_id of linking_message_ids) {
+ remove_links_from_message_to(linking_message_id, message.id);
+ }
} else {
- refile_pending_links_to_message(message.id, undefined);
+ const narrow = {stream_id: message.stream_id, topic: message.topic};
+ // Links to a muted user's message are hidden, but a topic link
+ // anchored on one still follows it, like navigating the link does.
+ refile_pending_links_to_message(
+ message.id,
+ muted_users.is_user_muted(message.sender_id) ? undefined : narrow,
+ narrow,
+ );
}
pending_message_link_targets.delete(message.id);
}
@@ -717,11 +772,12 @@ export function topic_links_from_narrow(stream_id: number, topic: string): Topic
for (const message_id of messages_with_links) {
for (const link of narrow_map.get(message_id)!) {
- // A message link points to the linked message's current
- // location, which may differ from where it was when the link
- // was created (e.g. if its topic was resolved since). Until
- // the linked message is in the store, the channel/topic in
- // the link's URL is the best we know.
+ // A message link, or a topic link anchored on a message,
+ // points to that message's current location, which may
+ // differ from where it was when the link was created (e.g.
+ // if its topic was resolved since). Until the message is in
+ // the store, the channel/topic in the link's URL is the best
+ // we know.
let target = link;
if (link.message_id !== undefined) {
const message = get(link.message_id);
@@ -746,7 +802,14 @@ export function topic_links_from_narrow(stream_id: number, topic: string): Topic
if (target.stream_id === stream_id && util.lower_same(target.topic, topic)) {
continue;
}
- const link_string = JSON.stringify({...target, topic: target.topic.toLowerCase()});
+ // Two message links are the same target only if they're to the
+ // same message, while two topic links to the same topic are still
+ // the same even if they use different anchor messages.
+ const link_string = JSON.stringify({
+ stream_id: target.stream_id,
+ topic: target.topic.toLowerCase(),
+ message_id: target.kind === "message" ? target.message_id : undefined,
+ });
if (added_links.has(link_string)) {
continue;
}
@@ -852,12 +915,26 @@ function update_or_remove_links_to_message(
}
}
} else {
- // An undefined new_message_id means we're deleting the old message id
- // from the links.
+ // An undefined new_message_id means the message was deleted,
+ // so message links to it are removed.
const filtered_links = links.filter(
(topic_link) => topic_link.message_id !== old_message_id,
);
links_from_narrow.set(message_id, filtered_links);
+ // Topic links anchored on it become plain links to the topic
+ // it was last in, the best narrow we have for them.
+ if (
+ links.some(
+ (link) => link.message_id === old_message_id && link.kind === "topic",
+ )
+ ) {
+ add_link_from_message(message_id, filtered_links, {
+ stream_id,
+ topic,
+ message_id: undefined,
+ kind: "topic",
+ });
+ }
}
}
}
@@ -977,10 +1054,16 @@ export function save_topic_links(message: Message): void {
}
// Ignore links to messages from muted users, to channels we don't
- // have access to, and buggy ones to a message id that isn't in a channel.
+ // have access to, and buggy ones to a message id that isn't in a
+ // channel. A topic link anchored on a muted user's message is
+ // kept, following it like navigating does.
const to_message = to_message_id === undefined ? undefined : get(to_message_id);
if (to_message !== undefined) {
- if (muted_users.is_user_muted(to_message.sender_id) || to_message.type !== "stream") {
+ if (
+ to_message.type !== "stream" ||
+ (linked_message.kind === "message" &&
+ muted_users.is_user_muted(to_message.sender_id))
+ ) {
continue;
}
to_stream_id = to_message.stream_id;
@@ -1008,8 +1091,8 @@ export function save_topic_links(message: Message): void {
message.topic,
);
const topic_links_from_message = topic_links_from_message_narrow.get(message.id) ?? [];
- // Note: This is O(# of links for message_id), and for topic links
- // O(# of links to the target narrow).
+ // Note: This is O(# of links for message_id), and for unanchored
+ // topic links O(# of links to the target narrow).
add_link_from_message(message.id, topic_links_from_message, {
stream_id: to_stream_id,
topic: to_topic,
@@ -1034,10 +1117,8 @@ export function process_topic_edit(opts: {
// 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_pending_links_to_message(message_id, {
- stream_id: new_stream_id,
- topic: new_topic,
- });
+ const new_narrow = {stream_id: new_stream_id, topic: new_topic};
+ refile_pending_links_to_message(message_id, new_narrow, new_narrow);
}
}
@@ -1121,7 +1202,7 @@ export function remove(message_ids: number[]): void {
} else if (message === undefined) {
// A deleted message we haven't fetched can still have links
// saved to it.
- refile_pending_links_to_message(message_id, undefined);
+ refile_pending_links_to_message(message_id, undefined, undefined);
pending_message_link_targets.delete(message_id);
}
stored_messages.delete(message_id);
diff --git a/web/src/topic_popover.ts b/web/src/topic_popover.ts
index 911f6909f76..9e35c0bd85c 100644
--- a/web/src/topic_popover.ts
+++ b/web/src/topic_popover.ts
@@ -9,6 +9,7 @@ import render_left_sidebar_topic_links_popover from "../templates/popovers/left_
import * as clipboard_handler from "./clipboard_handler.ts";
import * as confirm_dialog from "./confirm_dialog.ts";
+import * as hash_util from "./hash_util.ts";
import {$t, $t_html} from "./i18n.ts";
import * as message_delete from "./message_delete.ts";
import * as message_edit from "./message_edit.ts";
@@ -80,11 +81,18 @@ function get_cross_conversation_link_render_context(link: message_store.TopicLin
} {
const topic_name =
stream_topic_history.get_known_topic_name(link.stream_id, link.topic) ?? link.topic;
- const {url} = topic_link_util.get_topic_link_content_with_stream_id({
- stream_id: link.stream_id,
- topic_name,
- message_id: link.message_id?.toString(),
- });
+ let url: string;
+ if (link.kind === "message") {
+ ({url} = topic_link_util.get_topic_link_content_with_stream_id({
+ stream_id: link.stream_id,
+ topic_name,
+ message_id: link.message_id.toString(),
+ }));
+ } else if (link.message_id !== undefined) {
+ url = hash_util.by_stream_topic_permalink(link.stream_id, topic_name, link.message_id);
+ } else {
+ url = hash_util.by_stream_topic_url(link.stream_id, topic_name);
+ }
const stream = stream_data.get_sub_by_id(link.stream_id);
return {
url,
diff --git a/web/tests/hash_util.test.cjs b/web/tests/hash_util.test.cjs
index 1865950a359..7eeae8f7927 100644
--- a/web/tests/hash_util.test.cjs
+++ b/web/tests/hash_util.test.cjs
@@ -88,6 +88,12 @@ run_test("decode_stream_topic_from_url", () => {
topic_name: "testing",
message_id: "42",
});
+ // A topic permalink, anchored on a message in the topic.
+ assert.deepEqual(decode(`${topic_url}/with/42`), {
+ stream_id: 99,
+ topic_name: "testing",
+ with_message_id: "42",
+ });
// Another origin.
assert.equal(decode("https://other.example.com/#narrow/channel/99-Frontend"), null);
@@ -98,12 +104,13 @@ run_test("decode_stream_topic_from_url", () => {
assert.equal(decode("#narrow/channel/abc"), null);
// A message with no topic.
assert.equal(decode(`${channel_url}/near/42`), null);
- // Terms other than near after the topic.
+ // Terms other than near or with after the topic.
assert.equal(decode(`${topic_url}/id/42`), null);
assert.equal(decode(`${topic_url}/near/42/x/1`), null);
// A message id that isn't a number.
assert.equal(decode(`${topic_url}/near/abc`), null);
assert.equal(decode(`${topic_url}/near/12abc`), null);
+ assert.equal(decode(`${topic_url}/with/abc`), null);
});
run_test("pm_with_url", () => {
diff --git a/web/tests/message_store.test.cjs b/web/tests/message_store.test.cjs
index 6b26b80b43a..68d850f9021 100644
--- a/web/tests/message_store.test.cjs
+++ b/web/tests/message_store.test.cjs
@@ -590,8 +590,9 @@ function message_link(stream_id, topic, message_id) {
return {stream_id, topic, message_id, kind: "message"};
}
-function topic_link(stream_id, topic) {
- return {stream_id, topic, message_id: undefined, kind: "topic"};
+// A topic link is anchored on a message if it was a topic permalink.
+function topic_link(stream_id, topic, anchor_message_id = undefined) {
+ return {stream_id, topic, message_id: anchor_message_id, kind: "topic"};
}
test("save_topic_links", () => {
@@ -1819,6 +1820,247 @@ test("links to a message that isn't in the store yet are dropped when it is dele
);
});
+test("topic links follow the message their permalink is anchored on", () => {
+ 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;
+ }
+
+ // A topic permalink is tracked by the message it's anchored on,
+ // like a message link, but is still a topic link.
+ const anchor_message = add_message({
+ id: 801,
+ sender_id: alice.user_id,
+ type: "stream",
+ stream_id: design.stream_id,
+ topic: "Logo",
+ content: "<ignore>",
+ });
+ const linking_message = add_message({
+ id: 802,
+ sender_id: bob.user_id,
+ type: "stream",
+ stream_id: sales.stream_id,
+ topic: "Renewals",
+ content: `<a href="/#narrow/channel/10-design/topic/Logo/with/${anchor_message.id}">link</a>`,
+ });
+ assert.deepEqual(message_store.topic_links_from_narrow(sales.stream_id, "Renewals"), [
+ topic_link(design.stream_id, "Logo", anchor_message.id),
+ ]);
+ assert.deepEqual(
+ message_store.topic_links_incoming_for_testing().get(design.stream_id).get("logo"),
+ new Map([[anchor_message.id, [linking_message.id]]]),
+ );
+
+ // A plain link to the same topic is the same link, while a message
+ // link to the anchor message is a different one.
+ const other_linking_message = add_message({
+ id: 803,
+ sender_id: bob.user_id,
+ type: "stream",
+ stream_id: sales.stream_id,
+ topic: "Renewals",
+ content: `<a href="/#narrow/channel/10-design/topic/Logo">topic</a><a href="/#narrow/channel/10-design/topic/Logo/near/${anchor_message.id}">message</a>`,
+ });
+ assert.deepEqual(message_store.topic_links_from_narrow(sales.stream_id, "Renewals"), [
+ topic_link(design.stream_id, "Logo", anchor_message.id),
+ message_link(design.stream_id, "Logo", anchor_message.id),
+ ]);
+
+ // Moving the anchor message moves the topic link with it, like
+ // navigating the link does, while the plain topic link stays.
+ message_store.process_topic_edit({
+ message_ids: [anchor_message.id],
+ new_stream_id: design.stream_id,
+ new_topic: "Branding",
+ });
+ anchor_message.topic = "Branding";
+ assert.deepEqual(message_store.topic_links_from_narrow(sales.stream_id, "Renewals"), [
+ topic_link(design.stream_id, "Branding", anchor_message.id),
+ topic_link(design.stream_id, "Logo"),
+ message_link(design.stream_id, "Branding", anchor_message.id),
+ ]);
+ assert.deepEqual(message_store.topic_links_to_narrow(design.stream_id, "Branding"), [
+ message_link(sales.stream_id, "Renewals", linking_message.id),
+ ]);
+ assert.deepEqual(message_store.topic_links_to_narrow(design.stream_id, "Logo"), [
+ message_link(sales.stream_id, "Renewals", other_linking_message.id),
+ ]);
+
+ // Deleting the anchor message leaves the topic link as a plain link
+ // to the topic the message was last in, while the message link to
+ // it is dropped.
+ message_store.remove([anchor_message.id]);
+ assert.deepEqual(message_store.topic_links_from_narrow(sales.stream_id, "Renewals"), [
+ topic_link(design.stream_id, "Branding"),
+ topic_link(design.stream_id, "Logo"),
+ ]);
+ assert.deepEqual(
+ message_store.topic_links_incoming_for_testing().get(design.stream_id).get("branding"),
+ new Map([[0, [linking_message.id]]]),
+ );
+
+ // A permalink anchored on a message that isn't in the store yet is
+ // filed under the topic in its URL until the message arrives.
+ const pending_anchor_id = 810;
+ const pending_linking_message = add_message({
+ id: 811,
+ sender_id: bob.user_id,
+ type: "stream",
+ stream_id: sales.stream_id,
+ topic: "Hiring",
+ content: `<a href="/#narrow/channel/10-design/topic/Logo/with/${pending_anchor_id}">topic</a><a href="/#narrow/channel/10-design/topic/Logo/near/${pending_anchor_id}">message</a>`,
+ });
+ assert.deepEqual(message_store.topic_links_from_narrow(sales.stream_id, "Hiring"), [
+ topic_link(design.stream_id, "Logo", pending_anchor_id),
+ message_link(design.stream_id, "Logo", pending_anchor_id),
+ ]);
+ assert.deepEqual(
+ message_store.pending_message_link_targets_for_testing(),
+ new Map([[pending_anchor_id, new Set([pending_linking_message.id])]]),
+ );
+ // When it arrives in another topic, from a muted user, the topic link
+ // still follows it there, while the message link to it is hidden.
+ muted_users.set_muted_users([{id: cindy.user_id}]);
+ add_message({
+ id: pending_anchor_id,
+ sender_id: cindy.user_id,
+ type: "stream",
+ stream_id: design.stream_id,
+ topic: "Branding",
+ content: "<ignore>",
+ });
+ muted_users.set_muted_users([]);
+ assert.deepEqual(message_store.topic_links_from_narrow(sales.stream_id, "Hiring"), [
+ topic_link(design.stream_id, "Branding", pending_anchor_id),
+ ]);
+ assert.deepEqual(message_store.topic_links_to_narrow(design.stream_id, "Branding"), [
+ message_link(sales.stream_id, "Renewals", linking_message.id),
+ message_link(sales.stream_id, "Hiring", pending_linking_message.id),
+ ]);
+ assert.deepEqual(message_store.pending_message_link_targets_for_testing(), new Map());
+ // The same goes for a muted user's message that's already in the
+ // store when the links to it are saved.
+ muted_users.set_muted_users([{id: cindy.user_id}]);
+ const muted_anchor_linking_message = add_message({
+ id: 812,
+ sender_id: bob.user_id,
+ type: "stream",
+ stream_id: sales.stream_id,
+ topic: "Pricing",
+ content: `<a href="/#narrow/channel/10-design/topic/Branding/with/${pending_anchor_id}">topic</a><a href="/#narrow/channel/10-design/topic/Branding/near/${pending_anchor_id}">message</a>`,
+ });
+ muted_users.set_muted_users([]);
+ assert.deepEqual(message_store.topic_links_from_narrow(sales.stream_id, "Pricing"), [
+ topic_link(design.stream_id, "Branding", pending_anchor_id),
+ ]);
+ assert.deepEqual(message_store.topic_links_to_narrow(design.stream_id, "Branding"), [
+ message_link(sales.stream_id, "Renewals", linking_message.id),
+ message_link(sales.stream_id, "Hiring", pending_linking_message.id),
+ message_link(sales.stream_id, "Pricing", muted_anchor_linking_message.id),
+ ]);
+
+ // An anchor message deleted before it arrives leaves a plain link to
+ // the topic in the URL.
+ const deleted_anchor_id = 820;
+ const deleted_anchor_linking_message = add_message({
+ id: 821,
+ sender_id: bob.user_id,
+ type: "stream",
+ stream_id: sales.stream_id,
+ topic: "Hiring",
+ content: `<a href="/#narrow/channel/10-design/topic/Logo/with/${deleted_anchor_id}">link</a>`,
+ });
+ message_store.remove([deleted_anchor_id]);
+ assert.deepEqual(message_store.topic_links_from_narrow(sales.stream_id, "Hiring"), [
+ topic_link(design.stream_id, "Branding", pending_anchor_id),
+ topic_link(design.stream_id, "Logo"),
+ ]);
+ assert.deepEqual(message_store.topic_links_to_narrow(design.stream_id, "Logo"), [
+ message_link(sales.stream_id, "Renewals", other_linking_message.id),
+ message_link(sales.stream_id, "Hiring", deleted_anchor_linking_message.id),
+ ]);
+ assert.deepEqual(message_store.pending_message_link_targets_for_testing(), new Map());
+
+ // An anchor message moved before it arrives takes the permalink
+ // with it.
+ const moved_anchor_id = 840;
+ const moved_anchor_linking_message = add_message({
+ id: 841,
+ sender_id: bob.user_id,
+ type: "stream",
+ stream_id: sales.stream_id,
+ topic: "Legal",
+ content: `<a href="/#narrow/channel/10-design/topic/Logo/with/${moved_anchor_id}">link</a>`,
+ });
+ message_store.process_topic_edit({
+ message_ids: [moved_anchor_id],
+ new_stream_id: design.stream_id,
+ new_topic: "Branding",
+ });
+ assert.deepEqual(message_store.topic_links_from_narrow(sales.stream_id, "Legal"), [
+ topic_link(design.stream_id, "Branding", moved_anchor_id),
+ ]);
+ assert.deepEqual(
+ message_store
+ .topic_links_incoming_for_testing()
+ .get(design.stream_id)
+ .get("branding")
+ .get(moved_anchor_id),
+ [moved_anchor_linking_message.id],
+ );
+ assert.deepEqual(
+ message_store.pending_message_link_targets_for_testing(),
+ new Map([[moved_anchor_id, new Set([moved_anchor_linking_message.id])]]),
+ );
+ add_message({
+ id: moved_anchor_id,
+ sender_id: alice.user_id,
+ type: "stream",
+ stream_id: design.stream_id,
+ topic: "Branding",
+ content: "<ignore>",
+ });
+ assert.deepEqual(message_store.topic_links_from_narrow(sales.stream_id, "Legal"), [
+ topic_link(design.stream_id, "Branding", moved_anchor_id),
+ ]);
+ assert.deepEqual(message_store.pending_message_link_targets_for_testing(), new Map());
+
+ // A link anchored on a message that turns out not to be in a channel
+ // is dropped, since navigating it goes to that message's conversation.
+ const dm_anchor_id = 830;
+ add_message({
+ id: 831,
+ sender_id: bob.user_id,
+ type: "stream",
+ stream_id: sales.stream_id,
+ topic: "Onboarding",
+ content: `<a href="/#narrow/channel/10-design/topic/Logo/with/${dm_anchor_id}">link</a>`,
+ });
+ add_message({id: dm_anchor_id, sender_id: alice.user_id, type: "private", content: "<ignore>"});
+ assert.deepEqual(message_store.topic_links_from_narrow(sales.stream_id, "Onboarding"), []);
+ assert.deepEqual(message_store.topic_links_to_narrow(design.stream_id, "Logo"), [
+ message_link(sales.stream_id, "Renewals", other_linking_message.id),
+ message_link(sales.stream_id, "Hiring", deleted_anchor_linking_message.id),
+ ]);
+ // So is one anchored on such a message that's already in the store.
+ add_message({
+ id: 832,
+ sender_id: bob.user_id,
+ type: "stream",
+ stream_id: sales.stream_id,
+ topic: "Onboarding",
+ content: `<a href="/#narrow/channel/10-design/topic/Branding/with/${dm_anchor_id}">link</a>`,
+ });
+ assert.deepEqual(message_store.topic_links_from_narrow(sales.stream_id, "Onboarding"), []);
+ assert.deepEqual(message_store.pending_message_link_targets_for_testing(), new Map());
+});
+
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};