Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breaking: Full support for websocket-based notifications and eol'ed http notifications #279

Merged
merged 1 commit into from
Jul 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 0 additions & 67 deletions src/controllers/notifications.js

This file was deleted.

112 changes: 0 additions & 112 deletions src/controllers/subscriptions.js

This file was deleted.

22 changes: 1 addition & 21 deletions src/lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ const metadata = require('../controllers/metadata')
const health = require('../controllers/health')
const transfers = require('../controllers/transfers')
const accounts = require('../controllers/accounts')
const subscriptions = require('../controllers/subscriptions')
const notifications = require('../controllers/notifications')
const seedDB = require('./seed-db')
const createTables = require('./db').createTables
const readLookupTables = require('./db').readLookupTables
Expand All @@ -31,7 +29,7 @@ class App {
this.config = modules.config
this.db = modules.db
this.timerWorker = modules.timerWorker
this.notificationWorker = modules.notificationWorker
this.notificationBroadcaster = modules.notificationBroadcaster

const koaApp = this.koa = websockify(koa())
const router = this._makeRouter()
Expand Down Expand Up @@ -64,7 +62,6 @@ class App {
// Start timerWorker to trigger the transferExpiryMonitor
// when transfers are going to expire
yield this.timerWorker.start()
this.notificationWorker.start()

if (this.config.getIn(['db', 'sync'])) {
yield createTables()
Expand Down Expand Up @@ -143,23 +140,6 @@ class App {
},
accounts.putResource)

router.get('/subscriptions/:id',
passport.authenticate(['basic', 'http-signature', 'client-cert'], { session: false }),
subscriptions.getResource)
router.put('/subscriptions/:id',
passport.authenticate(['basic', 'http-signature', 'client-cert'], { session: false }),
function * (next) {
this.body = yield parseBody(this)
yield next
},
subscriptions.putResource)
router.delete('/subscriptions/:id',
passport.authenticate(['basic', 'http-signature', 'client-cert'], { session: false }),
subscriptions.deleteResource)

router.get('/subscriptions/:subscription_id/notifications/:notification_id',
passport.authenticate(['basic', 'http-signature', 'client-cert'], { session: false }),
notifications.getResource)
return router
}

Expand Down
2 changes: 0 additions & 2 deletions src/lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ const TABLE_NAMES = [
'L_ACCOUNTS',
'L_FULFILLMENTS',
'L_ENTRIES',
'L_NOTIFICATIONS',
'L_SUBSCRIPTIONS',
'L_TRANSFERS',
'L_LU_REJECTION_REASON',
'L_LU_TRANSFER_STATUS'
Expand Down
58 changes: 58 additions & 0 deletions src/lib/notificationBroadcasterWebsocket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict'

const _ = require('lodash')
const EventEmitter = require('events').EventEmitter
const transferDictionary = require('five-bells-shared').TransferStateDictionary
const transferStates = transferDictionary.transferStates
const isTransferFinalized = require('./transferUtils').isTransferFinalized
const convertToExternalTransfer = require('../models/converters/transfers')
.convertToExternalTransfer
const getFulfillment = require('../models/db/fulfillments').getFulfillment
const convertToExternalFulfillment = require('../models/converters/fulfillments')
.convertToExternalFulfillment

class NotificationBroadcaster extends EventEmitter {
constructor (log) {
super()
this.log = log
}

* sendNotifications (transfer, transaction) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@justmoon To follow up -- the updated implementation doesn't sign its notification. The idea is that the authentication is done when the websocket connection is established. Sounds good?

const affectedAccounts = _([transfer.debits, transfer.credits])
.flatten().pluck('account').value()
affectedAccounts.push('*')

// Prepare notification for websocket subscribers
const notificationBody = {
resource: convertToExternalTransfer(transfer)
}

// If the transfer is finalized, see if it was finalized by a fulfillment
let fulfillment
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I mentioned on #277, unless we have a good reason for including the condition fulfillment in the notification, I'd advocate (in the interests of performance) removing this extra DB read. The client may fetch the fulfillment on an as-needed basis via the GET /transfers/:id/fulfillment endpoint.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For Universal it is essential that the fulfillment be included with the notification. The connector needs to get that as quickly as possible to pass it on and doing a GET would make it much slower

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good reason.

if (isTransferFinalized(transfer)) {
fulfillment = yield getFulfillment(transfer.id, { transaction })

if (fulfillment) {
if (transfer.state === transferStates.TRANSFER_STATE_EXECUTED) {
notificationBody.related_resources = {
execution_condition_fulfillment:
convertToExternalFulfillment(fulfillment)
}
} else if (transfer.state === transferStates.TRANSFER_STATE_REJECTED) {
notificationBody.related_resources = {
cancellation_condition_fulfillment:
convertToExternalFulfillment(fulfillment)
}
}
}
}

this.log.debug('emitting transfer-{' + affectedAccounts.join(',') + '}')
for (let account of affectedAccounts) {
this.emit('transfer-' + account, notificationBody)
}
}

}

module.exports = NotificationBroadcaster
28 changes: 0 additions & 28 deletions src/lib/notificationUtils.js

This file was deleted.

Loading