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