diff --git a/src/eventlog.js b/src/eventlog.js index 5acf534a6..6ac6b4623 100644 --- a/src/eventlog.js +++ b/src/eventlog.js @@ -105,28 +105,11 @@ function add(action, source, data, callback) { api(uuid.v4(), action, source, data, function (error, id) { if (error) return callback(new EventLogError(EventLogError.INTERNAL_ERROR, error)); - // decide if we want to add notifications as well - if (action === exports.ACTION_USER_ADD) { - notifications.userAdded(source.userId, id, data.user); - } else if (action === exports.ACTION_USER_REMOVE) { - notifications.userRemoved(source.userId, id, data.user); - } else if (action === exports.ACTION_USER_UPDATE && data.adminStatusChanged) { - notifications.adminChanged(source.userId, id, data.user); - } else if (action === exports.ACTION_APP_OOM) { - notifications.oomEvent(id, data.app ? data.app.id : data.containerId, { app: data.app, details: data }); - } else if (action === exports.ACTION_APP_DOWN) { - notifications.appDied(id, data.app); - } else if (action === exports.ACTION_APP_UP) { - notifications.appUp(id, data.app); - } else if (action === exports.ACTION_APP_TASK_CRASH) { - notifications.apptaskCrash(id, data.appId, data.crashLogFile); - } else if (action === exports.ACTION_PROCESS_CRASH) { - notifications.processCrash(id, data.processName, data.crashLogFile); - } else { - // no notification - } + notifications.onEvent(id, action, source, data, function (error) { + if (error) return callback(new EventLogError(EventLogError.INTERNAL_ERROR, error)); - callback(null, { id: id }); + callback(null, { id: id }); + }); }); } diff --git a/src/notifications.js b/src/notifications.js index 6a343ed23..46969cc8b 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -9,15 +9,9 @@ exports = module.exports = { ack: ack, getAllPaged: getAllPaged, + onEvent: onEvent, + // specialized notifications - userAdded: userAdded, - userRemoved: userRemoved, - adminChanged: adminChanged, - oomEvent: oomEvent, - appUp: appUp, - appDied: appDied, - processCrash: processCrash, - apptaskCrash: apptaskCrash, backupConfigWarning: backupConfigWarning, diskSpaceWarning: diskSpaceWarning, mailStatusWarning: mailStatusWarning, @@ -29,6 +23,7 @@ var assert = require('assert'), config = require('./config.js'), DatabaseError = require('./databaseerror.js'), debug = require('debug')('box:notifications'), + eventlog = require('./eventlog.js'), mailer = require('./mailer.js'), notificationdb = require('./notificationdb.js'), safe = require('safetydance'), @@ -328,3 +323,35 @@ function diskSpaceWarning(message) { if (error) console.error(error); }); } + +function onEvent(id, action, source, data, callback) { + assert.strictEqual(typeof id, 'string'); + assert.strictEqual(typeof action, 'string'); + assert.strictEqual(typeof source, 'object'); + assert.strictEqual(typeof data, 'object'); + assert.strictEqual(typeof callback, 'function'); + + // decide if we want to add notifications as well + if (action === eventlog.ACTION_USER_ADD) { + userAdded(source.userId, id, data.user); + } else if (action === eventlog.ACTION_USER_REMOVE) { + userRemoved(source.userId, id, data.user); + } else if (action === eventlog.ACTION_USER_UPDATE && data.adminStatusChanged) { + adminChanged(source.userId, id, data.user); + } else if (action === eventlog.ACTION_APP_OOM) { + oomEvent(id, data.app ? data.app.id : data.containerId, { app: data.app, details: data }); + } else if (action === eventlog.ACTION_APP_DOWN) { + appDied(id, data.app); + } else if (action === eventlog.ACTION_APP_UP) { + appUp(id, data.app); + } else if (action === eventlog.ACTION_APP_TASK_CRASH) { + apptaskCrash(id, data.appId, data.crashLogFile); + } else if (action === eventlog.ACTION_PROCESS_CRASH) { + processCrash(id, data.processName, data.crashLogFile); + } else { + // no notification + } + + callback(); +} +