Skip to content

Commit

Permalink
Merge pull request #279 from interledger/ws_notifications
Browse files Browse the repository at this point in the history
Breaking: Full support for websocket-based notifications and eol'ed http notifications
  • Loading branch information
gip authored Jul 7, 2016
2 parents 062dacd + d3215fb commit 8cfe503
Show file tree
Hide file tree
Showing 28 changed files with 376 additions and 1,525 deletions.
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) {
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
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

0 comments on commit 8cfe503

Please sign in to comment.