From 61ba3cbfc232d9983c5882c8f2fbc1452309f090 Mon Sep 17 00:00:00 2001 From: Girish Ramakrishnan Date: Sun, 26 Mar 2023 14:18:37 +0200 Subject: [PATCH] notifications: clearAlert --- src/cloudron.js | 8 ++++++-- src/notifications.js | 35 +++++++++++++++++++--------------- src/test/notifications-test.js | 4 ++-- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/cloudron.js b/src/cloudron.js index 403688b09..d4ff341bf 100644 --- a/src/cloudron.js +++ b/src/cloudron.js @@ -168,7 +168,7 @@ async function getConfig() { } async function reboot() { - await notifications.alert(notifications.ALERT_REBOOT, 'Reboot Required', ''); + await notifications.clearAlert(notifications.ALERT_REBOOT, 'Reboot Required'); const [error] = await safe(shell.promises.sudo('reboot', [ REBOOT_CMD ], {})); if (error) debug('reboot: could not reboot', error); @@ -198,7 +198,11 @@ async function checkMailStatus() { async function checkRebootRequired() { const rebootRequired = await isRebootRequired(); - await notifications.alert(notifications.ALERT_REBOOT, 'Reboot Required', rebootRequired ? 'To finish ubuntu security updates, a reboot is necessary.' : ''); + if (rebootRequired) { + await notifications.alert(notifications.ALERT_REBOOT, 'Reboot Required', 'To finish ubuntu security updates, a reboot is necessary.'); + } else { + await notifications.clearAlert(notifications.ALERT_REBOOT, 'Reboot Required'); + } } async function checkUbuntuVersion() { diff --git a/src/notifications.js b/src/notifications.js index 8920563f4..b526f51fd 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -17,6 +17,7 @@ exports = module.exports = { ALERT_MANUAL_APP_UPDATE: 'manualAppUpdate', alert, + clearAlert, // exported for testing _add: add @@ -233,25 +234,29 @@ async function alert(id, title, message) { const result = await getByTitle(title); - if (message) { - if (!result) { - return await add(null /* eventId */, title, message); - } else { - await update(result, { - eventId: null, - title, - message, - acknowledged: false, - creationTime: new Date() - }); - return result.id; - } + if (!result) { + return await add(null /* eventId */, title, message); } else { - await database.query('DELETE FROM notifications WHERE title = ?', [ title ]); - return null; + await update(result, { + eventId: null, + title, + message, + acknowledged: false, + creationTime: new Date() + }); + return result.id; } } +// id is unused but nice to search code +async function clearAlert(id, title) { + assert.strictEqual(typeof id, 'string'); + assert.strictEqual(typeof title, 'string'); + + await database.query('DELETE FROM notifications WHERE title = ?', [ title ]); + return null; +} + async function onEvent(id, action, source, data) { assert.strictEqual(typeof id, 'string'); assert.strictEqual(typeof action, 'string'); diff --git a/src/test/notifications-test.js b/src/test/notifications-test.js index 7d4cbd3d8..05ae8dc1d 100644 --- a/src/test/notifications-test.js +++ b/src/test/notifications-test.js @@ -105,8 +105,8 @@ describe('Notifications', function () { expect(result.acknowledged).to.be(false); // notification resurfaces }); - it('can delete the alert', async function () { - const id = await notifications.alert(notifications.ALERT_BOX_UPDATE, 'Cloudron xx is available', ''); + it('can clear the alert', async function () { + const id = await notifications.clearAlert(notifications.ALERT_BOX_UPDATE, 'Cloudron xx is available'); expect(id).to.be(null); const result = await notifications.get(alertId);