2015-03-06 07:00:10 -08:00
|
|
|
'use strict';
|
|
|
|
angular.module('copayApp.services')
|
2015-04-30 09:03:30 -07:00
|
|
|
.factory('notificationService', function profileServiceFactory($filter, notification, lodash, configService, gettext) {
|
2015-03-06 07:00:10 -08:00
|
|
|
|
|
|
|
var root = {};
|
|
|
|
|
2015-05-18 12:21:36 -07:00
|
|
|
var groupingTime = 5000;
|
2015-03-06 07:00:10 -08:00
|
|
|
var lastNotificationOnWallet = {};
|
|
|
|
|
|
|
|
root.getLast = function(walletId) {
|
|
|
|
var last = lastNotificationOnWallet[walletId];
|
|
|
|
if (!last) return null;
|
|
|
|
|
|
|
|
return Date.now() - last.ts < groupingTime ? last : null;
|
|
|
|
};
|
|
|
|
|
|
|
|
root.storeLast = function(notificationData, walletId) {
|
2015-05-29 08:39:17 -07:00
|
|
|
|
|
|
|
if (notificationData.type == 'NewAddress')
|
|
|
|
return;
|
|
|
|
|
2015-03-06 07:00:10 -08:00
|
|
|
lastNotificationOnWallet[walletId] = {
|
|
|
|
creatorId: notificationData.creatorId,
|
|
|
|
type: notificationData.type,
|
|
|
|
ts: Date.now(),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
root.shouldSkip = function(notificationData, last) {
|
|
|
|
if (!last) return false;
|
|
|
|
|
|
|
|
// rules...
|
2016-06-21 10:51:33 -07:00
|
|
|
if (last.type === 'NewTxProposal' &&
|
|
|
|
notificationData.type === 'TxProposalAcceptedBy')
|
2015-03-06 07:00:10 -08:00
|
|
|
return true;
|
|
|
|
|
2016-06-21 10:51:33 -07:00
|
|
|
if (last.type === 'TxProposalFinallyAccepted' &&
|
|
|
|
notificationData.type === 'NewOutgoingTx')
|
2015-03-06 07:00:10 -08:00
|
|
|
return true;
|
|
|
|
|
2016-06-21 10:51:33 -07:00
|
|
|
if (last.type === 'TxProposalRejectedBy' &&
|
|
|
|
notificationData.type === 'TxProposalFinallyRejected')
|
2015-03-06 07:00:10 -08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
root.newBWCNotification = function(notificationData, walletId, walletName) {
|
|
|
|
var last = root.getLast(walletId);
|
|
|
|
root.storeLast(notificationData, walletId);
|
|
|
|
|
|
|
|
if (root.shouldSkip(notificationData, last))
|
|
|
|
return;
|
|
|
|
|
|
|
|
var config = configService.getSync();
|
|
|
|
config.colorFor = config.colorFor || {};
|
2016-06-21 10:51:33 -07:00
|
|
|
var color = config.colorFor[walletId] || '#4A90E2';
|
2015-05-14 06:39:22 -07:00
|
|
|
var name = config.aliasFor[walletId] || walletName;
|
2015-03-06 07:00:10 -08:00
|
|
|
|
|
|
|
switch (notificationData.type) {
|
|
|
|
case 'NewTxProposal':
|
2015-05-18 12:21:36 -07:00
|
|
|
notification.new(gettext('New Payment Proposal'),
|
2016-06-21 10:51:33 -07:00
|
|
|
name, {
|
|
|
|
color: color
|
|
|
|
});
|
2015-03-06 07:00:10 -08:00
|
|
|
break;
|
|
|
|
case 'TxProposalAcceptedBy':
|
2015-05-18 12:21:36 -07:00
|
|
|
notification.success(gettext('Payment Proposal Signed by Copayer'),
|
2016-06-21 10:51:33 -07:00
|
|
|
name, {
|
|
|
|
color: color
|
|
|
|
});
|
2015-03-06 07:00:10 -08:00
|
|
|
break;
|
|
|
|
case 'TxProposalRejectedBy':
|
2015-05-18 12:21:36 -07:00
|
|
|
notification.error(gettext('Payment Proposal Rejected by Copayer'),
|
2016-06-21 10:51:33 -07:00
|
|
|
name, {
|
|
|
|
color: color
|
|
|
|
});
|
2015-03-06 07:00:10 -08:00
|
|
|
break;
|
|
|
|
case 'TxProposalFinallyRejected':
|
2015-05-18 20:49:07 -07:00
|
|
|
notification.error(gettext('Payment Proposal Rejected'),
|
2016-06-21 10:51:33 -07:00
|
|
|
name, {
|
|
|
|
color: color
|
|
|
|
});
|
2015-03-06 07:00:10 -08:00
|
|
|
break;
|
|
|
|
case 'NewOutgoingTx':
|
2015-05-29 08:39:17 -07:00
|
|
|
notification.sent(gettext('Payment Sent'),
|
2016-06-21 10:51:33 -07:00
|
|
|
name, {
|
|
|
|
color: color
|
|
|
|
});
|
2015-03-06 07:00:10 -08:00
|
|
|
break;
|
|
|
|
case 'NewIncomingTx':
|
2015-04-30 09:03:30 -07:00
|
|
|
notification.funds(gettext('Funds received'),
|
2016-06-21 10:51:33 -07:00
|
|
|
name, {
|
|
|
|
color: color
|
|
|
|
});
|
2015-03-06 07:00:10 -08:00
|
|
|
break;
|
|
|
|
case 'ScanFinished':
|
2015-04-30 09:03:30 -07:00
|
|
|
notification.success(gettext('Scan Finished'),
|
2016-06-21 10:51:33 -07:00
|
|
|
name, {
|
|
|
|
color: color
|
|
|
|
});
|
2015-03-06 07:00:10 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'NewCopayer':
|
|
|
|
// No UX notification
|
|
|
|
break;
|
2016-01-13 07:08:13 -08:00
|
|
|
case 'BalanceUpdated':
|
|
|
|
// No UX notification
|
|
|
|
break;
|
2015-03-06 07:00:10 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return root;
|
|
|
|
});
|