debug -> status-im.commands.handlers.debug (#2285); restructuring of the namespace

This commit is contained in:
alwx 2017-10-31 13:21:00 +01:00 committed by Roman Volosovskyi
parent 09b290346f
commit 0656a3a093
5 changed files with 182 additions and 164 deletions

View File

@ -87,11 +87,10 @@
(i18n/label :t/faucet-error)))}})) (i18n/label :t/faucet-error)))}}))
"debug" "debug"
(fn [{:keys [random-id] :as cofx} {:keys [params id]}] (fn [{:keys [random-id db] :as cofx} {:keys [params id]}]
(let [debug? (= "On" (:mode params)) (let [debug? (= "On" (:mode params))]
fx (accounts-events/account-update cofx {:debug? debug?})]
(assoc fx :dispatch-n (if debug? (assoc fx :dispatch-n (if debug?
[[:debug-server-start] [[:initialize-debugging {:force-start? true}]
[:received-message [:received-message
{:message-id random-id {:message-id random-id
:content (i18n/label :t/debug-enabled) :content (i18n/label :t/debug-enabled)
@ -100,7 +99,7 @@
:chat-id const/console-chat-id :chat-id const/console-chat-id
:from const/console-chat-id :from const/console-chat-id
:to "me"}]] :to "me"}]]
[[:debug-server-stop]]))))}) [[:stop-debugging]]))))})
(def commands-names (set (keys console-commands->fx))) (def commands-names (set (keys console-commands->fx)))

View File

