57 lines
1.7 KiB
JavaScript
57 lines
1.7 KiB
JavaScript
function createSnoozeReminderService({
|
|
readAccountStore,
|
|
updateAccountStore,
|
|
showNotification,
|
|
sendMailSyncUpdate,
|
|
now = Date.now,
|
|
logError = (...args) => console.error(...args)
|
|
}) {
|
|
let checkInProgress = false;
|
|
|
|
async function checkSnoozeReminders() {
|
|
if (checkInProgress) return;
|
|
checkInProgress = true;
|
|
try {
|
|
const current = await readAccountStore();
|
|
if (!current?.accounts.some(account =>
|
|
(account.snoozedMessages || []).some(item => Number(item.dueAt) <= now())
|
|
)) return;
|
|
const due = await updateAccountStore(store => {
|
|
const reminders = [];
|
|
for (const account of store.accounts) {
|
|
const snoozes = account.snoozedMessages || [];
|
|
const remaining = [];
|
|
for (const item of snoozes) {
|
|
if (Number(item.dueAt) <= now()) reminders.push({
|
|
accountId: account.id,
|
|
email: account.email,
|
|
notificationsEnabled: account.notificationsEnabled !== false,
|
|
...item
|
|
});
|
|
else remaining.push(item);
|
|
}
|
|
account.snoozedMessages = remaining;
|
|
}
|
|
return reminders;
|
|
});
|
|
for (const reminder of due) {
|
|
if (reminder.notificationsEnabled !== false) showNotification(reminder);
|
|
sendMailSyncUpdate({
|
|
accountId: reminder.accountId,
|
|
snoozeReminder: true,
|
|
snoozedFolder: reminder.folder,
|
|
foldersChanged: ['Snoozed', reminder.folder]
|
|
});
|
|
}
|
|
} catch (error) {
|
|
logError('Could not process snooze reminders:', error);
|
|
} finally {
|
|
checkInProgress = false;
|
|
}
|
|
}
|
|
|
|
return { checkSnoozeReminders };
|
|
}
|
|
|
|
module.exports = { createSnoozeReminderService };
|