add on-message-input-change and on-message-send listeners
logging in jail
This commit is contained in:
Roman Volosovskyi 2017-04-27 16:43:38 +03:00 committed by Roman Volosovskyi
parent 1fbb447761
commit d044b1cf43
4 changed files with 92 additions and 80 deletions

View File

@ -43,45 +43,6 @@ var jsDescriptionStyle = {
color: "#838c93de" color: "#838c93de"
}; };
var messages = [];
console = (function (old) {
return {
log: function (text) {
old.log(text);
var message = {
type: 'log',
message: JSON.stringify(text)
};
messages.push(message);
context.messages.push(message);
},
info: function (text) {
old.info(text);
context.messages.push({
type: 'info',
message: JSON.stringify(text)
});
},
warn: function (text) {
old.warn(text);
context.messages.push({
type: 'warn',
message: JSON.stringify(text)
});
},
error: function (text) {
old.error(text);
context.messages.push({
type: 'error',
message: JSON.stringify(text)
});
}
};
}(console));
if (!String.prototype.startsWith) { if (!String.prototype.startsWith) {
String.prototype.startsWith = function (searchString, position) { String.prototype.startsWith = function (searchString, position) {
position = position || 0; position = position || 0;
@ -257,7 +218,7 @@ function getPartialSuggestions(doc, fullCode, code) {
suggestions.push(createObjectSuggestion(suggestedFunction, docInfo, null, parameters.length - 1)); suggestions.push(createObjectSuggestion(suggestedFunction, docInfo, null, parameters.length - 1));
} }
} }
console.log(suggestions); //console.debug(suggestions);
return suggestions; return suggestions;
} }
@ -265,10 +226,10 @@ function getJsSuggestions(code, context) {
var suggestions = []; var suggestions = [];
var doc = DOC_MAP; var doc = DOC_MAP;
// TODO: what's /c / doing there ??? // TODO: what's /c / doing there ???
console.log(code); //console.debug(code);
if (!code || code == "" || code == "c ") { if (!code || code == "" || code == "c ") {
code = ""; code = "";
console.log("Last message: " + context.data); //console.debug("Last message: " + context.data);
if (context.data != null) { if (context.data != null) {
suggestions.push({ suggestions.push({
title: 'Last command used:', title: 'Last command used:',
@ -306,8 +267,8 @@ function getJsSuggestions(code, context) {
suggestions = getPartialSuggestions(doc, originalCode, levelCode); suggestions = getPartialSuggestions(doc, originalCode, levelCode);
} }
console.log("Final code: " + code); //console.debug("Final code: " + code);
console.log("Level code: " + levelCode); //console.debug("Level code: " + levelCode);
suggestions = suggestions.concat(getPartialSuggestions(doc, originalCode, code)); suggestions = suggestions.concat(getPartialSuggestions(doc, originalCode, code));
} }
return suggestions; return suggestions;
@ -344,7 +305,7 @@ function createMarkupText(text) {
) )
); );
} }
console.log(parts); //console.debug(parts);
return parts; return parts;
} }
@ -392,7 +353,7 @@ function jsHandler(params, context) {
}; };
messages = []; messages = [];
try { try {
result.data = JSON.stringify(eval(params.code)); result["text-message"] = JSON.stringify(eval(params.code));
localStorage.set(params.code); localStorage.set(params.code);
} catch (e) { } catch (e) {
result.err = e; result.err = e;
@ -743,10 +704,10 @@ status.response({
} }
}); });
status.registerFunction("message-suggestions", function (params, context) { status.addListener("on-message-input-change", function (params, context) {
return jsSuggestions({code: params.message}, context); return jsSuggestions({code: params.message}, context);
}); });
status.registerFunction("message-handler", function (params, context) { status.addListener("on-message-send", function (params, context) {
return jsHandler({code: params.message}, context); return jsHandler({code: params.message}, context);
}); });

View File

