2021-05-05 19:54:26 +02:00
|
|
|
import {addDays} from "date-fns";
|
2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2021-06-05 11:16:17 +02:00
|
|
|
import render_bankruptcy_alert_content from "../templates/navbar_alerts/bankruptcy.hbs";
|
|
|
|
import render_configure_email_alert_content from "../templates/navbar_alerts/configure_outgoing_email.hbs";
|
2021-09-06 21:25:46 +02:00
|
|
|
import render_demo_organization_deadline_content from "../templates/navbar_alerts/demo_organization_deadline.hbs";
|
2021-06-05 11:16:17 +02:00
|
|
|
import render_desktop_notifications_alert_content from "../templates/navbar_alerts/desktop_notifications.hbs";
|
|
|
|
import render_insecure_desktop_app_alert_content from "../templates/navbar_alerts/insecure_desktop_app.hbs";
|
|
|
|
import render_navbar_alert_wrapper from "../templates/navbar_alerts/navbar_alert_wrapper.hbs";
|
|
|
|
import render_profile_incomplete_alert_content from "../templates/navbar_alerts/profile_incomplete.hbs";
|
|
|
|
import render_server_needs_upgrade_alert_content from "../templates/navbar_alerts/server_needs_upgrade.hbs";
|
|
|
|
|
2021-07-15 21:26:16 +02:00
|
|
|
import * as compose_ui from "./compose_ui";
|
2022-09-28 08:27:24 +02:00
|
|
|
import * as keydown_util from "./keydown_util";
|
2021-02-28 01:27:14 +01:00
|
|
|
import {localstorage} from "./localstorage";
|
|
|
|
import * as notifications from "./notifications";
|
2021-03-25 22:35:45 +01:00
|
|
|
import {page_params} from "./page_params";
|
2022-10-27 05:27:56 +02:00
|
|
|
import * as unread from "./unread";
|
2021-02-28 21:30:08 +01:00
|
|
|
import * as unread_ops from "./unread_ops";
|
2021-02-28 01:27:14 +01:00
|
|
|
import * as unread_ui from "./unread_ui";
|
|
|
|
import * as util from "./util";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-04-27 12:02:03 +02:00
|
|
|
/* This is called by resize.js, and thus indirectly when we trigger
|
|
|
|
* resize events in the logic below. */
|
2021-02-28 01:27:14 +01:00
|
|
|
export function resize_app() {
|
2021-05-17 10:01:02 +02:00
|
|
|
const navbar_alerts_wrapper_height = $("#navbar_alerts_wrapper").height();
|
|
|
|
$("body > .app").height("calc(100% - " + navbar_alerts_wrapper_height + "px)");
|
2022-03-31 04:46:02 +02:00
|
|
|
$(".recent_topics_container").height("calc(100vh - " + navbar_alerts_wrapper_height + "px)");
|
2020-05-27 23:00:51 +02:00
|
|
|
|
|
|
|
// the floating recipient bar is usually positioned right below
|
|
|
|
// the `.header` element (including padding).
|
2020-07-15 00:34:28 +02:00
|
|
|
const frb_top =
|
2021-05-17 10:01:02 +02:00
|
|
|
navbar_alerts_wrapper_height +
|
2020-10-07 09:17:30 +02:00
|
|
|
$(".header").height() +
|
|
|
|
Number.parseInt($(".header").css("paddingBottom"), 10);
|
2020-05-27 23:00:51 +02:00
|
|
|
$("#floating_recipient_bar").css("top", frb_top + "px");
|
2021-07-15 21:26:16 +02:00
|
|
|
|
|
|
|
// If the compose-box is in expanded state,
|
|
|
|
// reset its height as well.
|
|
|
|
if (compose_ui.is_full_size()) {
|
|
|
|
compose_ui.set_compose_box_top(true);
|
|
|
|
}
|
2021-02-28 01:27:14 +01:00
|
|
|
}
|
2017-10-13 00:13:51 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const show_step = function ($process, step) {
|
2020-07-15 00:34:28 +02:00
|
|
|
$process
|
|
|
|
.find("[data-step]")
|
|
|
|
.hide()
|
|
|
|
.filter("[data-step=" + step + "]")
|
|
|
|
.show();
|
2017-09-23 11:27:18 +02:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const get_step = function ($process) {
|
2020-10-07 11:38:36 +02:00
|
|
|
return $process.find("[data-step]:visible").data("step");
|
2017-09-23 11:27:18 +02:00
|
|
|
};
|
|
|
|
|
2021-05-07 11:45:19 +02:00
|
|
|
export function should_show_notifications(ls) {
|
2020-03-26 20:49:28 +01:00
|
|
|
// if the user said to never show banner on this computer again, it will
|
|
|
|
// be stored as `true` so we want to negate that.
|
2020-12-22 11:26:39 +01:00
|
|
|
if (localstorage.supported() && ls.get("dontAskForNotifications") === true) {
|
|
|
|
return false;
|
2018-01-26 22:10:30 +01:00
|
|
|
}
|
2017-09-23 11:27:18 +02:00
|
|
|
|
2020-03-26 20:49:28 +01:00
|
|
|
return (
|
2020-12-24 06:10:37 +01:00
|
|
|
// Spectators cannot receive desktop notifications, so never
|
|
|
|
// request permissions to send them.
|
|
|
|
!page_params.is_spectator &&
|
2017-10-20 22:02:52 +02:00
|
|
|
// notifications *basically* don't work on any mobile platforms, so don't
|
|
|
|
// event show the banners. This prevents trying to access things that
|
|
|
|
// don't exist like `Notification.permission`.
|
|
|
|
!util.is_mobile() &&
|
2017-10-19 00:53:26 +02:00
|
|
|
// if permission has not been granted yet.
|
|
|
|
!notifications.granted_desktop_notifications_permission() &&
|
|
|
|
// if permission is allowed to be requested (e.g. not in "denied" state).
|
|
|
|
notifications.permission_state() !== "denied"
|
2020-03-26 20:49:28 +01:00
|
|
|
);
|
|
|
|
}
|
2018-01-26 22:10:30 +01:00
|
|
|
|
2021-05-05 19:54:26 +02:00
|
|
|
export function should_show_server_upgrade_notification(ls) {
|
|
|
|
// We do not show the server upgrade nag for a week after the user
|
|
|
|
// clicked "dismiss".
|
|
|
|
if (!localstorage.supported() || ls.get("lastUpgradeNagDismissalTime") === undefined) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const last_notification_dismissal_time = ls.get("lastUpgradeNagDismissalTime");
|
|
|
|
|
|
|
|
const upgrade_nag_dismissal_duration = addDays(new Date(last_notification_dismissal_time), 7);
|
|
|
|
|
|
|
|
// show the notification only if the time duration is completed.
|
|
|
|
return Date.now() > upgrade_nag_dismissal_duration;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function dismiss_upgrade_nag(ls) {
|
|
|
|
$(".alert[data-process='server-needs-upgrade'").hide();
|
|
|
|
if (localstorage.supported()) {
|
|
|
|
ls.set("lastUpgradeNagDismissalTime", Date.now());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-28 01:27:14 +01:00
|
|
|
export function check_profile_incomplete() {
|
2020-03-26 08:15:26 +01:00
|
|
|
if (!page_params.is_admin) {
|
2021-03-04 19:28:02 +01:00
|
|
|
return false;
|
2020-03-26 08:15:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Eventually, we might also check page_params.realm_icon_source,
|
|
|
|
// but it feels too aggressive to ask users to do change that
|
|
|
|
// since their organization might not have a logo yet.
|
2020-07-15 00:34:28 +02:00
|
|
|
if (
|
|
|
|
page_params.realm_description === "" ||
|
2021-03-04 19:15:12 +01:00
|
|
|
/^Organization imported from [A-Za-z]+[!.]$/.test(page_params.realm_description)
|
2020-07-15 00:34:28 +02:00
|
|
|
) {
|
2021-03-04 19:28:02 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function show_profile_incomplete(is_profile_incomplete) {
|
|
|
|
if (is_profile_incomplete) {
|
2021-06-05 11:16:17 +02:00
|
|
|
// Note that this will be a noop unless we'd already displayed
|
|
|
|
// the notice in this session. This seems OK, given that
|
|
|
|
// this is meant to be a one-time task for administrators.
|
2020-03-26 08:15:26 +01:00
|
|
|
$("[data-process='profile-incomplete']").show();
|
|
|
|
} else {
|
|
|
|
$("[data-process='profile-incomplete']").hide();
|
|
|
|
}
|
2021-02-28 01:27:14 +01:00
|
|
|
}
|
2020-03-26 08:15:26 +01:00
|
|
|
|
2021-09-06 21:25:46 +02:00
|
|
|
export function get_demo_organization_deadline_days_remaining() {
|
|
|
|
const now = new Date(Date.now());
|
|
|
|
const deadline = new Date(page_params.demo_organization_scheduled_deletion_date * 1000);
|
|
|
|
const day = 24 * 60 * 60 * 1000; // hours * minutes * seconds * milliseconds
|
|
|
|
const days_remaining = Math.round(Math.abs(deadline - now) / day);
|
|
|
|
return days_remaining;
|
|
|
|
}
|
|
|
|
|
2021-02-28 01:27:14 +01:00
|
|
|
export function initialize() {
|
2020-03-26 20:49:28 +01:00
|
|
|
const ls = localstorage();
|
2021-09-06 21:25:46 +02:00
|
|
|
|
|
|
|
if (page_params.demo_organization_scheduled_deletion_date) {
|
|
|
|
const days_remaining = get_demo_organization_deadline_days_remaining();
|
|
|
|
open({
|
|
|
|
data_process: "demo-organization-deadline",
|
|
|
|
custom_class: days_remaining <= 7 ? "red" : "",
|
|
|
|
rendered_alert_content_html: render_demo_organization_deadline_content({
|
|
|
|
days_remaining,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
} else if (page_params.insecure_desktop_app) {
|
2021-06-09 10:30:58 +02:00
|
|
|
open({
|
|
|
|
data_process: "insecure-desktop-app",
|
|
|
|
custom_class: "red",
|
|
|
|
rendered_alert_content_html: render_insecure_desktop_app_alert_content(),
|
|
|
|
});
|
2021-04-24 13:25:20 +02:00
|
|
|
} else if (page_params.server_needs_upgrade) {
|
2021-05-05 19:54:26 +02:00
|
|
|
if (should_show_server_upgrade_notification(ls)) {
|
2021-06-09 10:30:58 +02:00
|
|
|
open({
|
|
|
|
data_process: "server-needs-upgrade",
|
|
|
|
custom_class: "red",
|
|
|
|
rendered_alert_content_html: render_server_needs_upgrade_alert_content(),
|
|
|
|
});
|
2021-05-05 19:54:26 +02:00
|
|
|
}
|
2020-03-26 20:49:28 +01:00
|
|
|
} else if (page_params.warn_no_email === true && page_params.is_admin) {
|
|
|
|
// if email has not been set up and the user is the admin,
|
|
|
|
// display a warning to tell them to set up an email server.
|
2021-06-09 10:30:58 +02:00
|
|
|
open({
|
|
|
|
data_process: "email-server",
|
|
|
|
custom_class: "red",
|
|
|
|
rendered_alert_content_html: render_configure_email_alert_content(),
|
|
|
|
});
|
2020-03-26 20:49:28 +01:00
|
|
|
} else if (should_show_notifications(ls)) {
|
2021-06-09 10:30:58 +02:00
|
|
|
open({
|
|
|
|
data_process: "notifications",
|
|
|
|
rendered_alert_content_html: render_desktop_notifications_alert_content(),
|
|
|
|
});
|
2020-03-28 19:33:46 +01:00
|
|
|
} else if (unread_ui.should_display_bankruptcy_banner()) {
|
2022-10-27 05:27:56 +02:00
|
|
|
const unread_msgs_count = unread.get_unread_message_count();
|
2021-06-09 10:30:58 +02:00
|
|
|
open({
|
|
|
|
data_process: "bankruptcy",
|
|
|
|
custom_class: "bankruptcy",
|
|
|
|
rendered_alert_content_html: render_bankruptcy_alert_content({unread_msgs_count}),
|
|
|
|
});
|
2021-03-04 19:28:02 +01:00
|
|
|
} else if (check_profile_incomplete()) {
|
2021-06-09 10:30:58 +02:00
|
|
|
open({
|
|
|
|
data_process: "profile-incomplete",
|
|
|
|
rendered_alert_content_html: render_profile_incomplete_alert_content(),
|
|
|
|
});
|
2017-09-23 11:27:18 +02:00
|
|
|
}
|
|
|
|
|
2020-03-26 20:49:28 +01:00
|
|
|
// Configure click handlers.
|
2017-09-23 11:27:18 +02:00
|
|
|
$(".request-desktop-notifications").on("click", function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
$(this).closest(".alert").hide();
|
|
|
|
notifications.request_desktop_notifications_permission();
|
2021-04-27 12:02:03 +02:00
|
|
|
$(window).trigger("resize");
|
2017-09-23 11:27:18 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$(".reject-notifications").on("click", function () {
|
|
|
|
$(this).closest(".alert").hide();
|
|
|
|
ls.set("dontAskForNotifications", true);
|
2021-04-27 12:02:03 +02:00
|
|
|
$(window).trigger("resize");
|
2017-09-23 11:27:18 +02:00
|
|
|
});
|
|
|
|
|
2021-09-06 21:25:46 +02:00
|
|
|
$(".hide-demo-org-notice").on("click", function () {
|
|
|
|
$(this).closest(".alert").hide();
|
|
|
|
$(window).trigger("resize");
|
|
|
|
});
|
|
|
|
|
2020-03-28 19:33:46 +01:00
|
|
|
$(".accept-bankruptcy").on("click", function (e) {
|
|
|
|
e.preventDefault();
|
2021-06-08 08:55:08 +02:00
|
|
|
const $process = $(this).closest("[data-process]");
|
|
|
|
show_step($process, 2);
|
2020-06-18 08:11:41 +02:00
|
|
|
setTimeout(unread_ops.mark_all_as_read, 1000);
|
2021-04-27 12:02:03 +02:00
|
|
|
$(window).trigger("resize");
|
2020-03-28 19:33:46 +01:00
|
|
|
});
|
|
|
|
|
2021-05-05 19:54:26 +02:00
|
|
|
$(".dismiss-upgrade-nag").on("click", (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
dismiss_upgrade_nag(ls);
|
|
|
|
});
|
|
|
|
|
2021-05-17 10:01:02 +02:00
|
|
|
$("#navbar_alerts_wrapper").on("click", ".alert .close, .alert .exit", function (e) {
|
2017-09-23 11:27:18 +02:00
|
|
|
e.stopPropagation();
|
2020-03-26 20:49:28 +01:00
|
|
|
const $process = $(e.target).closest("[data-process]");
|
2018-01-26 22:10:30 +01:00
|
|
|
if (get_step($process) === 1 && $process.data("process") === "notifications") {
|
|
|
|
show_step($process, 2);
|
2017-09-23 11:27:18 +02:00
|
|
|
} else {
|
|
|
|
$(this).closest(".alert").hide();
|
|
|
|
}
|
2021-04-27 12:02:03 +02:00
|
|
|
$(window).trigger("resize");
|
2017-09-23 11:27:18 +02:00
|
|
|
});
|
2020-08-10 19:36:07 +02:00
|
|
|
|
2021-05-17 10:01:02 +02:00
|
|
|
// Treat Enter with links in the navbar alerts UI focused like a click.,
|
|
|
|
$("#navbar_alerts_wrapper").on("keyup", ".alert-link[role=button]", function (e) {
|
2020-08-10 19:36:07 +02:00
|
|
|
e.stopPropagation();
|
2022-09-28 08:27:24 +02:00
|
|
|
if (keydown_util.is_enter_event(e)) {
|
2021-05-20 02:37:42 +02:00
|
|
|
$(this).trigger("click");
|
2020-08-10 19:36:07 +02:00
|
|
|
}
|
|
|
|
});
|
2021-02-28 01:27:14 +01:00
|
|
|
}
|
2017-09-23 11:27:18 +02:00
|
|
|
|
2021-06-09 10:30:58 +02:00
|
|
|
export function open(args) {
|
|
|
|
const rendered_alert_wrapper_html = render_navbar_alert_wrapper(args);
|
2021-06-05 11:16:17 +02:00
|
|
|
|
|
|
|
// Note: We only support one alert being rendered at a time; as a
|
|
|
|
// result, we just replace the alert area in the DOM with the
|
|
|
|
// indicated alert. We do this to avoid bad UX, as it'd look weird
|
|
|
|
// to have more than one alert visible at a time.
|
|
|
|
$("#navbar_alerts_wrapper").html(rendered_alert_wrapper_html);
|
2021-04-27 12:02:03 +02:00
|
|
|
$(window).trigger("resize");
|
2021-02-28 01:27:14 +01:00
|
|
|
}
|