notifications for android - without include creator

This commit is contained in:
Gabriel Bazán 2016-01-05 18:26:51 -03:00
parent bd109a37d5
commit 2d923e34f5
1 changed files with 51 additions and 26 deletions

View File

@ -1,48 +1,50 @@
'use strict';
var _ = require('lodash');
var async = require('async');
var log = require('npmlog');
log.debug = log.verbose;
var request = require('request');
var MessageBroker = require('./messagebroker');
var Storage = require('./storage');
var PUSHNOTIFICATIONS_TYPES = {
'NewCopayer': {
title: "New copayer",
message: function(notification) {
return ("Copayer: " + notification.data.copayerName + " has joined!");
};
}
},
'WalletComplete': {
title: "Wallet complete",
message: function(notification) {
return ("All copayers has joined!");
};
}
},
'NewTxProposal': {
title: "New proposal",
message: function(notification) {
return ("New transaction proposal created");
};
}
},
'NewOutgoingTx': {
title: "New outgoing transaction",
message: function(notification) {
return ((notification.data.amount / 100) + " bits");
};
}
},
'NewIncomingTx': {
title: "New incoming transaction",
message: function(notification) {
return ((notification.data.amount / 100) + " bits");
};
}
},
'TxProposalFinallyRejected': {
title: "Rejected",
message: function(notification) {
return ("Transaction proposal finally rejected");
};
},
}
}
};
function PushNotificationsService() {};
@ -58,6 +60,10 @@ PushNotificationsService.prototype.start = function(opts, cb) {
self.messageBroker.onMessage(_.bind(self.sendPushNotifications, self));
done();
},
function(done) {
self.storage = new Storage();
self.storage.connect(opts.storageOpts, done);
},
], function(err) {
if (err) {
log.error(err);
@ -67,12 +73,25 @@ PushNotificationsService.prototype.start = function(opts, cb) {
};
PushNotificationsService.prototype.sendPushNotifications = function(notification, cb) {
log.debug(notification);
var self = this;
cb = cb || function() {};
if (!PUSHNOTIFICATIONS_TYPES[notification.type]) return cb();
if (!PUSHNOTIFICATIONS_TYPES[notification.type]) return;
console.log(notification);
self.storage.fetchWallet(notification.walletId, function(err, wallet) {
if (err) return cb(err);
var copayers = _.reject(wallet.copayers, {
id: notification.creatorId
});
var url = 'http://192.168.1.121:8000/send';
var opts = {};
opts.users = [notification.walletId];
async.each(copayers,
function(c, next) {
opts.users = [notification.walletId + '$' + c.id];
opts.android = {
"data": {
"title": PUSHNOTIFICATIONS_TYPES[notification.type].title,
@ -80,16 +99,22 @@ PushNotificationsService.prototype.sendPushNotifications = function(notification
}
};
var url = 'http://192.168.1.121:8000/send';
request({
url: url,
method: 'POST',
json: true,
body: opts
}, function(error, response, body) {
log.debug(response.statusCode);
console.log(response.statusCode);
next();
});
},
function(err) {
log.error(err);
return cb(err);
}
);
});
}
};
module.exports = PushNotificationsService;