jail: sendSignal handler (#310)

This commit is contained in:
Roman Volosovskyi 2017-09-14 13:43:01 +03:00 committed by Ivan Tomilov
parent 01448d53fc
commit 03b868402c
2 changed files with 11 additions and 76 deletions

View File

@ -11,9 +11,7 @@ import (
// signals // signals
const ( const (
EventLocalStorageSet = "local_storage.set" EventSignal = "jail.signal"
EventSendMessage = "jail.send_message"
EventShowSuggestions = "jail.show_suggestions"
// EventConsoleLog defines the event type for the console.log call. // EventConsoleLog defines the event type for the console.log call.
eventConsoleLog = "vm.console.log" eventConsoleLog = "vm.console.log"
@ -52,21 +50,6 @@ func registerHandlers(jail *Jail, cell common.JailCell, chatID string) error {
return err return err
} }
// define localStorage
if err = cell.Set("localStorage", struct{}{}); err != nil {
return err
}
// register localStorage.set handler
localStorage, err := cell.Get("localStorage")
if err != nil {
return err
}
if err = localStorage.Object().Set("set", makeLocalStorageSetHandler(chatID)); err != nil {
return err
}
// register sendMessage/showSuggestions handlers // register sendMessage/showSuggestions handlers
if err = cell.Set("statusSignals", struct{}{}); err != nil { if err = cell.Set("statusSignals", struct{}{}); err != nil {
return err return err
@ -76,10 +59,7 @@ func registerHandlers(jail *Jail, cell common.JailCell, chatID string) error {
return err return err
} }
registerHandler = statusSignals.Object().Set registerHandler = statusSignals.Object().Set
if err = registerHandler("sendMessage", makeSendMessageHandler(chatID)); err != nil { if err = registerHandler("sendSignal", makeSignalHandler(chatID)); err != nil {
return err
}
if err = registerHandler("showSuggestions", makeShowSuggestionsHandler(chatID)); err != nil {
return err return err
} }
@ -112,66 +92,21 @@ func makeJethIsConnectedHandler(jail *Jail) func(call otto.FunctionCall) (respon
} }
} }
// LocalStorageSetEvent is a signal sent whenever local storage Set method is called // SignalEvent wraps Jail send signals
type LocalStorageSetEvent struct { type SignalEvent struct {
ChatID string `json:"chat_id"` ChatID string `json:"chat_id"`
Data string `json:"data"` Data string `json:"data"`
} }
// makeLocalStorageSetHandler returns localStorage.set() handler func makeSignalHandler(chatID string) func(call otto.FunctionCall) otto.Value {
func makeLocalStorageSetHandler(chatID string) func(call otto.FunctionCall) (response otto.Value) {
return func(call otto.FunctionCall) otto.Value {
data := call.Argument(0).String()
node.SendSignal(node.SignalEnvelope{
Type: EventLocalStorageSet,
Event: LocalStorageSetEvent{
ChatID: chatID,
Data: data,
},
})
return newResultResponse(call.Otto, 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 { return func(call otto.FunctionCall) otto.Value {
message := call.Argument(0).String() message := call.Argument(0).String()
node.SendSignal(node.SignalEnvelope{ node.SendSignal(node.SignalEnvelope{
Type: EventSendMessage, Type: EventSignal,
Event: SendMessageEvent{ Event: SignalEvent{
ChatID: chatID,
Message: message,
},
})
return newResultResponse(call.Otto, 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, ChatID: chatID,
Markup: suggestionsMarkup, Data: message,
}, },
}) })

View File

@ -180,7 +180,7 @@ func (s *JailTestSuite) TestIsConnected() {
require.Equal(expectedResponse, response) require.Equal(expectedResponse, response)
} }
func (s *JailTestSuite) TestLocalStorageSet() { func (s *JailTestSuite) TestEventSignal() {
require := s.Require() require := s.Require()
s.jail.Parse(testChatID, "") s.jail.Parse(testChatID, "")
@ -199,7 +199,7 @@ func (s *JailTestSuite) TestLocalStorageSet() {
err := json.Unmarshal([]byte(jsonEvent), &envelope) err := json.Unmarshal([]byte(jsonEvent), &envelope)
require.NoError(err) require.NoError(err)
if envelope.Type == jail.EventLocalStorageSet { if envelope.Type == jail.EventSignal {
event := envelope.Event.(map[string]interface{}) event := envelope.Event.(map[string]interface{})
chatID, ok := event["chat_id"].(string) chatID, ok := event["chat_id"].(string)
require.True(ok, "chat id is required, but not found") require.True(ok, "chat id is required, but not found")
@ -215,7 +215,7 @@ func (s *JailTestSuite) TestLocalStorageSet() {
}) })
_, err = cell.Run(` _, err = cell.Run(`
var responseValue = localStorage.set("` + testData + `"); var responseValue = statusSignals.sendSignal("` + testData + `");
responseValue = JSON.stringify(responseValue); responseValue = JSON.stringify(responseValue);
`) `)
s.NoError(err) s.NoError(err)