2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2023-04-23 19:57:17 +02:00
|
|
|
import render_mark_as_read_disabled_banner from "../templates/unread_banner/mark_as_read_disabled_banner.hbs";
|
|
|
|
import render_mark_as_read_only_in_conversation_view from "../templates/unread_banner/mark_as_read_only_in_conversation_view.hbs";
|
|
|
|
import render_mark_as_read_turned_off_banner from "../templates/unread_banner/mark_as_read_turned_off_banner.hbs";
|
2021-08-02 20:09:56 +02:00
|
|
|
|
2021-02-28 01:13:42 +01:00
|
|
|
import * as activity from "./activity";
|
2021-08-02 20:09:56 +02:00
|
|
|
import * as message_lists from "./message_lists";
|
2023-04-19 07:00:47 +02:00
|
|
|
import * as narrow_state from "./narrow_state";
|
2021-02-28 01:06:34 +01:00
|
|
|
import * as notifications from "./notifications";
|
2021-03-25 22:35:45 +01:00
|
|
|
import {page_params} from "./page_params";
|
2021-02-28 00:44:12 +01:00
|
|
|
import * as pm_list from "./pm_list";
|
2023-03-08 02:27:45 +01:00
|
|
|
import {web_mark_read_on_scroll_policy_values} from "./settings_config";
|
2021-02-28 21:31:02 +01:00
|
|
|
import * as stream_list from "./stream_list";
|
2021-02-28 00:44:57 +01:00
|
|
|
import * as top_left_corner from "./top_left_corner";
|
2021-02-28 00:43:33 +01:00
|
|
|
import * as topic_list from "./topic_list";
|
2021-02-28 21:30:38 +01:00
|
|
|
import * as unread from "./unread";
|
2021-08-02 20:09:56 +02:00
|
|
|
import {notify_server_messages_read} from "./unread_ops";
|
2023-03-08 02:27:45 +01:00
|
|
|
import {user_settings} from "./user_settings";
|
2021-02-28 00:43:33 +01:00
|
|
|
|
2023-04-11 23:36:11 +02:00
|
|
|
let user_closed_unread_banner = false;
|
2023-03-08 02:27:45 +01:00
|
|
|
|
|
|
|
export function update_unread_banner() {
|
2023-04-19 07:00:47 +02:00
|
|
|
const filter = narrow_state.filter();
|
|
|
|
const is_conversation_view = filter === undefined ? false : filter.is_conversation_view();
|
2023-03-08 02:27:45 +01:00
|
|
|
if (
|
|
|
|
user_settings.web_mark_read_on_scroll_policy ===
|
|
|
|
web_mark_read_on_scroll_policy_values.never.code
|
|
|
|
) {
|
2023-05-01 22:23:27 +02:00
|
|
|
$("#mark_read_on_scroll_state_banner").html(render_mark_as_read_disabled_banner());
|
2023-04-19 07:00:47 +02:00
|
|
|
} else if (
|
|
|
|
user_settings.web_mark_read_on_scroll_policy ===
|
|
|
|
web_mark_read_on_scroll_policy_values.conversation_only.code &&
|
|
|
|
!is_conversation_view
|
|
|
|
) {
|
2023-05-01 22:23:27 +02:00
|
|
|
$("#mark_read_on_scroll_state_banner").html(
|
|
|
|
render_mark_as_read_only_in_conversation_view(),
|
|
|
|
);
|
2023-03-08 02:27:45 +01:00
|
|
|
} else {
|
2023-05-01 22:23:27 +02:00
|
|
|
$("#mark_read_on_scroll_state_banner").html(render_mark_as_read_turned_off_banner());
|
2023-03-08 02:27:45 +01:00
|
|
|
if (message_lists.current.can_mark_messages_read_without_setting()) {
|
|
|
|
hide_unread_banner();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-11 23:36:11 +02:00
|
|
|
export function hide_unread_banner() {
|
2022-03-29 21:50:43 +02:00
|
|
|
// Use visibility instead of hide() to prevent messages on the screen from
|
|
|
|
// shifting vertically.
|
2023-05-01 22:23:27 +02:00
|
|
|
$("#mark_read_on_scroll_state_banner").toggleClass("invisible", true);
|
2022-03-29 21:50:43 +02:00
|
|
|
}
|
|
|
|
|
2023-04-11 23:36:11 +02:00
|
|
|
export function reset_unread_banner() {
|
|
|
|
hide_unread_banner();
|
|
|
|
user_closed_unread_banner = false;
|
2022-03-29 21:50:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function notify_messages_remain_unread() {
|
2023-04-11 23:36:11 +02:00
|
|
|
if (!user_closed_unread_banner) {
|
2023-05-01 22:23:27 +02:00
|
|
|
$("#mark_read_on_scroll_state_banner").toggleClass("invisible", false);
|
2022-03-29 21:50:43 +02:00
|
|
|
}
|
2022-04-26 20:33:17 +02:00
|
|
|
}
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
export function set_count_toggle_button($elem, count) {
|
2016-11-14 17:14:59 +01:00
|
|
|
if (count === 0) {
|
2022-01-25 11:36:19 +01:00
|
|
|
if ($elem.is(":animated")) {
|
|
|
|
return $elem.stop(true, true).hide();
|
2016-11-14 17:14:59 +01:00
|
|
|
}
|
2022-01-25 11:36:19 +01:00
|
|
|
return $elem.hide(500);
|
2018-06-06 18:50:09 +02:00
|
|
|
} else if (count > 0 && count < 1000) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$elem.show(500);
|
|
|
|
return $elem.text(count);
|
2016-11-14 17:14:59 +01:00
|
|
|
}
|
2022-01-25 11:36:19 +01:00
|
|
|
$elem.show(500);
|
|
|
|
return $elem.text("1k+");
|
2021-02-28 00:40:18 +01:00
|
|
|
}
|
2016-11-14 17:14:59 +01:00
|
|
|
|
2021-02-28 00:40:18 +01:00
|
|
|
export function update_unread_counts() {
|
2017-02-11 09:55:18 +01:00
|
|
|
// Pure computation:
|
2019-11-02 00:06:25 +01:00
|
|
|
const res = unread.get_counts();
|
2017-02-11 09:55:18 +01:00
|
|
|
|
|
|
|
// Side effects from here down:
|
|
|
|
// This updates some DOM elements directly, so try to
|
|
|
|
// avoid excessive calls to this.
|
|
|
|
activity.update_dom_with_unread_counts(res);
|
2017-08-12 17:26:12 +02:00
|
|
|
top_left_corner.update_dom_with_unread_counts(res);
|
2017-02-11 09:55:18 +01:00
|
|
|
stream_list.update_dom_with_unread_counts(res);
|
|
|
|
pm_list.update_dom_with_unread_counts(res);
|
2020-01-31 20:15:46 +01:00
|
|
|
topic_list.update();
|
2019-11-02 00:06:25 +01:00
|
|
|
const notifiable_unread_count = unread.calculate_notifiable_count(res);
|
2020-10-21 04:07:35 +02:00
|
|
|
notifications.update_unread_counts(notifiable_unread_count, res.private_message_count);
|
2017-08-12 17:26:12 +02:00
|
|
|
|
2021-03-30 17:40:37 +02:00
|
|
|
// Set the unread counts that we show in the buttons that
|
|
|
|
// toggle open the sidebar menus when we have a thin window.
|
2021-02-28 00:40:18 +01:00
|
|
|
set_count_toggle_button($("#streamlist-toggle-unreadcount"), res.home_unread_messages);
|
2023-01-16 19:46:26 +01:00
|
|
|
// Bots and group PMs do not appear in the right sidebar user list, so
|
|
|
|
// we show unread count for only non bot 1:1 private messages there.
|
|
|
|
set_count_toggle_button(
|
|
|
|
$("#userlist-toggle-unreadcount"),
|
|
|
|
res.right_sidebar_private_message_count,
|
|
|
|
);
|
2021-02-28 00:40:18 +01:00
|
|
|
}
|
2017-02-11 09:55:18 +01:00
|
|
|
|
2021-02-28 00:40:18 +01:00
|
|
|
export function should_display_bankruptcy_banner() {
|
2017-02-11 08:59:19 +01:00
|
|
|
// Until we've handled possibly declaring bankruptcy, don't show
|
|
|
|
// unread counts since they only consider messages that are loaded
|
|
|
|
// client side and may be different from the numbers reported by
|
|
|
|
// the server.
|
|
|
|
|
|
|
|
if (!page_params.furthest_read_time) {
|
|
|
|
// We've never read a message.
|
2020-03-28 19:33:46 +01:00
|
|
|
return false;
|
2017-02-11 08:59:19 +01:00
|
|
|
}
|
|
|
|
|
2021-02-05 21:20:14 +01:00
|
|
|
const now = Date.now() / 1000;
|
2020-07-15 00:34:28 +02:00
|
|
|
if (
|
2022-10-27 05:27:56 +02:00
|
|
|
unread.get_unread_message_count() > 500 &&
|
2020-07-15 00:34:28 +02:00
|
|
|
now - page_params.furthest_read_time > 60 * 60 * 24 * 2
|
|
|
|
) {
|
|
|
|
// 2 days.
|
2020-03-28 19:33:46 +01:00
|
|
|
return true;
|
2017-02-11 08:59:19 +01:00
|
|
|
}
|
|
|
|
|
2020-03-28 19:33:46 +01:00
|
|
|
return false;
|
2021-02-28 00:40:18 +01:00
|
|
|
}
|
2016-11-14 17:14:59 +01:00
|
|
|
|
2021-02-28 00:40:18 +01:00
|
|
|
export function initialize() {
|
|
|
|
update_unread_counts();
|
2023-03-08 02:27:45 +01:00
|
|
|
$("body").on("click", "#mark_view_read", () => {
|
2021-08-02 20:09:56 +02:00
|
|
|
// Mark all messages in the current view as read.
|
|
|
|
//
|
|
|
|
// BUG: This logic only supports marking messages visible in
|
|
|
|
// the present view as read; we need a server API to mark
|
|
|
|
// every message matching the current search as read.
|
|
|
|
const unread_messages = message_lists.current.data
|
|
|
|
.all_messages()
|
|
|
|
.filter((message) => unread.message_unread(message));
|
|
|
|
notify_server_messages_read(unread_messages);
|
2021-08-05 00:59:03 +02:00
|
|
|
// New messages received may be marked as read based on narrow type.
|
|
|
|
message_lists.current.resume_reading();
|
2021-08-02 20:09:56 +02:00
|
|
|
|
2023-04-11 23:36:11 +02:00
|
|
|
hide_unread_banner();
|
2022-03-29 21:50:43 +02:00
|
|
|
});
|
2023-03-08 02:27:45 +01:00
|
|
|
$("body").on("click", "#mark_as_read_close", () => {
|
2023-04-11 23:36:11 +02:00
|
|
|
hide_unread_banner();
|
|
|
|
user_closed_unread_banner = true;
|
2021-08-02 20:09:56 +02:00
|
|
|
});
|
2023-03-08 02:27:45 +01:00
|
|
|
|
|
|
|
// The combination of these functions in sequence ensures we have
|
|
|
|
// at least one copy of the unread banner in the DOM, invisible;
|
|
|
|
// this somewhat strange pattern allows our CSS to reserve space for
|
|
|
|
// the banner, to avoid scroll position jumps when it is shown/hidden.
|
|
|
|
update_unread_banner();
|
|
|
|
hide_unread_banner();
|
2021-02-28 00:40:18 +01:00
|
|
|
}
|