2016-10-04 14:49:59 +03:00
|
|
|
(ns status-im.data-store.accounts
|
2018-05-14 19:55:30 +02:00
|
|
|
(:require [re-frame.core :as re-frame]
|
2018-05-21 15:20:00 +02:00
|
|
|
[status-im.data-store.realm.core :as core]
|
|
|
|
[status-im.utils.types :as types]))
|
2016-10-04 14:49:59 +03:00
|
|
|
|
2018-04-02 18:17:15 +02:00
|
|
|
;; TODO janherich: define as cofx once debug handlers are refactored
|
2016-10-04 14:49:59 +03:00
|
|
|
(defn get-by-address [address]
|
2018-05-14 19:55:30 +02:00
|
|
|
(-> @core/base-realm
|
|
|
|
(core/get-by-field :account :address address)
|
|
|
|
(core/single-clj :account)
|
|
|
|
(update :settings core/deserialize)))
|
2016-10-04 14:49:59 +03:00
|
|
|
|
2018-05-23 10:41:59 +02:00
|
|
|
(defn- deserialize-bootnodes [bootnodes]
|
|
|
|
(reduce-kv
|
|
|
|
(fn [acc id {:keys [chain] :as bootnode}]
|
|
|
|
(assoc-in acc [chain id] bootnode))
|
|
|
|
{}
|
|
|
|
bootnodes))
|
|
|
|
|
2018-10-12 22:14:44 +02:00
|
|
|
(defn- deserialize-networks [networks]
|
|
|
|
(reduce-kv
|
|
|
|
(fn [acc network-id props]
|
|
|
|
(assoc acc network-id (update props :config types/json->clj)))
|
|
|
|
{}
|
|
|
|
networks))
|
|
|
|
|
|
|
|
(defn- deserialize-extensions [extensions]
|
|
|
|
(reduce-kv
|
2018-11-07 16:13:41 +01:00
|
|
|
(fn [acc _ {:keys [url] :as extension}]
|
|
|
|
(assoc acc url extension))
|
2018-10-12 22:14:44 +02:00
|
|
|
{}
|
|
|
|
extensions))
|
2018-05-23 10:41:59 +02:00
|
|
|
|
2018-05-21 15:20:00 +02:00
|
|
|
(defn- deserialize-account [account]
|
|
|
|
(-> account
|
|
|
|
(update :settings core/deserialize)
|
2018-10-12 22:14:44 +02:00
|
|
|
(update :extensions deserialize-extensions)
|
2018-05-23 10:41:59 +02:00
|
|
|
(update :bootnodes deserialize-bootnodes)
|
2018-10-12 22:14:44 +02:00
|
|
|
(update :networks deserialize-networks)))
|
2018-05-21 15:20:00 +02:00
|
|
|
|
2018-10-12 22:14:44 +02:00
|
|
|
(defn- serialize-bootnodes [bootnodes]
|
|
|
|
(->> bootnodes
|
|
|
|
vals
|
|
|
|
(mapcat vals)))
|
|
|
|
|
|
|
|
(defn- serialize-networks [networks]
|
|
|
|
(map (fn [[_ props]]
|
|
|
|
(update props :config types/clj->json))
|
|
|
|
networks))
|
|
|
|
|
|
|
|
(defn- serialize-extensions [extensions]
|
|
|
|
(or (vals extensions) '()))
|
2018-05-21 15:20:00 +02:00
|
|
|
|
|
|
|
(defn- serialize-account [account]
|
|
|
|
(-> account
|
|
|
|
(update :settings core/serialize)
|
2018-10-12 22:14:44 +02:00
|
|
|
(update :extensions serialize-extensions)
|
2018-05-23 10:41:59 +02:00
|
|
|
(update :bootnodes serialize-bootnodes)
|
2019-01-07 13:50:06 +01:00
|
|
|
(update :networks serialize-networks)
|
|
|
|
(update :recent-stickers #(if (nil? %) [] %))
|
|
|
|
(update :stickers #(if (nil? %) [] %))))
|
2018-04-02 18:17:15 +02:00
|
|
|
|
2018-05-14 19:55:30 +02:00
|
|
|
(defn save-account-tx
|
|
|
|
"Returns tx function for saving account"
|
2018-09-18 18:46:31 +02:00
|
|
|
[account]
|
2018-05-14 19:55:30 +02:00
|
|
|
(fn [realm]
|
2018-09-18 18:46:31 +02:00
|
|
|
(core/create realm :account (serialize-account account) true)))
|
2018-10-12 22:14:44 +02:00
|
|
|
|
|
|
|
(re-frame/reg-cofx
|
|
|
|
:data-store/get-all-accounts
|
|
|
|
(fn [coeffects _]
|
|
|
|
(assoc coeffects :all-accounts (-> @core/base-realm
|
|
|
|
(core/get-all :account)
|
|
|
|
(core/all-clj :account)
|
|
|
|
(as-> accounts
|
|
|
|
(map deserialize-account accounts))))))
|