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";
|
2024-03-19 14:22:03 +01:00
|
|
|
import render_empty_required_profile_fields from "../templates/navbar_alerts/empty_required_profile_fields.hbs";
|
2021-06-05 11:16:17 +02:00
|
|
|
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";
|
|
|
|
|
2023-10-07 00:15:22 +02:00
|
|
|
import * as desktop_notifications from "./desktop_notifications";
|
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";
|
2021-03-25 22:35:45 +01:00
|
|
|
import {page_params} from "./page_params";
|
2024-03-19 14:22:03 +01:00
|
|
|
import * as people from "./people";
|
2024-02-13 02:08:24 +01:00
|
|
|
import {current_user, realm} from "./state_data";
|
2023-08-11 19:46:58 +02:00
|
|
|
import {should_display_profile_incomplete_alert} from "./timerender";
|
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
|
|
|
|
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.
|
2023-10-07 00:15:22 +02:00
|
|
|
!desktop_notifications.granted_desktop_notifications_permission() &&
|
2017-10-19 00:53:26 +02:00
|
|
|
// if permission is allowed to be requested (e.g. not in "denied" state).
|
2023-10-07 00:15:22 +02:00
|
|
|
desktop_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;
|
|
|
|
}
|
|
|
|
|
2024-03-19 14:22:03 +01:00
|
|
|
export function maybe_show_empty_required_profile_fields_alert() {
|
|
|
|
const empty_required_profile_fields_exist = realm.custom_profile_fields
|
|
|
|
.map((f) => ({
|
|
|
|
...f,
|
|
|
|
value: people.my_custom_profile_data(f.id)?.value,
|
|
|
|
}))
|
|
|
|
.find((f) => f.required && !f.value);
|
|
|
|
if (!empty_required_profile_fields_exist) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const $navbar_alert = $("#navbar_alerts_wrapper").children(".alert").first();
|
|
|
|
if (
|
|
|
|
!$navbar_alert?.length ||
|
|
|
|
$navbar_alert.is("#empty-required-profile-fields-warning") ||
|
|
|
|
$navbar_alert.is(":hidden")
|
|
|
|
) {
|
|
|
|
open({
|
|
|
|
data_process: "profile-missing-required",
|
|
|
|
rendered_alert_content_html: render_empty_required_profile_fields(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-05 19:54:26 +02:00
|
|
|
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() {
|
2024-02-13 02:08:16 +01:00
|
|
|
if (!current_user.is_admin) {
|
2021-03-04 19:28:02 +01:00
|
|
|
return false;
|
2020-03-26 08:15:26 +01:00
|
|
|
}
|
2024-02-13 02:08:24 +01:00
|
|
|
if (!should_display_profile_incomplete_alert(realm.realm_date_created)) {
|
2023-08-11 19:46:58 +02:00
|
|
|
return false;
|
|
|
|
}
|
2020-03-26 08:15:26 +01:00
|
|
|
|
2024-02-13 02:08:24 +01:00
|
|
|
// Eventually, we might also check realm.realm_icon_source,
|
2020-03-26 08:15:26 +01:00
|
|
|
// 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 (
|
2024-02-13 02:08:24 +01:00
|
|
|
realm.realm_description === "" ||
|
|
|
|
/^Organization imported from [A-Za-z]+[!.]$/.test(realm.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());
|
2024-02-13 02:08:24 +01:00
|
|
|
const deadline = new Date(realm.demo_organization_scheduled_deletion_date * 1000);
|
2021-09-06 21:25:46 +02:00
|
|
|
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
|
|
|
|
2024-02-13 02:08:24 +01:00
|
|
|
if (realm.demo_organization_scheduled_deletion_date) {
|
2021-09-06 21:25:46 +02:00
|
|
|
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(),
|
|
|
|
});
|
2024-02-13 02:08:24 +01:00
|
|
|
} else if (realm.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
|
|
|
}
|
2024-02-13 02:08:16 +01:00
|
|
|
} else if (page_params.warn_no_email === true && current_user.is_admin) {
|
2020-03-26 20:49:28 +01:00
|
|
|
// 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()) {
|
2023-09-22 13:10:19 +02:00
|
|
|
const old_unreads_missing = unread.old_unreads_missing;
|
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",
|
2023-09-22 13:10:19 +02:00
|
|
|
rendered_alert_content_html: render_bankruptcy_alert_content({
|
|
|
|
old_unreads_missing,
|
|
|
|
unread_msgs_count,
|
|
|
|
}),
|
2021-06-09 10:30:58 +02:00
|
|
|
});
|
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(),
|
|
|
|
});
|
2024-03-19 14:22:03 +01:00
|
|
|
} else {
|
|
|
|
maybe_show_empty_required_profile_fields_alert();
|
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();
|
2023-10-07 00:15:22 +02:00
|
|
|
desktop_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
|
|
|
});
|
|
|
|
|
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();
|
2024-03-19 14:22:03 +01:00
|
|
|
maybe_show_empty_required_profile_fields_alert();
|
2017-09-23 11:27:18 +02:00
|
|
|
}
|
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
|
|
|
}
|