Browse Source

notifications for android - without include creator

activeAddress
Gabriel Bazán 9 years ago
parent
commit
2d923e34f5
  1. 77
      lib/pushnotificationsservice.js

77
lib/pushnotificationsservice.js

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

Loading…
Cancel
Save