This commit is contained in:
Gabriel Bazán 2016-01-13 13:14:42 -03:00
parent ba1c603977
commit 5c1a95d5d2
3 changed files with 30 additions and 73 deletions

View File

@ -50,11 +50,7 @@ var config = {
defaultLanguage: 'en', defaultLanguage: 'en',
defaultUnit: 'btc', defaultUnit: 'btc',
subjectPrefix: '', subjectPrefix: '',
publicTxUrlTemplate: { pushServerUrl: 'http://localhost:8000/send',
livenet: 'https://insight.bitpay.com/tx/{{txid}}',
testnet: 'https://test-insight.bitpay.com/tx/{{txid}}',
},
pushServerUrl: 'http://192.168.1.111:8000/send',
}, },
// To use email notifications uncomment this: // To use email notifications uncomment this:
// emailOpts: { // emailOpts: {

View File

@ -58,7 +58,6 @@ PushNotificationsService.prototype.start = function(opts, cb) {
self.defaultLanguage = opts.pushNotificationsOpts.defaultLanguage || 'en'; self.defaultLanguage = opts.pushNotificationsOpts.defaultLanguage || 'en';
self.defaultUnit = opts.pushNotificationsOpts.defaultUnit || 'btc'; self.defaultUnit = opts.pushNotificationsOpts.defaultUnit || 'btc';
self.subjectPrefix = opts.pushNotificationsOpts.subjectPrefix || ''; self.subjectPrefix = opts.pushNotificationsOpts.subjectPrefix || '';
self.publicTxUrlTemplate = opts.pushNotificationsOpts.publicTxUrlTemplate || {};
self.pushServerUrl = opts.pushNotificationsOpts.pushServerUrl; self.pushServerUrl = opts.pushNotificationsOpts.pushServerUrl;
async.parallel([ async.parallel([
@ -133,12 +132,14 @@ PushNotificationsService.prototype._sendPushNotifications = function(notificatio
function(optsList, next) { function(optsList, next) {
async.each(optsList, async.each(optsList,
function(opts, next) { 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) { function(err) {
if (err) return next(err);
log.error(err);
return cb(err);
} }
); );
}, },
@ -172,7 +173,7 @@ PushNotificationsService.prototype._getRecipientsList = function(notification, c
if (err) log.error(err); if (err) log.error(err);
if (_.isEmpty(preferences)) preferences = []; 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 (!_.contains(self.availableLanguages, p.language)) {
if (p.language) if (p.language)
@ -181,13 +182,22 @@ PushNotificationsService.prototype._getRecipientsList = function(notification, c
} }
return { return {
id: p.copayerId, copayerId: p.copayerId,
language: p.language, 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 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) { PushNotificationsService.prototype._readAndApplyTemplates = function(notification, notifType, recipientsList, cb) {
var self = this; var self = this;
@ -299,16 +284,6 @@ PushNotificationsService.prototype._getDataForTemplate = function(notification,
data.rejectorsNames = rejectors.join(', '); 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); return cb(null, data);
}); });
}; };
@ -370,10 +345,8 @@ PushNotificationsService.prototype._makeRequest = function(opts, cb) {
method: 'POST', method: 'POST',
json: true, json: true,
body: opts body: opts
}, function(error, response) { }, function(err, response) {
if (error) log.error(error); return cb(err, response);
log.debug('Post status : ', response);
return;
}); });
}; };

View File

@ -59,11 +59,8 @@ describe('Push notifications', function() {
defaultLanguage: 'en', defaultLanguage: 'en',
defaultUnit: 'btc', defaultUnit: 'btc',
subjectPrefix: '', subjectPrefix: '',
publicTxUrlTemplate: {
livenet: 'https://insight.bitpay.com/tx/{{txid}}', pushServerUrl: 'http://localhost:8000/send',
testnet: 'https://test-insight.bitpay.com/tx/{{txid}}',
},
pushServerUrl: 'http://192.168.1.111:8000/send',
}, },
}, function(err) { }, function(err) {
should.not.exist(err); should.not.exist(err);
@ -180,11 +177,8 @@ describe('Push notifications', function() {
defaultLanguage: 'en', defaultLanguage: 'en',
defaultUnit: 'btc', defaultUnit: 'btc',
subjectPrefix: '', subjectPrefix: '',
publicTxUrlTemplate: {
livenet: 'https://insight.bitpay.com/tx/{{txid}}', pushServerUrl: 'http://localhost:8000/send',
testnet: 'https://test-insight.bitpay.com/tx/{{txid}}',
},
pushServerUrl: 'http://192.168.1.111:8000/send',
}, },
}, function(err) { }, function(err) {
should.not.exist(err); should.not.exist(err);
@ -392,11 +386,9 @@ describe('Push notifications', function() {
args[0].body.android.data.title.should.contain('Payment sent'); args[0].body.android.data.title.should.contain('Payment sent');
args[1].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[0].body.users[0]).split('$')[1]); server.copayerId.should.not.equal((args[1].body.users[0]).split('$')[1]);
server.copayerId.should.not.equal((args[1].body.users[0]).split('$')[1]); done();
done();
});
}, 100); }, 100);
}); });
}); });
@ -431,11 +423,7 @@ describe('Push notifications', function() {
defaultLanguage: 'en', defaultLanguage: 'en',
defaultUnit: 'btc', defaultUnit: 'btc',
subjectPrefix: '', subjectPrefix: '',
publicTxUrlTemplate: { pushServerUrl: 'http://localhost:8000/send',
livenet: 'https://insight.bitpay.com/tx/{{txid}}',
testnet: 'https://test-insight.bitpay.com/tx/{{txid}}',
},
pushServerUrl: 'http://192.168.1.111:8000/send',
}, },
}, function(err) { }, function(err) {
should.not.exist(err); should.not.exist(err);