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.

47.0 KB
Raw Blame
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";
import type {UserStatusEmojiInfo} from "./user_status.ts";
import * as util from "./util.ts";

const stored_messages = new Map<number, ProcessedMessage>();

const matched_message_schema = z.object({
    match_content: z.optional(z.string()),
    match_subject: z.optional(z.string()),
});

export type MatchedMessage = z.infer<typeof matched_message_schema>;

const message_reaction_type_schema = z.enum(["unicode_emoji", "realm_emoji", "zulip_extra_emoji"]);

export type MessageReactionType = z.infer<typeof message_reaction_type_schema>;

const display_recipient_user_schema = z.object({
    email: z.string(),
    full_name: z.string(),
    id: z.number(),
});

export type DisplayRecipientUser = z.infer<typeof display_recipient_user_schema>;

const display_recipient_schema = z.union([z.string(), z.array(display_recipient_user_schema)]);

export type DisplayRecipient = z.infer<typeof display_recipient_schema>;

const message_edit_history_entry_schema = z.object({
    user_id: z.nullable(z.number()),
    timestamp: z.number(),
    prev_content: z.optional(z.string()),
    prev_rendered_content: z.optional(z.string()),
    prev_stream: z.optional(z.number()),
    prev_topic: z.optional(z.string()),
    stream: z.optional(z.number()),
    topic: z.optional(z.string()),
});

export type MessageEditHistoryEntry = z.infer<typeof message_edit_history_entry_schema>;

const message_reaction_schema = z.object({
    emoji_name: z.string(),
    emoji_code: z.string(),
    reaction_type: message_reaction_type_schema,
    user_id: z.number(),
});

export type MessageReaction = z.infer<typeof message_reaction_schema>;

export const single_message_content_schema = z.object({
    message: z.object({
        content: z.string(),
        content_type: z.enum(["text/html", "text/x-markdown"]),
    }),
});

export const message_render_response_schema = z.object({
    msg: z.string(),
    result: z.string(),
    rendered: z.string(),
});

export const submessage_schema = z.object({
    id: z.number(),
    sender_id: z.number(),
    message_id: z.number(),
    content: z.string(),
    msg_type: z.string(),
});

export const raw_message_schema = z.intersection(
    z.intersection(
        z.object({
            avatar_url: z.nullable(z.string()),
            client: z.string(),
            content: z.string(),
            content_type: z.enum(["text/html", "text/x-markdown"]),
            display_recipient: display_recipient_schema,
            edit_history: z.optional(z.array(message_edit_history_entry_schema)),
            id: z.number(),
            is_me_message: z.boolean(),
            last_edit_timestamp: z.optional(z.number()),
            last_moved_timestamp: z.optional(z.number()),
            reactions: z.array(message_reaction_schema),
            sender_email: z.string(),
            sender_full_name: z.string(),
            sender_id: z.number(),
            // The web app doesn't use sender_realm_str; ignore.
            // sender_realm_str: z.string(),
            submessages: z.array(submessage_schema),
            timestamp: z.number(),
            flags: z.array(z.string()),
        }),
        z.discriminatedUnion("type", [
            z.object({
                type: z.literal("private"),
                topic_links: z.optional(z.array(z.never())),
            }),
            z.object({
                type: z.literal("stream"),
                stream_id: z.number(),
                // Messages that come from the server use `subject`.
                // Messages that come from `send_message` use `topic`.
                subject: z.optional(z.string()),
                topic: z.optional(z.string()),
                topic_links: z.array(topic_link_schema),
            }),
        ]),
    ),
    matched_message_schema,
);

export type RawMessage = z.infer<typeof raw_message_schema>;

// We add these boolean properties to Raw message in
// `message_store.convert_raw_message_to_message_with_booleans` method.
type Booleans = {
    unread: boolean;
    historical: boolean;
    starred: boolean;
    mentioned: boolean;
    mentioned_me_directly: boolean;
    stream_wildcard_mentioned: boolean;
    topic_wildcard_mentioned: boolean;
    collapsed: boolean;
    hide_link_previews: boolean;
    condensed?: boolean;
    alerted: boolean;
};

type RawMessageWithBooleans = (
    Omit<RawMessage & {type: "private"}, "flags"> | Omit<RawMessage & {type: "stream"}, "flags">
) &
    Booleans;

