From aacf21778da19aa827ddb3002297b6ed6e832997 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Baz=C3=A1n?= Date: Fri, 8 Jan 2016 13:26:51 -0300 Subject: [PATCH] refactor --- lib/pushnotificationsservice.js | 56 ++++++++++++++++----------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/lib/pushnotificationsservice.js b/lib/pushnotificationsservice.js index 11745d3..a497f17 100644 --- a/lib/pushnotificationsservice.js +++ b/lib/pushnotificationsservice.js @@ -13,6 +13,8 @@ var Model = require('./model'); var log = require('npmlog'); log.debug = log.verbose; +var self = this; + var PUSHNOTIFICATIONS_TYPES = { 'NewCopayer': { filename: 'new_copayer', @@ -34,6 +36,8 @@ var PUSHNOTIFICATIONS_TYPES = { } }; +var url = 'http://192.168.1.143:8000/send'; + function PushNotificationsService() {}; PushNotificationsService.prototype.start = function(opts, cb) { @@ -53,7 +57,6 @@ PushNotificationsService.prototype.start = function(opts, cb) { }); }; - var self = this; self.templatePath = path.normalize(((__dirname + '/templates')) + '/'); self.defaultLanguage = 'en'; self.defaultUnit = 'btc'; @@ -85,18 +88,19 @@ PushNotificationsService.prototype.start = function(opts, cb) { }; PushNotificationsService.prototype._sendPushNotifications = function(notification, cb) { - var self = this; - var url = 'http://192.168.1.143:8000/send'; cb = cb || function() {}; var notifType = PUSHNOTIFICATIONS_TYPES[notification.type]; if (!notifType) return cb(); self._getRecipientsList(notification.walletId, function(err, recipientsList) { + if (err) log.error(err); + self.storage.fetchWallet(notification.walletId, function(err, wallet) { + if (err) log.error(err); var resultedRecipientsList = _.reject(self._getJoinedRecipientsList(wallet, recipientsList), { - id: notification.creatorId || null + id: notification.creatorId }); async.waterfall([ function(next) { @@ -114,9 +118,8 @@ PushNotificationsService.prototype._sendPushNotifications = function(notificatio } }; opts.ios = { - "badge": 0, "alert": content.plain.body, - "sound": "soundName" + "sound": "" }; return next(err, opts); }, next); @@ -124,14 +127,11 @@ PushNotificationsService.prototype._sendPushNotifications = function(notificatio function(optsList, next) { async.each(optsList, function(opts, next) { - request({ - url: url, - method: 'POST', - json: true, - body: opts - }, function(error, response, body) { + self._makeRequest(opts, function(err, response) { + if (err) log.error(err); + log.debug('Post status : ', response); next(); - }); + }) }, function(err) { log.error(err); @@ -150,7 +150,6 @@ PushNotificationsService.prototype._sendPushNotifications = function(notificatio }; PushNotificationsService.prototype._getRecipientsList = function(walletId, cb) { - var self = this; self.storage.fetchPreferences(walletId, null, function(err, preferences) { if (err) return cb(err); @@ -159,10 +158,10 @@ PushNotificationsService.prototype._getRecipientsList = function(walletId, cb) { var recipients = _.compact(_.map(preferences, function(p) { if (!_.contains(self.availableLanguages, p.language)) { - if (!p.language) { + if (p.language) { log.warn('Language for notifications "' + p.language + '" not available.'); - p.language = self.defaultLanguage; } + p.language = self.defaultLanguage; } return { @@ -177,15 +176,8 @@ PushNotificationsService.prototype._getRecipientsList = function(walletId, cb) { }; PushNotificationsService.prototype._getJoinedRecipientsList = function(wallet, recipientsList) { - var self = this; var _recipientsList = _.compact(_.map(wallet.copayers, function(c) { - if (!recipientsList) return { - id: c.id, - language: 'en', - unit: self.defaultUnit - }; - var structure = {}; _.forEach(recipientsList, function(r) { @@ -198,7 +190,7 @@ PushNotificationsService.prototype._getJoinedRecipientsList = function(wallet, r if (_.isEmpty(structure)) { structure.id = c.id; - structure.language = 'en'; + structure.language = self.defaultLanguage; structure.unit = self.defaultUnit; } @@ -208,7 +200,6 @@ PushNotificationsService.prototype._getJoinedRecipientsList = function(wallet, r }; PushNotificationsService.prototype._readAndApplyTemplates = function(notification, notifType, recipientsList, cb) { - var self = this; async.map(recipientsList, function(recipient, next) { async.waterfall([ @@ -241,7 +232,6 @@ PushNotificationsService.prototype._readAndApplyTemplates = function(notificatio }; PushNotificationsService.prototype._getDataForTemplate = function(notification, recipient, cb) { - var self = this; var UNIT_LABELS = { btc: 'BTC', bit: 'bits' @@ -316,7 +306,6 @@ PushNotificationsService.prototype._applyTemplate = function(template, data, cb) }; PushNotificationsService.prototype._loadTemplate = function(notifType, recipient, extension, cb) { - var self = this; self._readTemplateFile(recipient.language, notifType.filename + extension, function(err, template) { if (err) return cb(err); @@ -325,7 +314,6 @@ PushNotificationsService.prototype._loadTemplate = function(notifType, recipient }; PushNotificationsService.prototype._readTemplateFile = function(language, filename, cb) { - var self = this; var fullFilename = path.join(self.templatePath, language, filename); fs.readFile(fullFilename, 'utf8', function(err, template) { @@ -347,4 +335,16 @@ PushNotificationsService.prototype._compileTemplate = function(template, extensi }; }; +PushNotificationService.prototype._makeRequest = function(opts, cb) { + request({ + url: url, + method: 'POST', + json: true, + body: opts + }, function(error, response) { + if (error) return cb(error); + return cb(null, response); + }); +}; + module.exports = PushNotificationsService;