From 444a2d93b77732b5dfc1d777f74af8be8f6da352 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Tue, 18 Jul 2017 17:26:24 +0300 Subject: [PATCH] jail: sendMessage and showSuggestions handlers (#195) Merging what's already implemented in bugs/whisper-on-geth.1.6.1 --- geth/jail/handlers.go | 49 ++++++++++++++++++++++++++++++++++++++++--- geth/jail/jail.go | 6 ++++++ 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/geth/jail/handlers.go b/geth/jail/handlers.go index bce221100..5d9b22310 100644 --- a/geth/jail/handlers.go +++ b/geth/jail/handlers.go @@ -6,10 +6,9 @@ import ( ) const ( - // EventLocalStorageSet is triggered when set request is sent to local storage EventLocalStorageSet = "local_storage.set" - - // LocalStorageMaxDataLen is maximum length of data that you can store in local storage + EventSendMessage = "jail.send_message" + EventShowSuggestions = "jail.show_suggestions" LocalStorageMaxDataLen = 256 ) @@ -107,3 +106,47 @@ func makeLocalStorageSetHandler(chatID string) func(call otto.FunctionCall) (res return newResultResponse(call, true) } } + +// SendMessageEvent wraps Jail send signals +type SendMessageEvent struct { + ChatID string `json:"chat_id"` + Message string `json:"message"` +} + +func makeSendMessageHandler(chatID string) func(call otto.FunctionCall) (response otto.Value) { + return func(call otto.FunctionCall) otto.Value { + message := call.Argument(0).String() + + node.SendSignal(node.SignalEnvelope{ + Type: EventSendMessage, + Event: SendMessageEvent{ + ChatID: chatID, + Message: message, + }, + }) + + return newResultResponse(call, true) + } +} + +// ShowSuggestionsEvent wraps Jail show suggestion signals +type ShowSuggestionsEvent struct { + ChatID string `json:"chat_id"` + Markup string `json:"markup"` +} + +func makeShowSuggestionsHandler(chatID string) func(call otto.FunctionCall) (response otto.Value) { + return func(call otto.FunctionCall) otto.Value { + suggestionsMarkup := call.Argument(0).String() + + node.SendSignal(node.SignalEnvelope{ + Type: EventShowSuggestions, + Event: ShowSuggestionsEvent{ + ChatID: chatID, + Markup: suggestionsMarkup, + }, + }) + + return newResultResponse(call, true) + } +} diff --git a/geth/jail/jail.go b/geth/jail/jail.go index 5ce493cf6..0bdeef4bd 100644 --- a/geth/jail/jail.go +++ b/geth/jail/jail.go @@ -182,6 +182,12 @@ func (jail *Jail) Parse(chatID string, js string) string { return makeError(err.Error()) } + // sendMessage/showSuggestions handlers + vm.Set("statusSignals", struct{}{}) + statusSignals, _ := vm.Get("statusSignals") + statusSignals.Object().Set("sendMessage", makeSendMessageHandler(chatID)) + statusSignals.Object().Set("showSuggestions", makeShowSuggestionsHandler(chatID)) + jjs := string(web3JSCode) + ` var Web3 = require('web3'); var web3 = new Web3(jeth);