type LocalMessageWithBooleans = (
    | Omit<RawLocalMessage & {type: "private"}, "flags">
    | Omit<RawLocalMessage & {type: "stream"}, "flags">
) &
    Booleans;

export type MessageWithBooleans = RawMessageWithBooleans | LocalMessageWithBooleans;

export type MessageCleanReaction = {
    class: string;
    count: number;
    emoji_alt_code: boolean;
    emoji_code: string;
    emoji_name: string;
    is_realm_emoji: boolean;
    label: string;
    local_id: string;
    reaction_type: "zulip_extra_emoji" | "realm_emoji" | "unicode_emoji";
    user_ids: number[];
    vote_text: string;
};

export type Message = (
    | Omit<RawMessageWithBooleans & {type: "private"}, "reactions">
    | Omit<RawMessageWithBooleans & {type: "stream"}, "reactions" | "subject">
) & {
    clean_reactions: Map<string, MessageCleanReaction>;

    // Local echo state cluster of fields.
    locally_echoed?: boolean;
    failed_request?: boolean;
    show_slow_send_spinner?: boolean;
    resend?: boolean;
    local_id?: string;

    // The original markup for the message, which we'll have if we
    // sent it or if we fetched it (usually, because the current user
    // tried to edit the message).
    raw_content?: string | undefined;

    // Added in `message_helper.process_new_message`.
    sent_by_me: boolean;
    reply_to: string;

    // These properties are set and used in `message_list_view.ts`.
    // TODO: It would be nice if we could not store these on the message
    // object and only reference them within `message_list_view`.
    message_reactions?: MessageCleanReaction[];
    url?: string;

    // Used in `markdown.js`, `server_events.js`, and
    // `convert_raw_message_to_message_with_booleans`
    flags?: string[];

    // Used in `message_avatar.hbs` to render sender avatar in
    // message list.
    small_avatar_url?: string | null;

    // Used in `message_body.hbs` to show sender status emoji alongside
    // their name in message list.
    status_emoji_info?: UserStatusEmojiInfo | undefined;

    // Used for edited messages to show their last edit time.
    local_edit_timestamp?: number;

    // Used in message_notifications to track if a notification has already
    // been sent for this message.
    notification_sent?: boolean;

    // Added during message rendering in message_list_view.ts. Should
    // never be accessed outside rendering, as the value may be stale.
    reminders?: TimeFormattedReminder[] | undefined;

    // Cache for whether the message has widget edits (e.g. poll question changes).
    has_widget_edits?: boolean;
} & (
        | {
              type: "private";
              is_private: true;
              is_stream: false;
              pm_with_url: string;
              to_user_ids: string;
              display_reply_to: string;
          }
        | {
              type: "stream";
              is_private: false;
              is_stream: true;
              stream: string;
              topic: string;
              display_reply_to: undefined;
          }
    );

export function update_message_cache(message_data: ProcessedMessage): void {
    // You should only call this from message_helper (or in tests), for
    // messages that aren't in the store yet.
    stored_messages.set(message_data.message.id, message_data);
    resolve_pending_links_to_message(message_data.message);
    save_topic_links(message_data.message);
}

export function get_cached_message(message_id: number): ProcessedMessage | undefined {
    // You should only call this from message_helper.
    // Use the get() wrapper below for most other use cases.
    return stored_messages.get(message_id);
}

export function clear_for_testing(): void {
    stored_messages.clear();
}

// This can return a LocalMessage, but unless anything needs that,
// it's easier to type it as just returning a Message.
// TODO: If we finish converting to typescript and find that
// nothing needs LocalMessage, explicitly remove its extra fields
// here before returning the Message.
export function get(message_id: number): Message | undefined {
    return stored_messages.get(message_id)?.message;
}

export function set_messages_for_tests(messages: ProcessedMessage[]): void {
    stored_messages.clear();
    for (const message of messages) {
        stored_messages.set(message.message.id, message);
    }
}

export function get_pm_emails(
    message: Message | MessageWithBooleans | LocalMessageWithBooleans,
): string {
    const user_ids = people.pm_with_user_ids(message) ?? [];
    const emails = user_ids.map((user_id) => {
        const person = people.maybe_get_user_by_id(user_id);
        if (!person) {
            blueslip.error("Unknown user id", {user_id});
            return "?";
        }
        return person.email;
    });
    emails.sort();

    return emails.join(", ");
}

