clean code
This commit is contained in:
parent
aacf21778d
commit
a2f00de699
12
config.js
12
config.js
|
@ -45,6 +45,17 @@ var config = {
|
|||
url: 'https://test-insight.bitpay.com:443',
|
||||
},
|
||||
},
|
||||
pushNotificationsOpts: {
|
||||
templatePath: './lib/templates',
|
||||
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.143:8000/send',
|
||||
},
|
||||
// To use email notifications uncomment this:
|
||||
// emailOpts: {
|
||||
// host: 'localhost',
|
||||
|
@ -60,5 +71,6 @@ var config = {
|
|||
// testnet: 'https://test-insight.bitpay.com/tx/{{txid}}',
|
||||
// },
|
||||
//},
|
||||
|
||||
};
|
||||
module.exports = config;
|
||||
|
|
|
@ -13,8 +13,6 @@ var Model = require('./model');
|
|||
var log = require('npmlog');
|
||||
log.debug = log.verbose;
|
||||
|
||||
var self = this;
|
||||
|
||||
var PUSHNOTIFICATIONS_TYPES = {
|
||||
'NewCopayer': {
|
||||
filename: 'new_copayer',
|
||||
|
@ -36,12 +34,10 @@ var PUSHNOTIFICATIONS_TYPES = {
|
|||
}
|
||||
};
|
||||
|
||||
var url = 'http://192.168.1.143:8000/send';
|
||||
|
||||
function PushNotificationsService() {};
|
||||
|
||||
PushNotificationsService.prototype.start = function(opts, cb) {
|
||||
|
||||
var self = this;
|
||||
opts = opts || {};
|
||||
|
||||
function _readDirectories(basePath, cb) {
|
||||
|
@ -57,11 +53,12 @@ PushNotificationsService.prototype.start = function(opts, cb) {
|
|||
});
|
||||
};
|
||||
|
||||
self.templatePath = path.normalize(((__dirname + '/templates')) + '/');
|
||||
self.defaultLanguage = 'en';
|
||||
self.defaultUnit = 'btc';
|
||||
self.subjectPrefix = '';
|
||||
self.publicTxUrlTemplate = {};
|
||||
self.templatePath = opts.pushNotificationsOpts.templatePath || templatePathpath.normalize(((__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) {
|
||||
|
@ -88,118 +85,118 @@ PushNotificationsService.prototype.start = function(opts, cb) {
|
|||
};
|
||||
|
||||
PushNotificationsService.prototype._sendPushNotifications = function(notification, cb) {
|
||||
var self = this;
|
||||
cb = cb || function() {};
|
||||
|
||||
var notifType = PUSHNOTIFICATIONS_TYPES[notification.type];
|
||||
if (!notifType) return cb();
|
||||
|
||||
self._getRecipientsList(notification.walletId, function(err, recipientsList) {
|
||||
if (err) log.error(err);
|
||||
self._getRecipientsList(notification, function(err, recipientsList) {
|
||||
if (err) return cb(err);
|
||||
|
||||
self.storage.fetchWallet(notification.walletId, function(err, wallet) {
|
||||
if (err) log.error(err);
|
||||
|
||||
var resultedRecipientsList = _.reject(self._getJoinedRecipientsList(wallet, recipientsList), {
|
||||
id: notification.creatorId
|
||||
});
|
||||
async.waterfall([
|
||||
function(next) {
|
||||
self._readAndApplyTemplates(notification, notifType, resultedRecipientsList, next);
|
||||
},
|
||||
function(contents, next) {
|
||||
async.map(resultedRecipientsList, function(recipient, next) {
|
||||
var opts = {};
|
||||
var content = contents[recipient.language];
|
||||
opts.users = [notification.walletId + '$' + recipient.id];
|
||||
opts.android = {
|
||||
"data": {
|
||||
"title": content.plain.subject,
|
||||
"message": content.plain.body
|
||||
}
|
||||
};
|
||||
opts.ios = {
|
||||
"alert": content.plain.body,
|
||||
"sound": ""
|
||||
};
|
||||
return next(err, opts);
|
||||
}, next);
|
||||
},
|
||||
function(optsList, next) {
|
||||
async.each(optsList,
|
||||
function(opts, next) {
|
||||
self._makeRequest(opts, function(err, response) {
|
||||
if (err) log.error(err);
|
||||
log.debug('Post status : ', response);
|
||||
next();
|
||||
})
|
||||
},
|
||||
function(err) {
|
||||
log.error(err);
|
||||
return cb(err);
|
||||
async.waterfall([
|
||||
function(next) {
|
||||
self._readAndApplyTemplates(notification, notifType, recipientsList, next);
|
||||
},
|
||||
function(contents, next) {
|
||||
async.map(recipientsList, function(recipient, next) {
|
||||
var opts = {};
|
||||
var content = contents[recipient.language];
|
||||
opts.users = [notification.walletId + '$' + recipient.copayerId];
|
||||
opts.android = {
|
||||
"data": {
|
||||
"title": content.plain.subject,
|
||||
"message": content.plain.body
|
||||
}
|
||||
);
|
||||
},
|
||||
], function(err) {
|
||||
if (err) {
|
||||
log.error('An error ocurred generating notification', err);
|
||||
}
|
||||
return cb(err);
|
||||
});
|
||||
};
|
||||
opts.ios = {
|
||||
"alert": content.plain.body,
|
||||
"sound": ""
|
||||
};
|
||||
return next(err, opts);
|
||||
}, next);
|
||||
},
|
||||
function(optsList, next) {
|
||||
async.each(optsList,
|
||||
function(opts, next) {
|
||||
self._makeRequest(opts, next());
|
||||
},
|
||||
function(err) {
|
||||
log.error(err);
|
||||
return cb(err);
|
||||
}
|
||||
);
|
||||
},
|
||||
], function(err) {
|
||||
if (err) {
|
||||
log.error('An error ocurred generating notification', err);
|
||||
}
|
||||
return cb(err);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
PushNotificationsService.prototype._getRecipientsList = function(walletId, cb) {
|
||||
PushNotificationsService.prototype._getRecipientsList = function(notification, cb) {
|
||||
var self = this;
|
||||
|
||||
self.storage.fetchPreferences(walletId, null, function(err, preferences) {
|
||||
self.storage.fetchWallet(notification.walletId, function(err, wallet) {
|
||||
if (err) return cb(err);
|
||||
if (_.isEmpty(preferences)) return cb(null, []);
|
||||
|
||||
var recipients = _.compact(_.map(preferences, function(p) {
|
||||
self.storage.fetchPreferences(notification.walletId, null, function(err, preferences) {
|
||||
if (err) log.error(err);
|
||||
if (_.isEmpty(preferences)) preferences = [];
|
||||
|
||||
if (!_.contains(self.availableLanguages, p.language)) {
|
||||
if (p.language) {
|
||||
log.warn('Language for notifications "' + p.language + '" not available.');
|
||||
var recipients = _.compact(_.map(preferences, function(p) {
|
||||
|
||||
if (!_.contains(self.availableLanguages, p.language)) {
|
||||
if (p.language)
|
||||
log.warn('Language for notifications "' + p.language + '" not available.');
|
||||
p.language = self.defaultLanguage;
|
||||
}
|
||||
p.language = self.defaultLanguage;
|
||||
}
|
||||
|
||||
return {
|
||||
id: p.copayerId,
|
||||
language: p.language,
|
||||
unit: p.unit || self.defaultUnit,
|
||||
};
|
||||
}));
|
||||
return {
|
||||
id: p.copayerId,
|
||||
language: p.language,
|
||||
unit: p.unit || self.defaultUnit,
|
||||
};
|
||||
}));
|
||||
|
||||
return cb(null, recipients);
|
||||
var recipientsList = _.reject(self._join(wallet.copayers, recipients), {
|
||||
id: notification.creatorId
|
||||
});
|
||||
|
||||
return cb(null, recipientsList);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
PushNotificationsService.prototype._getJoinedRecipientsList = function(wallet, recipientsList) {
|
||||
var _recipientsList = _.compact(_.map(wallet.copayers, function(c) {
|
||||
PushNotificationsService.prototype._join = function(copayers, recipients) {
|
||||
var self = this;
|
||||
var recipientsList = _.compact(_.map(copayers, function(c) {
|
||||
|
||||
var structure = {};
|
||||
|
||||
_.forEach(recipientsList, function(r) {
|
||||
_.forEach(recipients, function(r) {
|
||||
if (r.id == c.id) {
|
||||
structure.id = r.id;
|
||||
structure.copayerId = r.id;
|
||||
structure.language = r.language;
|
||||
structure.unit = r.unit || self.defaultUnit;
|
||||
}
|
||||
});
|
||||
|
||||
if (_.isEmpty(structure)) {
|
||||
structure.id = c.id;
|
||||
structure.copayerId = c.id;
|
||||
structure.language = self.defaultLanguage;
|
||||
structure.unit = self.defaultUnit;
|
||||
}
|
||||
|
||||
return structure;
|
||||
}));
|
||||
return _recipientsList;
|
||||
return recipientsList;
|
||||
};
|
||||
|
||||
PushNotificationsService.prototype._readAndApplyTemplates = function(notification, notifType, recipientsList, cb) {
|
||||
var self = this;
|
||||
|
||||
async.map(recipientsList, function(recipient, next) {
|
||||
async.waterfall([
|
||||
|
@ -232,6 +229,7 @@ PushNotificationsService.prototype._readAndApplyTemplates = function(notificatio
|
|||
};
|
||||
|
||||
PushNotificationsService.prototype._getDataForTemplate = function(notification, recipient, cb) {
|
||||
var self = this;
|
||||
var UNIT_LABELS = {
|
||||
btc: 'BTC',
|
||||
bit: 'bits'
|
||||
|
@ -306,6 +304,7 @@ PushNotificationsService.prototype._applyTemplate = function(template, data, cb)
|
|||
};
|
||||
|
||||
PushNotificationsService.prototype._loadTemplate = function(notifType, recipient, extension, cb) {
|
||||
var self = this;
|
||||
|
||||
self._readTemplateFile(recipient.language, notifType.filename + extension, function(err, template) {
|
||||
if (err) return cb(err);
|
||||
|
@ -314,6 +313,7 @@ PushNotificationsService.prototype._loadTemplate = function(notifType, recipient
|
|||
};
|
||||
|
||||
PushNotificationsService.prototype._readTemplateFile = function(language, filename, cb) {
|
||||
var self = this;
|
||||
|
||||
var fullFilename = path.join(self.templatePath, language, filename);
|
||||
fs.readFile(fullFilename, 'utf8', function(err, template) {
|
||||
|
@ -335,15 +335,18 @@ PushNotificationsService.prototype._compileTemplate = function(template, extensi
|
|||
};
|
||||
};
|
||||
|
||||
PushNotificationService.prototype._makeRequest = function(opts, cb) {
|
||||
PushNotificationsService.prototype._makeRequest = function(opts, cb) {
|
||||
var self = this;
|
||||
|
||||
request({
|
||||
url: url,
|
||||
url: self.pushServerUrl,
|
||||
method: 'POST',
|
||||
json: true,
|
||||
body: opts
|
||||
}, function(error, response) {
|
||||
if (error) return cb(error);
|
||||
return cb(null, response);
|
||||
if (error) log.error(error);
|
||||
log.debug('Post status : ', response);
|
||||
return;
|
||||
});
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue