var JsonUserManager = exports.JsonUserManager = function(file) { EventEmitter.call(this); var adminName = "admin"; var users = {}; var lastModified = null; Object.defineProperties(this, { /** * The JSON file containing the user accounts * @type String */ "file": { "value": file, "enumerable": true }, /** * An object containing the user accounts * @type Object */ "users": { "get": function() { return users || {}; }, "enumerable": true }, /** * Returns the name of the admin account (default: admin) * @returns {String} The name of the admin account */ "getAdminName": { "value": function() { return adminName; }, "enumerable": true }, /** * Sets the admin name to the one passed as argument * @param {String} name The name of the admin account */ "setAdminName": { "value": function(name) { adminName = name; return; }, "enumerable": true }, /** * Loads the users from the JSON file. This method triggers a "reloaded" * event on this usermanager instance. * @param {String} jsonFile Optional path to the JSON file to load. If given * this file is used for all other file-related operations. */ "loadUsers": { "value": function(jsonFile) { if (typeof(jsonFile) === "string") { file = jsonFile; } if (fs.exists(file) && fs.isFile(file)) { users = JSON.parse(fs.read(file)); lastModified = fs.lastModified(file).getTime(); } else { throw new Error('File does not exist or is not a file: "' + file + '"'); } this.emit("reloaded"); }, "enumerable": true }, /** * Saves the users as JSON string into the file this manager operates on */ "saveUsers": { "value": function() { fs.write(file, JSON.stringify(users, null, 4)); lastModified = fs.lastModified(file).getTime(); }, "enumerable": true }, /** * Checks if the user file has been modified and reloads if necessary */ "checkFile": { "value": function() { if (lastModified != fs.lastModified(file).getTime()) { this.loadUsers(); } }, "enumerable": false } }); // load the users from file this.loadUsers(); return this; };
var Message = exports.Message = function(data, options) { var options = options || {}; if (!data) { throw new Error("Unable to construct APNSMessage without data"); } EventEmitter.call(this); if (data instanceof ApnsPayloadBuilder) { this.payload = data; } else { this.payload = new ApnsPayloadBuilder(); if (data && typeof(data) != "object") { this.payload.addCustomProperty("message", data); } else if (data) { for (let prop in data) { switch (prop) { case "alertBody": this.payload.setAlertBody(data.alertBody); break; case "alertTitle": this.payload.setAlertTitle(data.alertTitle); // title only shows on apple-watch atm break; case "badgenumber": this.payload.setBadgeNumber(parseInt(data.badgenumber,10)); break; case "categoryName": this.payload.setCategoryName(data.category); break; case "launchImageFileName": this.payload.setLaunchImageFileName(data.launchImageFileName); break; case "localizedActionButtonKey": this.payload.setLocalizedActionButtonKey(data.localizedActionButtonKey); break; case "localizedAlertMessage": this.payload.setLocalizedAlertMessage(data.localizedAlertMessage.key, data.localizedAlertMessage.arguments); break; case "localizedAlertTitle": this.payload.setLocalizedAlertMessage(data.localizedAlertTitle.key, data.localizedAlertTitle.arguments); break; case "soundFileName": this.payload.setSoundFileName(data.soundFileName); break; default: this.payload.addCustomProperty(prop, data[prop]); break; } } } } this.options = options; if (options.soundFileName) { this.payload.setSoundFileName(options.soundFileName); } if (options.contentAvailable) { this.payload.setContentAvailable(true); } if (options.showActionButton) { this.payload.setShowActionButton(true); } this.recipients = []; if (options.time_to_live != undefined) { this.setTtl(options.time_to_live); } if (options.recipients != undefined) { this.addRecipients(options.recipients); } this.delay = options.delay || 0; this.createtime = new Date(); return this; };
exports.testMixin = function() { var e = {}; EventEmitter.call(e); assert.isFalse(e instanceof EventEmitter); testEmitter(e); };