diff --git a/lib/pushnotificationsservice.js b/lib/pushnotificationsservice.js index badd855..85114f3 100644 --- a/lib/pushnotificationsservice.js +++ b/lib/pushnotificationsservice.js @@ -33,6 +33,10 @@ var PUSHNOTIFICATIONS_TYPES = { 'TxProposalFinallyRejected': { filename: 'txp_finally_rejected', }, + 'TxConfirmation': { + filename: 'tx_confirmation', + notifyCreatorOnly: true, + }, }; function PushNotificationsService() {}; @@ -111,7 +115,7 @@ PushNotificationsService.prototype._sendPushNotifications = function(notificatio log.debug('Should send notification: ', should); if (!should) return cb(); - self._getRecipientsList(notification, function(err, recipientsList) { + self._getRecipientsList(notification, notifType, function(err, recipientsList) { if (err) return cb(err); async.waterfall([ @@ -188,7 +192,7 @@ PushNotificationsService.prototype._checkShouldSendNotif = function(notification }); }; -PushNotificationsService.prototype._getRecipientsList = function(notification, cb) { +PushNotificationsService.prototype._getRecipientsList = function(notification, notificationType, cb) { var self = this; self.storage.fetchWallet(notification.walletId, function(err, wallet) { @@ -216,16 +220,19 @@ PushNotificationsService.prototype._getRecipientsList = function(notification, c 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, + var recipientsList = _.compact(_.map(wallet.copayers, function(copayer) { + if ((copayer.id == notification.creatorId && notificationType.notifyCreatorOnly) || + (copayer.id != notification.creatorId && !notificationType.notifyCreatorOnly)) { + var p = recipientPreferences[copayer.id] || {}; + return { + copayerId: copayer.id, + language: p.language || self.defaultLanguage, + unit: p.unit || self.defaultUnit, + } } - }), { - copayerId: notification.creatorId - }); + })); + console.log('*** [pushnotificationsservice.js ln234] recipientsList:', recipientsList); // TODO + return cb(null, recipientsList); }); diff --git a/lib/templates/en/tx_confirmation.plain b/lib/templates/en/tx_confirmation.plain new file mode 100644 index 0000000..096437c --- /dev/null +++ b/lib/templates/en/tx_confirmation.plain @@ -0,0 +1,2 @@ +{{subjectPrefix}} Transaction confirmed +The transaction you were waiting for has been confirmed. diff --git a/test/integration/pushNotifications.js b/test/integration/pushNotifications.js index 364382d..3d9b9b8 100644 --- a/test/integration/pushNotifications.js +++ b/test/integration/pushNotifications.js @@ -158,6 +158,29 @@ describe('Push notifications', function() { }); }); }); + + it('should notify copayers when tx is confirmed if they are subscribed', function(done) { + server.createAddress({}, function(err, address) { + should.not.exist(err); + + server.txConfirmationSubscribe({ + txid: '123' + }, function(err) { + should.not.exist(err); + + // Simulate tx confirmation notification + server._notify('TxConfirmation', { + txid: '123', + }, function(err) { + setTimeout(function() { + var calls = requestStub.getCalls(); + calls.length.should.equal(1); + done(); + }, 100); + }); + }); + }); + }); }); describe('Shared wallet', function() {