-
Notifications
You must be signed in to change notification settings - Fork 21
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
This file was deleted.
There was a problem hiding this comment.
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?