@ -51,10 +51,15 @@ Response.prototype.onReceiveResponse = function (handler) {
this.onReceive = handler; this.onReceive = handler;
}; };
var context = {} var context = {};
function addContext(ns, key, value) {
context[ns][key] = value; function addContext(key, value) {
context[status.message_id][key] = value;
}
function getContext(key) {
return context[status.message_id][key];
} }
function call(pathStr, paramsStr) { function call(pathStr, paramsStr) {
@ -147,7 +152,7 @@ var status = {
var response = new Response(); var response = new Response();
return response.create(h); return response.create(h);
}, },
registerFunction: function (name, fn) { addListener: function (name, fn) {
_status_catalog.functions[name] = fn; _status_catalog.functions[name] = fn;
}, },
localizeNumber: function (num, del, sep) { localizeNumber: function (num, del, sep) {
@ -179,5 +184,49 @@ var status = {
webView: webView, webView: webView,
validationMessage: validationMessage, validationMessage: validationMessage,
bridgedWebView: bridgedWebView bridgedWebView: bridgedWebView
},
setSuggestions: function (view) {
addContext("suggestions", view);
},
sendMessage: function (text) {
addContext("text-message", text);
},
addLogMessage: function (type, message) {
var message = {
type: type,
message: JSON.stringify(message)
};
var logMessages = getContext("log-messages");
if (!logMessages) {
logMessages = [];
}
logMessages.push(message);
addContext("log-messages", logMessages);
} }
}; };
console = (function (old) {
return {
log: function (text) {
old.log(text);
status.addLogMessage('log', text);
},
debug: function (text) {
old.debug(text);
status.addLogMessage('debug', text);
},
info: function (text) {
old.info(text);
status.addLogMessage('info', text);
},
warn: function (text) {
old.warn(text);
status.addLogMessage('warn', text);
},
error: function (text) {
old.error(text);
status.addLogMessage('error', text);
}
};
}(console));

View File

@ -129,11 +129,11 @@
(status/call-jail jail-id (status/call-jail jail-id
path path
params params
#(dispatch [:suggestions-handler #(dispatch [:received-bot-response
{:chat-id current-chat-id {:chat-id current-chat-id
:command command :command command
:parameter-index parameter-index :parameter-index parameter-index}
:result %}])))))))) %]))))))))
(handlers/register-handler (handlers/register-handler
::send-message ::send-message
@ -278,16 +278,14 @@
(handlers/side-effect! (handlers/side-effect!
(fn [db [_ chat-id text]] (fn [db [_ chat-id text]]
(let [data (get-in db [:local-storage chat-id]) (let [data (get-in db [:local-storage chat-id])
path [:functions path [:functions :on-message-input-change]
:message-suggestions]
params {:parameters {:message text} params {:parameters {:message text}
:context {:data data}}] :context {:data data}}]
(status/call-jail chat-id (status/call-jail chat-id
path path
params params
(fn [{:keys [result] :as data}] #(dispatch [:received-bot-response
(dispatch [:suggestions-handler {:chat-id chat-id {:chat-id chat-id} %]))))))
:result data}])))))))
(handlers/register-handler (handlers/register-handler
:clear-seq-arguments :clear-seq-arguments

View File

@ -192,36 +192,40 @@
(u/side-effect! (u/side-effect!
(fn [db [_ chat-id {:keys [content]}]] (fn [db [_ chat-id {:keys [content]}]]
(let [data (get-in db [:local-storage chat-id]) (let [data (get-in db [:local-storage chat-id])
path [:functions path [:functions :on-message-send]
:message-handler]
params {:parameters {:message content} params {:parameters {:message content}
:context {:data data}}] :context {:data data}}]
(status/call-jail chat-id (status/call-jail chat-id
path path
params params
(fn [{:keys [result]}] (fn [resp]
(log/debug "Message handler result: " result) (log/debug "Message handler result: " resp)
(dispatch [::received-dapp-message chat-id result]))))))) (dispatch [:received-bot-response
{:chat-id chat-id} resp])))))))
(register-handler ::received-dapp-message (register-handler :received-bot-response
(u/side-effect! (u/side-effect!
(fn [_ [_ chat-id {:keys [returned]}]] (fn [_ [_ {:keys [chat-id] :as params} {:keys [result] :as data}]]
(let [{:keys [data messages err]} returned (let [{:keys [returned context]} result
content (or err data)] {:keys [markup text-message]} returned
(doseq [message messages] {:keys [log-messages]} context]
(when markup
(dispatch [:suggestions-handler (assoc params :result data)]))
(doseq [message log-messages]
(let [{:keys [message type]} message] (let [{:keys [message type]} message]
(dispatch [:received-message (when (or (not= type "debug") js/goog.DEBUG)
{:message-id (random/id) (dispatch [:received-message
:content (str type ": " message) {:message-id (random/id)
:content-type text-content-type :content (str type ": " message)
:outgoing false :content-type text-content-type
:chat-id chat-id :outgoing false
:from chat-id :chat-id chat-id
:to "me"}]))) :from chat-id
(when content :to "me"}]))))
(when text-message
(dispatch [:received-message (dispatch [:received-message
{:message-id (random/id) {:message-id (random/id)
:content (str content) :content (str text-message)
:content-type text-content-type :content-type text-content-type
:outgoing false :outgoing false
:chat-id chat-id :chat-id chat-id