mirror of
https://github.com/zulip/zulip.git
synced 2025-11-04 14:03:30 +00:00
I moved four functions, verbatim, to a new module. They were in message_util before, which led to filter.js having several accidental indirect dependencies. I considered just putting these four functions in filter.js, but I think it's a nice abstraction boundary that filter.js delegates actual message parsing, and the original author apparently had a similar thought process. I also wanted to make it so that a casual reader of filter.js doesn't think we are manipulating DOM. It's true that we still indirectly require jquery here, but it's only for parsing, and it seems plausible we would eventually use a more low-level parser. I can see us maybe using these functions in something like MessageListData in the future, so speculatively splitting them out might future-proof us from some cyclical dependencies. I also think it's plausible that we will just modify our two markdown processors to attach that kind of metadata to the messages. Last but not least, I think there might be opportunity here to simplify the filter tests and remove some of the zjquery hacks. We would instead just mock the message_has_* helpers for the filter tests, and then do more detailed direct testing on the functions themselves.
83 lines
2.8 KiB
JavaScript
83 lines
2.8 KiB
JavaScript
import $ from "jquery";
|
|
|
|
import * as loading from "./loading";
|
|
import * as message_list from "./message_list";
|
|
import * as message_store from "./message_store";
|
|
import * as resize from "./resize";
|
|
import * as unread from "./unread";
|
|
import * as unread_ui from "./unread_ui";
|
|
|
|
export function do_unread_count_updates(messages) {
|
|
unread.process_loaded_messages(messages);
|
|
unread_ui.update_unread_counts();
|
|
resize.resize_page_components();
|
|
}
|
|
|
|
function add_messages(messages, msg_list, opts) {
|
|
if (!messages) {
|
|
return undefined;
|
|
}
|
|
|
|
loading.destroy_indicator($("#page_loading_indicator"));
|
|
|
|
const render_info = msg_list.add_messages(messages, opts);
|
|
|
|
return render_info;
|
|
}
|
|
|
|
export function add_old_messages(messages, msg_list) {
|
|
return add_messages(messages, msg_list, {messages_are_new: false});
|
|
}
|
|
|
|
export function add_new_messages(messages, msg_list) {
|
|
if (!msg_list.data.fetch_status.has_found_newest()) {
|
|
// We don't render newly received messages for the message list,
|
|
// if we haven't found the latest messages to be displayed in the
|
|
// narrow. Otherwise the new message would be rendered just after
|
|
// the previously fetched messages when that's inaccurate.
|
|
msg_list.data.fetch_status.update_expected_max_message_id(messages);
|
|
return undefined;
|
|
}
|
|
return add_messages(messages, msg_list, {messages_are_new: true});
|
|
}
|
|
|
|
export function get_messages_in_topic(stream_id, topic) {
|
|
return message_list.all
|
|
.all_messages()
|
|
.filter(
|
|
(x) =>
|
|
x.type === "stream" &&
|
|
x.stream_id === stream_id &&
|
|
x.topic.toLowerCase() === topic.toLowerCase(),
|
|
);
|
|
}
|
|
|
|
export function get_max_message_id_in_stream(stream_id) {
|
|
let max_message_id = 0;
|
|
for (const msg of message_list.all.all_messages()) {
|
|
if (msg.type === "stream" && msg.stream_id === stream_id && msg.id > max_message_id) {
|
|
max_message_id = msg.id;
|
|
}
|
|
}
|
|
return max_message_id;
|
|
}
|
|
|
|
export function get_topics_for_message_ids(message_ids) {
|
|
const topics = new Map(); // key = stream_id:topic
|
|
for (const msg_id of message_ids) {
|
|
// message_store still has data on deleted messages when this runs.
|
|
const message = message_store.get(msg_id);
|
|
if (message === undefined) {
|
|
// We may not have the deleted message cached locally in
|
|
// message_store; if so, we can just skip processing it.
|
|
continue;
|
|
}
|
|
if (message.type === "stream") {
|
|
// Create unique keys for stream_id and topic.
|
|
const topic_key = message.stream_id + ":" + message.topic;
|
|
topics.set(topic_key, [message.stream_id, message.topic]);
|
|
}
|
|
}
|
|
return topics;
|
|
}
|