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.

topic_menu: Add popover to view cross-link topics.

Evy Kassirer26e0f9f27f94
diff --git a/starlight_help/src/content/docs/link-to-a-message-or-conversation.mdx b/starlight_help/src/content/docs/link-to-a-message-or-conversation.mdx
index 000b2439a69..cb5ec942957 100644
--- a/starlight_help/src/content/docs/link-to-a-message-or-conversation.mdx
+++ b/starlight_help/src/content/docs/link-to-a-message-or-conversation.mdx
@@ -229,6 +229,34 @@ it is automatically formatted for you. You can use
</TabItem>
</Tabs>
+## View links to and from a conversation
+
+Zulip can show you how a [conversation](/help/reading-conversations) is connected
+to others through links, making it easy to navigate between related discussions.
+
+* **Links to** shows the conversations that messages in the current conversation
+ link to.
+* **Linked from** shows the conversations whose messages link to the current
+ conversation.
+
+<Tabs>
+ <TabItem label="Desktop/Web">
+ <FlattenedSteps>
+ <TopicActions />
+
+ 1. Click **View links**.
+ </FlattenedSteps>
+ </TabItem>
+</Tabs>
+
+The **View links** option appears only when a conversation has links to or from
+it.
+
+<ZulipTip>
+ These lists are built from messages Zulip has loaded in your current session,
+ so they may be incomplete until you have visited the relevant conversations.
+</ZulipTip>
+
## Related articles
* [Add a custom linkifier](/help/add-a-custom-linkifier)
diff --git a/web/icons/README.md b/web/icons/README.md
index 6e8d02d084d..66f5fde640b 100644
--- a/web/icons/README.md
+++ b/web/icons/README.md
@@ -13,6 +13,8 @@ that file in the future, below is a list of custom icons created in-house
for the Zulip project:
arrow-down.svg
+arrow-down-left.svg
+arrow-up-right.svg
bold.svg
bot.svg
browse-channels.svg
diff --git a/web/icons/arrow-down-left.svg b/web/icons/arrow-down-left.svg
new file mode 100644
index 00000000000..b98faf4123c
--- /dev/null
+++ b/web/icons/arrow-down-left.svg
@@ -0,0 +1,3 @@
+<svg width="16" height="16" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg">
+ <path d="M2.87308 13.8397C2.45899 13.8396 2.12308 13.5039 2.12308 13.0897L2.12308 6.58973C2.12308 6.17561 2.45899 5.83988 2.87308 5.83973C3.2873 5.83973 3.62308 6.17551 3.62308 6.58973L3.62308 11.3534L12.5967 2.37977C12.8896 2.08713 13.3644 2.08702 13.6573 2.37977C13.9501 2.67259 13.9499 3.1474 13.6573 3.44032L4.7598 12.3378L9.37308 12.3378C9.78741 12.338 10.123 12.6744 10.1231 13.0888C10.1231 13.5032 9.78749 13.8395 9.37308 13.8397L2.87308 13.8397Z" />
+</svg>
diff --git a/web/icons/arrow-up-right.svg b/web/icons/arrow-up-right.svg
new file mode 100644
index 00000000000..5ad23edb1ef
--- /dev/null
+++ b/web/icons/arrow-up-right.svg
@@ -0,0 +1,3 @@
+<svg width="16" height="16" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg">
+ <path d="M13.1269 2.16028C13.541 2.16043 13.8769 2.49616 13.8769 2.91028V9.41028C13.8769 9.8244 13.541 10.1601 13.1269 10.1603C12.7127 10.1603 12.3769 9.82449 12.3769 9.41028V4.64661L3.40328 13.6202C3.1104 13.9129 2.63556 13.913 2.34274 13.6202C2.04992 13.3274 2.05007 12.8526 2.34274 12.5597L11.2402 3.66223H6.62692C6.21259 3.66198 5.87704 3.32562 5.87692 2.91125C5.87692 2.49679 6.21251 2.16053 6.62692 2.16028H13.1269Z" />
+</svg>
diff --git a/web/src/alert_words.ts b/web/src/alert_words.ts
index a7c1dd9e178..bd1a8dd07d2 100644
--- a/web/src/alert_words.ts
+++ b/web/src/alert_words.ts
@@ -86,7 +86,7 @@ export function process_message(message: Message): void {
}
const updated_content = highlight_alert_words(message.content);
- message_store.update_message_content(message, updated_content);
+ message_store.update_message_content(message, updated_content, false);
}
export function notifies(message: Message): boolean {
diff --git a/web/src/hash_util.ts b/web/src/hash_util.ts
index 5c96c0d3a3f..285e6c10bde 100644
--- a/web/src/hash_util.ts
+++ b/web/src/hash_util.ts
@@ -563,7 +563,8 @@ export function decode_stream_topic_from_url(
return {stream_id, topic_name: terms[1].operand};
}
if (terms[2]?.operator === "with") {
- // For with operators, we currently discard the message ID.
+ // 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") {
diff --git a/web/src/message_events.ts b/web/src/message_events.ts
index abbceeca0f0..0ada3500ac7 100644
--- a/web/src/message_events.ts
+++ b/web/src/message_events.ts
@@ -637,6 +637,24 @@ export function update_messages(events: UpdateMessageEvent[]): void {
drafts.rename_stream_recipient(old_stream_id, orig_topic, new_stream_id, new_topic);
}
+ // Before editing the topic or stream on any message, update the
+ // topic links to point to the right place. This must run while
+ // the messages still have their old stream and topic, so that we
+ // can fetch their old records in the link maps.
+ if (topic_edited || stream_changed) {
+ message_store.process_topic_edit({
+ message_ids: event.message_ids,
+ new_stream_id: new_stream_id ?? old_stream_id,
+ new_topic: get_post_edit_topic(
+ topic_edited,
+ only_topic_case_changed,
+ event,
+ new_topic,
+ anchor_message,
+ ),
+ });
+ }
+
for (const moved_message of event_messages) {
if (
realm.realm_message_edit_history_visibility_policy !==
diff --git a/web/src/message_store.ts b/web/src/message_store.ts
index 4123ca0c8d1..b4323cf2757 100644
--- a/web/src/message_store.ts
+++ b/web/src/message_store.ts
@@ -1,10 +1,13 @@
import _ from "lodash";
+import assert from "minimalistic-assert";
import * as z from "zod/mini";
import * as blueslip from "./blueslip.ts";
import type {RawLocalMessage} from "./echo.ts";
+import * as hash_util from "./hash_util.ts";
import type {LocalMessage, NewMessage, ProcessedMessage} from "./message_helper.ts";
import type {TimeFormattedReminder} from "./message_reminder.ts";
+import * as muted_users from "./muted_users.ts";
import * as people from "./people.ts";
import * as stream_data from "./stream_data.ts";
import {topic_link_schema} from "./types.ts";
@@ -240,6 +243,12 @@ export type Message = (
export function update_message_cache(message_data: ProcessedMessage): void {
// You should only call this from message_helper (or in tests).
stored_messages.set(message_data.message.id, message_data);
+
+ // Recalculate the message's topic links data. Removing and re-adding them
+ // is as much work as calculating them and comparing them to existing links
+ // links, and is tidier.
+ remove_message_from_topic_links(message_data.message.id);
+ save_topic_links(message_data.message);
}
export function get_cached_message(message_id: number): ProcessedMessage | undefined {
@@ -441,6 +450,9 @@ export function reify_message_id({old_id, new_id}: {old_id: number; new_id: numb
delete server_message.topic;
}
}
+ if (server_message.type === "stream") {
+ update_message_id_in_topic_links(old_id, new_id);
+ }
server_message.id = new_id;
server_message.locally_echoed = false;
stored_messages.set(new_id, {type: "server_message", message: server_message});
@@ -448,12 +460,416 @@ export function reify_message_id({old_id, new_id}: {old_id: number; new_id: numb
}
}
-export function update_message_content(message: Message, new_content: string): void {
+// 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.
+export type TopicLink = {
+ stream_id: number;
+ topic: string;
+ message_id: number | undefined;
+};
+
+const NO_MESSAGE_ID = 0;
+
+function get_or_create_link_map_for_narrow<T>(
+ map: Map<number, Map<string, Map<number, T[]>>>,
+ stream_id: number,
+ topic: string,
+): Map<number, T[]> {
+ 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)!;
+}
+
+// from_stream_id -> 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,
+ Map<string, Map<number, TopicLink[]>>
+> {
+ return topic_links_by_from;
+}
+
+// to_stream_id -> 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[]>>>();
+export function topic_links_by_to_for_testing(): Map<number, Map<string, Map<number, number[]>>> {
+ return topic_links_by_to;
+}
+
+export function clear_topic_links_for_testing(): void {
+ topic_links_by_from.clear();
+ topic_links_by_to.clear();
+}
+
+// Returns the cross-conversation links found in this narrow, resolved to
+// each target's current location. The popover view turns these into the
+// render context for the channel-icon link templates.
+export function topic_links_from_narrow(stream_id: number, topic: string): TopicLink[] {
+ const narrow_map = topic_links_by_from.get(stream_id)?.get(topic);
+ if (narrow_map === undefined) {
+ return [];
+ }
+
+ // `topic_links_by_from` stores messages by the order they're inserted,
+ // which is the order they're fetched from the server, which isn't necessarily
+ // by most recent, e.g. if a user fetches scrolls back in a conversation to
+ // load more messages. So now we sort them, but we can sort by ID instead of
+ // timestamp since messages are sorted temporally by ID.
+ const messages_with_links = narrow_map
+ .keys()
+ .toArray()
+ .toSorted((a, b) => a - b);
+
+ const links_from_narrow: TopicLink[] = [];
+ // We keep a set of links for deduplication, and stringify the links because
+ // sets of objects won't work due to the way object equality works in JavaScript.
+ const added_links = new Set<string>();
+
+ 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).
+ let target = link;
+ if (link.message_id !== undefined) {
+ const message = get(link.message_id);
+ assert(message?.type === "stream");
+ target = {
+ stream_id: message.stream_id,
+ topic: message.topic,
+ message_id: message.id,
+ };
+ }
+ // Hide links from this topic to the same topic, since those feel
+ // unnecessary to reference.
+ if (target.stream_id === stream_id && target.topic === topic) {
+ continue;
+ }
+ const link_string = JSON.stringify(target);
+ if (added_links.has(link_string)) {
+ continue;
+ }
+ added_links.add(link_string);
+ links_from_narrow.push(target);
+ }
+ }
+
+ return links_from_narrow;
+}
+
+export function topic_links_to_narrow(stream_id: number, topic: string): TopicLink[] {
+ const narrow_map = topic_links_by_to.get(stream_id)?.get(topic);
+ if (narrow_map === undefined) {
+ return [];
+ }
+
+ // `topic_links_by_to` stores messages by the order they're inserted,
+ // which is the order they're fetched from the server, which isn't necessarily
+ // by most recent, e.g. if a user fetches scrolls back in a conversation to
+ // load more messages. So now we sort them, but we can sort by ID instead of
+ // timestamp since messages are sorted temporally by ID.
+ const messages_linking_to_narrow = narrow_map
+ .values()
+ .toArray()
+ .flat()
+ .toSorted((a, b) => a - b);
+
+ const topic_links_content: TopicLink[] = [];
+ // We keep a set of stream/topic data for deduplication, since we only want to show
+ // one link per narrow that links to this narrow. We use the first message that does
+ // so, and discard all following messages from the same narrow. We have to stringify
+ // the stream/topic data because sets of objects won't work due to the way object
+ // equality works in JavaScript.
+ const added_narrows = new Set<string>();
+
+ for (const from_message_id of messages_linking_to_narrow) {
+ const message = get(from_message_id);
+ 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) {
+ continue;
+ }
+ const stream_topic = JSON.stringify({stream_id: message.stream_id, topic: message.topic});
+ if (added_narrows.has(stream_topic)) {
+ continue;
+ }
+ added_narrows.add(stream_topic);
+ topic_links_content.push({
+ stream_id: message.stream_id,
+ topic: message.topic,
+ message_id: message.id,
+ });
+ }
+ return topic_links_content;
+}
+
+// If new_message_id = undefined, the message has been deleted, so remove
+// all references to it. Otherwise update our maps to store the new message id.
+// If updating, this must be called while the message object still has the
+// old message id, so that we can fetch it from the message store.
+function _remove_or_update_message_id_from_topic_links(
+ old_message_id: number,
+ new_message_id: number | undefined,
+): void {
+ assert(old_message_id !== NO_MESSAGE_ID);
+ assert(new_message_id !== NO_MESSAGE_ID);
+ const updated_message = get(old_message_id);
+ if (updated_message?.type !== "stream") {
+ return;
+ }
+
+ const {stream_id, topic} = updated_message;
+
+ // (1) Update any record of links to this message.
+ const links_to_narrow = topic_links_by_to.get(stream_id)?.get(topic);
+ if (links_to_narrow?.has(old_message_id)) {
+ // (1a) Update records in `topic_links_by_to` for messages pointing to the message
+ // we're updating.
+ const messages_linking_to_updated_message = links_to_narrow.get(old_message_id)!;
+ links_to_narrow.delete(old_message_id);
+ // If we're updating the message id, save this data under the new message id.
+ if (new_message_id) {
+ links_to_narrow.set(
+ new_message_id,
+ // Handle edge case where messages_linking_to_updated_message contains
+ // the old_message_id - we need to update it to be the new_message_id.
+ messages_linking_to_updated_message.map((message_id) =>
+ message_id === old_message_id ? new_message_id : message_id,
+ ),
+ );
+ }
+
+ // (1b) Update/delete the matching links in `topic_links_by_from`
+ // i.e. links from messages to this updated message, since they need
+ // to point to the correct new message id.
+ for (const message_id of messages_linking_to_updated_message) {
+ const message = get(message_id);
+ assert(message?.type === "stream");
+ const links = topic_links_by_from
+ .get(message.stream_id)!
+ .get(message.topic)!
+ .get(message.id)!;
+ if (new_message_id) {
+ for (const link of links) {
+ if (link.message_id === old_message_id) {
+ link.message_id = new_message_id;
+ }
+ }
+ } else {
+ // An undefined new_message_id means we're deleting the old message id
+ // from the links.
+ const filtered_links = links.filter(
+ (topic_link) => topic_link.message_id !== old_message_id,
+ );
+ topic_links_by_from
+ .get(message.stream_id)!
+ .get(message.topic)!
+ .set(message_id, filtered_links);
+ }
+ }
+ }
+
+ // (2) Update links from this message to other streams/topics/messages.
+ const links_from_narrow = topic_links_by_from.get(stream_id)?.get(topic);
+ if (links_from_narrow?.has(old_message_id)) {
+ // (2a) Update records in `topic_links_by_from` from message we're updating.
+ const messages_linked_from_updated_message = links_from_narrow.get(old_message_id)!;
+ links_from_narrow.delete(old_message_id);
+ if (new_message_id) {
+ links_from_narrow.set(new_message_id, messages_linked_from_updated_message);
+ }
+
+ // (2b) Delete matching records in `topic_links_by_to`,
+ // i.e. links to messages from this updated message, since they need
+ // to store correct new message id.
+ for (const link of messages_linked_from_updated_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 = topic_links_by_to.get(link.stream_id)?.get(link.topic);
+ if (link_map?.has(to_message_id)) {
+ const links_to_message = link_map.get(to_message_id)!;
+ if (new_message_id) {
+ links_to_message.push(new_message_id);
+ }
+ link_map.set(
+ to_message_id,
+ links_to_message.filter((message_id) => message_id !== old_message_id),
+ );
+ }
+ }
+ }
+}
+
+function update_message_id_in_topic_links(old_message_id: number, new_message_id: number): void {
+ _remove_or_update_message_id_from_topic_links(old_message_id, new_message_id);
+}
+
+function remove_message_from_topic_links(message_id: number): void {
+ _remove_or_update_message_id_from_topic_links(message_id, undefined);
+}
+
+export function save_topic_links(message: Message): void {
+ if (message.type !== "stream") {
+ return;
+ }
+ if (muted_users.is_user_muted(message.sender_id)) {
+ return;
+ }
+
+ // Extract the URLs from the message content.
+ const link_elements = new DOMParser()
+ .parseFromString(message.content, "text/html")
+ .querySelectorAll("a");
+ for (const link_element of link_elements) {
+ const link = link_element.href;
+ const hash = hash_util.get_link_hash(link);
+ if (!hash.startsWith("#narrow/")) {
+ continue;
+ }
+ const link_data = hash_util.decode_stream_topic_from_url(hash);
+ if (link_data === null) {
+ continue;
+ }
+
+ const to_stream_id = link_data.stream_id;
+ const to_topic = link_data.topic_name;
+ const to_message_id = link_data.message_id
+ ? Number.parseInt(link_data.message_id, 10)
+ : undefined;
+
+ // A link to just a channel (no topic) isn't information-rich
+ // enough to be worth tracking as a cross-conversation link.
+ if (to_topic === undefined) {
+ continue;
+ }
+
+ // 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) {
+ continue;
+ }
+ if (muted_users.is_user_muted(to_message.sender_id)) {
+ continue;
+ }
+ }
+
+ // (1) Save link in topic_links_by_from
+ const topic_links_from_message_narrow = get_or_create_link_map_for_narrow(
+ topic_links_by_from,
+ message.stream_id,
+ message.topic,
+ );
+ const topic_links_from_message = topic_links_from_message_narrow.get(message.id) ?? [];
+ // Note: This is O(# of links for message_id)
+ util.unique_array_insert(topic_links_from_message, {
+ stream_id: to_stream_id,
+ topic: to_topic,
+ message_id: to_message_id,
+ });
+ topic_links_from_message_narrow.set(message.id, topic_links_from_message);
+
+ // (2) Save link in topic_links_by_to
+ const topic_links_to_message_narrow = get_or_create_link_map_for_narrow(
+ topic_links_by_to,
+ to_stream_id,
+ to_topic,
+ );
+ const topic_links_to_message =
+ topic_links_to_message_narrow.get(to_message_id ?? NO_MESSAGE_ID) ?? [];
+ // Note: This is O(# of links for message_id, which could be a lot for NO_MESSAGE_ID)
+ util.unique_array_insert(topic_links_to_message, message.id);
+ topic_links_to_message_narrow.set(to_message_id ?? NO_MESSAGE_ID, topic_links_to_message);
+ }
+}
+
+// Important: Messages should still have the old stream and topic when this
+// function is called, so that we can fetch their old records in the link maps.
+export function process_topic_edit(opts: {
+ message_ids: number[];
+ new_stream_id: number;
+ new_topic: string;
+}): void {
+ const {message_ids, new_stream_id, new_topic} = opts;
+ 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;
+ const old_topic = message.topic;
+
+ // Move any links from this message stored with the old topic
+ const links_from_old_narrow = topic_links_by_from.get(old_stream_id)?.get(old_topic);
+ const links_from_edited_message = links_from_old_narrow?.get(message_id);
+ if (links_from_edited_message !== undefined) {
+ const new_narrow_link_map = get_or_create_link_map_for_narrow(
+ topic_links_by_from,
+ new_stream_id,
+ new_topic,
+ );
+ new_narrow_link_map.set(message_id, links_from_edited_message);
+ links_from_old_narrow!.delete(message_id);
+ }
+
+ // Move any links to this message stored with the old topic
+ const links_to_old_narrow = topic_links_by_to.get(old_stream_id)?.get(old_topic);
+ const links_to_edited_message = links_to_old_narrow?.get(message_id);
+ if (links_to_edited_message !== undefined) {
+ const new_narrow_link_map = get_or_create_link_map_for_narrow(
+ topic_links_by_to,
+ new_stream_id,
+ new_topic,
+ );
+ new_narrow_link_map.set(message_id, links_to_edited_message);
+ links_to_old_narrow!.delete(message_id);
+ }
+ }
+}
+
+export function update_message_content(
+ message: Message,
+ new_content: string,
+ may_have_updated_links = true,
+): void {
message.content = new_content;
+
+ // DM messages aren't currently part of cross-conversation links.
+ // TODO: Consider adding this in the future.
+ if (message.type !== "stream" || !may_have_updated_links) {
+ return;
+ }
+ // The content might have a different set of message links, so reparse
+ // the content and update the map. A prior check to see if anything
+ // changed takes a comparable amount of time (recalculating the topic
+ // links, and sorting them to compare to current links), so it's easier
+ // to just wipe the data and recalculate.
+ remove_message_from_topic_links(message.id);
+ save_topic_links(message);
}
export function remove(message_ids: number[]): void {
for (const message_id of message_ids) {
+ const message = get(message_id);
+ if (message?.type === "stream") {
+ remove_message_from_topic_links(message.id);
+ }
stored_messages.delete(message_id);
}
}
diff --git a/web/src/popover_menus.ts b/web/src/popover_menus.ts
index 3e442b9449f..bb8c4ba2317 100644
--- a/web/src/popover_menus.ts
+++ b/web/src/popover_menus.ts
@@ -28,6 +28,7 @@ type PopoverName =
| "stream_settings"
| "scroll_to_time"
| "topics_menu"
+ | "topic_links"
| "send_later"
| "change_visibility_policy"
| "personal_menu"
@@ -54,6 +55,7 @@ export const popover_instances: Record<PopoverName, tippy.Instance | null> = {
stream_settings: null,
scroll_to_time: null,
topics_menu: null,
+ topic_links: null,
send_later: null,
change_visibility_policy: null,
personal_menu: null,
diff --git a/web/src/popover_menus_data.ts b/web/src/popover_menus_data.ts
index 5168b6a8783..3092618ea8d 100644
--- a/web/src/popover_menus_data.ts
+++ b/web/src/popover_menus_data.ts
@@ -12,6 +12,7 @@ import * as message_delete from "./message_delete.ts";
import * as message_edit from "./message_edit.ts";
import * as message_lists from "./message_lists.ts";
import type {Message} from "./message_store.ts";
+import * as message_store from "./message_store.ts";
import * as narrow_state from "./narrow_state.ts";
import {page_params} from "./page_params.ts";
import * as people from "./people.ts";
@@ -75,6 +76,7 @@ type TopicPopoverContext = {
all_visibility_policies: AllVisibilityPolicies;
can_summarize_topics: boolean;
show_ai_features: boolean;
+ has_topic_links: boolean;
};
type VisibilityChangePopoverContext = {
@@ -335,6 +337,9 @@ export function get_topic_popover_content_context({
const all_visibility_policies = user_topics.all_visibility_policies;
const is_spectator = page_params.is_spectator;
const is_topic_empty = is_topic_definitely_empty(stream_id, topic_name);
+ const has_topic_links =
+ message_store.topic_links_from_narrow(stream_id, topic_name).length > 0 ||
+ message_store.topic_links_to_narrow(stream_id, topic_name).length > 0;
return {
stream_name: sub.name,
stream_id: sub.stream_id,
@@ -357,6 +362,7 @@ export function get_topic_popover_content_context({
all_visibility_policies,
can_summarize_topics: settings_data.user_can_summarize_topics(),
show_ai_features: !user_settings.hide_ai_features,
+ has_topic_links,
};
}
diff --git a/web/src/tippyjs.ts b/web/src/tippyjs.ts
index 9117210c06c..e77199e0c2b 100644
--- a/web/src/tippyjs.ts
+++ b/web/src/tippyjs.ts
@@ -1,6 +1,7 @@
import {$} from "jquery";
import assert from "minimalistic-assert";
import * as tippy from "tippy.js";
+import * as z from "zod/mini";
import render_buddy_list_title_tooltip from "../templates/buddy_list/title_tooltip.hbs";
import render_change_visibility_policy_button_tooltip from "../templates/change_visibility_policy_button_tooltip.hbs";
@@ -155,6 +156,34 @@ export const topic_visibility_policy_tooltip_props = {
},
};
+// Ideally we could get this from `props.placement`, but it always returns
+// the default placement. If the popover flips and opens in another direction
+// due to size constraints, it doesn't return that new value of its actual
+// placement. There doesn't seem to be a way to get the real placement value
+// other than getting it from the DOM, so we're parsing it with zod.
+export function get_actual_placement(instance: tippy.Instance): tippy.Placement {
+ const raw_placement = $(instance.popper).find(".tippy-box").attr("data-placement");
+ return z
+ .enum([
+ "top",
+ "bottom",
+ "left",
+ "right",
+ "top-start",
+ "top-end",
+ "bottom-start",
+ "bottom-end",
+ "right-start",
+ "right-end",
+ "left-start",
+ "left-end",
+ "auto",
+ "auto-start",
+ "auto-end",
+ ])
+ .parse(raw_placement);
+}
+
export function initialize(): void {
$("#tooltip-templates-container").html(render_tooltip_templates());
diff --git a/web/src/topic_popover.ts b/web/src/topic_popover.ts
index 43ea73c9c6a..a787e3c4988 100644
--- a/web/src/topic_popover.ts
+++ b/web/src/topic_popover.ts
@@ -1,23 +1,30 @@
import {$} from "jquery";
+import _ from "lodash";
import assert from "minimalistic-assert";
import type * as tippy from "tippy.js";
import render_delete_topic_modal from "../templates/confirm_dialog/confirm_delete_topic.hbs";
import render_left_sidebar_topic_actions_popover from "../templates/popovers/left_sidebar/left_sidebar_topic_actions_popover.hbs";
+import render_left_sidebar_topic_links_popover from "../templates/popovers/left_sidebar/left_sidebar_topic_links_popover.hbs";
import * as clipboard_handler from "./clipboard_handler.ts";
import * as confirm_dialog from "./confirm_dialog.ts";
import {$t, $t_html} from "./i18n.ts";
import * as message_delete from "./message_delete.ts";
import * as message_edit from "./message_edit.ts";
+import * as message_store from "./message_store.ts";
import * as message_summary from "./message_summary.ts";
import * as popover_menus from "./popover_menus.ts";
import * as popover_menus_data from "./popover_menus_data.ts";
import * as recent_view_ui from "./recent_view_ui.ts";
import * as starred_messages_ui from "./starred_messages_ui.ts";
import {realm} from "./state_data.ts";
+import * as stream_data from "./stream_data.ts";
import * as stream_popover from "./stream_popover.ts";
import * as stream_topic_history from "./stream_topic_history.ts";
+import type {StreamSubscription} from "./sub_store.ts";
+import * as tippyjs from "./tippyjs.ts";
+import * as topic_link_util from "./topic_link_util.ts";
import * as ui_util from "./ui_util.ts";
import * as unread_ops from "./unread_ops.ts";
import * as user_topics from "./user_topics.ts";
@@ -58,6 +65,73 @@ function get_conversation(instance: tippy.Instance): {
return {stream_id, topic_name, url};
}
+// Builds the render context for a cross-conversation link, matching the
+// data expected by the `channel_message_link` / `topic_link` /
+// `decorated_channel_name` templates so the popover renders these links
+// identically to how they appear inline in message content.
+function get_cross_conversation_link_render_context(link: message_store.TopicLink): {
+ url: string;
+ stream: StreamSubscription | undefined;
+ channel_id: number;
+ channel_name: string;
+ topic_display_name_html: string;
+ is_empty_string_topic: boolean;
+ is_message_link: boolean;
+} {
+ const {url} = topic_link_util.get_topic_link_content_with_stream_id({
+ stream_id: link.stream_id,
+ topic_name: link.topic,
+ message_id: link.message_id?.toString(),
+ });
+ const stream = stream_data.get_sub_by_id(link.stream_id);
+ return {
+ url,
+ stream,
+ channel_id: link.stream_id,
+ channel_name: stream?.name ?? "",
+ topic_display_name_html: _.escape(util.get_final_topic_display_name(link.topic)),
+ is_empty_string_topic: link.topic === "",
+ is_message_link: link.message_id !== undefined,
+ };
+}
+
+export function open_cross_topic_links(
+ stream_id: number,
+ topic_name: string,
+ placement: tippy.Placement,
+ target: tippy.ReferenceElement,
+): void {
+ popover_menus.toggle_popover_menu(target, {
+ theme: "popover-menu",
+ placement,
+ onShow(instance) {
+ popover_menus.popover_instances.topic_links = instance;
+ ui_util.show_left_sidebar_menu_icon(instance.reference);
+ popover_menus.on_show_prep(instance);
+
+ const links_from_narrow = message_store
+ .topic_links_from_narrow(stream_id, topic_name)
+ .map((link) => get_cross_conversation_link_render_context(link));
+ const links_to_narrow = message_store
+ .topic_links_to_narrow(stream_id, topic_name)
+ .map((link) => get_cross_conversation_link_render_context(link));
+ const context = {
+ has_links_from_narrow: links_from_narrow.length > 0,
+ links_from_narrow,
+ has_links_to_narrow: links_to_narrow.length > 0,
+ links_to_narrow,
+ };
+ instance.setContent(
+ ui_util.parse_html(render_left_sidebar_topic_links_popover(context)),
+ );
+ },
+ onHidden(instance) {
+ instance.destroy();
+ popover_menus.popover_instances.topic_links = null;
+ },
+ });
+}
+
export function initialize(): void {
$("body").on("click", ".recipient-bar-copy-link-to-topic", (e) => {
e.preventDefault();
@@ -271,6 +345,15 @@ function register_popover_menu(target: string, placement: tippy.Placement): void
popover_menus.hide_current_popover_if_visible(instance);
});
+ $popper.on("click", ".sidebar-open-topic-links", () => {
+ const {stream_id, topic_name} = get_conversation(instance);
+ // Keep the same placement when replacing the popover.
+ const placement = tippyjs.get_actual_placement(instance);
+ const reference = instance.reference;
+ popover_menus.hide_current_popover_if_visible(instance);
+ open_cross_topic_links(stream_id, topic_name, placement, reference);
+ });
+
$popper.on(
"click",
".sidebar-popover-copy-link-to-topic",
diff --git a/web/styles/popovers.css b/web/styles/popovers.css
index c8236d1019f..e8fe6d607b5 100644
--- a/web/styles/popovers.css
+++ b/web/styles/popovers.css
@@ -1342,11 +1342,11 @@ ul.popover-menu-list {
list-style: none;
margin: 0;
padding: 4px 0;
+}
- li.popover-menu-separator {
- margin: 4px 0;
- border-bottom: solid 1px var(--color-border-popover-menu-separator);
- }
+.popover-menu-separator {
+ margin: 4px 0;
+ border-bottom: solid 1px var(--color-border-popover-menu-separator);
}
.popover-menu-hotkey-hints {
@@ -1416,3 +1416,64 @@ ul.popover-menu-list {
flex: 1;
min-width: 0;
}
+
+#topic-links-menu-popover {
+ color: var(--color-text-popover-menu) !important;
+
+ .popover-topic-links-list {
+ /* 3px at 15px/1em; with the separator's margin, this matches
+ the vertical rhythm of other popover menus. The larger bottom
+ padding stacks with the next section title's top padding to
+ leave a bigger gap between the two link sections. */
+ padding: 0.2em 0.6666em 0.6666em;
+ }
+
+ .topic-links-menu-title {
+ display: flex;
+ align-items: center;
+ gap: 5px;
+ /* 3px bottom at 15px/1em, matching other popover menus. */
+ padding: 0.6666em 0.6666em 0.2em;
+ font-weight: 600;
+ margin: 0;
+
+ .help_link_widget {
+ /* Override bootstrap defaults */
+ &:focus {
+ outline: none;
+ }
+
+ &:focus-visible {
+ /* Undo a.help_link_widget's 0.7 opacity so the outline isn't dimmed. */
+ opacity: 1;
+ outline: 2px solid var(--color-outline-focus);
+ outline-offset: 2px;
+ }
+ }
+ }
+
+ .topic-links-menu-title,
+ .popover-topic-links-list a {
+ width: fit-content;
+ max-width: 85vw;
+ white-space: nowrap;
+ }
+
+ .popover-topic-links-list a {
+ display: block;
+ text-overflow: ellipsis;
+ overflow: hidden;
+ /* External monitors sometimes cut off the edge of the message icon,
+ so add some extra margin. */
+ margin-right: 3px;
+ }
+
+ .popover-topic-links-list li + li {
+ margin-top: 0.5em;
+ }
+
+ ul {
+ margin: 0;
+ list-style: none;
+ }
+}
diff --git a/web/templates/popovers/left_sidebar/cross_conversation_link.hbs b/web/templates/popovers/left_sidebar/cross_conversation_link.hbs
new file mode 100644
index 00000000000..aa3ec5ba370
--- /dev/null
+++ b/web/templates/popovers/left_sidebar/cross_conversation_link.hbs
@@ -0,0 +1,9 @@
+<li>
+ {{~!-- Render each link identically to how it appears inline in
+ message content, reusing the markdown link templates. --~}}
+ {{~#if is_message_link~}}
+ {{> ../../channel_message_link stream=stream channel_name=channel_name topic_display_name_html=topic_display_name_html is_empty_string_topic=is_empty_string_topic href=url}}
+ {{~else~}}
+ {{> ../../topic_link channel_id=channel_id stream=stream channel_name=channel_name topic_display_name_html=topic_display_name_html is_empty_string_topic=is_empty_string_topic href=url}}
+ {{~/if~}}
+</li>
diff --git a/web/templates/popovers/left_sidebar/left_sidebar_topic_actions_popover.hbs b/web/templates/popovers/left_sidebar/left_sidebar_topic_actions_popover.hbs
index 9b28aeb29ee..8bb319da459 100644
--- a/web/templates/popovers/left_sidebar/left_sidebar_topic_actions_popover.hbs
+++ b/web/templates/popovers/left_sidebar/left_sidebar_topic_actions_popover.hbs
@@ -71,6 +71,14 @@
</a>
</li>
{{/if}}
+ {{#if has_topic_links}}
+ <li role="none" class="link-item popover-menu-list-item">
+ <a role="menuitem" class="sidebar-open-topic-links popover-menu-link" data-clipboard-text="{{ url }}" tabindex="0">
+ <i class="popover-menu-icon zulip-icon zulip-icon-link-2" aria-hidden="true"></i>
+ <span class="popover-menu-label">{{t "View links" }}</span>
+ </a>
+ </li>
+ {{/if}}
<li role="none" class="link-item popover-menu-list-item">
<a role="menuitem" class="sidebar-popover-copy-link-to-topic popover-menu-link" data-clipboard-text="{{ url }}" tabindex="0">
<i class="popover-menu-icon zulip-icon zulip-icon-link-alt" aria-hidden="true"></i>
diff --git a/web/templates/popovers/left_sidebar/left_sidebar_topic_links_popover.hbs b/web/templates/popovers/left_sidebar/left_sidebar_topic_links_popover.hbs
new file mode 100644
index 00000000000..27723a41f74
--- /dev/null
+++ b/web/templates/popovers/left_sidebar/left_sidebar_topic_links_popover.hbs
@@ -0,0 +1,32 @@
+<div class="popover-menu no-auto-hide-left-sidebar-overlay" id="topic-links-menu-popover" data-simplebar data-simplebar-tab-index="-1">
+ {{#if has_links_from_narrow}}
+ <div class="topic-links-menu-link-list">
+ <p class="topic-links-menu-title">
+ <i class="zulip-icon zulip-icon-arrow-up-right"></i>
+ {{t "Links to"}}
+ {{> ../../help_link_widget link="/help/link-to-a-message-or-conversation#view-links-to-and-from-a-conversation" }}
+ </p>
+ <div role="separator" class="popover-menu-separator"></div>
+ <ul class="popover-topic-links-list">
+ {{#each links_from_narrow}}
+ {{> ./cross_conversation_link this}}
+ {{/each}}
+ </ul>
+ </div>
+ {{/if}}
+ {{#if has_links_to_narrow}}
+ <div class="topic-links-menu-link-list">
+ <p class="topic-links-menu-title">
+ <i class="zulip-icon zulip-icon-arrow-down-left"></i>
+ {{t "Linked from"}}
+ {{> ../../help_link_widget link="/help/link-to-a-message-or-conversation#view-links-to-and-from-a-conversation" }}
+ </p>
+ <div role="separator" class="popover-menu-separator"></div>
+ <ul class="popover-topic-links-list">
+ {{#each links_to_narrow}}
+ {{> ./cross_conversation_link this}}
+ {{/each}}
+ </ul>
+ </div>
+ {{/if}}
+</div>
diff --git a/web/tests/message_store.test.cjs b/web/tests/message_store.test.cjs
index a44cc581497..ffc8a87d07f 100644
--- a/web/tests/message_store.test.cjs
+++ b/web/tests/message_store.test.cjs
@@ -9,7 +9,6 @@ const {mock_esm, set_global, zrequire} = require("./lib/namespace.cjs");
const {run_test, noop} = require("./lib/test.cjs");
const blueslip = require("./lib/zblueslip.cjs");
-const stream_data = zrequire("stream_data");
mock_esm("../src/electron_bridge", {
electron_bridge: {},
});
@@ -31,7 +30,9 @@ const pm_conversations = zrequire("pm_conversations");
const message_helper = zrequire("message_helper");
const message_store = zrequire("message_store");
const message_user_ids = zrequire("message_user_ids");
+const muted_users = zrequire("muted_users");
const {set_realm} = zrequire("state_data");
+const stream_data = zrequire("stream_data");
const {initialize_user_settings} = zrequire("user_settings");
set_realm(make_realm());
@@ -572,3 +573,889 @@ test("maybe_update_raw_content", () => {
message_store.maybe_update_raw_content(message1.id, "bye world");
assert.equal(message1.raw_content, "hello world");
});
+
+test("save_topic_links", () => {
+ function assert_maps_empty() {
+ assert.deepEqual(message_store.topic_links_by_to_for_testing(), new Map());
+ assert.deepEqual(message_store.topic_links_by_from_for_testing(), new Map());
+ }
+
+ function message_with_content(content) {
+ return {
+ id: 5,
+ type: "stream",
+ stream_id: 9,
+ sender_id: bob.user_id,
+ topic: "worldly issues",
+ content,
+ };
+ }
+
+ // If the link isn't a narrow link, it doesn't save anything.
+ let link = "bad-link";
+ message_store.save_topic_links(message_with_content(`<div><a href='${link}'>a link!</a>`));
+ assert_maps_empty();
+
+ // Malformed link doesn't throw an error
+ link = "/#narrow/which-channel/10-design/bad-topic/hello/";
+ message_store.save_topic_links(message_with_content(`<div><a href='${link}'>a link!</a>`));
+ assert_maps_empty();
+
+ // A message for a stream we don't know exists (including private streams)
+ // doesn't save anything.
+ link = "/#narrow/channel/10-design/topic/hello/";
+ message_store.save_topic_links(message_with_content(`<div><a href='${link}'>a link!</a>`));
+ assert_maps_empty();
+
+ // If the stream does exist and we know about it, we save the data.
+ stream_data.add_sub_for_tests({name: "design", subscribed: true, stream_id: 10});
+ let message = message_with_content(`<div><a href='${link}'>a link!</a>`);
+ message_store.save_topic_links(message);
+ assert.deepEqual(
+ message_store.topic_links_by_to_for_testing(),
+ new Map([[10, new Map([["hello", new Map([[0, [message.id]]])]])]]),
+ );
+ assert.deepEqual(
+ message_store.topic_links_by_from_for_testing(),
+ new Map([
+ [
+ message.stream_id,
+ new Map([
+ [
+ message.topic,
+ new Map([
+ [
+ message.id,
+ [
+ {
+ stream_id: 10,
+ topic: "hello",
+ message_id: undefined,
+ },
+ ],
+ ],
+ ]),
+ ],
+ ]),
+ ],
+ ]),
+ );
+
+ message_store.clear_topic_links_for_testing();
+
+ // A link to a message we don't know about doesn't save
+ link = "/#narrow/channel/10-design/topic/hello/near/22";
+ message = message_with_content(`<div><a href='${link}'>a link!</a>`);
+ message_store.save_topic_links(message);
+ assert_maps_empty();
+
+ // A link to a message we do know about does save
+ const linked_message = {
+ id: 22,
+ sender_id: alice.user_id,
+ type: "stream",
+ stream_id: 10,
+ topic: "hello",
+ content: "<ignore>",
+ };
+ message_store.update_message_cache({
+ type: "server_message",
+ message: linked_message,
+ });
+ message_store.save_topic_links(message);
+ assert.deepEqual(
+ message_store.topic_links_by_to_for_testing(),
+ new Map([[10, new Map([["hello", new Map([[linked_message.id, [message.id]]])]])]]),
+ );
+ assert.deepEqual(
+ message_store.topic_links_by_from_for_testing(),
+ new Map([
+ [
+ message.stream_id,
+ new Map([
+ [
+ message.topic,
+ new Map([
+ [
+ message.id,
+ [
+ {
+ stream_id: 10,
+ topic: "hello",
+ message_id: linked_message.id,
+ },
+ ],
+ ],
+ ]),
+ ],
+ ]),
+ ],
+ ]),
+ );
+ message_store.clear_topic_links_for_testing();
+
+ // A link to a message from a muted user doesn't save
+ muted_users.set_muted_users([{id: linked_message.sender_id}]);
+ message_store.save_topic_links(message);
+ assert_maps_empty();
+
+ // A link from a message from a muted user doesn't save
+ muted_users.set_muted_users([{id: message.sender_id}]);
+ message_store.save_topic_links(message);
+ assert_maps_empty();
+
+ muted_users.set_muted_users([]);
+});
+
+test("get and update topic links to/from narrow", () => {
+ function save_message_link(from_stream_id, from_topic, from_message_id, to_link) {
+ if (from_message_id) {
+ const from_message = message_store.get(from_message_id);
+ assert.ok(from_message !== undefined);
+ assert.ok(from_message.stream_id === from_stream_id);
+ assert.ok(from_message.topic === from_topic);
+ }
+ message_store.save_topic_links({
+ id: from_message_id,
+ type: "stream",
+ stream_id: from_stream_id,
+ sender_id: bob.user_id,
+ topic: from_topic,
+ content: `<div><a href='${to_link}'>a link!</a>`,
+ });
+ }
+
+ function add_message(message_id, stream_id, topic) {
+ const message = {
+ id: message_id,
+ sender_id: alice.user_id,
+ type: "stream",
+ stream_id,
+ topic,
+ content: "<ignore>",
+ };
+ message_store.update_message_cache({
+ type: "server_message",
+ message,
+ });
+ return message;
+ }
+
+ const design = {name: "design", subscribed: true, stream_id: 10};
+ stream_data.add_sub_for_tests(design);
+ const development = {name: "development", subscribed: true, stream_id: 11};
+ stream_data.add_sub_for_tests(development);
+ const sales = {name: "sales", subscribed: true, stream_id: 12};
+ stream_data.add_sub_for_tests(sales);
+
+ // set_message_links([]);
+ const message1 = add_message(201, development.stream_id, "engineering");
+ const message2 = add_message(5, design.stream_id, "goodbye");
+ const message3 = add_message(100, development.stream_id, "engineering");
+ const message4 = add_message(99, development.stream_id, "engineering");
+ const message5 = add_message(3, sales.stream_id, "new client");
+ const message6 = add_message(20, sales.stream_id, "new client");
+ const message7 = add_message(400, development.stream_id, "testing");
+ const NO_MESSAGE_ID = 0;
+
+ // Save a link from a message in #development>engineering
+ save_message_link(
+ development.stream_id,
+ "engineering",
+ message1.id,
+ "/#narrow/channel/10-design/topic/goodbye/",
+ );
+ // Test that we ignore when the exact same data added again
+ // (e.g. two links to the same narrow in the same message)
+ save_message_link(
+ development.stream_id,
+ "engineering",
+ message1.id,
+ "/#narrow/channel/10-design/topic/goodbye/",
+ );
+ assert.deepEqual(
+ message_store.topic_links_by_to_for_testing(),
+ new Map([
+ [design.stream_id, new Map([["goodbye", new Map([[NO_MESSAGE_ID, [message1.id]]])]])],
+ ]),
+ );
+ assert.deepEqual(
+ message_store.topic_links_by_from_for_testing(),
+ new Map([
+ [
+ development.stream_id,
+ new Map([
+ [
+ "engineering",
+ new Map([
+ [
+ message1.id,
+ [
+ {
+ stream_id: design.stream_id,
+ topic: "goodbye",
+ message_id: undefined,
+ },
+ ],
+ ],
+ ]),
+ ],
+ ]),
+ ],
+ ]),
+ );
+
+ // Save a second link *to* the same narrow, but *from* a different narrow
+ save_message_link(
+ sales.stream_id,
+ "new client",
+ message5.id,
+ "/#narrow/channel/10-design/topic/goodbye/",
+ );
+ // Save a second link *from* the same message, but *to* a different narrow
+ save_message_link(
+ development.stream_id,
+ "engineering",
+ message1.id,
+ "/#narrow/channel/10-design/topic/second-goodbye/",
+ );
+ assert.deepEqual(
+ message_store.topic_links_by_to_for_testing(),
+ new Map([
+ [
+ design.stream_id,
+ new Map([
+ [
+ "goodbye",
+ new Map([
+ // from message5 is new
+ [NO_MESSAGE_ID, [message1.id, message5.id]],
+ ]),
+ ],
+ // to "second-goodbye" is new
+ ["second-goodbye", new Map([[NO_MESSAGE_ID, [message1.id]]])],
+ ]),
+ ],
+ ]),
+ );
+ assert.deepEqual(
+ message_store.topic_links_by_from_for_testing(),
+ new Map([
+ [
+ development.stream_id,
+ new Map([
+ [
+ "engineering",
+ new Map([
+ [
+ message1.id,
+ [
+ {
+ stream_id: design.stream_id,
+ topic: "goodbye",
+ message_id: undefined,
+ },
+ // to "second-goodbye" is new
+ {
+ stream_id: design.stream_id,
+ topic: "second-goodbye",
+ message_id: undefined,
+ },
+ ],
+ ],
+ ]),
+ ],
+ ]),
+ ],
+ // from sales is new
+ [
+ sales.stream_id,
+ new Map([
+ [
+ "new client",
+ new Map([
+ [
+ message5.id,
+ [
+ {
+ stream_id: design.stream_id,
+ topic: "goodbye",
+ message_id: undefined,
+ },
+ ],
+ ],
+ ]),
+ ],
+ ]),
+ ],
+ ]),
+ );
+
+ // Save a link from an earlier message in #development (smaller id). Even though
+ // we saved the later message first, this should be sorted to show up first in
+ // the list of outgoing links.
+ assert.ok(message3.id < message1.id);
+ save_message_link(
+ development.stream_id,
+ "engineering",
+ message3.id,
+ "/#narrow/channel/10-design/topic/hello/",
+ );
+ assert.deepEqual(
+ message_store.topic_links_by_to_for_testing(),
+ new Map([
+ [
+ design.stream_id,
+ new Map([
+ // Link to "hello" is new and earlier
+ ["hello", new Map([[NO_MESSAGE_ID, [message3.id]]])],
+ ["goodbye", new Map([[NO_MESSAGE_ID, [message1.id, message5.id]]])],
+ ["second-goodbye", new Map([[NO_MESSAGE_ID, [message1.id]]])],
+ ]),
+ ],
+ ]),
+ );
+ assert.deepEqual(
+ message_store.topic_links_by_from_for_testing(),
+ new Map([
+ [
+ development.stream_id,
+ new Map([
+ [
+ "engineering",
+ new Map([
+ // message3 is new and earlier
+ [
+ message3.id,
+ [
+ {
+ stream_id: design.stream_id,
+ topic: "hello",
+ message_id: undefined,
+ },
+ ],
+ ],
+ [
+ message1.id,
+ [
+ {
+ stream_id: design.stream_id,
+ topic: "goodbye",
+ message_id: undefined,
+ },
+ {
+ stream_id: design.stream_id,
+ topic: "second-goodbye",
+ message_id: undefined,
+ },
+ ],
+ ],
+ ]),
+ ],
+ ]),
+ ],
+ [
+ sales.stream_id,
+ new Map([
+ [
+ "new client",
+ new Map([
+ [
+ message5.id,
+ [
+ {
+ stream_id: design.stream_id,
+ topic: "goodbye",
+ message_id: undefined,
+ },
+ ],
+ ],
+ ]),
+ ],
+ ]),
+ ],
+ ]),
+ );
+
+ // A link from the same narrow to the same narrow, but in a different message.
+ // In links to this narrow we only keep the message with the smaller/earlier id
+ // (which will be this one). In links from this narrow, we show the link only
+ // once even though it's linked from multiple messages. But the high level topic
+ // maps will have data from both messages.
+ assert.deepEqual(message_store.topic_links_to_narrow(design.stream_id, "hello"), [
+ {stream_id: development.stream_id, topic: "engineering", message_id: message3.id},
+ ]);
+ // There are currently three links from #development>engineering
+ assert.equal(
+ message_store.topic_links_from_narrow(development.stream_id, "engineering").length,
+ 3,
+ );
+ // Now save an earlier message with the same link
+ assert.ok(message4.id < message3.id);
+ save_message_link(
+ development.stream_id,
+ "engineering",
+ message4.id,
+ "/#narrow/channel/10-design/topic/hello/",
+ );
+ // Now only message4 will show up, since it occurs first.
+ assert.deepEqual(message_store.topic_links_to_narrow(design.stream_id, "hello"), [
+ {stream_id: development.stream_id, topic: "engineering", message_id: message4.id},
+ ]);
+ // * There are still only three links from #development>engineering, since message4's
+ // "hello" link was redundant.
+ // * We sort the data by message id, so message 99 to "hello" is listed before message
+ // 201 to goodbye and second-goodbye, even though we added the "hello" link after.
+ assert.deepEqual(message_store.topic_links_from_narrow(development.stream_id, "engineering"), [
+ {stream_id: design.stream_id, topic: "hello", message_id: undefined},
+ {stream_id: design.stream_id, topic: "goodbye", message_id: undefined},
+ {stream_id: design.stream_id, topic: "second-goodbye", message_id: undefined},
+ ]);
+
+ // The high level topic maps will have data from both messages.
+ assert.deepEqual(
+ message_store.topic_links_by_to_for_testing(),
+ new Map([
+ [
+ design.stream_id,
+ new Map([
+ [
+ "hello",
+ new Map([
+ // message4 is new
+ [NO_MESSAGE_ID, [message3.id, message4.id]],
+ ]),
+ ],
+ ["goodbye", new Map([[NO_MESSAGE_ID, [message1.id, message5.id]]])],
+ ["second-goodbye", new Map([[NO_MESSAGE_ID, [message1.id]]])],
+ ]),
+ ],
+ ]),
+ );
+ assert.deepEqual(
+ message_store.topic_links_by_from_for_testing(),
+ new Map([
+ [
+ development.stream_id,
+ new Map([
+ [
+ "engineering",
+ new Map([
+ [
+ message3.id,
+ [
+ {
+ stream_id: design.stream_id,
+ topic: "hello",
+ message_id: undefined,
+ },
+ ],
+ ],
+ [
+ message1.id,
+ [
+ {
+ stream_id: design.stream_id,
+ topic: "goodbye",
+ message_id: undefined,
+ },
+ {
+ stream_id: design.stream_id,
+ topic: "second-goodbye",
+ message_id: undefined,
+ },
+ ],
+ ],
+ // message4 is new
+ [
+ message4.id,
+ [
+ {
+ stream_id: design.stream_id,
+ topic: "hello",
+ message_id: undefined,
+ },
+ ],
+ ],
+ ]),
+ ],
+ ]),
+ ],
+ [
+ sales.stream_id,
+ new Map([
+ [
+ "new client",
+ new Map([
+ [
+ message5.id,
+ [
+ {
+ stream_id: design.stream_id,
+ topic: "goodbye",
+ message_id: undefined,
+ },
+ ],
+ ],
+ ]),
+ ],
+ ]),
+ ],
+ ]),
+ );
+
+ // Save a link to a specific message.
+ save_message_link(
+ development.stream_id,
+ "testing",
+ message7.id,
+ "/#narrow/channel/10-design/topic/goodbye/near/5",
+ );
+ // A link to a whole channel (no topic) isn't tracked at all, so it
+ // won't be saved in either link map (asserted below).
+ save_message_link(sales.stream_id, "new client", message5.id, "/#narrow/channel/10-design");
+ // Links from a topic to itself are ignored when we fetch to/from links for a narrow,
+ // but we still save them in case those messages are moved later (and, in a new narrow,
+ // could stop being a link from a narrrow to itself).
+ save_message_link(
+ sales.stream_id,
+ "new client",
+ message5.id,
+ "/#narrow/channel/12-sales/topic/new.20client/",
+ );
+ save_message_link(
+ sales.stream_id,
+ "new client",
+ message6.id,
+ `/#narrow/channel/12-sales/topic/new.20client/near/${message5.id}`,
+ );
+
+ assert.deepEqual(
+ message_store.topic_links_by_to_for_testing(),
+ new Map([
+ [
+ design.stream_id,
+ new Map([
+ ["hello", new Map([[NO_MESSAGE_ID, [message3.id, message4.id]]])],
+ [
+ "goodbye",
+ new Map([
+ [NO_MESSAGE_ID, [message1.id, message5.id]],
+ // This link to a specific message is new
+ [5, [message7.id]],
+ ]),
+ ],
+ ["second-goodbye", new Map([[NO_MESSAGE_ID, [message1.id]]])],
+ ]),
+ ],
+ // The links to sales are new
+ [
+ sales.stream_id,
+ new Map([
+ [
+ "new client",
+ new Map([
+ [NO_MESSAGE_ID, [message5.id]],
+ [message5.id, [message6.id]],
+ ]),
+ ],
+ ]),
+ ],
+ ]),
+ );
+ assert.deepEqual(
+ message_store.topic_links_by_from_for_testing(),
+ new Map([
+ [
+ development.stream_id,
+ new Map([
+ [
+ "engineering",
+ new Map([
+ [
+ message3.id,
+ [
+ {
+ stream_id: design.stream_id,
+ topic: "hello",
+ message_id: undefined,
+ },
+ ],
+ ],
+ [
+ message1.id,
+ [
+ {
+ stream_id: design.stream_id,
+ topic: "goodbye",
+ message_id: undefined,
+ },
+ {
+ stream_id: design.stream_id,
+ topic: "second-goodbye",
+ message_id: undefined,
+ },
+ ],
+ ],
+ [
+ message4.id,
+ [
+ {
+ stream_id: design.stream_id,
+ topic: "hello",
+ message_id: undefined,
+ },
+ ],
+ ],
+ ]),
+ ],
+ // link from "testing" to a specific message is new
+ [
+ "testing",
+ new Map([
+ [
+ message7.id,
+ [
+ {
+ stream_id: design.stream_id,
+ topic: "goodbye",
+ message_id: 5,
+ },
+ ],
+ ],
+ ]),
+ ],
+ ]),
+ ],
+ [
+ sales.stream_id,
+ new Map([
+ [
+ "new client",
+ new Map([
+ [
+ message5.id,
+ [
+ {
+ stream_id: design.stream_id,
+ topic: "goodbye",
+ message_id: undefined,
+ },
+ // The link to the #design channel (no topic) is not tracked.
+ // Link to #sales>new-client is new
+ {
+ stream_id: sales.stream_id,
+ topic: "new client",
+ message_id: undefined,
+ },
+ ],
+ ],
+ // Link from message6 is new
+ [
+ message6.id,
+ [
+ {
+ stream_id: sales.stream_id,
+ topic: "new client",
+ message_id: message5.id,
+ },
+ ],
+ ],
+ ]),
+ ],
+ ]),
+ ],
+ ]),
+ );
+
+ // ------ Now let's test `topic_links_from_narrow` and `topic_links_to_narrow`
+
+ // Test empty cases.
+ assert.deepEqual(
+ message_store.topic_links_from_narrow(development.stream_id, "nothing here"),
+ [],
+ );
+ assert.deepEqual(
+ message_store.topic_links_to_narrow(development.stream_id, "nothing here"),
+ [],
+ );
+
+ // Links are sorted by message id even though the earlier message
+ // was added after the later message.
+ assert.deepEqual(message_store.topic_links_to_narrow(design.stream_id, "goodbye"), [
+ {stream_id: sales.stream_id, topic: "new client", message_id: 3},
+ {stream_id: development.stream_id, topic: "engineering", message_id: 201},
+ {stream_id: development.stream_id, topic: "testing", message_id: 400},
+ ]);
+
+ // Links from #sales>new-client to #sales>new-client don't show up in either list
+ assert.deepEqual(message_store.topic_links_from_narrow(sales.stream_id, "new client"), [
+ {stream_id: design.stream_id, topic: "goodbye", message_id: undefined},
+ ]);
+ assert.deepEqual(message_store.topic_links_to_narrow(sales.stream_id, "new client"), []);
+
+ // ------ Now let's test editing a message's topic
+ message_store.process_topic_edit({
+ message_ids: [message1.id],
+ // previously #development
+ new_stream_id: sales.stream_id,
+ // previously "engineering"
+ new_topic: "super engineering",
+ });
+ // Mirroring how message_events.ts updates the message object after.
+ message1.stream_id = sales.stream_id;
+ message1.topic = "super engineering";
+ assert.deepEqual(message_store.topic_links_to_narrow(design.stream_id, "goodbye"), [
+ {stream_id: sales.stream_id, topic: "new client", message_id: 3},
+ // This existing link is updated to the new stream and topic for message 201
+ {stream_id: sales.stream_id, topic: "super engineering", message_id: 201},
+ {stream_id: development.stream_id, topic: "testing", message_id: 400},
+ ]);
+
+ // This message has the link to the message with id 400 in #development>testing
+ // and we'll move it to a new stream/topic.
+ message_store.process_topic_edit({
+ message_ids: [message2.id],
+ // previously #design
+ new_stream_id: sales.stream_id,
+ // previously "goodbye"
+ new_topic: "signing off",
+ });
+ // Mirroring how message_events.ts updates the message object after.
+ message2.stream_id = sales.stream_id;
+ message2.topic = "signing off";
+ assert.deepEqual(
+ message_store.topic_links_to_narrow(design.stream_id, "goodbye"),
+ // Links to the original narrow aren't affected
+ [
+ {stream_id: sales.stream_id, topic: "new client", message_id: 3},
+ {stream_id: sales.stream_id, topic: "super engineering", message_id: 201},
+ ],
+ );
+ assert.deepEqual(message_store.topic_links_to_narrow(sales.stream_id, "signing off"), [
+ // This link from the moved message now links from the new topic
+ {stream_id: development.stream_id, topic: "testing", message_id: 400},
+ ]);
+
+ // Moving message5, which has both these links in it (which you can see above in the map)
+ assert.deepEqual(message_store.topic_links_from_narrow(sales.stream_id, "new client"), [
+ {stream_id: design.stream_id, topic: "goodbye", message_id: undefined},
+ ]);
+ message_store.process_topic_edit({
+ message_ids: [message5.id],
+ // previously #sales
+ new_stream_id: design.stream_id,
+ // previously "new client"
+ new_topic: "client logo",
+ });
+ // Mirroring how message_events.ts updates the message object after.
+ message5.stream_id = design.stream_id;
+ message5.topic = "client logo";
+ // message5's own links moved with it, but message6's link to message5
+ // (which was hidden as a link to its own topic) now shows up, following
+ // message5 to its new conversation.
+ assert.deepEqual(message_store.topic_links_from_narrow(sales.stream_id, "new client"), [
+ {stream_id: design.stream_id, topic: "client logo", message_id: message5.id},
+ ]);
+ // Now they're both in the new stream/topic links, but we get an extra link too!
+ assert.deepEqual(message_store.topic_links_from_narrow(design.stream_id, "client logo"), [
+ {stream_id: design.stream_id, topic: "goodbye", message_id: undefined},
+ // This was an existing link to the old topic from message5. It still links to the
+ // old topic (the topic still exists, separately from the message that was moved)
+ // and previously wasn't showing up because it was a link from a topic to itself.
+ // Now it does show up here because it's a link to a topic different from itself,
+ // now that the message is moved.
+ {stream_id: sales.stream_id, topic: "new client", message_id: undefined},
+ ]);
+
+ // ------ Now let's test editing a message's id
+ // We already have a link to this message, but let's also make a link from
+ // this message (to a topic) to test that both update correctly.
+ save_message_link(
+ sales.stream_id,
+ "signing off",
+ message2.id,
+ "/#narrow/channel/10-design/topic/goodbye/",
+ );
+ assert.deepEqual(message_store.topic_links_from_narrow(development.stream_id, "testing"), [
+ {stream_id: sales.stream_id, topic: "signing off", message_id: 5},
+ ]);
+ assert.deepEqual(message_store.topic_links_from_narrow(sales.stream_id, "signing off"), [
+ {stream_id: design.stream_id, topic: "goodbye", message_id: undefined},
+ ]);
+ message_store.reify_message_id({old_id: message2.id, new_id: 55});
+ assert.deepEqual(message_store.topic_links_from_narrow(development.stream_id, "testing"), [
+ {stream_id: sales.stream_id, topic: "signing off", message_id: 55},
+ ]);
+ // This looks the same but is now stored under a message by a different id,
+ // which will let us delete it shortly.
+ assert.deepEqual(message_store.topic_links_from_narrow(sales.stream_id, "signing off"), [
+ {stream_id: design.stream_id, topic: "goodbye", message_id: undefined},
+ ]);
+
+ // ------ Now let's test removing a message
+ message_store.remove([55]);
+ assert.deepEqual(message_store.topic_links_from_narrow(development.stream_id, "testing"), []);
+ assert.deepEqual(message_store.topic_links_from_narrow(sales.stream_id, "signing off"), []);
+
+ // ------ Now let's test that links to a message in the same conversation
+ // stay hidden when the whole conversation moves (e.g. the topic is resolved).
+ const message8 = add_message(500, development.stream_id, "budget");
+ const message9 = add_message(501, development.stream_id, "budget");
+ // A link to a message in the same topic (e.g. from quoting it) isn't shown.
+ save_message_link(
+ development.stream_id,
+ "budget",
+ message9.id,
+ `/#narrow/channel/11-development/topic/budget/near/${message8.id}`,
+ );
+ assert.deepEqual(message_store.topic_links_from_narrow(development.stream_id, "budget"), []);
+ // Resolve the topic, i.e. move all of its messages to "✔ budget".
+ message_store.process_topic_edit({
+ message_ids: [message8.id, message9.id],
+ new_stream_id: development.stream_id,
+ new_topic: "✔ budget",
+ });
+ // Mirroring how message_events.ts updates the message objects after.
+ message8.topic = "✔ budget";
+ message9.topic = "✔ budget";
+ // The linked message is still in the same conversation, so the link
+ // stays hidden even though it was saved under the unresolved topic name.
+ assert.deepEqual(message_store.topic_links_from_narrow(development.stream_id, "✔ budget"), []);
+ assert.deepEqual(message_store.topic_links_to_narrow(development.stream_id, "✔ budget"), []);
+ // But once the linked message moves to another conversation, the link
+ // is shown, pointing to the message's new location.
+ message_store.process_topic_edit({
+ message_ids: [message8.id],
+ new_stream_id: design.stream_id,
+ new_topic: "spending",
+ });
+ // Mirroring how message_events.ts updates the message object after.
+ message8.stream_id = design.stream_id;
+ message8.topic = "spending";
+ assert.deepEqual(message_store.topic_links_from_narrow(development.stream_id, "✔ budget"), [
+ {stream_id: design.stream_id, topic: "spending", message_id: message8.id},
+ ]);
+ assert.deepEqual(message_store.topic_links_to_narrow(design.stream_id, "spending"), [
+ {stream_id: development.stream_id, topic: "✔ budget", message_id: message9.id},
+ ]);
+});
+
+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.
+ const missing_message_id = 987654;
+ assert.equal(message_store.get(missing_message_id), undefined);
+
+ message_store.process_topic_edit({
+ message_ids: [missing_message_id],
+ new_stream_id: 10,
+ new_topic: "moved",
+ });
+
+ assert.deepEqual(message_store.topic_links_by_from_for_testing(), new Map());
+ assert.deepEqual(message_store.topic_links_by_to_for_testing(), new Map());
+});