|
|
@ -3,7 +3,7 @@ |
|
|
|
var _ = require('lodash'); |
|
|
|
var async = require('async'); |
|
|
|
var Mustache = require('mustache'); |
|
|
|
var request = require('request'); |
|
|
|
var defaultRequest = require('request'); |
|
|
|
var MessageBroker = require('./messagebroker'); |
|
|
|
var Storage = require('./storage'); |
|
|
|
var fs = require('fs'); |
|
|
@ -39,6 +39,7 @@ function PushNotificationsService() {}; |
|
|
|
PushNotificationsService.prototype.start = function(opts, cb) { |
|
|
|
var self = this; |
|
|
|
opts = opts || {}; |
|
|
|
self.request = opts.request || defaultRequest; |
|
|
|
|
|
|
|
function _readDirectories(basePath, cb) { |
|
|
|
fs.readdir(basePath, function(err, files) { |
|
|
@ -53,14 +54,14 @@ PushNotificationsService.prototype.start = function(opts, cb) { |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
self.templatePath = opts.pushNotificationsOpts.templatePath || templatePathpath.normalize(((__dirname + '/templates')) + '/'); |
|
|
|
self.templatePath = path.normalize((opts.pushNotificationsOpts.templatePath || (__dirname + '/templates')) + '/'); |
|
|
|
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([ |
|
|
|
|
|
|
|
function(done) { |
|
|
|
_readDirectories(self.templatePath, function(err, res) { |
|
|
|
self.availableLanguages = res; |
|
|
@ -68,8 +69,13 @@ PushNotificationsService.prototype.start = function(opts, cb) { |
|
|
|
}); |
|
|
|
}, |
|
|
|
function(done) { |
|
|
|
if (opts.storage) { |
|
|
|
self.storage = opts.storage; |
|
|
|
done(); |
|
|
|
} else { |
|
|
|
self.storage = new Storage(); |
|
|
|
self.storage.connect(opts.storageOpts, done); |
|
|
|
} |
|
|
|
}, |
|
|
|
function(done) { |
|
|
|
self.messageBroker = opts.messageBroker || new MessageBroker(opts.messageBrokerOpts); |
|
|
@ -82,6 +88,7 @@ PushNotificationsService.prototype.start = function(opts, cb) { |
|
|
|
} |
|
|
|
return cb(err); |
|
|
|
}); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
PushNotificationsService.prototype._sendPushNotifications = function(notification, cb) { |
|
|
@ -91,12 +98,13 @@ PushNotificationsService.prototype._sendPushNotifications = function(notificatio |
|
|
|
var notifType = PUSHNOTIFICATIONS_TYPES[notification.type]; |
|
|
|
if (!notifType) return cb(); |
|
|
|
|
|
|
|
console.log(notification); |
|
|
|
// console.log(notification);
|
|
|
|
|
|
|
|
self._getRecipientsList(notification, function(err, recipientsList) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
async.waterfall([ |
|
|
|
|
|
|
|
function(next) { |
|
|
|
self._readAndApplyTemplates(notification, notifType, recipientsList, next); |
|
|
|
}, |
|
|
@ -124,6 +132,7 @@ PushNotificationsService.prototype._sendPushNotifications = function(notificatio |
|
|
|
self._makeRequest(opts, next()); |
|
|
|
}, |
|
|
|
function(err) { |
|
|
|
if (err) |
|
|
|
log.error(err); |
|
|
|
return cb(err); |
|
|
|
} |
|
|
@ -145,6 +154,7 @@ PushNotificationsService.prototype._getRecipientsList = function(notification, c |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
self.storage.fetchPreferences(notification.walletId, null, function(err, preferences) { |
|
|
|
|
|
|
|
if (err) log.error(err); |
|
|
|
if (_.isEmpty(preferences)) preferences = []; |
|
|
|
|
|
|
@ -202,6 +212,7 @@ PushNotificationsService.prototype._readAndApplyTemplates = function(notificatio |
|
|
|
|
|
|
|
async.map(recipientsList, function(recipient, next) { |
|
|
|
async.waterfall([ |
|
|
|
|
|
|
|
function(next) { |
|
|
|
self._getDataForTemplate(notification, recipient, next); |
|
|
|
}, |
|
|
@ -340,7 +351,7 @@ PushNotificationsService.prototype._compileTemplate = function(template, extensi |
|
|
|
PushNotificationsService.prototype._makeRequest = function(opts, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
request({ |
|
|
|
self.request({ |
|
|
|
url: self.pushServerUrl, |
|
|
|
method: 'POST', |
|
|
|
json: true, |
|
|
|