diff --git a/lib/pushnotificationsservice.js b/lib/pushnotificationsservice.js index d680612..7b4ff48 100644 --- a/lib/pushnotificationsservice.js +++ b/lib/pushnotificationsservice.js @@ -31,7 +31,7 @@ var PUSHNOTIFICATIONS_TYPES = { }, 'TxProposalFinallyRejected': { filename: 'txp_finally_rejected', - } + }, }; function PushNotificationsService() {}; @@ -100,53 +100,67 @@ PushNotificationsService.prototype._sendPushNotifications = function(notificatio // console.log(notification); - self._getRecipientsList(notification, function(err, recipientsList) { + self._checkShouldSendNotif(notification, function(err, should) { if (err) return cb(err); + if (!should) return cb(); - async.waterfall([ + self._getRecipientsList(notification, function(err, recipientsList) { + if (err) return cb(err); - function(next) { - self._readAndApplyTemplates(notification, notifType, recipientsList, next); - }, - function(contents, next) { - async.map(recipientsList, function(recipient, next) { - var opts = {}; - var content = contents[recipient.language]; - opts.users = [notification.walletId + '$' + recipient.copayerId]; - opts.android = { - "data": { - "title": content.plain.subject, - "message": content.plain.body + async.waterfall([ + + function(next) { + self._readAndApplyTemplates(notification, notifType, recipientsList, next); + }, + function(contents, next) { + async.map(recipientsList, function(recipient, next) { + var opts = {}; + var content = contents[recipient.language]; + opts.users = [notification.walletId + '$' + recipient.copayerId]; + opts.android = { + "data": { + "title": content.plain.subject, + "message": content.plain.body + } + }; + opts.ios = { + "alert": content.plain.body, + "sound": "" + }; + return next(err, opts); + }, next); + }, + function(optsList, next) { + async.each(optsList, + function(opts, next) { + self._makeRequest(opts, next()); + }, + function(err) { + if (err) + log.error(err); + return cb(err); } - }; - opts.ios = { - "alert": content.plain.body, - "sound": "" - }; - return next(err, opts); - }, next); - }, - function(optsList, next) { - async.each(optsList, - function(opts, next) { - self._makeRequest(opts, next()); - }, - function(err) { - if (err) - log.error(err); - return cb(err); - } - ); - }, - ], function(err) { - if (err) { - log.error('An error ocurred generating notification', err); - } - return cb(err); + ); + }, + ], function(err) { + if (err) { + log.error('An error ocurred generating notification', err); + } + return cb(err); + }); }); }); }; +PushNotificationsService.prototype._checkShouldSendNotif = function(notification, cb) { + var self = this; + + if (notification.type != 'NewTxProposal') return cb(null, true); + self.storage.fetchWallet(notification.walletId, function(err, wallet) { + return cb(err, wallet.m > 1); + }); +}; + PushNotificationsService.prototype._getRecipientsList = function(notification, cb) { var self = this; diff --git a/test/integration/pushNotifications.js b/test/integration/pushNotifications.js index 4b8701d..55249b1 100644 --- a/test/integration/pushNotifications.js +++ b/test/integration/pushNotifications.js @@ -38,6 +38,7 @@ describe('Push notifications', function() { helpers.getAuthServer(copayer.id, function(server) { server.savePreferences({ email: 'copayer' + (++i) + '@domain.com', + language: 'en', unit: 'bit', }, next); }); @@ -74,33 +75,37 @@ describe('Push notifications', function() { }); it('should build each notifications using preferences of the copayers', function(done) { - server.createAddress({}, function(err, address) { - should.not.exist(err); - - // Simulate incoming tx notification - server._notify('NewIncomingTx', { - txid: '999', - address: address, - amount: 12300000, - }, { - isGlobal: true - }, function(err) { - setTimeout(function() { - var calls = requestStub.getCalls(); - var args = _.map(calls, function(c) { - return c.args[0]; - }); - - args[0].body.android.data.title.should.contain('New payment received'); - args[0].body.android.data.message.should.contain('123,000'); + server.savePreferences({ + language: 'en', + unit: 'bit', + }, function(err) { + server.createAddress({}, function(err, address) { + should.not.exist(err); - done(); - }, 100); + // Simulate incoming tx notification + server._notify('NewIncomingTx', { + txid: '999', + address: address, + amount: 12300000, + }, { + isGlobal: true + }, function(err) { + setTimeout(function() { + var calls = requestStub.getCalls(); + var args = _.map(calls, function(c) { + return c.args[0]; + }); + calls.length.should.equal(1); + args[0].body.android.data.title.should.contain('New payment received'); + args[0].body.android.data.message.should.contain('123,000'); + done(); + }, 100); + }); }); }); }); - it('number of calls should be 0', function(done) { + it('should not notify auto-payments to creator', function(done) { server.createAddress({}, function(err, address) { should.not.exist(err); @@ -115,14 +120,13 @@ describe('Push notifications', function() { setTimeout(function() { var calls = requestStub.getCalls(); calls.length.should.equal(0); - done(); }, 100); }); }); }); - it('number of calls should be 1', function(done) { + it('should notify copayers when payment is received', function(done) { server.createAddress({}, function(err, address) { should.not.exist(err); @@ -137,7 +141,6 @@ describe('Push notifications', function() { setTimeout(function() { var calls = requestStub.getCalls(); calls.length.should.equal(1); - done(); }, 100); }); @@ -193,33 +196,46 @@ describe('Push notifications', function() { }); it('should build each notifications using preferences of the copayers', function(done) { - server.createAddress({}, function(err, address) { - should.not.exist(err); + server.savePreferences({ + email: 'copayer1@domain.com', + language: 'es', + unit: 'btc', + }, function(err) { + server.createAddress({}, function(err, address) { + should.not.exist(err); - // Simulate incoming tx notification - server._notify('NewIncomingTx', { - txid: '999', - address: address, - amount: 12300000, - }, { - isGlobal: true - }, function(err) { - setTimeout(function() { - var calls = requestStub.getCalls(); - var args = _.map(calls, function(c) { - return c.args[0]; - }); + // Simulate incoming tx notification + server._notify('NewIncomingTx', { + txid: '999', + address: address, + amount: 12300000, + }, { + isGlobal: true + }, function(err) { + setTimeout(function() { + var calls = requestStub.getCalls(); + var args = _.map(calls, function(c) { + return c.args[0]; + }); - args[0].body.android.data.title.should.contain('New payment received'); - args[0].body.android.data.message.should.contain('123,000'); + calls.length.should.equal(3); - done(); - }, 100); + args[0].body.android.data.title.should.contain('Nuevo pago recibido'); + args[0].body.android.data.message.should.contain('0.123'); + + args[1].body.android.data.title.should.contain('New payment received'); + args[1].body.android.data.message.should.contain('123,000'); + + args[2].body.android.data.title.should.contain('New payment received'); + args[2].body.android.data.message.should.contain('123,000'); + done(); + }, 100); + }); }); }); }); - it('number of calls should be 3', function(done) { + it('should notify copayers when payment is received', function(done) { server.createAddress({}, function(err, address) { should.not.exist(err); @@ -241,7 +257,7 @@ describe('Push notifications', function() { }); }); - it('number of calls should be 2', function(done) { + it('should not notify auto-payments to creator', function(done) { server.createAddress({}, function(err, address) { should.not.exist(err); @@ -328,6 +344,63 @@ describe('Push notifications', function() { }); }); }); + + it('should notify copayers a new outgoing tx has been created', function(done) { + helpers.stubUtxos(server, wallet, 1, function() { + var txOpts = helpers.createSimpleProposalOpts('18PzpUFkFZE8zKWUPvfykkTxmB9oMR8qP7', 0.8, TestData.copayers[0].privKey_1H_0, { + message: 'some message' + }); + + var txp; + async.waterfall([ + + function(next) { + server.createTxLegacy(txOpts, next); + }, + function(t, next) { + txp = t; + async.eachSeries(_.range(1, 3), function(i, next) { + var copayer = TestData.copayers[i]; + helpers.getAuthServer(copayer.id44, function(s) { + server = s; + var signatures = helpers.clientSign(txp, copayer.xPrivKey_44H_0H_0H); + server.signTx({ + txProposalId: txp.id, + signatures: signatures, + }, function(err, t) { + txp = t; + next(); + }); + }); + }, next); + }, + function(next) { + helpers.stubBroadcast(); + server.broadcastTx({ + txProposalId: txp.id, + }, next); + }, + ], function(err) { + should.not.exist(err); + + setTimeout(function() { + var calls = requestStub.getCalls(); + var args = _.map(_.takeRight(calls, 2), function(c) { + return c.args[0]; + }); + + 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(); + }); + }, 100); + }); + }); + }); }); describe('joinWallet', function() { @@ -372,7 +445,7 @@ describe('Push notifications', function() { }); }); - it.only('should notify copayers when a new copayer just joined into your wallet except the one who joined', function(done) { + it('should notify copayers when a new copayer just joined into your wallet except the one who joined', function(done) { async.eachSeries(_.range(3), function(i, next) { var copayerOpts = helpers.getSignedCopayerOpts({ walletId: walletId,