UNPKG

@webgap/notifier

Version:

Webgap abstraction notification module.

118 lines (112 loc) 4.27 kB
/** * (C) Copyright 2015 WebGAP (http://www.webgap.eu/). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Created by: ManuelMartins * Created on: 05-03-2015 * */ "use strict"; var underscore = require('underscore'); var async = require('async'); var util = require('util'); var systems = {}; var defaultSystem; /** * Sends a notification to the specified systems * * @param {Object} options * @param {Object} options.notification * @param {Array} options.notificationSystems * @param callback */ module.exports.notify = function notify(options, callback) { callback = callback || function noop(err) { console.warn('Something went wrong. Couldn\'t process the notification. %s', err); return; }; if (options) { // handle notification systems if (!options.notificationSystems) { if (!defaultSystem) { return callback(new Error('No \'defaultSystem\' defined and no option \'notificationSystems\' provided.')); } else { options.notificationSystems = [defaultSystem]; } } else { if (!Array.isArray(options.notificationSystems)) { options.notificationSystems = [options.notificationSystems]; } } // ensure the message is not delivered through the same system multiple times options.notificationSystems = underscore.uniq(options.notificationSystems); // notify using the requested systems async.each(options.notificationSystems, function sendNotification(system, done) { var instance = systems[system] ? systems[system].instance : null; if (instance) { try { instance.notify(options.notification, function afterNotify(err) { return done(err); }); } catch (err) { return done(err); } } else { console.warn('Trying to notify using a non registered system: %s', system); return done(); } }, callback); } else { return callback(new Error('No \'options\' provided. Cannot do anything from nothing.')); } }; /** * Register a new Notification System * * @param {Object} system * @param {string} system.name the name of the system - will be used later to identify this system * @param {Object} system.instance an instance of a class which implements a method notify * @param {boolean} system.defaultSystem true if you want it tobe the default system to notify * @throws Error if class doesn't implements a \'notify\' function */ module.exports.register = function register(system) { system = system || {}; // check if class implements a notify method if (system.instance && typeof system.instance.notify !== 'function') { throw new Error(util.format('The class provided must implement a \'notify\' function. Cannot register Notification System: %s', system.name)); } if (systems[system.name]) { console.warn('Notification system with name %s is already registered. Please unregister if is the case, or change the name and try again.', system.name); return false; } systems[system.name] = {}; systems[system.name].instance = system.instance; systems[system.name].name = system.name; systems[system.name].default = Boolean(system.defaultSystem) ? system.defaultSystem : false; console.info('%s notification system registered.', system.name); if (systems[system.name].default) { defaultSystem = system.name; console.info('%s is now the default notification system.', defaultSystem); } return true; }; /** * Unregister a Notification System * * @param {string} systemName the name of the system to unregister */ module.exports.unregister = function unregister(systemName) { delete systems[systemName]; console.info('%s notification system unregistered.', systemName); };