bitcore-wallet-service/lib/pushnotificationsservice.js

164 lines
4.2 KiB
JavaScript
Raw Normal View History

2016-01-04 09:43:21 -08:00
'use strict';
var _ = require('lodash');
var $ = require('preconditions').singleton();
var async = require('async');
var Mustache = require('mustache');
var log = require('npmlog');
log.debug = log.verbose;
var fs = require('fs');
var path = require('path');
var nodemailer = require('nodemailer');
var request = require('request');
var Utils = require('./common/utils');
var Storage = require('./storage');
var MessageBroker = require('./messagebroker');
var Lock = require('./lock');
var BlockchainExplorer = require('./blockchainexplorer');
var Model = require('./model');
2016-01-04 09:47:57 -08:00
var PUSHNOTIFICATIONS_TYPES = {
2016-01-04 09:43:21 -08:00
'NewCopayer': {
filename: 'new_copayer',
notifyDoer: false,
},
'WalletComplete': {
filename: 'wallet_complete',
notifyDoer: true,
},
'NewTxProposal': {
filename: 'new_tx_proposal',
notifyDoer: false,
},
'NewOutgoingTx': {
filename: 'new_outgoing_tx',
notifyDoer: true,
},
'NewIncomingTx': {
filename: 'new_incoming_tx',
notifyDoer: true,
},
'TxProposalFinallyRejected': {
filename: 'txp_finally_rejected',
notifyDoer: false,
},
};
2016-01-04 09:47:57 -08:00
function PushNotificationsService() {};
2016-01-04 09:43:21 -08:00
2016-01-04 09:47:57 -08:00
PushNotificationsService.prototype.start = function(opts, cb) {
2016-01-04 10:56:14 -08:00
opts = opts || {};
2016-01-04 09:43:21 -08:00
var self = this;
2016-01-04 10:56:14 -08:00
2016-01-04 09:43:21 -08:00
async.parallel([
function(done) {
self.messageBroker = opts.messageBroker || new MessageBroker(opts.messageBrokerOpts);
2016-01-04 10:56:14 -08:00
self.messageBroker.onMessage(_.bind(self.sendPushNotifications, self));
2016-01-04 09:43:21 -08:00
done();
},
], function(err) {
if (err) {
log.error(err);
}
return cb(err);
});
};
2016-01-04 09:47:57 -08:00
PushNotificationsService.prototype.sendPushNotifications = function(notification, cb) {
2016-01-04 09:43:21 -08:00
console.log(notification);
2016-01-04 10:56:14 -08:00
if (PUSHNOTIFICATIONS_TYPES[notification.type]) {
2016-01-04 09:43:21 -08:00
if (notification.type == 'NewIncomingTx') {
var opts = {};
opts.users = [notification.walletId];
opts.android = {
"collapseKey": "optional",
"data": {
"title": "New incoming transaction",
2016-01-04 11:38:47 -08:00
"message": notification.data.amount / 100 + " bits"
2016-01-04 09:43:21 -08:00
}
};
}
if (notification.type == 'NewOutgoingTx') {
var opts = {};
opts.users = [notification.walletId];
opts.android = {
"collapseKey": "optional",
"data": {
"title": "New outgoing transaction",
2016-01-04 11:38:47 -08:00
"message": notification.data.amount / 100 + " bits"
2016-01-04 09:43:21 -08:00
}
};
}
if (notification.type == 'NewCopayer') {
var opts = {};
opts.users = [notification.walletId];
opts.android = {
"collapseKey": "optional",
"data": {
"title": "New copayer",
"message": "Copayer: " + notification.data.copayerName + " has joined"
}
};
}
// if (notification.type == 'WalletComplete') {
// var opts = {};
// opts.users = [notification.walletId];
// opts.android = {
// "collapseKey": "optional",
// "data": {
// "title": "Wallet complete",
// "message": "Wallet complete"
// }
// };
// }
2016-01-04 10:56:14 -08:00
if (notification.type == 'NewTxProposal') {
2016-01-04 09:43:21 -08:00
var opts = {};
opts.users = [notification.walletId];
opts.android = {
"collapseKey": "optional",
"data": {
2016-01-04 10:56:14 -08:00
"title": "New proposal",
2016-01-04 09:43:21 -08:00
"message": "New transaction proposal created"
}
};
}
if (notification.type == 'TxProposalFinallyRejected') {
var opts = {};
opts.users = [notification.walletId];
opts.android = {
"collapseKey": "optional",
"data": {
2016-01-04 10:56:14 -08:00
"title": "Rejected",
2016-01-04 09:43:21 -08:00
"message": "Transaction proposal finally rejected"
}
};
}
2016-01-04 10:56:14 -08:00
if (notification.type == 'TxProposalAcceptedBy') {
2016-01-04 09:43:21 -08:00
var opts = {};
opts.users = [notification.walletId];
opts.android = {
"collapseKey": "optional",
"data": {
2016-01-04 10:56:14 -08:00
"title": "Accepted",
2016-01-04 09:43:21 -08:00
"message": "Transaction proposal accepted"
}
};
}
2016-01-04 10:56:14 -08:00
var url = 'http://192.168.1.121:8000/send';
2016-01-04 09:43:21 -08:00
request({
url: url,
method: 'POST',
json: true,
body: opts
}, function(error, response, body) {
console.log(response.statusCode);
});
}
};
2016-01-04 09:47:57 -08:00
module.exports = PushNotificationsService;