From 11bf667ad44d710936dd360d92a32052792f2d5d Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Wed, 8 Aug 2018 12:35:01 -0400 Subject: [PATCH] conflict in saga and action --- embark-ui/src/actions/index.js | 15 ++++ embark-ui/src/api/index.js | 9 ++- .../src/containers/CommunicationContainer.js | 61 ++++++++++------ embark-ui/src/reducers/messagesReducer.js | 22 ++++++ embark-ui/src/sagas/index.js | 27 +++++-- lib/modules/whisper/index.js | 71 +++++++++++++++++++ lib/modules/whisper/js/message_events.js | 3 + 7 files changed, 180 insertions(+), 28 deletions(-) create mode 100644 embark-ui/src/reducers/messagesReducer.js diff --git a/embark-ui/src/actions/index.js b/embark-ui/src/actions/index.js index 02c1d74d..ee340614 100644 --- a/embark-ui/src/actions/index.js +++ b/embark-ui/src/actions/index.js @@ -76,6 +76,21 @@ export const processLogs = { failure: (error) => action(PROCESS_LOGS[FAILURE], {error}) }; +export const MESSAGE_LISTEN = createRequestTypes('MESSAGE_LISTEN'); +export const messageListen = { + request: (channel) => action(MESSAGE_LISTEN[REQUEST], {channel}), + success: (message) => action(MESSAGE_LISTEN[SUCCESS], {message}), + failure: (error) => action(MESSAGE_LISTEN[FAILURE], {error}) +}; + +export const MESSAGE_SEND = createRequestTypes('MESSAGE_SEND'); +export const messageSend = { + request: (body) => action(MESSAGE_SEND[REQUEST], {body}), + success: () => action(MESSAGE_SEND[SUCCESS]), + failure: (error) => action(MESSAGE_SEND[FAILURE], {error}) +}; + + export const CONTRACTS = createRequestTypes('CONTRACTS'); export const contracts = { request: () => action(CONTRACTS[REQUEST]), diff --git a/embark-ui/src/api/index.js b/embark-ui/src/api/index.js index d30b7b3b..e59665b3 100644 --- a/embark-ui/src/api/index.js +++ b/embark-ui/src/api/index.js @@ -1,7 +1,6 @@ import axios from "axios"; import constants from '../constants'; - function get(path, params) { return axios.get(constants.httpEndpoint + path, params) .then((response) => { @@ -65,6 +64,14 @@ export function fetchContract(payload) { return get(`/contract/${payload.contractName}`); } +export function sendMessage(payload) { + return post(`/communication/sendMessage`, payload); +} + +export function listenToChannel(channel) { + return new WebSocket(`${constants.wsEndpoint}/communication/listenTo/${channel}`); +} + export function fetchContractProfile(payload) { return get(`/profiler/${payload.contractName}`); } diff --git a/embark-ui/src/containers/CommunicationContainer.js b/embark-ui/src/containers/CommunicationContainer.js index bfd38a06..6efc1b48 100644 --- a/embark-ui/src/containers/CommunicationContainer.js +++ b/embark-ui/src/containers/CommunicationContainer.js @@ -1,5 +1,8 @@ +import PropTypes from "prop-types"; import React, {Component} from 'react'; +import connect from "react-redux/es/connect/connect"; import {Alert, Button, Form, Icon} from 'tabler-react'; +import {messageSend, messageListen} from "../actions"; class CommunicationContainer extends Component { constructor(props) { @@ -23,8 +26,7 @@ class CommunicationContainer extends Component { sendMessage(e) { e.preventDefault(); - // TODO send message via API - console.log('Send', this.state.message); + this.props.messageSend({topic: this.state.channel, message: this.state.message}); this.addToLog("EmbarkJS.Messages.sendMessage({topic: '" + this.state.channel + "', data: '" + this.state.message + "'})"); } @@ -37,20 +39,7 @@ class CommunicationContainer extends Component { subscribedChannels }); - console.log('Listen to', this.state.listenTo); - // TODO listen to channel via API - /*EmbarkJS.Messages.listenTo({topic: [this.state.listenTo]}, (error, message) => { - const messageList = this.state.messageList; - if (error) { - messageList.push(Error: {error}); - } else { - messageList.push(Channel: {message.topic} | Message: {message.data}); - } - this.setState({ - messageList - }); - });*/ - + this.props.messageListen(this.state.listenTo); this.addToLog("EmbarkJS.Messages.listenTo({topic: ['" + this.state.listenTo + "']}).then(function(message) {})"); } @@ -85,16 +74,25 @@ class CommunicationContainer extends Component {
{this.state.subscribedChannels.map((item, i) =>

{item}

)}
-

Messages received:

-
- {this.state.messageList.map((item, i) =>

{item}

)} -
+ {this.props.messages && this.props.messages.channels && this.props.messages.channels.length && + +

Messages received:

+
+ {Object.keys(this.props.messages.channels).map((channelName, i) => { + return ( +

{channelName}

+ {this.props.messages.channels[channelName].messages.map((message, f) => { + return

{message}

; + })} +
); + })} +
+
+ } -

Send Message

- { + symKeyID = id; + paraCb(err); + }); + }, + function(paraCb) { + self.web3.shh.newKeyPair((err, id) => { + sig = id; + paraCb(err); + }); + } + ], (err) => { + if (err) { + self.logger.error('Error getting Whisper keys:', err.message || err); + return; + } + self.embark.registerAPICall( + 'post', + '/embark-api/communication/sendMessage', + (req, res) => { + sendMessage({ + topic: req.body.topic, + data: req.body.message, + sig, + symKeyID, + fromAscii: self.web3.utils.asciiToHex, + toHex: self.web3.utils.toHex, + post: self.web3.shh.post + }, (err, result) => { + if (err) { + return res.status(500).send({error: err}); + } + res.send(result); + }); + }); + + self.embark.registerAPICall( + 'ws', + // FIXME channel name + '/embark-api/communication/listenTo/jorain', + (ws, _req) => { + console.log('Listen to', 'jorain'); + listenTo({ + topic: 'jorain', + messageEvents, + toHex: self.web3.utils.toHex, + toAscii: self.web3.utils.hexToAscii, + sig, + symKeyID, + subscribe: self.web3.shh.subscribe + }, (err, result) => { + if (err) { + return ws.status(500).send(JSON.stringify({error: err})); + } + ws.send(JSON.stringify(result)); + }); + }); + }); + } } module.exports = Whisper; diff --git a/lib/modules/whisper/js/message_events.js b/lib/modules/whisper/js/message_events.js index dd931903..dd258e13 100644 --- a/lib/modules/whisper/js/message_events.js +++ b/lib/modules/whisper/js/message_events.js @@ -14,3 +14,6 @@ __MessageEvents.prototype.stop = function() { this.filter.stopWatching(); }; +if (typeof module !== 'undefined' && module.exports) { + module.exports = __MessageEvents; +}