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({ status.command({
name: "help", name: "help",
description: "Help", description: "Help",
color: "#9a5dcf", color: "#7099e6",
params: [{ params: [{
name: "query", name: "query",
type: status.types.STRING type: status.types.STRING

View File

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

View File

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

View File

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

View File

@ -1,6 +1,7 @@
(ns status-im.components.jail (ns status-im.components.jail
(:require-macros [status-im.utils.slurp :refer [slurp]]) (: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")) (def status-js (slurp "resources/status.js"))
@ -18,5 +19,9 @@
(.stringify js/JSON (clj->js data))) (.stringify js/JSON (clj->js data)))
(defn call [chat-id path params callback] (defn call [chat-id path params callback]
;(println :call chat-id (cljs->json path) (cljs->json params)) (println :call chat-id (cljs->json path) (cljs->json params))
(.call jail chat-id (cljs->json path) (cljs->json params) callback)) (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] (defn clj->json [data]
(.stringify js/JSON (clj->js data))) (.stringify js/JSON (clj->js data)))
(defn json->clj [data] (defn json->clj [json]
(js->clj (.parse js/JSON data) :keywordize-keys true)) (when-not (= json "undefined")
(js->clj (.parse js/JSON json) :keywordize-keys true)))