export function get_pm_full_names(user_ids: number[]): string {
    user_ids = people.sorted_other_user_ids(user_ids);
    const sorted_names = people.get_display_full_names(user_ids);
    sorted_names.sort(util.make_strcmp());

    return sorted_names.join(", ");
}

export function convert_raw_message_to_message_with_booleans(opts: NewMessage):
    | {
          type: "server_message";
          message: RawMessageWithBooleans;
      }
    | {
          type: "local_message";
          message: LocalMessageWithBooleans;
      } {
    const flags = opts.raw_message.flags ?? [];

    function convert_flag(flag_name: string): boolean {
        return flags.includes(flag_name);
    }

    const converted_flags = {
        unread: !convert_flag("read"),
        historical: convert_flag("historical"),
        starred: convert_flag("starred"),
        mentioned:
            convert_flag("mentioned") ||
            convert_flag("stream_wildcard_mentioned") ||
            convert_flag("topic_wildcard_mentioned"),
        mentioned_me_directly: convert_flag("mentioned"),
        stream_wildcard_mentioned: convert_flag("stream_wildcard_mentioned"),
        topic_wildcard_mentioned: convert_flag("topic_wildcard_mentioned"),
        collapsed: convert_flag("collapsed"),
        hide_link_previews: convert_flag("hide_link_previews"),
        alerted: convert_flag("has_alert_word"),
    };

    // Once we have set boolean flags here, the `flags` attribute is
    // just a distraction, so we delete it.  (All the downstream code
    // uses booleans.)

    // We have to return these separately because of how the `MessageWithBooleans`
    // type is set up.
    if (opts.type === "local_message") {
        if (opts.raw_message.type === "private") {
            return {
                type: "local_message",
                message: {
                    ..._.omit(opts.raw_message, "flags"),
                    ...converted_flags,
                },
            };
        }
        return {
            type: "local_message",
            message: {
                ..._.omit(opts.raw_message, "flags"),
                ...converted_flags,
            },
        };
    }
    if (opts.raw_message.type === "private") {
        return {
            type: "server_message",
            message: {
                ..._.omit(opts.raw_message, "flags"),
                ...converted_flags,
            },
        };
    }
    return {
        type: "server_message",
        message: {
            ..._.omit(opts.raw_message, "flags"),
            ...converted_flags,
        },
    };
}

export function update_booleans(message: Message, flags: string[]): void {
    // When we get server flags for local echo or message edits,
    // we are vulnerable to race conditions, so only update flags
    // that are driven by message content.
    function convert_flag(flag_name: string): boolean {
        return flags.includes(flag_name);
    }

    message.mentioned =
        convert_flag("mentioned") ||
        convert_flag("stream_wildcard_mentioned") ||
        convert_flag("topic_wildcard_mentioned");
    message.mentioned_me_directly = convert_flag("mentioned");
    message.stream_wildcard_mentioned = convert_flag("stream_wildcard_mentioned");
    message.topic_wildcard_mentioned = convert_flag("topic_wildcard_mentioned");
    message.alerted = convert_flag("has_alert_word");
}

export function update_sender_full_name(user_id: number, new_name: string): void {
    for (const message_data of stored_messages.values()) {
        const message = message_data.message;
        if (message.sender_id && message.sender_id === user_id) {
            message.sender_full_name = new_name;
        }
    }
}

export function update_small_avatar_url(user_id: number, new_url: string | null): void {
    for (const message_data of stored_messages.values()) {
        const message = message_data.message;
        if (message.sender_id && message.sender_id === user_id) {
            message.small_avatar_url = new_url;
        }
    }
}

export function update_stream_name(stream_id: number, new_name: string): void {
    for (const message_data of stored_messages.values()) {
        const message = message_data.message;
        if (message.type === "stream" && message.stream_id === stream_id) {
            message.display_recipient = new_name;
        }
    }
}

export function update_status_emoji_info(
    user_id: number,
    new_info: UserStatusEmojiInfo | undefined,
): void {
    for (const message_data of stored_messages.values()) {
        const message = message_data.message;
        if (message.sender_id && message.sender_id === user_id) {
            message.status_emoji_info = new_info;
        }
    }
}

