handle responses from jail

Former-commit-id: 7632de08cf
This commit is contained in:
Roman Volosovskyi 2016-06-25 16:28:45 +03:00
parent e84af2595f
commit 771f046e96
7 changed files with 53 additions and 43 deletions

View File

@ -124,7 +124,7 @@ status.response({
status.command({
name: "help",
description: "Help",
color: "#9a5dcf",
color: "#7099e6",
params: [{
name: "query",
type: status.types.STRING

View File

@ -53,6 +53,10 @@ function call(pathStr, paramsStr) {
_status_catalog
);
if(!fn) {
return null;
}
res = fn(params);
return JSON.stringify(res);

View File

@ -4,15 +4,14 @@
[status-im.utils.handlers :as u]
[status-im.utils.utils :refer [http-get toast]]
[status-im.components.jail :as j]
[status-im.commands.utils :refer [json->cljs generate-hiccup
reg-handler]]))
[status-im.utils.types :refer [json->clj]]
[status-im.commands.utils :refer [generate-hiccup reg-handler]]
[clojure.string :as s]))
(defn init-render-command!
[_ [chat-id command message-id data]]
(j/call chat-id [command :render] data
(fn [res]
(dispatch [::render-command chat-id message-id (json->cljs res)]))))
#(dispatch [::render-command chat-id message-id %])))
(defn render-command
[db [chat-id message-id markup]]
@ -26,30 +25,19 @@
(def regular-events {})
(defn print-error! [error]
(toast error)
(println error))
(defn command-hadler!
[_ [{:keys [to] :as command} response]]
(let [{:keys [error result]} (json->cljs response)]
(if error
(let [m (str "Error on command handling!\n" command error)]
(print-error! m))
(let [{:keys [event params]} result
events (if (= "console" to)
(merge regular-events console-events)
regular-events)]
(when-let [handler (events (keyword event))]
(apply handler params))))))
[_ [{:keys [to]} {:keys [result]} ]]
(when result
(let [{:keys [event params]} result
events (if (= "console" to)
(merge regular-events console-events)
regular-events)]
(when-let [handler (events (keyword event))]
(apply handler params)))))
(defn suggestions-handler!
[db [{:keys [chat-id]} response-json]]
(let [{:keys [error result]} (json->cljs response-json)]
(when error
(let [m (str "Error on param suggestions!\n" error)]
(print-error! m)))
(assoc-in db [:suggestions chat-id] (generate-hiccup result))))
[db [{:keys [chat-id]} {:keys [result]} ]]
(assoc-in db [:suggestions chat-id] (generate-hiccup result)))
(defn suggestions-events-handler!
[db [[n data]]]
@ -59,23 +47,35 @@
nil))
(defn command-preview
[db [chat-id response-json]]
(if-let [response (json->cljs response-json)]
[db [chat-id {:keys [result]}]]
(println :wuuut)
(if result
(let [path [:chats chat-id :staged-commands]
commands-cnt (count (get-in db path))]
;; todo (dec commands-cnt) looks like hack have to find better way to
;; do this
(update-in db (conj path (dec commands-cnt)) assoc
:preview (generate-hiccup response)
:preview-string (str response)))
:preview (generate-hiccup result)
:preview-string (str result)))
db))
(defn print-error-message! [message]
(fn [_ params]
(when (:error (last params))
(toast (s/join "\n" [message params]))
(println message params))))
(reg-handler :init-render-command! init-render-command!)
(reg-handler ::render-command render-command)
(reg-handler :command-handler! (u/side-effect! command-hadler!))
(reg-handler :command-handler!
(after (print-error-message! "Error on command handling"))
(u/side-effect! command-hadler!))
(reg-handler :suggestions-handler
(after #(dispatch [:animate-show-response]))
[(after #(dispatch [:animate-show-response]))
(after (print-error-message! "Error on param suggestions"))]
suggestions-handler!)
(reg-handler :suggestions-event! (u/side-effect! suggestions-events-handler!))
(reg-handler :command-preview command-preview)
(reg-handler :command-preview
(after (print-error-message! "Error on command preview"))
command-preview)

View File

@ -6,7 +6,8 @@
[clojure.string :as s]
[status-im.persistence.realm :as realm]
[status-im.components.jail :as j]
[status-im.commands.utils :refer [json->cljs reg-handler]]))
[status-im.utils.types :refer [json->clj]]
[status-im.commands.utils :refer [reg-handler]]))
(def commands-js "commands.js")
@ -44,7 +45,7 @@
(defn parse-commands! [_ [identity file]]
(j/parse identity file
(fn [result]
(let [{:keys [error result]} (json->cljs result)]
(let [{:keys [error result]} (json->clj result)]
(if error
(dispatch [::loading-failed! identity ::error-in-jail error])
(dispatch [::add-commands identity file result]))))))

View File

@ -5,12 +5,11 @@
image touchable-highlight]]
[re-frame.core :refer [register-handler dispatch trim-v debug]]))
(defn json->cljs [json]
(defn json->clj [json]
(if (= json "undefined")
nil
(js->clj (.parse js/JSON json) :keywordize-keys true)))
(def elements
{:text text
:view view

View File

@ -1,6 +1,7 @@
(ns status-im.components.jail
(:require-macros [status-im.utils.slurp :refer [slurp]])
(:require [status-im.components.react :as r]))
(:require [status-im.components.react :as r]
[status-im.utils.types :as t]))
(def status-js (slurp "resources/status.js"))
@ -18,5 +19,9 @@
(.stringify js/JSON (clj->js data)))
(defn call [chat-id path params callback]
;(println :call chat-id (cljs->json path) (cljs->json params))
(.call jail chat-id (cljs->json path) (cljs->json params) callback))
(println :call chat-id (cljs->json path) (cljs->json params))
(let [cb (fn [r]
(let [r' (t/json->clj r)]
(println r')
(callback r')))]
(.call jail chat-id (cljs->json path) (cljs->json params) cb)))

View File

@ -11,5 +11,6 @@
(defn clj->json [data]
(.stringify js/JSON (clj->js data)))
(defn json->clj [data]
(js->clj (.parse js/JSON data) :keywordize-keys true))
(defn json->clj [json]
(when-not (= json "undefined")
(js->clj (.parse js/JSON json) :keywordize-keys true)))