From 5c1a95d5d20d96f1ee487d7bd1b3d256f3973cd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Baz=C3=A1n?= Date: Wed, 13 Jan 2016 13:14:42 -0300 Subject: [PATCH] refactor --- config.js | 6 +-- lib/pushnotificationsservice.js | 69 ++++++++------------------- test/integration/pushNotifications.js | 28 ++++------- 3 files changed, 30 insertions(+), 73 deletions(-) diff --git a/config.js b/config.js index f5f944f..c3887e4 100644 --- a/config.js +++ b/config.js @@ -50,11 +50,7 @@ var config = { defaultLanguage: 'en', defaultUnit: 'btc', subjectPrefix: '', - publicTxUrlTemplate: { - livenet: 'https://insight.bitpay.com/tx/{{txid}}', - testnet: 'https://test-insight.bitpay.com/tx/{{txid}}', - }, - pushServerUrl: 'http://192.168.1.111:8000/send', + pushServerUrl: 'http://localhost:8000/send', }, // To use email notifications uncomment this: // emailOpts: { diff --git a/lib/pushnotificationsservice.js b/lib/pushnotificationsservice.js index 7b4ff48..8d8b81c 100644 --- a/lib/pushnotificationsservice.js +++ b/lib/pushnotificationsservice.js @@ -58,7 +58,6 @@ PushNotificationsService.prototype.start = function(opts, cb) { self.defaultLanguage = opts.pushNotificationsOpts.defaultLanguage || 'en'; self.defaultUnit = opts.pushNotificationsOpts.defaultUnit || 'btc'; self.subjectPrefix = opts.pushNotificationsOpts.subjectPrefix || ''; - self.publicTxUrlTemplate = opts.pushNotificationsOpts.publicTxUrlTemplate || {}; self.pushServerUrl = opts.pushNotificationsOpts.pushServerUrl; async.parallel([ @@ -133,12 +132,14 @@ PushNotificationsService.prototype._sendPushNotifications = function(notificatio function(optsList, next) { async.each(optsList, function(opts, next) { - self._makeRequest(opts, next()); + self._makeRequest(opts, function(err, response) { + if (err) log.error(err); + log.debug('Post status : ', response); + next(); + }); }, function(err) { - if (err) - log.error(err); - return cb(err); + return next(err); } ); }, @@ -172,7 +173,7 @@ PushNotificationsService.prototype._getRecipientsList = function(notification, c if (err) log.error(err); if (_.isEmpty(preferences)) preferences = []; - var recipients = _.compact(_.map(preferences, function(p) { + var recipientPreferences = _.compact(_.map(preferences, function(p) { if (!_.contains(self.availableLanguages, p.language)) { if (p.language) @@ -181,13 +182,22 @@ PushNotificationsService.prototype._getRecipientsList = function(notification, c } return { - id: p.copayerId, + copayerId: p.copayerId, language: p.language, - unit: p.unit || self.defaultUnit, + unit: p.unit, }; })); - var recipientsList = _.reject(self._join(wallet.copayers, recipients), { + recipientPreferences = _.indexBy(recipientPreferences, 'copayerId'); + + var recipientsList = _.reject(_.map(wallet.copayers, function(copayer) { + var p = recipientPreferences[copayer.id] || {}; + return { + copayerId: copayer.id, + language: p.language || self.defaultLanguage, + unit: p.unit || self.defaultUnit, + } + }), { copayerId: notification.creatorId }); @@ -196,31 +206,6 @@ PushNotificationsService.prototype._getRecipientsList = function(notification, c }); }; -PushNotificationsService.prototype._join = function(copayers, recipients) { - var self = this; - var recipientsList = _.compact(_.map(copayers, function(c) { - - var structure = {}; - - _.forEach(recipients, function(r) { - if (r.id == c.id) { - structure.copayerId = r.id; - structure.language = r.language; - structure.unit = r.unit || self.defaultUnit; - } - }); - - if (_.isEmpty(structure)) { - structure.copayerId = c.id; - structure.language = self.defaultLanguage; - structure.unit = self.defaultUnit; - } - - return structure; - })); - return recipientsList; -}; - PushNotificationsService.prototype._readAndApplyTemplates = function(notification, notifType, recipientsList, cb) { var self = this; @@ -299,16 +284,6 @@ PushNotificationsService.prototype._getDataForTemplate = function(notification, data.rejectorsNames = rejectors.join(', '); } - if (_.contains(['NewIncomingTx', 'NewOutgoingTx'], notification.type) && data.txid) { - var urlTemplate = self.publicTxUrlTemplate[wallet.network]; - if (urlTemplate) { - try { - data.urlForTx = Mustache.render(urlTemplate, data); - } catch (ex) { - log.warn('Could not render public url for tx', ex); - } - } - } return cb(null, data); }); }; @@ -370,10 +345,8 @@ PushNotificationsService.prototype._makeRequest = function(opts, cb) { method: 'POST', json: true, body: opts - }, function(error, response) { - if (error) log.error(error); - log.debug('Post status : ', response); - return; + }, function(err, response) { + return cb(err, response); }); }; diff --git a/test/integration/pushNotifications.js b/test/integration/pushNotifications.js index 55249b1..c71d837 100644 --- a/test/integration/pushNotifications.js +++ b/test/integration/pushNotifications.js @@ -59,11 +59,8 @@ describe('Push notifications', function() { defaultLanguage: 'en', defaultUnit: 'btc', subjectPrefix: '', - publicTxUrlTemplate: { - livenet: 'https://insight.bitpay.com/tx/{{txid}}', - testnet: 'https://test-insight.bitpay.com/tx/{{txid}}', - }, - pushServerUrl: 'http://192.168.1.111:8000/send', + + pushServerUrl: 'http://localhost:8000/send', }, }, function(err) { should.not.exist(err); @@ -180,11 +177,8 @@ describe('Push notifications', function() { defaultLanguage: 'en', defaultUnit: 'btc', subjectPrefix: '', - publicTxUrlTemplate: { - livenet: 'https://insight.bitpay.com/tx/{{txid}}', - testnet: 'https://test-insight.bitpay.com/tx/{{txid}}', - }, - pushServerUrl: 'http://192.168.1.111:8000/send', + + pushServerUrl: 'http://localhost:8000/send', }, }, function(err) { should.not.exist(err); @@ -392,11 +386,9 @@ describe('Push notifications', function() { args[0].body.android.data.title.should.contain('Payment sent'); args[1].body.android.data.title.should.contain('Payment sent'); - server.getWallet(null, function(err, w) { - server.copayerId.should.not.equal((args[0].body.users[0]).split('$')[1]); - server.copayerId.should.not.equal((args[1].body.users[0]).split('$')[1]); - done(); - }); + server.copayerId.should.not.equal((args[0].body.users[0]).split('$')[1]); + server.copayerId.should.not.equal((args[1].body.users[0]).split('$')[1]); + done(); }, 100); }); }); @@ -431,11 +423,7 @@ describe('Push notifications', function() { defaultLanguage: 'en', defaultUnit: 'btc', subjectPrefix: '', - publicTxUrlTemplate: { - livenet: 'https://insight.bitpay.com/tx/{{txid}}', - testnet: 'https://test-insight.bitpay.com/tx/{{txid}}', - }, - pushServerUrl: 'http://192.168.1.111:8000/send', + pushServerUrl: 'http://localhost:8000/send', }, }, function(err) { should.not.exist(err);