refactor
This commit is contained in:
parent
ba1c603977
commit
5c1a95d5d2
|
@ -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: {
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
};
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue