Add notifications for past events since our last visit

pull/1/head
yflory 4 years ago
parent e5d923e521
commit 11c0b752bd

@ -2288,7 +2288,8 @@ define([
cache: rdyCfg.cache,
noDrive: rdyCfg.noDrive,
disableCache: localStorage['CRYPTPAD_STORE|disableCache'],
driveEvents: !rdyCfg.noDrive //rdyCfg.driveEvents // Boolean
driveEvents: !rdyCfg.noDrive, //rdyCfg.driveEvents // Boolean
lastVisit: Number(localStorage.lastVisit) || undefined
};
common.userHash = userHash;
@ -2570,6 +2571,12 @@ define([
AppConfig.afterLogin(common, waitFor());
}
}).nThen(function () {
// Last visit is used to warn you about missed events from your calendars
localStorage.lastVisit = +new Date();
setInterval(function () {
// Bump last visit every minute
localStorage.lastVisit = +new Date();
}, 60000);
f(void 0, env);
if (typeof(window.onhashchange) === 'function') { window.onhashchange(); }
});

@ -459,19 +459,37 @@ define([
}
};
Messages.reminder_missed = "You missed <b>{0}</b> on {1}"; // XXX
Messages.reminder_inProgress = "<b>{0}</b> has started on {1}"; // XXX
Messages.reminder_inProgressAllDay = "<b>{0}</b> is happening today"; // XXX
Messages.reminder_minutes = "<b>{0}</b> will start in {1} minutes!"; // XXX
Messages.reminder_hour = "<b>{0}</b> will start in 1 hour!"; // XXX
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);
content.getFormatText = function () {
// Missed events
if (start < now && missed) {
return Messages._getKey('reminder_missed', [title, new Date(start).toLocaleString()]);
}
// In progress, is all day
if (start < now && msg.isAllDay) {
return Messages._getKey('reminder_inProgressAllDay', [title]);
}
// In progress, normal event
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', [Util.fixHTML(msg.title)]);
return Messages._getKey('reminder_hour', [title]);
}
var minutes = Math.round((start - now) / 60000);
return Messages._getKey('reminder_minutes', [Util.fixHTML(msg.title), minutes]);
return Messages._getKey('reminder_minutes', [title, minutes]);
};
if (!content.archived) {
content.dismissHandler = defaultDismiss(common, data);

@ -90,31 +90,43 @@ define([
});
};
var updateEventReminders = function (ctx, reminders, ev) {
var updateEventReminders = function (ctx, reminders, ev, useLastVisit) {
var now = +new Date();
var time10 = now + (600 * 1000); // 10 minutes from now
var time60 = now + (3600 * 1000); // 1 hour from now
var uid = ev.id;
ctx.store.data.lastVisit = 1617922639683; // XXX Friday Apr 09
// Clear reminders for this event
if (Array.isArray(reminders[uid])) {
reminders[uid].forEach(function (to) { clearTimeout(to); });
}
reminders[uid] = [];
// No reminder for past events
if (ev.start <= now) {
var last = ctx.store.data.lastVisit;
// XXX add a limit to make sure we don't go too far in the past?
var missed = useLastVisit && ev.start > last && ev.end <= now;
if (ev.end <= now && !missed) {
// No reminder for past events
delete reminders[uid];
return;
}
// XXX
// TODO
// use custom notifications per event
// if you missed a notification, show it instantly (eg: load cryptpad 45min before an event, show the 1h notification)
var send = function () {
var hide = Util.find(ctx, ['store', 'proxy', 'settings', 'general', 'calendar', 'hideNotif']);
if (hide) { return; }
var ctime = ev.start <= now ? ev.start : +new Date(); // Correct order for past events
ctx.store.mailbox.showMessage('reminders', {
msg: {
ctime: +new Date(),
ctime: ctime,
type: "REMINDER",
missed: Boolean(missed),
content: ev
},
hash: 'REMINDER|'+uid
@ -152,7 +164,7 @@ define([
updateEventReminders(ctx, calendar.reminders, ev);
};
var addInitialReminders = function (ctx, id) {
var addInitialReminders = function (ctx, id, useLastVisit) {
var calendar = ctx.calendars[id];
if (!calendar || !calendar.reminders) { return; }
if (Object.keys(calendar.reminders).length) { return; } // Already initialized
@ -164,7 +176,7 @@ define([
var content = Util.find(calendar, ['proxy', 'content']);
if (!content) { return; }
Object.keys(content).forEach(function (uid) {
updateEventReminders(ctx, calendar.reminders, content[uid]);
updateEventReminders(ctx, calendar.reminders, content[uid], useLastVisit);
});
};
var openChannel = function (ctx, cfg, _cb) {
@ -325,7 +337,7 @@ define([
c.cacheready = true;
setTimeout(update);
if (cb) { cb(null, lm.proxy); }
addInitialReminders(ctx, channel);
addInitialReminders(ctx, channel, cfg.lastVisitNotif);
}).on('ready', function (info) {
var md = info.metadata;
c.owners = md.owners || [];
@ -342,7 +354,7 @@ define([
}
setTimeout(update);
if (cb) { cb(null, lm.proxy); }
addInitialReminders(ctx, channel);
addInitialReminders(ctx, channel, cfg.lastVisitNotif);
}).on('change', [], function () {
if (!c.ready) { return; }
setTimeout(update);
@ -455,6 +467,7 @@ define([
decryptTeamCalendarHref(store, cal);
openChannel(ctx, {
storeId: storeId,
lastVisitNotif: true,
data: cal
});
});

Loading…
Cancel
Save