@ -0,0 +1,175 @@
(ns status-im.commands.handlers.debug
(:require [re-frame.core :as re-frame]
[status-im.components.react :as react]
[status-im.data-store.accounts :as accounts]
[status-im.data-store.messages :as messages]
[status-im.utils.handlers :as handlers]
[status-im.utils.platform :as platform]
[status-im.utils.types :as types]
[taoensso.timbre :as log]))
(def debug-server-port 5561)
(defn respond [data]
(.respond react/http-bridge
200
"application/json"
(types/clj->json data)))
(defn debug-server-start []
(.start react/http-bridge
debug-server-port
(if platform/ios?
"Status iOS"
"Status Android")
(fn [req]
(try
(let [{:keys [postData url]} (js->clj req :keywordize-keys true)
postData (if (string? postData)
(-> (.parse js/JSON postData)
(js->clj :keywordize-keys true))
postData)]
(re-frame/dispatch [::process-request url postData]))
(catch js/Error e
(log/debug "Error: " e))))))
;;;; Specific debug methods
;; TODO: there are still a lot of dispatch calls here. we can remove or restructure most of them,
;; but to do this we need to also rewrite a lot of already existing functions
(defn add-contact
[{:contacts/keys [contacts]} {:keys [name whisper-identity dapp-url bot-url] :as dapp-data}]
(if (and name
whisper-identity
(or dapp-url bot-url))
(if (or (not (get contacts whisper-identity))
(get-in contacts [whisper-identity :debug?]))
(let [dapp (merge dapp-data {:dapp? true
:debug? true})]
(re-frame/dispatch [:upsert-chat! {:chat-id whisper-identity
:name name
:debug? true}])
(if (get contacts whisper-identity)
(do (re-frame/dispatch [:update-contact! dapp])
(respond {:type :ok
:text "The DApp or bot has been updated."}))
(do (re-frame/dispatch [:add-contacts [dapp]])
(re-frame/dispatch [:open-chat-with-contact dapp])
(respond {:type :ok
:text "The DApp or bot has been added."}))))
(respond {:type :error
:text "Your DApp or bot should be debuggable."}))
(respond {:type :error
:text (str "You can add either DApp or bot. The object should contain \"name\", "
"\"whisper-identity\", and \"dapp-url\" or \"bot-url\" fields.")})))
(defn remove-contact
[{:keys [chats]} {:keys [whisper-identity]}]
(if (get chats whisper-identity)
(if (get-in chats [whisper-identity :debug?])
(do (re-frame/dispatch [:remove-chat whisper-identity])
(respond {:type :ok
:text "The DApp or bot has been removed."}))
(respond {:type :error
:text "Your DApp or bot should be debuggable."}))
(respond {:type :error
:text "There is no such DApp or bot."}))
(re-frame/dispatch [:remove-contact whisper-identity #(and (:dapp? %) (:debug? %))]))
(defn contact-changed
[{:keys [webview-bridge current-chat-id]
:contacts/keys [contacts]} {:keys [whisper-identity] :as dapp-data}]
(when (get-in contacts [whisper-identity :debug?])
(when (and (= current-chat-id whisper-identity)
webview-bridge)
(.reload webview-bridge))
(when (get-in contacts [whisper-identity :bot-url])
(re-frame/dispatch [:load-commands! whisper-identity])))
(respond {:type :ok
:text "Command has been executed."}))
(defn switch-node
[{:accounts/keys [current-account-id]} {:keys [url]}]
(re-frame/dispatch [:initialize-protocol current-account-id url])
(respond {:type :ok
:text "You've successfully switched the node."}))
(defn dapps-list
[{:contacts/keys [contacts]}]
(let [contacts (->> (vals contacts)
(filter :debug?)
(map #(select-keys % [:name :whisper-identity :dapp-url :bot-url])))]
(if (seq contacts)
(respond {:type :ok
:data contacts})
(respond {:type :error
:text "No DApps or bots found."}))))
(defn log [db {:keys [identity]}]
(let [log (messages/get-log-messages identity)]
(if (seq log)
(respond {:type :ok
:data log})
(respond {:type :error
:text "No log messages found."}))))
;;;; FX
(re-frame/reg-fx
::initialize-debugging-fx
(fn [[address force-start?]]
(if force-start?
(debug-server-start)
(let [{:keys [debug?]} (accounts/get-by-address address)]
(when debug?
(debug-server-start))))))
(re-frame/reg-fx
::stop-debugging-fx
(fn [_]
(.stop react/http-bridge)))
(re-frame/reg-fx
::process-request-fx
(fn [[{:keys [web3] :as db} url {:keys [encoded] :as post-data}]]
(try
(let [json (some->> encoded
(.toAscii web3)
(.parse js/JSON))
obj (when json
(js->clj json :keywordize-keys true))]
(case url
"/add-dapp" (add-contact db obj)
"/remove-dapp" (remove-contact db obj)
"/dapp-changed" (contact-changed db obj)
"/switch-node" (switch-node db obj)
"/list" (dapps-list db)
"/log" (log db post-data)
:default))
(catch js/Error e
(respond {:type :error :text (str "Error: " e)})
(log/debug "Error: " e)))))
;;;; Handlers
(handlers/register-handler-fx
:initialize-debugging
[re-frame/trim-v]
(fn [_ [{:keys [address force-start?]}]]
{::initialize-debugging-fx [address force-start?]}))
(handlers/register-handler-fx
:stop-debugging
(fn [_]
{::stop-debugging-fx nil}))
(handlers/register-handler-fx
::process-request
[re-frame/trim-v]
(fn [{:keys [db]} [url post-data]]
{::process-request-fx [db url post-data]}))

View File

@ -1,156 +0,0 @@
(ns status-im.debug.handlers
(:require [re-frame.core :refer [after dispatch]]
[status-im.utils.handlers :refer [register-handler] :as u]
[status-im.components.react :refer [http-bridge]]
[status-im.data-store.messages :as messages]
[status-im.data-store.accounts :as accounts]
[taoensso.timbre :as log]
[status-im.utils.platform :as platform]
[status-im.utils.types :as types]))
(def debug-server-port 5561)
(defn respond [data]
(.respond http-bridge
200
"application/json"
(types/clj->json data)))
(register-handler :init-debug-mode
(u/side-effect!
(fn [_ [_ address]]
(let [{:keys [debug?]} (accounts/get-by-address address)]
(when debug?
(dispatch [:debug-server-start]))))))
(register-handler :debug-server-start
(u/side-effect!
(fn [_]
(.start http-bridge
debug-server-port
(if platform/ios?
"Status iOS"
"Status Android")
(fn [req]
(try
(let [{:keys [postData url]} (js->clj req :keywordize-keys true)
postData (if (string? postData)
(-> (.parse js/JSON postData)
(js->clj :keywordize-keys true))
postData)]
(dispatch [:debug-request {:url url :postData postData}]))
(catch js/Error e
(log/debug "Error: " e))))))))
(register-handler :debug-server-stop
(u/side-effect!
(fn [_]
(.stop http-bridge))))
(register-handler :debug-request
(u/side-effect!
(fn [{:keys [web3]} [_ {url :url
{:keys [encoded]
:as post-data} :postData}]]
(try
(let [json (some->> encoded
(.toAscii web3)
(.parse js/JSON))
obj (when json
(js->clj json :keywordize-keys true))]
(case url
"/add-dapp" (dispatch [:debug-add-contact obj])
"/remove-dapp" (dispatch [:debug-remove-contact obj])
"/dapp-changed" (dispatch [:debug-contact-changed obj])
"/switch-node" (dispatch [:debug-switch-node obj])
"/list" (dispatch [:debug-dapps-list])
"/log" (dispatch [:debug-log post-data])
:default))
(catch js/Error e
(respond {:type :error :text (str "Error: " e)})
(log/debug "Error: " e))))))
(register-handler :debug-add-contact
(u/side-effect!
(fn [{:contacts/keys [contacts]} [_ {:keys [name whisper-identity dapp-url bot-url] :as dapp-data}]]
(if (and name
whisper-identity
(or dapp-url bot-url))
(if (or (not (get contacts whisper-identity))
(get-in contacts [whisper-identity :debug?]))
(let [dapp (merge dapp-data {:dapp? true
:debug? true})]
(dispatch [:upsert-chat! {:chat-id whisper-identity
:name name
:debug? true}])
(if (get contacts whisper-identity)
(do (dispatch [:update-contact! dapp])
(respond {:type :ok
:text "The DApp or bot has been updated."}))
(do (dispatch [:add-contacts [dapp]])
(dispatch [:open-chat-with-contact dapp])
(respond {:type :ok
:text "The DApp or bot has been added."}))))
(respond {:type :error
:text "Your DApp or bot should be debuggable."}))
(respond {:type :error
:text (str "You can add either DApp or bot. The object should contain \"name\", "
"\"whisper-identity\", and \"dapp-url\" or \"bot-url\" fields.")})))))
(register-handler :debug-remove-contact
(u/side-effect!
(fn [{:keys [chats]} [_ {:keys [whisper-identity]}]]
(if (get chats whisper-identity)
(if (get-in chats [whisper-identity :debug?])
(do (dispatch [:remove-chat whisper-identity])
(respond {:type :ok
:text "The DApp or bot has been removed."}))
(respond {:type :error
:text "Your DApp or bot should be debuggable."}))
(respond {:type :error
:text "There is no such DApp or bot."}))
(dispatch [:remove-contact whisper-identity #(and (:dapp? %) (:debug? %))]))))
(register-handler :debug-contact-changed
(u/side-effect!
(fn [{:keys [webview-bridge current-chat-id]
:contacts/keys [contacts]} [_ {:keys [whisper-identity] :as dapp-data}]]
(when (get-in contacts [whisper-identity :debug?])
(when (and (= current-chat-id whisper-identity)
webview-bridge)
(.reload webview-bridge))
(when (get-in contacts [whisper-identity :bot-url])
(dispatch [:load-commands! whisper-identity])))
(respond {:type :ok
:text "Command has been executed."}))))
(register-handler :debug-switch-node
(u/side-effect!
(fn [{:accounts/keys [current-account-id]} [_ {:keys [url]}]]
(dispatch [:initialize-protocol current-account-id url])
(respond {:type :ok
:text "You've successfully switched the node."}))))
(register-handler :debug-dapps-list
(u/side-effect!
(fn [{:contacts/keys [contacts]}]
(let [contacts (->> (vals contacts)
(filter :debug?)
(map #(select-keys % [:name :whisper-identity :dapp-url :bot-url])))]
(if (seq contacts)
(respond {:type :ok
:data contacts})
(respond {:type :error
:text "No DApps or bots found."}))))))
(register-handler :debug-log
(u/side-effect!
(fn [db [_ {:keys [identity]}]]
(let [log (messages/get-log-messages identity)]
(if (seq log)
(respond {:type :ok
:data log})
(respond {:type :error
:text "No log messages found."}))))))

View File

@ -120,7 +120,7 @@
(if (nil? error) (if (nil? error)
{:db (assoc db :accounts/login {}) {:db (assoc db :accounts/login {})
:dispatch-n (concat :dispatch-n (concat
[[:debug-server-stop] [[:stop-debugging]
[:set-current-account address] [:set-current-account address]
[:initialize-account address]] [:initialize-account address]]
(if new-account? (if new-account?

View File

@ -3,7 +3,7 @@
status-im.chat.handlers status-im.chat.handlers
status-im.commands.handlers.jail status-im.commands.handlers.jail
status-im.commands.handlers.loading status-im.commands.handlers.loading
status-im.debug.handlers status-im.commands.handlers.debug
status-im.network.handlers status-im.network.handlers
status-im.protocol.handlers status-im.protocol.handlers
status-im.ui.screens.accounts.events status-im.ui.screens.accounts.events
@ -252,7 +252,7 @@
[:load-contact-groups] [:load-contact-groups]
[:init-chat] [:init-chat]
[:init-discoveries] [:init-discoveries]
[:init-debug-mode address] [:initialize-debugging {:address address}]
[:send-account-update-if-needed] [:send-account-update-if-needed]
[:start-requesting-discoveries] [:start-requesting-discoveries]
[:remove-old-discoveries!] [:remove-old-discoveries!]