-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Breaking: Full support for websocket-based notifications and eol'ed h…
…ttp notifications
- Loading branch information
Showing
28 changed files
with
376 additions
and
1,525 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.