2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2021-02-28 01:10:31 +01:00
|
|
|
import * as loading from "./loading";
|
2021-02-28 21:31:33 +01:00
|
|
|
import * as message_list from "./message_list";
|
2021-02-28 01:10:31 +01:00
|
|
|
import * as message_store from "./message_store";
|
|
|
|
import * as resize from "./resize";
|
2021-02-28 21:30:38 +01:00
|
|
|
import * as unread from "./unread";
|
2021-02-28 01:10:31 +01:00
|
|
|
import * as unread_ui from "./unread_ui";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-02-28 01:10:31 +01:00
|
|
|
export function do_unread_count_updates(messages) {
|
2017-03-19 22:43:38 +01:00
|
|
|
unread.process_loaded_messages(messages);
|
|
|
|
unread_ui.update_unread_counts();
|
|
|
|
resize.resize_page_components();
|
2021-02-28 01:10:31 +01:00
|
|
|
}
|
2017-03-19 22:43:38 +01:00
|
|
|
|
2019-01-08 01:26:02 +01:00
|
|
|
function add_messages(messages, msg_list, opts) {
|
2017-03-19 22:43:38 +01:00
|
|
|
if (!messages) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2017-03-19 22:43:38 +01:00
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
loading.destroy_indicator($("#page_loading_indicator"));
|
2017-03-19 22:43:38 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const render_info = msg_list.add_messages(messages, opts);
|
message scrolling: Fix "Scroll down to view" warning.
We recently added a feature to warn users that they
may need to scroll down to view messages that they
just sent, but it was broken due to various complexities
in the rendering code path.
Now we compute it a bit more rigorously.
It requires us to pass some info about rendering up
and down the stack, which is why it's kind of a long
commit, but the bulk of the logic is in these JS files:
* message_list_view.js
* notifications.js
I choose to pass structs around instead of booleans,
because I anticipate we may eventually add more metadata
about rendering to it, plus bools are just kinda brittle.
(The exceptions are that `_maybe_autoscroll`, which
is at the bottom of the stack, just passes back a simple
boolean, and `notify_local_mixes`, also at the bottom
of the stack, just accepts a simple boolean.)
This errs on the side of warning the user, even if the
new message is partially visible.
Fixes #11138
2019-01-07 21:00:03 +01:00
|
|
|
|
|
|
|
return render_info;
|
2019-01-08 01:26:02 +01:00
|
|
|
}
|
|
|
|
|
2020-08-13 01:13:56 +02:00
|
|
|
// We need to check if the message content contains the specified HTML
|
|
|
|
// elements. We wrap the message.content in a <div>; this is
|
|
|
|
// important because $("Text <a>link</a>").find("a") returns nothing;
|
|
|
|
// one needs an outer element wrapping an object to use this
|
|
|
|
// construction.
|
|
|
|
function is_element_in_message_content(message, element_selector) {
|
2021-01-23 02:36:54 +01:00
|
|
|
return $(`<div>${message.content}</div>`).find(`${element_selector}`).length > 0;
|
2020-08-13 01:13:56 +02:00
|
|
|
}
|
|
|
|
|
2021-02-28 01:10:31 +01:00
|
|
|
export function message_has_link(message) {
|
2020-08-13 01:13:56 +02:00
|
|
|
return is_element_in_message_content(message, "a");
|
2021-02-28 01:10:31 +01:00
|
|
|
}
|
2020-06-13 19:05:31 +02:00
|
|
|
|
2021-02-28 01:10:31 +01:00
|
|
|
export function message_has_image(message) {
|
2020-08-13 01:13:56 +02:00
|
|
|
return is_element_in_message_content(message, ".message_inline_image");
|
2021-02-28 01:10:31 +01:00
|
|
|
}
|
2020-06-13 19:05:31 +02:00
|
|
|
|
2021-02-28 01:10:31 +01:00
|
|
|
export function message_has_attachment(message) {
|
2020-08-13 01:13:56 +02:00
|
|
|
return is_element_in_message_content(message, "a[href^='/user_uploads']");
|
2021-02-28 01:10:31 +01:00
|
|
|
}
|
2020-06-13 19:05:31 +02:00
|
|
|
|
2021-02-28 01:10:31 +01:00
|
|
|
export function add_old_messages(messages, msg_list) {
|
message scrolling: Fix "Scroll down to view" warning.
We recently added a feature to warn users that they
may need to scroll down to view messages that they
just sent, but it was broken due to various complexities
in the rendering code path.
Now we compute it a bit more rigorously.
It requires us to pass some info about rendering up
and down the stack, which is why it's kind of a long
commit, but the bulk of the logic is in these JS files:
* message_list_view.js
* notifications.js
I choose to pass structs around instead of booleans,
because I anticipate we may eventually add more metadata
about rendering to it, plus bools are just kinda brittle.
(The exceptions are that `_maybe_autoscroll`, which
is at the bottom of the stack, just passes back a simple
boolean, and `notify_local_mixes`, also at the bottom
of the stack, just accepts a simple boolean.)
This errs on the side of warning the user, even if the
new message is partially visible.
Fixes #11138
2019-01-07 21:00:03 +01:00
|
|
|
return add_messages(messages, msg_list, {messages_are_new: false});
|
2021-02-28 01:10:31 +01:00
|
|
|
}
|
2020-05-30 17:34:07 +02:00
|
|
|
|
2021-02-28 01:10:31 +01:00
|
|
|
export function add_new_messages(messages, msg_list) {
|
2020-05-30 17:34:07 +02:00
|
|
|
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);
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2020-05-30 17:34:07 +02:00
|
|
|
}
|
message scrolling: Fix "Scroll down to view" warning.
We recently added a feature to warn users that they
may need to scroll down to view messages that they
just sent, but it was broken due to various complexities
in the rendering code path.
Now we compute it a bit more rigorously.
It requires us to pass some info about rendering up
and down the stack, which is why it's kind of a long
commit, but the bulk of the logic is in these JS files:
* message_list_view.js
* notifications.js
I choose to pass structs around instead of booleans,
because I anticipate we may eventually add more metadata
about rendering to it, plus bools are just kinda brittle.
(The exceptions are that `_maybe_autoscroll`, which
is at the bottom of the stack, just passes back a simple
boolean, and `notify_local_mixes`, also at the bottom
of the stack, just accepts a simple boolean.)
This errs on the side of warning the user, even if the
new message is partially visible.
Fixes #11138
2019-01-07 21:00:03 +01:00
|
|
|
return add_messages(messages, msg_list, {messages_are_new: true});
|
2021-02-28 01:10:31 +01:00
|
|
|
}
|
2017-03-19 22:43:38 +01:00
|
|
|
|
2021-02-28 01:10:31 +01:00
|
|
|
export function get_messages_in_topic(stream_id, topic) {
|
2020-07-15 00:34:28 +02:00
|
|
|
return message_list.all
|
|
|
|
.all_messages()
|
|
|
|
.filter(
|
|
|
|
(x) =>
|
|
|
|
x.type === "stream" &&
|
|
|
|
x.stream_id === stream_id &&
|
|
|
|
x.topic.toLowerCase() === topic.toLowerCase(),
|
|
|
|
);
|
2021-02-28 01:10:31 +01:00
|
|
|
}
|
2017-03-19 22:43:38 +01:00
|
|
|
|
2021-02-28 01:10:31 +01:00
|
|
|
export function get_max_message_id_in_stream(stream_id) {
|
2020-08-04 11:13:20 +02:00
|
|
|
let max_message_id = 0;
|
|
|
|
for (const msg of message_list.all.all_messages()) {
|
2020-12-22 11:26:39 +01:00
|
|
|
if (msg.type === "stream" && msg.stream_id === stream_id && msg.id > max_message_id) {
|
|
|
|
max_message_id = msg.id;
|
2020-08-04 11:13:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return max_message_id;
|
2021-02-28 01:10:31 +01:00
|
|
|
}
|
2020-08-04 11:13:20 +02:00
|
|
|
|
2021-02-28 01:10:31 +01:00
|
|
|
export function get_topics_for_message_ids(message_ids) {
|
2020-08-07 09:15:47 +02:00
|
|
|
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;
|
2021-02-28 01:10:31 +01:00
|
|
|
}
|