From a1553db2ccb5c6f09cb84a998d1f29c807660500 Mon Sep 17 00:00:00 2001 From: Jack Mallers Date: Sat, 23 Sep 2017 16:55:45 -0500 Subject: [PATCH 1/2] feature(subscribe invoices): subscribe to invoices for live updates and notification setup --- app/lnd/subscribe/index.js | 2 ++ app/lnd/subscribe/invoices.js | 8 ++++++++ app/reducers/invoice.js | 29 ++++++++++++++++++++++++++++- app/reducers/ipc.js | 3 ++- 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 app/lnd/subscribe/invoices.js diff --git a/app/lnd/subscribe/index.js b/app/lnd/subscribe/index.js index 297a815c..a0a2fa16 100644 --- a/app/lnd/subscribe/index.js +++ b/app/lnd/subscribe/index.js @@ -1,5 +1,7 @@ import subscribeToTransactions from './transactions' +import subscribeToInvoices from './invoices' export default (mainWindow, lnd) => { subscribeToTransactions(mainWindow, lnd) + subscribeToInvoices(mainWindow, lnd) } \ No newline at end of file diff --git a/app/lnd/subscribe/invoices.js b/app/lnd/subscribe/invoices.js new file mode 100644 index 00000000..5a54c71d --- /dev/null +++ b/app/lnd/subscribe/invoices.js @@ -0,0 +1,8 @@ +export default function subscribeToInvoices(mainWindow, lnd) { + const call = lnd.subscribeInvoices({}) + + call.on('data', invoice => mainWindow.send('invoiceUpdate', { invoice })) + call.on('end', () => console.log('end')) + call.on('error', error => console.log('error: ', error)) + call.on('status', status => console.log('status: ', status)) +} \ No newline at end of file diff --git a/app/reducers/invoice.js b/app/reducers/invoice.js index 8cedcc2b..d328ffb7 100644 --- a/app/reducers/invoice.js +++ b/app/reducers/invoice.js @@ -1,5 +1,6 @@ import { createSelector } from 'reselect' import { ipcRenderer } from 'electron' +import { showNotification } from '../notifications' import { btc, usd } from '../utils' // ------------------------------------ // Constants @@ -20,6 +21,8 @@ export const INVOICE_SUCCESSFUL = 'INVOICE_SUCCESSFUL' export const INVOICE_FAILED = 'INVOICE_FAILED' +export const UPDATE_INVOICE = 'UPDATE_INVOICE' + // ------------------------------------ // Actions // ------------------------------------ @@ -95,6 +98,17 @@ export const createInvoice = (amount, memo, currency, rate) => (dispatch) => { // Receive IPC event for newly created invoice export const createdInvoice = (event, invoice) => dispatch => dispatch({ type: INVOICE_SUCCESSFUL, invoice }) + +// Listen for invoice updates pushed from backend from subscribeToInvoices +export const invoiceUpdate = (event, { invoice }) => (dispatch) => { + dispatch({ type: UPDATE_INVOICE, invoice }) + + // HTML 5 desktop notification for the invoice update + const notifTitle = `You've been Zapped` + const notifBody = `Congrats, someone just paid an invoice of yours` + + showNotification(notifTitle, notifBody) +} // ------------------------------------ // Action Handlers // ------------------------------------ @@ -116,7 +130,20 @@ const ACTION_HANDLERS = { [INVOICE_SUCCESSFUL]: (state, { invoice }) => ( { ...state, invoiceLoading: false, invoices: [invoice, ...state.invoices] } ), - [INVOICE_FAILED]: state => ({ ...state, invoiceLoading: false, data: null }) + [INVOICE_FAILED]: state => ({ ...state, invoiceLoading: false, data: null }), + + [UPDATE_INVOICE]: (state, action) => { + const updatedInvoices = state.invoices.map(invoice => { + if (invoice.r_hash.toString('hex') !== action.invoice.r_hash.toString('hex')) { return invoice } + + return { + ...invoice, + ...action.invoice + } + }) + + return { ...state, invoices: updatedInvoices } + } } const invoiceSelectors = {} diff --git a/app/reducers/ipc.js b/app/reducers/ipc.js index 21a822cc..b1c58f1a 100644 --- a/app/reducers/ipc.js +++ b/app/reducers/ipc.js @@ -20,7 +20,7 @@ import { } from './channels' import { receivePayments, paymentSuccessful } from './payment' -import { receiveInvoices, createdInvoice, receiveFormInvoice } from './invoice' +import { receiveInvoices, createdInvoice, receiveFormInvoice, invoiceUpdate } from './invoice' import { receiveBalance } from './balance' import { receiveTransactions, @@ -42,6 +42,7 @@ const ipc = createIpc({ receiveInvoices, receiveInvoice: receiveFormInvoice, createdInvoice, + invoiceUpdate, receiveBalance, From 15eacc7f47689d2b134ac8fca7077451cabe34a3 Mon Sep 17 00:00:00 2001 From: Jack Mallers Date: Sat, 23 Sep 2017 17:04:34 -0500 Subject: [PATCH 2/2] fix(lint): fix eslint errors --- .eslintrc | 3 ++- app/lnd/subscribe/index.js | 2 +- app/lnd/subscribe/invoices.js | 4 +++- app/lnd/subscribe/transactions.js | 4 +++- app/notifications/index.js | 2 +- app/reducers/invoice.js | 10 +++++----- app/reducers/transaction.js | 6 +++--- 7 files changed, 18 insertions(+), 13 deletions(-) diff --git a/.eslintrc b/.eslintrc index 8c55168e..ebfe171a 100644 --- a/.eslintrc +++ b/.eslintrc @@ -22,7 +22,8 @@ "camelcase": 0, "react/require-default-props": 0, "max-len": ["error", 150], - "import/no-extraneous-dependencies": 0 + "import/no-extraneous-dependencies": 0, + "no-new": 0 }, "plugins": [ "flowtype", diff --git a/app/lnd/subscribe/index.js b/app/lnd/subscribe/index.js index a0a2fa16..170adf60 100644 --- a/app/lnd/subscribe/index.js +++ b/app/lnd/subscribe/index.js @@ -4,4 +4,4 @@ import subscribeToInvoices from './invoices' export default (mainWindow, lnd) => { subscribeToTransactions(mainWindow, lnd) subscribeToInvoices(mainWindow, lnd) -} \ No newline at end of file +} diff --git a/app/lnd/subscribe/invoices.js b/app/lnd/subscribe/invoices.js index 5a54c71d..7a1dd101 100644 --- a/app/lnd/subscribe/invoices.js +++ b/app/lnd/subscribe/invoices.js @@ -1,3 +1,5 @@ +/* eslint-disable */ + export default function subscribeToInvoices(mainWindow, lnd) { const call = lnd.subscribeInvoices({}) @@ -5,4 +7,4 @@ export default function subscribeToInvoices(mainWindow, lnd) { call.on('end', () => console.log('end')) call.on('error', error => console.log('error: ', error)) call.on('status', status => console.log('status: ', status)) -} \ No newline at end of file +} diff --git a/app/lnd/subscribe/transactions.js b/app/lnd/subscribe/transactions.js index 4b4b22f2..fd0829a0 100644 --- a/app/lnd/subscribe/transactions.js +++ b/app/lnd/subscribe/transactions.js @@ -1,3 +1,5 @@ +/* eslint-disable */ + export default function subscribeToTransactions(mainWindow, lnd) { const call = lnd.subscribeTransactions({}) @@ -5,4 +7,4 @@ export default function subscribeToTransactions(mainWindow, lnd) { call.on('end', () => console.log('end')) call.on('error', error => console.log('error: ', error)) call.on('status', status => console.log('status: ', status)) -} \ No newline at end of file +} diff --git a/app/notifications/index.js b/app/notifications/index.js index 3e311921..1483efdd 100644 --- a/app/notifications/index.js +++ b/app/notifications/index.js @@ -5,4 +5,4 @@ export default { onClick }) } -} \ No newline at end of file +} diff --git a/app/reducers/invoice.js b/app/reducers/invoice.js index d328ffb7..06a44079 100644 --- a/app/reducers/invoice.js +++ b/app/reducers/invoice.js @@ -104,9 +104,9 @@ export const invoiceUpdate = (event, { invoice }) => (dispatch) => { dispatch({ type: UPDATE_INVOICE, invoice }) // HTML 5 desktop notification for the invoice update - const notifTitle = `You've been Zapped` - const notifBody = `Congrats, someone just paid an invoice of yours` - + const notifTitle = 'You\'ve been Zapped' + const notifBody = 'Congrats, someone just paid an invoice of yours' + showNotification(notifTitle, notifBody) } // ------------------------------------ @@ -131,9 +131,9 @@ const ACTION_HANDLERS = { { ...state, invoiceLoading: false, invoices: [invoice, ...state.invoices] } ), [INVOICE_FAILED]: state => ({ ...state, invoiceLoading: false, data: null }), - + [UPDATE_INVOICE]: (state, action) => { - const updatedInvoices = state.invoices.map(invoice => { + const updatedInvoices = state.invoices.map((invoice) => { if (invoice.r_hash.toString('hex') !== action.invoice.r_hash.toString('hex')) { return invoice } return { diff --git a/app/reducers/transaction.js b/app/reducers/transaction.js index af68557f..dc3ea36c 100644 --- a/app/reducers/transaction.js +++ b/app/reducers/transaction.js @@ -72,9 +72,9 @@ export const newTransaction = (event, { transaction }) => (dispatch) => { dispatch({ type: ADD_TRANSACTION, transaction }) // HTML 5 desktop notification for the new transaction - const notifTitle = transaction.amount > 0 ? `On-chain Transaction Received!` : `On-chain Transaction Sent!` - const notifBody = transaction.amount > 0 ? `Lucky you, you just received a new on-chain transaction. I'm jealous.` : `Hate to see 'em go but love to watch 'em leave. Your on-chain transaction successfully sent.` - + const notifTitle = transaction.amount > 0 ? 'On-chain Transaction Received!' : 'On-chain Transaction Sent!' + const notifBody = transaction.amount > 0 ? 'Lucky you, you just received a new on-chain transaction. I\'m jealous.' : 'Hate to see \'em go but love to watch \'em leave. Your on-chain transaction successfully sent.' // eslint-disable-line + showNotification(notifTitle, notifBody) }