export function reify_message_id({old_id, new_id}: {old_id: number; new_id: number}): void {
    const message_data = stored_messages.get(old_id);
    if (message_data !== undefined) {
        const server_message: Message & Partial<LocalMessage> = message_data.message;
        if (message_data.type === "local_message") {
            // Important: Messages are managed as singletons, so
            // MessageListData objects may already have pointers to
            // the LocalMessage object for this message. So we must
            // convert the LocalMessage into a Message by dropping the
            // extra local echo/drafts fields, not by constructing a
            // new object with the new type.

            delete server_message.queue_id;
            delete server_message.draft_id;
            delete server_message.to;
            if (server_message.type === "private") {
                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});
        stored_messages.delete(old_id);
    }
}

// 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: 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, 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:
            with_message_id === undefined ? undefined : Number.parseInt(with_message_id, 10),
    };
}

const NO_MESSAGE_ID = 0;

function get_link_map_for_narrow<T>(
    map: Map<number, Map<string, Map<number, T[]>>>,
    stream_id: number,
    topic: string,
): Map<number, T[]> | undefined {
    return map.get(stream_id)?.get(topic.toLowerCase());
}

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[]> {
    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[]>>());
    }
    const new_narrow_map = new Map<number, T[]>();
    map.get(stream_id)!.set(topic.toLowerCase(), new_narrow_map);
    return new_narrow_map;
}

// 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_outgoing = new Map<number, Map<string, Map<number, TopicLink[]>>>();
export function topic_links_outgoing_for_testing(): Map<
    number,
    Map<string, Map<number, TopicLink[]>>
> {
    return topic_links_outgoing;
}

// 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_incoming = new Map<number, Map<string, Map<number, number[]>>>();
export function topic_links_incoming_for_testing(): Map<
    number,
    Map<string, Map<number, number[]>>
> {
    return topic_links_incoming;
}

// 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.
const pending_message_link_targets = new Map<number, Set<number>>();
export function pending_message_link_targets_for_testing(): Map<number, Set<number>> {
    return pending_message_link_targets;
}

// Looks up links to the potentially stale narrow, and removes any
// links from from_message_id to to_message_id (since to_message_id
// might have moved).
function remove_incoming_record(
    stale_narrow: util.StreamTopic,
    to_message_id: number,
    from_message_id: number,
): void {
    const links_to_narrow = get_link_map_for_narrow(
        topic_links_incoming,
        stale_narrow.stream_id,
        stale_narrow.topic,
    );
    const links_to_message = links_to_narrow?.get(to_message_id);
    if (links_to_message !== undefined) {
        links_to_narrow!.set(
            to_message_id,
            links_to_message.filter((message_id) => message_id !== from_message_id),
        );
    }
}

// Adds from_message_id to the links to to_message_id filed under the
// specified narrow (since the message might not yet be in the store,
// which is where its narrow data is stored).
function add_incoming_record(
    to_narrow: util.StreamTopic,
    to_message_id: number,
    from_message_id: number,
): void {
    const links_to_narrow = get_or_create_link_map_for_narrow(
        topic_links_incoming,
        to_narrow.stream_id,
        to_narrow.topic,
    );
    const links_to_message = links_to_narrow.get(to_message_id) ?? [];
    util.unique_array_insert(links_to_message, from_message_id);
    links_to_narrow.set(to_message_id, links_to_message);
}

// Saves a link from a message in both maps, under the link's narrow.
function add_link_from_message(
    from_message_id: number,
    links_from_message: TopicLink[],
    link: TopicLink,
): void {
    util.unique_array_insert(links_from_message, link);
    add_incoming_record(link, link.message_id ?? NO_MESSAGE_ID, from_message_id);
}

// Removes the link(s) from a message to `to_message_id` from both maps.
// Returns the removed links and the message's remaining links, or
// undefined if there was nothing to remove. The remaining links are
// the array stored in topic_links_outgoing, so links pushed onto it
// are saved directly into that map.
function remove_links_from_message_to(
    from_message_id: number,
    to_message_id: number,
): {stale_links: TopicLink[]; updated_links: TopicLink[]} | undefined {
    const from_message = get(from_message_id);
    if (from_message === undefined) {
        // The message was removed from the store, which removed its
        // links too. Nothing to do here.
        return undefined;
    }

    assert(from_message.type === "stream");
    const links_from_narrow = get_link_map_for_narrow(
        topic_links_outgoing,
        from_message.stream_id,
        from_message.topic,
    );
    const links_from_message = links_from_narrow?.get(from_message_id) ?? [];
    const stale_links = links_from_message.filter((link) => link.message_id === to_message_id);
    if (stale_links.length === 0) {
        // The link was edited out of the message.
        return undefined;
    }

    // Remove the stale link(s) from topic_links_incoming.
    for (const link of stale_links) {
        remove_incoming_record(link, to_message_id, from_message_id);
    }
    // Remove the stale link(s) from topic_links_outgoing.
    const updated_links = links_from_message.filter((link) => link.message_id !== to_message_id);
    links_from_narrow!.set(from_message_id, updated_links);
    return {stale_links, updated_links};
}

