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]]
|
2016-07-18 19:29:51 +03:00
|
|
|
[status-im.persistence.realm.core :as realm]
|
2016-05-19 18:31:56 +02:00
|
|
|
[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-07-22 11:53:12 +03:00
|
|
|
[status-im.components.geth :as geth]
|
2016-06-27 12:07:14 +03:00
|
|
|
[status-im.utils.handlers :refer [register-handler] :as u]
|
2016-05-19 18:31:56 +02:00
|
|
|
status-im.chat.handlers
|
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-08-01 13:29:10 +03:00
|
|
|
status-im.profile.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-08-04 18:36:13 +03:00
|
|
|
status-im.protocol.handlers
|
2016-06-30 19:00:44 +03:00
|
|
|
[status-im.utils.datetime :as time]
|
|
|
|
status-im.transactions.handlers))
|
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 [_ _]
|
2016-07-18 19:29:51 +03:00
|
|
|
(realm/reset-account)
|
2016-08-01 13:29:10 +03:00
|
|
|
(assoc app-db :current-account-id nil)))
|
2016-07-18 15:42:48 +03:00
|
|
|
|
|
|
|
(register-handler :initialize-account-db
|
2016-07-18 22:11:54 +03:00
|
|
|
(fn [db _]
|
|
|
|
(assoc db
|
2016-06-30 23:03:43 +03:00
|
|
|
:signed-up (storage/get kv/kv-store :signed-up)
|
|
|
|
: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!
|
2016-08-01 13:29:10 +03:00
|
|
|
(fn [_ [_ address]]
|
|
|
|
(dispatch [:initialize-protocol address])
|
2016-07-18 19:50:13 +03:00
|
|
|
(dispatch [:initialize-account-db])
|
2016-07-18 15:42:48 +03:00
|
|
|
(dispatch [:initialize-chats])
|
|
|
|
(dispatch [:load-contacts])
|
2016-08-04 18:36:13 +03:00
|
|
|
(dispatch [:init-chat])
|
|
|
|
(dispatch [:init-discoveries])
|
|
|
|
(dispatch [:send-account-update-if-needed])
|
|
|
|
(dispatch [:remove-old-discoveries!]))))
|
2016-07-18 15:42:48 +03:00
|
|
|
|
|
|
|
(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-06-30 19:00:44 +03:00
|
|
|
(log/debug "Started Node: "))
|
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")
|
2016-07-22 11:53:12 +03:00
|
|
|
(geth/start-node (fn [result] (node-started db result))
|
|
|
|
#(log/debug "Geth already initialized")))))
|
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")))
|