From 362a439cb548ad6ebaf22dd0983b66f66bd64ba7 Mon Sep 17 00:00:00 2001 From: yflory Date: Mon, 12 Apr 2021 17:55:56 +0200 Subject: [PATCH] Support custom event notifications --- www/common/notifications.js | 40 ++++++++++++++++++++++++----- www/common/outer/calendar.js | 38 ++++++++++++++------------- www/common/sframe-common-mailbox.js | 9 +++++++ 3 files changed, 63 insertions(+), 24 deletions(-) diff --git a/www/common/notifications.js b/www/common/notifications.js index 35ebb447c..f797a984b 100644 --- a/www/common/notifications.js +++ b/www/common/notifications.js @@ -460,22 +460,37 @@ define([ }; Messages.reminder_missed = "You missed {0} on {1}"; // XXX + Messages.reminder_now = "{0} is starting!"; // XXX Messages.reminder_inProgress = "{0} has started on {1}"; // XXX Messages.reminder_inProgressAllDay = "{0} is happening today"; // XXX Messages.reminder_minutes = "{0} will start in {1} minutes!"; // XXX - Messages.reminder_hour = "{0} will start in 1 hour!"; // XXX + Messages.reminder_time = "{0} will start today at {1}!"; // XXX + Messages.reminder_date = "{0} will start on {1}!"; // XXX + var getDate = function (time) { + return new Date(time).toLocaleDateString(); + }; handlers['REMINDER'] = function (common, data) { var content = data.content; var msg = content.msg.content; var missed = content.msg.missed; - var now = +new Date(); var start = msg.start; var title = Util.fixHTML(msg.title); + var i = 0; content.getFormatText = function () { + var now = +new Date(); + + // Events that have already started + var wasRefresh = content.autorefresh; + content.autorefresh = false; + // Missed events if (start < now && missed) { return Messages._getKey('reminder_missed', [title, new Date(start).toLocaleString()]); } + // Starting now + if (start < now && wasRefresh) { + return Messages._getKey('reminder_now', [title]); + } // In progress, is all day if (start < now && msg.isAllDay) { return Messages._getKey('reminder_inProgressAllDay', [title]); @@ -484,12 +499,25 @@ define([ if (start < now) { return Messages._getKey('reminder_inProgress', [title, new Date(start).toLocaleString()]); } + // Not started yet - if ((start - now) > 600000) { - return Messages._getKey('reminder_hour', [title]); + + // In less than an hour: show countdown in minutes + if ((start - now) < 3600000) { + var minutes = Math.round((start - now) / 60000); + content.autorefresh = true; + return Messages._getKey('reminder_minutes', [title, minutes]); + } + + // Not today: show full date + var nowDateStr = new Date().toLocaleDateString(); + var startDate = new Date(start); + if (nowDateStr !== startDate.toLocaleDateString()) { + return Messages._getKey('reminder_date', [title, startDate.toLocaleString()]); } - var minutes = Math.round((start - now) / 60000); - return Messages._getKey('reminder_minutes', [title, minutes]); + + // Today: show time + return Messages._getKey('reminder_time', [title, startDate.toLocaleTimeString()]); }; if (!content.archived) { content.dismissHandler = defaultDismiss(common, data); diff --git a/www/common/outer/calendar.js b/www/common/outer/calendar.js index 2113dcc69..c23b647c3 100644 --- a/www/common/outer/calendar.js +++ b/www/common/outer/calendar.js @@ -96,7 +96,7 @@ define([ var time60 = now + (3600 * 1000); // 1 hour from now var uid = ev.id; - ctx.store.data.lastVisit = 1617922639683; // XXX Friday Apr 09 + //ctx.store.data.lastVisit = 1617922639683; // XXX Friday Apr 09, used to test // Clear reminders for this event if (Array.isArray(reminders[uid])) { @@ -135,27 +135,29 @@ define([ }; var sendNotif = function () { ctx.Store.onReadyEvt.reg(send); }; - if (ev.start <= time10) { - sendNotif(); - return; - } - - // setTimeout only work with 32bit timeout values. - // FIXME: call this function again in xxx days to reload these missing timeout? - if (ev.start - time10 >= 2147483647) { return; } + var notifs = [600000, 3600000]; // 10min, 60min + notifs.sort(); + notifs.some(function (delay) { + var time = now + delay; - // It starts in more than 10 minutes: prepare the 10 minutes notification - reminders[uid].push(setTimeout(function () { - sendNotif(); - }, (ev.start - time10))); + // setTimeout only work with 32bit timeout values. If the event is too far away, + // ignore this event for now + // FIXME: call this function again in xxx days to reload these missing timeout? + if (ev.start - time >= 2147483647) { return true; } - if (ev.start <= time60) { return; } + // If we're too late to send a notification, send it instantly and ignore + // all notifications that were supposed to be sent even earlier + if (ev.start <= time) { + sendNotif(); + return true; + } - // It starts in more than 1 hour: prepare the 1 hour notification - reminders[uid].push(setTimeout(function () { - sendNotif(); - }, (ev.start - time60))); + // It starts in more than "delay": prepare the notification + reminders[uid].push(setTimeout(function () { + sendNotif(); + }, (ev.start - time))); + }); }; var addReminders = function (ctx, id, ev) { var calendar = ctx.calendars[id]; diff --git a/www/common/sframe-common-mailbox.js b/www/common/sframe-common-mailbox.js index 215a4866d..7ea717d35 100644 --- a/www/common/sframe-common-mailbox.js +++ b/www/common/sframe-common-mailbox.js @@ -87,6 +87,15 @@ define([ if (typeof(data.content.getFormatText) === "function") { $(notif).find('.cp-notification-content p').html(data.content.getFormatText()); + if (data.content.autorefresh) { + var it = setInterval(function () { + if (!data.content.autorefresh) { + clearInterval(it); + return; + } + $(notif).find('.cp-notification-content p').html(data.content.getFormatText()); + }, 60000); + } } if (data.content.isClickable) {