notifications: clearAlert

This commit is contained in:
Girish Ramakrishnan
2023-03-26 14:18:37 +02:00
parent 0ab73d6c5e
commit 61ba3cbfc2
3 changed files with 28 additions and 19 deletions
+6 -2
View File
@@ -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() {
+20 -15
View File
@@ -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');
+2 -2
View File
@@ -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);