2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
2023-12-25 00:22:40 +01:00
|
|
|
import assert from "minimalistic-assert";
|
2021-03-11 05:43:45 +01:00
|
|
|
|
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
|
|
|
|
|
|
|
import * as message_lists from "./message_lists";
|
2023-12-25 00:22:40 +01:00
|
|
|
import type {Message} from "./message_store";
|
2023-04-19 07:00:47 +02:00
|
|
|
import * as narrow_state from "./narrow_state";
|
2021-03-25 22:35:45 +01:00
|
|
|
import {page_params} from "./page_params";
|
2023-03-08 02:27:45 +01:00
|
|
|
import {web_mark_read_on_scroll_policy_values} from "./settings_config";
|
2021-02-28 21:30:38 +01:00
|
|
|
import * as unread from "./unread";
|
2023-12-25 00:22:40 +01:00
|
|
|
import type {FullUnreadCountsData} from "./unread";
|
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
|
|
|
|
2023-12-25 00:22:40 +01:00
|
|
|
type UpdateUnreadCountsHook = (counts: FullUnreadCountsData, skip_animations: boolean) => void;
|
|
|
|
const update_unread_counts_hooks: UpdateUnreadCountsHook[] = [];
|
2023-06-01 01:46:19 +02:00
|
|
|
|
2023-12-25 00:22:40 +01:00
|
|
|
export function register_update_unread_counts_hook(f: UpdateUnreadCountsHook): void {
|
2023-06-01 01:46:19 +02:00
|
|
|
update_unread_counts_hooks.push(f);
|
|
|
|
}
|
|
|
|
|
2023-12-25 00:22:40 +01:00
|
|
|
export function update_unread_banner(): void {
|
2024-02-05 19:03:29 +01:00
|
|
|
if (message_lists.current === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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-12-25 00:22:40 +01:00
|
|
|
export function hide_unread_banner(): void {
|
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-12-25 00:22:40 +01:00
|
|
|
export function reset_unread_banner(): void {
|
2023-04-11 23:36:11 +02:00
|
|
|
hide_unread_banner();
|
|
|
|
user_closed_unread_banner = false;
|
2022-03-29 21:50:43 +02:00
|
|
|
}
|
|
|
|
|
2023-12-25 00:22:40 +01:00
|
|
|
export function notify_messages_remain_unread(): void {
|
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
|
|
|
}
|
|
|
|
|
2023-12-25 00:22:40 +01:00
|
|
|
export function set_count_toggle_button($elem: JQuery, count: number): JQuery {
|
2016-11-14 17:14:59 +01:00
|
|
|
if (count === 0) {
|
2023-10-25 20:38:25 +02:00
|
|
|
return $elem.hide();
|
2016-11-14 17:14:59 +01:00
|
|
|
}
|
2023-10-25 20:38:25 +02:00
|
|
|
return $elem.show();
|
2021-02-28 00:40:18 +01:00
|
|
|
}
|
2016-11-14 17:14:59 +01:00
|
|
|
|
2023-12-25 00:22:40 +01:00
|
|
|
export function update_unread_counts(skip_animations = false): void {
|
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.
|
2023-06-01 01:46:19 +02:00
|
|
|
// See `ui_init.initialize_unread_ui` for the registered hooks.
|
|
|
|
for (const hook of update_unread_counts_hooks) {
|
|
|
|
hook(res, skip_animations);
|
|
|
|
}
|
2017-08-12 17:26:12 +02:00
|
|
|
|
2023-10-25 20:38:25 +02:00
|
|
|
// Set the unread indicator on the toggle for the left sidebar
|
2024-04-22 10:31:01 +02:00
|
|
|
set_count_toggle_button($(".left-sidebar-toggle-unreadcount"), res.home_unread_messages);
|
2021-02-28 00:40:18 +01:00
|
|
|
}
|
2017-02-11 09:55:18 +01:00
|
|
|
|
2023-12-25 00:22:40 +01:00
|
|
|
export function should_display_bankruptcy_banner(): boolean {
|
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
|
|
|
|
2023-12-25 00:22:40 +01:00
|
|
|
export function initialize({
|
|
|
|
notify_server_messages_read,
|
|
|
|
}: {
|
|
|
|
notify_server_messages_read: (unread_messages: Message[]) => void;
|
|
|
|
}): void {
|
2023-05-03 19:32:40 +02:00
|
|
|
const skip_animations = true;
|
|
|
|
update_unread_counts(skip_animations);
|
2023-03-08 02:27:45 +01:00
|
|
|
$("body").on("click", "#mark_view_read", () => {
|
2023-12-25 00:22:40 +01:00
|
|
|
assert(message_lists.current !== undefined);
|
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
|
|
|
}
|