Browse Source

clean code

activeAddress
Gabriel Bazán 9 years ago
parent
commit
a2f00de699
  1. 12
      config.js
  2. 89
      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;

89
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,29 +85,24 @@ 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), {
id: notification.creatorId
});
async.waterfall([ async.waterfall([
function(next) { function(next) {
self._readAndApplyTemplates(notification, notifType, resultedRecipientsList, next); self._readAndApplyTemplates(notification, notifType, recipientsList, next);
}, },
function(contents, next) { function(contents, next) {
async.map(resultedRecipientsList, function(recipient, next) { async.map(recipientsList, function(recipient, next) {
var opts = {}; var opts = {};
var content = contents[recipient.language]; var content = contents[recipient.language];
opts.users = [notification.walletId + '$' + recipient.id]; opts.users = [notification.walletId + '$' + recipient.copayerId];
opts.android = { opts.android = {
"data": { "data": {
"title": content.plain.subject, "title": content.plain.subject,
@ -127,11 +119,7 @@ PushNotificationsService.prototype._sendPushNotifications = function(notificatio
function(optsList, next) { function(optsList, next) {
async.each(optsList, async.each(optsList,
function(opts, next) { function(opts, next) {
self._makeRequest(opts, function(err, response) { self._makeRequest(opts, next());
if (err) log.error(err);
log.debug('Post status : ', response);
next();
})
}, },
function(err) { function(err) {
log.error(err); log.error(err);
@ -146,21 +134,23 @@ PushNotificationsService.prototype._sendPushNotifications = function(notificatio
return cb(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, []);
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) { 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;
} }
@ -171,35 +161,42 @@ PushNotificationsService.prototype._getRecipientsList = function(walletId, cb) {
}; };
})); }));
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) { 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