// 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`. 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,
    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;
    }
    // Adding to updated_links updates the array that's already stored
    // in topic_links_outgoing.
    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, 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,
    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,
            message_link_narrow,
            topic_link_narrow,
        );
    }
}

// Files the links to a message that just arrived in the store under
// its current narrow data.
function resolve_pending_links_to_message(message: Message): void {
    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 {
        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);
}

export function clear_topic_links_for_testing(): void {
    topic_links_outgoing.clear();
    topic_links_incoming.clear();
    pending_message_link_targets.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 = get_link_map_for_narrow(topic_links_outgoing, stream_id, topic);
    if (narrow_map === undefined) {
        return [];
    }

    // `topic_links_outgoing` 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, 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);
                if (message !== undefined) {
                    assert(message.type === "stream");
                    target = {
                        stream_id: message.stream_id,
                        topic: message.topic,
                        message_id: message.id,
                        kind: link.kind,
                    };
                }
            }
            // Hide links to channels the user has lost access to, e.g.
            // after being unsubscribed from a private channel, since the
            // link maps aren't updated when that happens.
            if (!stream_data.get_sub_by_id(target.stream_id)) {
                continue;
            }
            // Hide links from this topic to the same topic, since those feel
            // unnecessary to reference.
            if (target.stream_id === stream_id && util.lower_same(target.topic, topic)) {
                continue;
            }
            // 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;
            }
            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 = get_link_map_for_narrow(topic_links_incoming, stream_id, topic);
    if (narrow_map === undefined) {
        return [];
    }

    // `topic_links_incoming` 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 && util.lower_same(message.topic, topic)) {
            continue;
        }
        const stream_topic = JSON.stringify({
            stream_id: message.stream_id,
            topic: message.topic.toLowerCase(),
        });
        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,
            kind: "message",
        });
    }
    return topic_links_content;
}

function update_or_remove_links_to_message(
    stream_id: number,
    topic: string,
    old_message_id: number,
    new_message_id: number | undefined,
): void {
    const links_to_narrow = get_link_map_for_narrow(topic_links_incoming, stream_id, topic);
    if (links_to_narrow?.has(old_message_id)) {
        // Update records in `topic_links_incoming` 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,
                ),
            );
        }

        // Update/delete the matching links in `topic_links_outgoing`
        // 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_from_narrow = get_link_map_for_narrow(
                topic_links_outgoing,
                message.stream_id,
                message.topic,
            )!;
            const links = links_from_narrow.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 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",
                    });
                }
            }
        }
    }
}

function update_or_remove_links_from_message(
    stream_id: number,
    topic: string,
    old_message_id: number,
    new_message_id: number | undefined,
): void {
    const links_from_narrow = get_link_map_for_narrow(topic_links_outgoing, stream_id, topic);
    if (links_from_narrow?.has(old_message_id)) {
        // Update records in `topic_links_outgoing` 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);
        }

        // Delete matching records in `topic_links_incoming`,
        // 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 to_message = link.message_id === undefined ? undefined : get(link.message_id);
            const to_narrow = to_message?.type === "stream" ? to_message : link;
            const link_map = get_link_map_for_narrow(
                topic_links_incoming,
                to_narrow.stream_id,
                to_narrow.topic,
            );
            if (link_map?.has(to_message_id)) {
                const links_to_message = link_map.get(to_message_id)!;
                if (new_message_id) {
                    links_to_message.push(new_message_id);
                }
                link_map.set(
                    to_message_id,
                    links_to_message.filter((message_id) => message_id !== old_message_id),
                );
            }
        }
    }
}

