Browse Source

clean code

activeAddress
Gabriel Bazán 9 years ago
parent
commit
a2f00de699
  1. 12
      config.js
  2. 175
      lib/pushnotificationsservice.js

12
config.js

@ -45,6 +45,17 @@ var config = {
url: 'https://test-insight.bitpay.com:443', 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: // To use email notifications uncomment this:
// emailOpts: { // emailOpts: {
// host: 'localhost', // host: 'localhost',
@ -60,5 +71,6 @@ var config = {
// testnet: 'https://test-insight.bitpay.com/tx/{{txid}}', // testnet: 'https://test-insight.bitpay.com/tx/{{txid}}',
// }, // },
//}, //},
}; };
module.exports = config; module.exports = config;

175
lib/pushnotificationsservice.js

@ -13,8 +13,6 @@ var Model = require('./model');
var log = require('npmlog'); var log = require('npmlog');
log.debug = log.verbose; log.debug = log.verbose;
var self = this;
var PUSHNOTIFICATIONS_TYPES = { var PUSHNOTIFICATIONS_TYPES = {
'NewCopayer': { 'NewCopayer': {
filename: 'new_copayer', filename: 'new_copayer',
@ -36,12 +34,10 @@ var PUSHNOTIFICATIONS_TYPES = {
} }
}; };
var url = 'http://192.168.1.143:8000/send';
function PushNotificationsService() {}; function PushNotificationsService() {};
PushNotificationsService.prototype.start = function(opts, cb) { PushNotificationsService.prototype.start = function(opts, cb) {
var self = this;
opts = opts || {}; opts = opts || {};
function _readDirectories(basePath, cb) { function _readDirectories(basePath, cb) {
@ -57,11 +53,12 @@ PushNotificationsService.prototype.start = function(opts, cb) {
}); });
}; };
self.templatePath = path.normalize(((__dirname + '/templates')) + '/'); self.templatePath = opts.pushNotificationsOpts.templatePath || templatePathpath.normalize(((__dirname + '/templates')) + '/');
self.defaultLanguage = 'en'; self.defaultLanguage = opts.pushNotificationsOpts.defaultLanguage || 'en';
self.defaultUnit = 'btc'; self.defaultUnit = opts.pushNotificationsOpts.defaultUnit || 'btc';
self.subjectPrefix = ''; self.subjectPrefix = opts.pushNotificationsOpts.subjectPrefix || '';
self.publicTxUrlTemplate = {}; self.publicTxUrlTemplate = opts.pushNotificationsOpts.publicTxUrlTemplate || {};
self.pushServerUrl = opts.pushNotificationsOpts.pushServerUrl;
async.parallel([ async.parallel([
function(done) { function(done) {
@ -88,118 +85,118 @@ PushNotificationsService.prototype.start = function(opts, cb) {
}; };
PushNotificationsService.prototype._sendPushNotifications = function(notification, cb) { PushNotificationsService.prototype._sendPushNotifications = function(notification, cb) {
var self = this;
cb = cb || function() {}; cb = cb || function() {};
var notifType = PUSHNOTIFICATIONS_TYPES[notification.type]; var notifType = PUSHNOTIFICATIONS_TYPES[notification.type];
if (!notifType) return cb(); if (!notifType) return cb();
self._getRecipientsList(notification.walletId, function(err, recipientsList) { self._getRecipientsList(notification, function(err, recipientsList) {
if (err) log.error(err); 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), { async.waterfall([
id: notification.creatorId function(next) {
}); self._readAndApplyTemplates(notification, notifType, recipientsList, next);
async.waterfall([ },
function(next) { function(contents, next) {
self._readAndApplyTemplates(notification, notifType, resultedRecipientsList, next); async.map(recipientsList, function(recipient, next) {
}, var opts = {};
function(contents, next) { var content = contents[recipient.language];
async.map(resultedRecipientsList, function(recipient, next) { opts.users = [notification.walletId + '$' + recipient.copayerId];
var opts = {}; opts.android = {
var content = contents[recipient.language]; "data": {
opts.users = [notification.walletId + '$' + recipient.id]; "title": content.plain.subject,
opts.android = { "message": content.plain.body
"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);
} }
); };
}, opts.ios = {
], function(err) { "alert": content.plain.body,
if (err) { "sound": ""
log.error('An error ocurred generating notification', err); };
} return next(err, opts);
return cb(err); }, 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 (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 = [];
var recipients = _.compact(_.map(preferences, function(p) {
if (!_.contains(self.availableLanguages, p.language)) { if (!_.contains(self.availableLanguages, p.language)) {
if (p.language) { if (p.language)
log.warn('Language for notifications "' + p.language + '" not available.'); log.warn('Language for notifications "' + p.language + '" not available.');
p.language = self.defaultLanguage;
} }
p.language = self.defaultLanguage;
}
return { return {
id: p.copayerId, id: p.copayerId,
language: p.language, language: p.language,
unit: p.unit || self.defaultUnit, unit: p.unit || self.defaultUnit,
}; };
})); }));
var recipientsList = _.reject(self._join(wallet.copayers, recipients), {
id: notification.creatorId
});
return cb(null, recipients); return cb(null, recipientsList);
});
}); });
}; };
PushNotificationsService.prototype._getJoinedRecipientsList = function(wallet, recipientsList) { PushNotificationsService.prototype._join = function(copayers, recipients) {
var _recipientsList = _.compact(_.map(wallet.copayers, function(c) { var self = this;
var recipientsList = _.compact(_.map(copayers, function(c) {
var structure = {}; var structure = {};
_.forEach(recipientsList, function(r) { _.forEach(recipients, function(r) {
if (r.id == c.id) { if (r.id == c.id) {
structure.id = r.id; structure.copayerId = r.id;
structure.language = r.language; structure.language = r.language;
structure.unit = r.unit || self.defaultUnit; structure.unit = r.unit || self.defaultUnit;
} }
}); });
if (_.isEmpty(structure)) { if (_.isEmpty(structure)) {
structure.id = c.id; structure.copayerId = c.id;
structure.language = self.defaultLanguage; structure.language = self.defaultLanguage;
structure.unit = self.defaultUnit; structure.unit = self.defaultUnit;
} }
return structure; return structure;
})); }));
return _recipientsList; return recipientsList;
}; };
PushNotificationsService.prototype._readAndApplyTemplates = function(notification, notifType, recipientsList, cb) { PushNotificationsService.prototype._readAndApplyTemplates = function(notification, notifType, recipientsList, cb) {
var self = this;
async.map(recipientsList, function(recipient, next) { async.map(recipientsList, function(recipient, next) {
async.waterfall([ async.waterfall([
@ -232,6 +229,7 @@ PushNotificationsService.prototype._readAndApplyTemplates = function(notificatio
}; };
PushNotificationsService.prototype._getDataForTemplate = function(notification, recipient, cb) { PushNotificationsService.prototype._getDataForTemplate = function(notification, recipient, cb) {
var self = this;
var UNIT_LABELS = { var UNIT_LABELS = {
btc: 'BTC', btc: 'BTC',
bit: 'bits' bit: 'bits'
@ -306,6 +304,7 @@ PushNotificationsService.prototype._applyTemplate = function(template, data, cb)
}; };
PushNotificationsService.prototype._loadTemplate = function(notifType, recipient, extension, cb) { PushNotificationsService.prototype._loadTemplate = function(notifType, recipient, extension, cb) {
var self = this;
self._readTemplateFile(recipient.language, notifType.filename + extension, function(err, template) { self._readTemplateFile(recipient.language, notifType.filename + extension, function(err, template) {
if (err) return cb(err); if (err) return cb(err);
@ -314,6 +313,7 @@ PushNotificationsService.prototype._loadTemplate = function(notifType, recipient
}; };
PushNotificationsService.prototype._readTemplateFile = function(language, filename, cb) { PushNotificationsService.prototype._readTemplateFile = function(language, filename, cb) {
var self = this;
var fullFilename = path.join(self.templatePath, language, filename); var fullFilename = path.join(self.templatePath, language, filename);
fs.readFile(fullFilename, 'utf8', function(err, template) { 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({ request({
url: url, url: self.pushServerUrl,
method: 'POST', method: 'POST',
json: true, json: true,
body: opts body: opts
}, function(error, response) { }, function(error, response) {
if (error) return cb(error); if (error) log.error(error);
return cb(null, response); log.debug('Post status : ', response);
return;
}); });
}; };

Loading…
Cancel
Save