notifications: rename alert to pin and unpin
This commit is contained in:
+36
-30
@@ -14,18 +14,21 @@ exports = module.exports = {
|
||||
TYPE_CLOUDRON_UPDATE_FAILED: 'cloudronUpdateFailed',
|
||||
TYPE_CERTIFICATE_RENEWAL_FAILED: 'certificateRenewalFailed',
|
||||
TYPE_BACKUP_CONFIG: 'backupConfig',
|
||||
TYPE_DISK_SPACE: 'diskSpace',
|
||||
TYPE_MAIL_STATUS: 'mailStatus',
|
||||
TYPE_REBOOT: 'reboot',
|
||||
TYPE_BOX_UPDATE: 'boxUpdate',
|
||||
TYPE_UPDATE_UBUNTU: 'ubuntuUpdate',
|
||||
TYPE_MANUAL_APP_UPDATE_NEEDED: 'manualAppUpdate',
|
||||
TYPE_APP_OOM: 'appOutOfMemory',
|
||||
TYPE_APP_UPDATED: 'appUpdated',
|
||||
TYPE_BACKUP_FAILED: 'backupFailed',
|
||||
|
||||
alert,
|
||||
clearAlert,
|
||||
// these are singleton types allowed in pin() and unpin()
|
||||
TYPE_DISK_SPACE: 'diskSpace',
|
||||
TYPE_MAIL_STATUS: 'mailStatus',
|
||||
TYPE_REBOOT: 'reboot',
|
||||
TYPE_UPDATE_UBUNTU: 'ubuntuUpdate',
|
||||
TYPE_BOX_UPDATE: 'boxUpdate',
|
||||
TYPE_MANUAL_APP_UPDATE_NEEDED: 'manualAppUpdate-',
|
||||
|
||||
// these work off singleton types
|
||||
pin,
|
||||
unpin,
|
||||
|
||||
// exported for testing
|
||||
_add: add
|
||||
@@ -38,6 +41,7 @@ const assert = require('assert'),
|
||||
constants = require('./constants.js'),
|
||||
dashboard = require('./dashboard.js'),
|
||||
database = require('./database.js'),
|
||||
debug = require('debug')('box:notifications'),
|
||||
eventlog = require('./eventlog.js'),
|
||||
mailer = require('./mailer.js'),
|
||||
users = require('./users.js');
|
||||
@@ -73,10 +77,10 @@ async function get(id) {
|
||||
return postProcess(result[0]);
|
||||
}
|
||||
|
||||
async function getByTitle(title) {
|
||||
assert.strictEqual(typeof title, 'string');
|
||||
async function getByType(type) {
|
||||
assert.strictEqual(typeof type, 'string');
|
||||
|
||||
const results = await database.query(`SELECT ${NOTIFICATION_FIELDS} from notifications WHERE title = ? ORDER BY creationTime LIMIT 1`, [ title ]);
|
||||
const results = await database.query(`SELECT ${NOTIFICATION_FIELDS} from notifications WHERE type = ? ORDER BY creationTime LIMIT 1`, [ type ]);
|
||||
if (results.length === 0) return null;
|
||||
|
||||
return postProcess(results[0]);
|
||||
@@ -235,36 +239,38 @@ async function backupFailed(eventId, taskId, errorMessage) {
|
||||
}
|
||||
}
|
||||
|
||||
// type must be one of TYPE_*
|
||||
async function alert(type, title, message, options) {
|
||||
assert.strictEqual(typeof type, 'string');
|
||||
async function pin(type, title, message, options) {
|
||||
assert.strictEqual(typeof type, 'string'); // TYPE_
|
||||
assert.strictEqual(typeof title, 'string');
|
||||
assert.strictEqual(typeof message, 'string');
|
||||
assert.strictEqual(typeof options, 'object');
|
||||
|
||||
const result = await getByTitle(title);
|
||||
// these are singletons. only one notification of such a type can be there
|
||||
if (type !== exports.TYPE_DISK_SPACE && type !== exports.TYPE_MAIL_STATUS && type !== exports.TYPE_REBOOT && type !== exports.TYPE_UPDATE_UBUNTU &&
|
||||
type !== exports.TYPE_BOX_UPDATE && type !== exports.TYPE_MANUAL_APP_UPDATE_NEEDED) {
|
||||
debug(`pin: notification of ${type} cannot be pinned`);
|
||||
return null;
|
||||
}
|
||||
|
||||
const isUpdateType = type === exports.TYPE_BOX_UPDATE || type === exports.TYPE_MANUAL_APP_UPDATE_NEEDED;
|
||||
if (options.context) type = `${type}-${options.context}`; // create a unique type for this context
|
||||
|
||||
const result = await getByType(type);
|
||||
if (!result) return await add(type, title, message, { eventId: null });
|
||||
if (!options.persist) return result.id;
|
||||
|
||||
await update(result, {
|
||||
id: result.id,
|
||||
eventId: null,
|
||||
type: type,
|
||||
title,
|
||||
message,
|
||||
acknowledged: false,
|
||||
creationTime: new Date()
|
||||
});
|
||||
// do not reset the ack state if user has already seen the update notification
|
||||
const acknowledged = (isUpdateType && result.message === message) ? result.acknowledged : false;
|
||||
|
||||
await update(result, { id: result.id, title, message, acknowledged, creationTime: new Date() });
|
||||
return result.id;
|
||||
}
|
||||
|
||||
// id is unused but nice to search code
|
||||
async function clearAlert(type) {
|
||||
assert.strictEqual(typeof type, 'string');
|
||||
async function unpin(type, options) {
|
||||
assert.strictEqual(typeof type, 'string'); // TYPE_
|
||||
assert.strictEqual(typeof options, 'object');
|
||||
|
||||
await database.query('DELETE FROM notifications WHERE type = ?', [ type ]);
|
||||
return null;
|
||||
if (options.context) type = `${type}-${options.context}`; // create a unique type for this context
|
||||
await database.query('UPDATE notifications SET acknowledged=1 WHERE type = ?', [ type ]);
|
||||
}
|
||||
|
||||
async function onEvent(id, action, source, data) {
|
||||
|
||||
Reference in New Issue
Block a user