send push notification

This commit is contained in:
Ivan Socolsky 2017-05-17 16:48:09 -03:00
parent c87350d7ef
commit c621d1c573
No known key found for this signature in database
GPG Key ID: FAECE6A05FAA4F56
3 changed files with 43 additions and 11 deletions

View File

@ -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);
});

View File

@ -0,0 +1,2 @@
{{subjectPrefix}} Transaction confirmed
The transaction you were waiting for has been confirmed.

View File

@ -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() {