2016-05-19 18:31:56 +02:00
|
|
|
(ns status-im.handlers
|
2016-03-23 16:48:26 +02:00
|
|
|
(:require
|
2016-07-18 15:42:48 +03:00
|
|
|
[re-frame.core :refer [after dispatch dispatch-sync debug]]
|
2016-04-29 16:24:03 +03:00
|
|
|
[schema.core :as s :include-macros true]
|
2016-05-19 18:31:56 +02:00
|
|
|
[status-im.db :refer [app-db schema]]
|
|
|
|
[status-im.persistence.simple-kv-store :as kv]
|
|
|
|
[status-im.protocol.state.storage :as storage]
|
|
|
|
[status-im.utils.logging :as log]
|
|
|
|
[status-im.utils.crypt :refer [gen-random-bytes]]
|
2016-06-30 13:37:36 +03:00
|
|
|
[status-im.components.react :refer [geth]]
|
2016-06-27 12:07:14 +03:00
|
|
|
[status-im.utils.handlers :refer [register-handler] :as u]
|
2016-06-30 23:03:43 +03:00
|
|
|
[status-im.models.protocol :as protocol]
|
2016-05-19 18:31:56 +02:00
|
|
|
status-im.chat.handlers
|
2016-06-23 15:47:58 +03:00
|
|
|
status-im.chat.handlers.animation
|
2016-05-21 16:07:37 +03:00
|
|
|
status-im.group-settings.handlers
|
2016-05-19 18:31:56 +02:00
|
|
|
status-im.navigation.handlers
|
|
|
|
status-im.contacts.handlers
|
2016-05-21 16:07:37 +03:00
|
|
|
status-im.discovery.handlers
|
|
|
|
status-im.new-group.handlers
|
|
|
|
status-im.participants.handlers
|
2016-06-14 15:56:03 +03:00
|
|
|
status-im.commands.handlers.loading
|
2016-06-15 10:47:38 +03:00
|
|
|
status-im.commands.handlers.jail
|
2016-06-02 13:43:26 +03:00
|
|
|
status-im.qr-scanner.handlers
|
2016-06-30 18:06:05 +03:00
|
|
|
status-im.accounts.handlers
|
2016-06-29 18:54:01 +03:00
|
|
|
status-im.protocol.handlers
|
|
|
|
status-im.chat.handlers.requests))
|
2016-03-23 16:48:26 +02:00
|
|
|
|
|
|
|
;; -- Middleware ------------------------------------------------------------
|
|
|
|
;;
|
|
|
|
;; See https://github.com/Day8/re-frame/wiki/Using-Handler-Middleware
|
|
|
|
;;
|
|
|
|
(defn check-and-throw
|
|
|
|
"throw an exception if db doesn't match the schema."
|
|
|
|
[a-schema db]
|
2016-03-23 21:05:42 +02:00
|
|
|
(if-let [problems (s/check a-schema db)]
|
|
|
|
(throw (js/Error. (str "schema check failed: " problems)))))
|
2016-03-23 16:48:26 +02:00
|
|
|
|
|
|
|
(def validate-schema-mw
|
|
|
|
(after (partial check-and-throw schema)))
|
|
|
|
|
2016-03-29 23:45:31 +03:00
|
|
|
|
|
|
|
;; -- Common --------------------------------------------------------------
|
2016-03-23 16:48:26 +02:00
|
|
|
|
2016-05-29 21:14:34 +03:00
|
|
|
(defn set-el [db [_ k v]]
|
|
|
|
(assoc db k v))
|
|
|
|
|
2016-06-27 14:35:33 +03:00
|
|
|
(register-handler :set set-el)
|
2016-05-29 21:14:34 +03:00
|
|
|
|
|
|
|
(defn set-in [db [_ path v]]
|
|
|
|
(assoc-in db path v))
|
2016-05-11 14:47:40 +03:00
|
|
|
|
2016-06-27 14:35:33 +03:00
|
|
|
(register-handler :set-in set-in)
|
2016-05-20 19:12:04 +03:00
|
|
|
|
2016-06-10 11:18:10 +03:00
|
|
|
(register-handler :set-animation
|
|
|
|
(fn [db [_ k v]]
|
|
|
|
(assoc-in db [:animations k] v)))
|
|
|
|
|
2016-03-23 21:05:42 +02:00
|
|
|
(register-handler :initialize-db
|
2016-07-18 15:42:48 +03:00
|
|
|
(fn [_ _]
|
|
|
|
(assoc app-db
|
|
|
|
:user-identity nil)))
|
|
|
|
|
|
|
|
(register-handler :initialize-account-db
|
2016-05-10 12:31:48 +03:00
|
|
|
(fn [_ _]
|
|
|
|
(assoc app-db
|
2016-06-30 23:03:43 +03:00
|
|
|
:signed-up (storage/get kv/kv-store :signed-up)
|
|
|
|
:user-identity (protocol/stored-identity nil)
|
|
|
|
:password (storage/get kv/kv-store :password))))
|
2016-03-23 16:48:26 +02:00
|
|
|
|
2016-07-18 15:42:48 +03:00
|
|
|
(register-handler :initialize-account
|
|
|
|
(u/side-effect!
|
|
|
|
(fn [_ [_ account]]
|
|
|
|
(dispatch [:initialize-protocol account])
|
|
|
|
(dispatch [:initialize-chats])
|
|
|
|
(dispatch [:load-contacts])
|
|
|
|
; TODO: initialize protocol here
|
|
|
|
(dispatch [:init-chat]))))
|
|
|
|
|
|
|
|
(register-handler :reset-app
|
|
|
|
(u/side-effect!
|
|
|
|
(fn [_ _]
|
|
|
|
(dispatch [:initialize-db])
|
|
|
|
(dispatch [:load-accounts])
|
|
|
|
(dispatch [:init-console-chat])
|
|
|
|
(dispatch [:load-commands! "console"]))))
|
|
|
|
|
2016-04-03 19:00:40 +03:00
|
|
|
(register-handler :initialize-crypt
|
2016-05-19 18:03:15 +03:00
|
|
|
(u/side-effect!
|
|
|
|
(fn [_ _]
|
|
|
|
(log/debug "initializing crypt")
|
|
|
|
(gen-random-bytes 1024 (fn [{:keys [error buffer]}]
|
|
|
|
(if error
|
|
|
|
(do
|
|
|
|
(log/error "Failed to generate random bytes to initialize sjcl crypto")
|
|
|
|
(dispatch [:notify-user {:type :error
|
|
|
|
:error error}]))
|
|
|
|
(do
|
|
|
|
(->> (.toString buffer "hex")
|
|
|
|
(.toBits (.. js/ecc -sjcl -codec -hex))
|
|
|
|
(.addEntropy (.. js/ecc -sjcl -random)))
|
|
|
|
(dispatch [:crypt-initialized]))))))))
|
2016-06-30 23:03:43 +03:00
|
|
|
|
|
|
|
(defn node-started [db result]
|
2016-07-05 19:59:03 +03:00
|
|
|
(log/debug "Started Node: " result))
|
2016-06-30 23:03:43 +03:00
|
|
|
|
2016-06-30 13:37:36 +03:00
|
|
|
(register-handler :initialize-geth
|
2016-06-30 23:03:43 +03:00
|
|
|
(u/side-effect!
|
|
|
|
(fn [db _]
|
|
|
|
(log/debug "Starting node")
|
|
|
|
(.startNode geth (fn [result] (node-started db result))))))
|
2016-04-03 19:00:40 +03:00
|
|
|
|
|
|
|
(register-handler :crypt-initialized
|
2016-05-19 18:03:15 +03:00
|
|
|
(u/side-effect!
|
|
|
|
(fn [_ _]
|
|
|
|
(log/debug "crypt initialized"))))
|
2016-04-03 19:00:40 +03:00
|
|
|
|
2016-03-28 15:14:57 +03:00
|
|
|
;; -- User data --------------------------------------------------------------
|
|
|
|
(register-handler :load-user-phone-number
|
|
|
|
(fn [db [_]]
|
2016-05-13 14:58:58 +03:00
|
|
|
;; todo fetch phone number from db
|
|
|
|
(assoc db :user-phone-number "123")))
|