// 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);
    assert(updated_message?.type === "stream");

    const {stream_id, topic} = updated_message;
    update_or_remove_links_to_message(stream_id, topic, old_message_id, new_message_id);
    update_or_remove_links_from_message(stream_id, topic, old_message_id, new_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);
    for (const linking_message_ids of pending_message_link_targets.values()) {
        if (linking_message_ids.delete(old_message_id)) {
            linking_message_ids.add(new_message_id);
        }
    }
}

function remove_message_from_topic_links(message_id: number): void {
    _remove_or_update_message_id_from_topic_links(message_id, undefined);
}

// Parsing message content into a template in a shared inert document
// avoids creating a new document, and firing its load events, for
// every message.
const inert_template = new DOMParser().parseFromString("", "text/html").createElement("template");

export function save_topic_links(message: Message): void {
    if (message.type !== "stream" || muted_users.is_user_muted(message.sender_id)) {
        return;
    }
    // Parsing HTML is expensive, and most messages have no narrow links.
    if (!message.content.includes("#narrow/")) {
        return;
    }

    // Extract the URLs from the message content.
    inert_template.innerHTML = message.content;
    const link_elements = inert_template.content.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;
        }

        let to_stream_id = link_data.stream_id;
        let to_topic = link_data.topic_name;
        const linked_message = linked_message_from_url(link_data);
        const to_message_id = linked_message.message_id;

        // 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;
        }

        // 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. 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 (
                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;
            to_topic = to_message.topic;
        }
        if (!stream_data.get_sub_by_id(to_stream_id)) {
            continue;
        }
        if (to_message_id !== undefined && to_message === undefined) {
            // Record that there's a link to an unstored message. Until
            // the message is fetched, we'll assume it's still in the same
            // location as its narrow link (i.e. unmoved). See
            // resolve_pending_links_to_message.
            let linking_message_ids = pending_message_link_targets.get(to_message_id);
            if (linking_message_ids === undefined) {
                linking_message_ids = new Set();
                pending_message_link_targets.set(to_message_id, linking_message_ids);
            }
            linking_message_ids.add(message.id);
        }

        const topic_links_from_message_narrow = get_or_create_link_map_for_narrow(
            topic_links_outgoing,
            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), 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,
            ...linked_message,
        });
        topic_links_from_message_narrow.set(message.id, topic_links_from_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;

    // A move can affect messages we haven't fetched locally, whose
    // only links are the ones saved to them while they aren't in the
    // store. Those are found through the linking messages' entries,
    // so they're re-filed before any of those entries move below.
    for (const message_id of message_ids) {
        if (get(message_id) === undefined) {
            const new_narrow = {stream_id: new_stream_id, topic: new_topic};
            refile_pending_links_to_message(message_id, new_narrow, new_narrow);
        }
    }

    for (const message_id of message_ids) {
        const message = get(message_id);
        if (message?.type !== "stream") {
            continue;
        }
        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(
            topic_links_outgoing,
            old_stream_id,
            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_outgoing,
                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 = get_link_map_for_narrow(
            topic_links_incoming,
            old_stream_id,
            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_incoming,
                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 this message's outgoing links and recalculate them.
    // Incoming links from other messages don't depend on this message's
    // content, so they stay as they are.
    update_or_remove_links_from_message(message.stream_id, message.topic, message.id, undefined);
    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);
        } 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, undefined);
            pending_message_link_targets.delete(message_id);
        }
        stored_messages.delete(message_id);
    }
}

export function get_message_ids_in_stream(stream_id: number): number[] {
    return stored_messages
        .values()
        .filter(
            (message_data) =>
                message_data.message.type === "stream" &&
                message_data.message.stream_id === stream_id,
        )
        .map((message_data) => message_data.message.id)
        .toArray();
}

export function maybe_update_raw_content(id: number, raw_content: string | undefined): void {
    const message = get(id);
    // In case the message was deleted from the cache after receiving a delete
    // event.
    if (message === undefined) {
        return;
    }
    // We shouldn't cache raw_content for messages we won't be receiving update events
    // for, which in this case are messages from channels the current user isn't
    // subscribed to.
    if (message.type === "stream" && !stream_data.is_subscribed(message.stream_id)) {
        // Clear any existing cached raw_content for this type of message.
        // Not doing so poses the risk of us using a stale version of the
        // raw_content after we manually fetch it.
        message.raw_content = undefined;
        return;
    }
    message.raw_content = raw_content;
}