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-10-26 18:30:31 +03:00
|
|
|
[status-im.db :refer [app-db]]
|
2016-10-04 14:49:59 +03:00
|
|
|
[status-im.data-store.core :as data-store]
|
2016-09-17 15:35:01 +03:00
|
|
|
[taoensso.timbre :as log]
|
2016-05-19 18:31:56 +02:00
|
|
|
[status-im.utils.crypt :refer [gen-random-bytes]]
|
2016-09-07 20:35:04 +03:00
|
|
|
[status-im.components.status :as status]
|
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-11-23 00:07:33 +08:00
|
|
|
status-im.discover.handlers
|
2016-05-21 16:07:37 +03:00
|
|
|
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-09-15 16:48:53 +03:00
|
|
|
status-im.transactions.handlers
|
2016-10-19 15:22:05 +03:00
|
|
|
status-im.network.handlers
|
2017-02-02 09:07:44 +03:00
|
|
|
status-im.debug.handlers
|
2016-09-23 16:22:35 +03:00
|
|
|
[status-im.utils.types :as t]
|
2017-01-10 03:59:39 -02:00
|
|
|
[status-im.i18n :refer [label]]
|
2016-11-17 19:33:38 +02:00
|
|
|
[status-im.constants :refer [console-chat-id]]
|
2017-01-19 15:23:06 +02:00
|
|
|
[status-im.utils.ethereum-network :as enet]
|
2017-02-22 11:01:17 +02:00
|
|
|
[status-im.utils.instabug :as inst]
|
|
|
|
[status-im.utils.platform :as p]))
|
2016-03-23 16:48:26 +02:00
|
|
|
|
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
|
2017-02-22 11:01:17 +02:00
|
|
|
(fn [{:keys [status-module-initialized? status-node-started?
|
|
|
|
network-status network]} _]
|
2016-10-04 14:49:59 +03:00
|
|
|
(data-store/init)
|
2017-02-22 11:01:17 +02:00
|
|
|
(assoc app-db :current-account-id nil
|
|
|
|
:network-status network-status
|
|
|
|
:status-module-initialized? (or p/ios? js/goog.DEBUG status-module-initialized?)
|
|
|
|
:status-node-started? status-node-started?
|
|
|
|
:network (or network :testnet))))
|
2016-07-18 15:42:48 +03:00
|
|
|
|
|
|
|
(register-handler :initialize-account-db
|
2016-07-18 22:11:54 +03:00
|
|
|
(fn [db _]
|
2016-12-01 17:32:11 +02:00
|
|
|
(-> db
|
|
|
|
(assoc :current-chat-id console-chat-id)
|
|
|
|
(dissoc :edit-mode
|
|
|
|
:transactions
|
|
|
|
:transactions-queue
|
|
|
|
:new-contact-identity))))
|
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]]
|
2016-07-18 19:50:13 +03:00
|
|
|
(dispatch [:initialize-account-db])
|
2016-12-05 13:50:47 +03:00
|
|
|
(dispatch [:load-processed-messages])
|
2016-09-04 18:39:05 +03:00
|
|
|
(dispatch [:initialize-protocol address])
|
2016-10-06 12:52:58 +03:00
|
|
|
(dispatch [:initialize-sync-listener])
|
2016-07-18 15:42:48 +03:00
|
|
|
(dispatch [:initialize-chats])
|
|
|
|
(dispatch [:load-contacts])
|
2017-02-16 20:21:57 +03:00
|
|
|
(dispatch [:load-groups])
|
2016-08-04 18:36:13 +03:00
|
|
|
(dispatch [:init-chat])
|
|
|
|
(dispatch [:init-discoveries])
|
2017-02-02 09:07:44 +03:00
|
|
|
(dispatch [:init-debug-mode address])
|
2016-08-04 18:36:13 +03:00
|
|
|
(dispatch [:send-account-update-if-needed])
|
2016-11-10 15:34:25 +03:00
|
|
|
(dispatch [:start-requesting-discoveries])
|
2017-02-13 13:02:42 +03:00
|
|
|
(dispatch [:remove-old-discoveries!])
|
|
|
|
(dispatch [:set :creating-account? false]))))
|
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])
|
2016-12-24 13:15:35 +03:00
|
|
|
(dispatch [:load-default-contacts!])
|
2016-11-01 19:00:26 +03:00
|
|
|
(dispatch [:load-commands!]))))
|
2016-07-18 15:42:48 +03:00
|
|
|
|
2016-10-24 23:46:06 +03:00
|
|
|
(def ecc (js/require "eccjs"))
|
|
|
|
|
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")
|
2016-10-24 23:46:06 +03:00
|
|
|
(.toBits (.. ecc -sjcl -codec -hex))
|
|
|
|
(.addEntropy (.. ecc -sjcl -random)))
|
2016-05-19 18:03:15 +03:00
|
|
|
(dispatch [:crypt-initialized]))))))))
|
2016-06-30 23:03:43 +03:00
|
|
|
|
2016-12-27 17:16:53 +02:00
|
|
|
(defn node-started [_ _]
|
2016-11-17 19:33:38 +02:00
|
|
|
(log/debug "Started Node")
|
|
|
|
(enet/get-network #(dispatch [:set :network %])))
|
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!
|
2016-10-24 23:46:06 +03:00
|
|
|
(fn [db _]
|
|
|
|
(log/debug "Starting node")
|
|
|
|
(status/start-node (fn [result] (node-started db result))))))
|
2016-09-15 16:48:53 +03:00
|
|
|
|
|
|
|
(register-handler :signal-event
|
|
|
|
(u/side-effect!
|
|
|
|
(fn [_ [_ event-str]]
|
2016-10-11 17:24:52 +03:00
|
|
|
(log/debug :event-str event-str)
|
2017-01-19 15:23:06 +02:00
|
|
|
(inst/log (str "Signal event: " event-str))
|
2016-09-15 16:48:53 +03:00
|
|
|
(let [{:keys [type event]} (t/json->clj event-str)]
|
|
|
|
(case type
|
|
|
|
"transaction.queued" (dispatch [:transaction-queued event])
|
2016-10-26 18:30:31 +03:00
|
|
|
"transaction.failed" (dispatch [:transaction-failed event])
|
2017-02-22 11:01:17 +02:00
|
|
|
"node.started" (dispatch [:status-node-started!])
|
2016-10-24 23:46:06 +03:00
|
|
|
"module.initialized" (dispatch [:status-module-initialized!])
|
2017-02-22 11:01:17 +02:00
|
|
|
"local_storage.set" (dispatch [:set-local-storage event])
|
2016-09-15 16:48:53 +03:00
|
|
|
(log/debug "Event " type " not handled"))))))
|
2016-04-03 19:00:40 +03:00
|
|
|
|
2016-10-24 23:46:06 +03:00
|
|
|
(register-handler :status-module-initialized!
|
2016-10-30 00:35:37 +03:00
|
|
|
(after (u/side-effect!
|
2016-12-27 17:16:53 +02:00
|
|
|
(fn [_]
|
2016-10-30 00:35:37 +03:00
|
|
|
(status/module-initialized!))))
|
|
|
|
(fn [db]
|
|
|
|
(assoc db :status-module-initialized? true)))
|
2016-10-24 23:46:06 +03:00
|
|
|
|
2017-02-22 11:01:17 +02:00
|
|
|
(register-handler :status-node-started!
|
|
|
|
(fn [db]
|
|
|
|
(assoc db :status-node-started? true)))
|
|
|
|
|
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")))
|