Replace accounts/accounts by account/account in app-db [#3907]

Signed-off-by: Goran Jovic <goranjovic@gmail.com>
This commit is contained in:
psdp 2018-04-23 12:49:09 +00:00 committed by Goran Jovic
parent 8604efdfff
commit ef0024c5dc
No known key found for this signature in database
GPG Key ID: D429D1A9B2EB8A8E
24 changed files with 92 additions and 113 deletions

View File

@ -20,7 +20,7 @@
(defn request-command-message-data
"Requests command message data from jail"
[{:accounts/keys [current-account-id] :contacts/keys [contacts] :as db}
[{:contacts/keys [contacts] :as db}
{{:keys [command command-scope-bitmask bot params type]} :content
:keys [chat-id group-id] :as message}
{:keys [data-type] :as opts}]
@ -31,8 +31,9 @@
[command command-scope-bitmask]
data-type]
to (get-in contacts [chat-id :address])
address (get-in db [:account/account :address])
jail-params {:parameters params
:context (generate-context current-account-id chat-id (models.message/group-message? message) to)}]
:context (generate-context address chat-id (models.message/group-message? message) to)}]
{:db db
:call-jail {:jail-id bot
:path path

View File

@ -42,8 +42,7 @@
(def console-commands->fx
{"faucet"
(fn [{:keys [db random-id] :as cofx} {:keys [params]}]
(let [{:accounts/keys [accounts current-account-id]} db
current-address (get-in accounts [current-account-id :address])
(let [current-address (get-in db [:account/account :address])
faucet-url (faucet-base-url->url (:url params))]
{:http-get {:url (gstring/format faucet-url current-address)
:success-event-creator (fn [_]

View File

@ -68,16 +68,17 @@
;; jail function in each participant's jail
(defn call-on-message-input-change
"Calls bot's `on-message-input-change` function"
[{:keys [current-chat-id current-account-id chats local-storage] :as db}]
(let [chat-text (string/trim (or (get-in chats [current-chat-id :input-text]) ""))
{:keys [dapp?]} (get-in db [:contacts/contacts current-chat-id])]
[{:keys [current-chat-id chats local-storage] :as db}]
(let [chat-text (string/trim (or (get-in chats [current-chat-id :input-text]) ""))
{:keys [dapp?]} (get-in db [:contacts/contacts current-chat-id])
address (get-in db [:account/account :address])]
(cond-> {:db db}
(and dapp? (not (string/blank? chat-text)))
(assoc :call-jail-function {:chat-id current-chat-id
:function :on-message-input-change
:parameters {:message chat-text}
:context {:data (get local-storage current-chat-id)
:from current-account-id}}))))
:from address}}))))
(defn set-chat-input-metadata
"Set input metadata for active chat. Takes db and metadata and returns updated db."
@ -121,7 +122,7 @@
(defn load-chat-parameter-box
"Returns fx for loading chat parameter box for active chat"
[{:keys [current-chat-id bot-db] :accounts/keys [current-account-id] :as db}
[{:keys [current-chat-id bot-db] :as db}
{:keys [name scope-bitmask type bot owner-id] :as command}]
(let [parameter-index (input-model/argument-position db)]
(when (and command (> parameter-index -1))
@ -137,11 +138,12 @@
rest)
seq-arg (get-in db [:chats current-chat-id :seq-argument-input-text])
to (get-in db [:contacts/contacts current-chat-id :address])
from (get-in db [:account/account :address])
params {:parameters {:args args
:bot-db bot-db
:seq-arg seq-arg}
:context {:data data
:from current-account-id
:from from
:to to}}]
{:call-jail {:jail-id owner-id
:path path
@ -330,18 +332,19 @@
::send-command
message-model/send-interceptors
(fn [{:keys [db] :as cofx} [command-message]]
(let [{:keys [current-public-key current-chat-id] :accounts/keys [current-account-id]} db
new-db (-> (model/set-chat-ui-props db {:sending-in-progress? false})
(clear-seq-arguments)
(set-chat-input-metadata nil)
(set-chat-input-text nil))]
(let [{:keys [current-chat-id current-public-key]} db
new-db (-> (model/set-chat-ui-props db {:sending-in-progress? false})
(clear-seq-arguments)
(set-chat-input-metadata nil)
(set-chat-input-text nil))
address (get-in db [:account/account :address])]
(merge {:db new-db}
(message-model/process-command (assoc cofx :db new-db)
{:message (get-in db [:chats current-chat-id :input-text])
:command command-message
:chat-id current-chat-id
:identity current-public-key
:address current-account-id})))))
:address address})))))
(defn command-complete?
[chat-command]

View File

@ -110,10 +110,9 @@
(rest command-args)
seq-arguments))})))
([{:keys [chats current-chat-id access-scope->commands-responses]
:contacts/keys [contacts]
:accounts/keys [accounts current-account-id] :as db}]
:contacts/keys [contacts] :as db}]
(let [chat (get chats current-chat-id)
account (get accounts current-account-id)]
account (:account/account db)]
(selected-chat-command chat
(commands-model/commands-responses :command
access-scope->commands-responses

View File

@ -51,9 +51,6 @@
(chat-model/upsert-chat {:chat-id chat-id
:timestamp now} cofx))
(defn- get-current-account [{:accounts/keys [accounts current-account-id]}]
(get accounts current-account-id))
(defn- send-message-seen [chat-id message-id send-seen? cofx]
(when send-seen?
(transport/send (protocol/map->MessagesSeen {:message-ids #{message-id}}) chat-id cofx)))
@ -66,7 +63,7 @@
view-id
access-scope->commands-responses]
:contacts/keys [contacts]} db
{:keys [public-key] :as current-account} (get-current-account db)
{:keys [public-key] :as current-account} (:account/account db)
current-chat? (and (= :chat view-id) (= current-chat-id chat-id))
{:keys [last-clock-value] :as chat} (get-in db [:chats chat-id])
request-command (:request-command content)
@ -157,7 +154,7 @@
(receive message cofx)))
(defn- send-dapp-message!
[{{:accounts/keys [current-account-id] :as db} :db :as cofx} chat-id {:keys [content-type] :as message}]
[{:keys [db] :as cofx} chat-id {:keys [content-type] :as message}]
(if (= content-type constants/content-type-command)
(when-let [text-message (get-in message [:content :handler-data :text-message])]
(handle-message-from-bot cofx {:message text-message
@ -167,7 +164,7 @@
:function :on-message-send
:parameters {:message (:content message)}
:context {:data data
:from current-account-id}}})))
:from (get-in db [:account/account :address])}}})))
(defn- send
[chat-id message-id send-record {{:contacts/keys [contacts] :keys [network-status current-public-key]} :db :as cofx}]
@ -307,8 +304,7 @@
(merge {:dispatch-n dn} (dissoc fx :dispatch-n) (dissoc command :dispatch-n)))))
(defn invoke-command-handlers
[{{:accounts/keys [accounts current-account-id]
:contacts/keys [contacts]} :db}
[{{:contacts/keys [contacts] :as db} :db}
{{:keys [command params id]} :command
:keys [chat-id address]
:as orig-params}]
@ -321,7 +317,7 @@
jail-params {:parameters params
:context (cond-> {:from address
:to to
:current-account (get accounts current-account-id)
:current-account (get db :account/account)
:message-id id}
(:async-handler command)
(assoc :orig-params orig-params))}]

View File

@ -157,7 +157,7 @@
(cond-> {:db (add-jail-result db jail-id result)
:call-jail-function {:chat-id jail-id
:function :init :context
{:from (:accounts/current-account-id db)}}}
{:from (get-in db [:account/account :address])}}}
(seq jail-loaded-events)
(-> (assoc :dispatch-n jail-loaded-events)
(update-in [:db :contacts/contacts jail-id] dissoc :jail-loaded-events)))))))

View File

@ -91,8 +91,8 @@
:text "Command has been executed."}))
(defn switch-node
[{:accounts/keys [current-account-id]} {:keys [url]}]
(re-frame/dispatch [:initialize-protocol current-account-id url])
[{:keys [account/account]} {:keys [url]}]
(re-frame/dispatch [:initialize-protocol (:address account) url])
(respond {:type :ok
:text "You've successfully switched the node."}))

View File

@ -19,7 +19,7 @@
- (optionally) initializing offline inboxing"
[current-account-id {:keys [db web3] :as cofx}]
(log/debug :init-whisper)
(when-let [public-key (get-in db [:accounts/accounts current-account-id :public-key])]
(when-let [public-key (get-in db [:account/account :public-key])]
(let [sym-key-added-callback (fn [chat-id sym-key sym-key-id]
(re-frame/dispatch [::sym-key-added {:chat-id chat-id
:sym-key sym-key

View File

@ -36,9 +36,9 @@
(error-fn error)
(success-fn result)))))
(defn get-current-wnode-address [{:accounts/keys [current-account-id accounts] :as db}]
(defn get-current-wnode-address [db]
(let [network (ethereum/network->chain-keyword (get db :network))
wnode-id (get-in accounts [current-account-id :settings :wnode network])]
wnode-id (get-in db [:account/account :settings :wnode network])]
(get-in db [:inbox/wnodes network wnode-id :address])))
(defn initialize-offline-inbox

View File

@ -42,12 +42,11 @@
(spec/def :accounts/accounts (spec/nilable (spec/map-of :account/address :accounts/account)))
;;id of logged in account
(spec/def :accounts/current-account-id (spec/nilable string?))
;;used during creating account
(spec/def :accounts/create (spec/nilable map?))
;;used during recovering account
(spec/def :accounts/recover (spec/nilable map?))
;;used during logging
(spec/def :accounts/login (spec/nilable map?))
;;logged in account
(spec/def :account/account (spec/nilable :accounts/account))

View File

@ -106,11 +106,9 @@
{:db (assoc-in db [:accounts/accounts id] new-account)
:data-store/save-account new-account})))
(defn update-settings [settings {:keys [db] :as cofx}]
(let [{:accounts/keys [current-account-id accounts]} db
new-account (-> (get accounts current-account-id)
(assoc :settings settings))]
{:db (assoc-in db [:accounts/accounts current-account-id] new-account)
(defn update-settings [settings {{:keys [account/account] :as db} :db :as cofx}]
(let [new-account (assoc account :settings settings)]
{:db (assoc db :account/account new-account)
:data-store/save-account new-account}))
(defn account-update
@ -118,10 +116,10 @@
Optionally, one can specify event to be dispatched after fields are persisted."
([new-account-fields cofx]
(account-update new-account-fields nil cofx))
([new-account-fields after-update-event {{:accounts/keys [accounts current-account-id] :as db} :db :as cofx}]
(let [current-account (get accounts current-account-id)
([new-account-fields after-update-event {:keys [db] :as cofx}]
(let [current-account (:account/account db)
new-account (merge current-account new-account-fields)
fx {:db (assoc-in db [:accounts/accounts current-account-id] new-account)
fx {:db (assoc db :account/account new-account)
:data-store/save-account (assoc new-account :after-update-event after-update-event)}
{:keys [name photo-path]} new-account]
(if (or (:name new-account-fields) (:photo-path new-account-fields))
@ -130,8 +128,8 @@
(handlers/register-handler-fx
:send-account-update-if-needed
(fn [{{:accounts/keys [accounts current-account-id] :as db} :db now :now :as cofx} _]
(let [{:keys [last-updated]} (get accounts current-account-id)
(fn [{:keys [db now] :as cofx} _]
(let [{:keys [last-updated]} (:account/account db)
needs-update? (> (- now last-updated) time/week)]
(log/info "Need to send account-update: " needs-update?)
(when needs-update?

View File

@ -13,19 +13,13 @@
(fn [db]
(:accounts/accounts db)))
(reg-sub :get-current-account-id
(fn [db]
(:accounts/current-account-id db)))
(reg-sub :get-current-account
:<- [:get-current-account-id]
:<- [:get-accounts]
(fn [[account-id accounts]]
(some-> accounts (get account-id))))
(fn [db]
(:account/account db)))
(reg-sub :get-current-account-hex
:<- [:get-current-account-id]
(fn [address]
:<- [:get-current-account]
(fn [{:keys [address]}]
(ethereum/normalized-address address)))
(reg-sub
@ -34,4 +28,4 @@
(let [{:keys [step password password-confirm name]} create]
(or (and password (= :enter-password step) (spec/valid? ::db/password password))
(and password-confirm (= :confirm-password step) (spec/valid? ::db/password password-confirm))
(and name (= :enter-name step) (not (string/blank? name)))))))
(and name (= :enter-name step) (not (string/blank? name)))))))

View File

@ -40,8 +40,8 @@
(assoc-in [:contacts/new-identity] ""))
:data-store/save-contact new-contact}))
(defn- own-info [{:accounts/keys [accounts current-account-id] :as db}]
(let [{:keys [name photo-path address]} (get accounts current-account-id)
(defn- own-info [db]
(let [{:keys [name photo-path address]} (:account/account db)
fcm-token (get-in db [:notifications :fcm-token])]
{:name name
:profile-image photo-path
@ -82,8 +82,8 @@
(handlers/register-handler-fx
:set-contact-identity-from-qr
[(re-frame/inject-cofx :random-id)]
(fn [{{:accounts/keys [accounts current-account-id] :as db} :db :as cofx} [_ _ contact-identity]]
(let [current-account (get accounts current-account-id)
(fn [{:keys [db] :as cofx} [_ _ contact-identity]]
(let [current-account (:account/account db)
fx {:db (assoc db :contacts/new-identity contact-identity)}]
(if (new-chat.db/validate-pub-key contact-identity current-account)
fx

View File

@ -22,7 +22,6 @@
:status-module-initialized? (or platform/ios? js/goog.DEBUG)
:keyboard-height 0
:tab-bar-visible? true
:accounts/accounts {}
:navigation-stack '()
:contacts/contacts {}
:qr-codes {}
@ -151,9 +150,9 @@
:group/groups-order
:accounts/accounts
:accounts/create
:accounts/current-account-id
:accounts/recover
:accounts/login
:account/account
:my-profile/profile
:my-profile/default-name
:my-profile/editing?

View File

@ -213,8 +213,8 @@
(handlers/register-handler-fx
:logout
(fn [{:keys [db] :as cofx} _]
(let [{:transport/keys [chats] :keys [current-account-id]} db
sharing-usage-data? (get-in db [:accounts/accounts current-account-id :sharing-usage-data?])]
(let [{:transport/keys [chats]} db
sharing-usage-data? (get-in db [:account/account :sharing-usage-data?])]
(handlers-macro/merge-fx cofx
{:dispatch-n (concat [[:initialize-db]
[:load-accounts]
@ -231,7 +231,6 @@
:or {network (get app-db :network)}} :db} _]
{::init-store nil
:db (assoc app-db
:accounts/current-account-id nil
:contacts/contacts {}
:network-status network-status
:status-module-initialized? (or platform/ios? js/goog.DEBUG status-module-initialized?)
@ -244,22 +243,19 @@
network network-status view-id navigation-stack
access-scope->commands-responses
status-module-initialized? status-node-started?]
:or [network (get app-db :network)]} [_ address]]
(let [console-contact (get contacts constants/console-chat-id)]
:or [network (get app-db :network)]} [_ address]]
(let [console-contact (get contacts constants/console-chat-id)
current-account (accounts address)]
(cond-> (assoc app-db
:access-scope->commands-responses access-scope->commands-responses
:accounts/current-account-id address
;; TODO (yenda) bad, this is derived data and shouldn't be stored in the db
;; the cost of retrieving public key from db with a function taking using
;; current-account-id is negligeable
:current-public-key (:public-key (accounts address))
:current-public-key (:public-key current-account)
:view-id view-id
:navigation-stack navigation-stack
:status-module-initialized? (or platform/ios? js/goog.DEBUG status-module-initialized?)
:status-node-started? status-node-started?
:accounts/accounts accounts
:accounts/create create
:networks/networks networks
:account/account current-account
:network-status network-status
:network network)
console-contact
@ -301,10 +297,9 @@
(handlers/register-handler-fx
:initialize-geth
(fn [{db :db} _]
(let [{:accounts/keys [current-account-id accounts]} db
default-networks (:networks/networks db)
(let [default-networks (:networks/networks db)
default-network (:network db)
{:keys [network networks]} (get accounts current-account-id)
{:keys [network networks]} (:account/account db)
network-config (or (get-in networks [network :config])
(get-in default-networks [default-network :config]))]
{:initialize-geth-fx network-config})))

View File

@ -43,10 +43,11 @@
chats (:transport/chats db)]
(if (utils/network-with-upstream-rpc? networks current-network)
(handlers-macro/merge-fx cofx
{:dispatch [:navigate-to-clean :accounts]}
(transport/stop-whisper)
(accounts-events/account-update {:network network
:last-updated now}))
{:dispatch-n [[:load-accounts]
[:navigate-to-clean :accounts]]}
(transport/stop-whisper)
(accounts-events/account-update {:network network
:last-updated now}))
{:show-confirmation {:title (i18n/label :t/close-app-title)
:content (i18n/label :t/close-app-content)
:confirm-button-text (i18n/label :t/close-app-button)

View File

@ -10,12 +10,11 @@
(handlers/register-handler-fx
::save-wnode
(fn [{:keys [db now] :as cofx} [_ wnode]]
(let [{:accounts/keys [current-account-id accounts]} db
network (ethereum/network->chain-keyword (:network db))
settings (get-in accounts [current-account-id :settings])]
(let [network (ethereum/network->chain-keyword (:network db))
settings (get-in db [:account/account :settings])]
(handlers-macro/merge-fx cofx
{:dispatch [:logout]}
(accounts-events/update-settings (assoc-in settings [:wnode network] wnode))))))
{:dispatch [:logout]}
(accounts-events/update-settings (assoc-in settings [:wnode network] wnode))))))
(handlers/register-handler-fx
:connect-wnode

View File

@ -35,8 +35,8 @@
(chat-events/start-chat chat-id {:navigation-replace? true})
(input-events/select-chat-input-command send-command nil true)))))
(defn get-current-account [{:accounts/keys [current-account-id] :as db}]
(get-in db [:accounts/accounts current-account-id]))
(defn get-current-account [db]
(:account/account db))
(defn valid-name? [name]
(spec/valid? :profile/name name))
@ -55,11 +55,11 @@
{:db (assoc-in db [:my-profile/profile :photo-path] (str "data:image/jpeg;base64," base64-image))}
{:open-image-picker this-event})))
(defn clean-name [{:accounts/keys [current-account-id] :as db} edit-view]
(defn clean-name [db edit-view]
(let [name (get-in db [edit-view :name])]
(if (valid-name? name)
name
(get-in db [:accounts/accounts current-account-id :name]))))
(get-in db [:account/account :name]))))
(defn clear-profile [{:keys [db] :as cofx}]
{:db (dissoc db :my-profile/profile :my-profile/default-name :my-profile/editing?)})

View File

@ -4,11 +4,11 @@
;;TODO(goranjovic) - replace this with an atomic navigation event that calls functions, not dispatch
;; possibly use the generic event, see https://github.com/status-im/status-react/issues/2987
(defmethod navigation/preload-data! :profile-qr-viewer
[{:accounts/keys [current-account-id] :as db} [_ _ {:keys [contact source value]}]]
(update db :qr-modal #(merge % {:contact (or contact (get-in db [:accounts/accounts current-account-id]))
[db [_ _ {:keys [contact source value]}]]
(update db :qr-modal #(merge % {:contact (or contact (get db :account/account))
:source source
:value value})))
(defmethod navigation/preload-data! :backup-seed
[db]
(assoc db :my-profile/seed {:step :intro}))
(assoc db :my-profile/seed {:step :intro}))

View File

@ -93,16 +93,17 @@
;; Handlers
(handlers/register-handler-fx
:update-wallet
(fn [{{:keys [web3 accounts/current-account-id network network-status] :as db} :db} _]
(fn [{{:keys [web3 account/account network network-status] :as db} :db} _]
(let [chain (ethereum/network->chain-keyword network)
symbols (get-in db [:accounts/accounts current-account-id :settings :wallet :visible-tokens chain])]
address (:address account)
symbols (get-in account [:settings :wallet :visible-tokens chain])]
(when (not= network-status :offline)
{:get-balance {:web3 web3
:account-id current-account-id
:account-id address
:success-event :update-balance-success
:error-event :update-balance-fail}
:get-tokens-balance {:web3 web3
:account-id current-account-id
:account-id address
:symbols symbols
:chain chain
:success-event :update-token-balance-success
@ -119,9 +120,9 @@
(handlers/register-handler-fx
:update-transactions
(fn [{{:keys [accounts/current-account-id network network-status] :as db} :db} _]
(fn [{{:keys [network network-status] :as db} :db} _]
(when (not= network-status :offline)
{:get-transactions {:account-id current-account-id
{:get-transactions {:account-id (get-in db [:account/account :address])
:network network
:success-event :update-transactions-success
:error-event :update-transactions-fail}

View File

@ -232,8 +232,7 @@
(handlers/register-handler-fx
:wallet/sign-transaction
(fn [{{:keys [web3]
:accounts/keys [accounts current-account-id] :as db} :db} [_ later?]]
(fn [{{:keys [web3] :as db} :db} [_ later?]]
(let [db' (assoc-in db [:wallet :send-transaction :wrong-password?] false)
network (:network db)
{:keys [amount id password to symbol gas gas-price]} (get-in db [:wallet :send-transaction])]
@ -247,7 +246,7 @@
:later? later?
:in-progress? true)
::send-transaction {:web3 web3
:from (get-in accounts [current-account-id :address])
:from (get-in db [:account/account :address])
:to to
:value amount
:gas gas

View File

@ -10,8 +10,8 @@
(handlers/register-handler-fx
:wallet.settings/toggle-visible-token
(fn [{{:keys [network] :accounts/keys [current-account-id] :as db} :db :as cofx} [_ symbol checked?]]
(fn [{{:keys [network account/account] :as db} :db :as cofx} [_ symbol checked?]]
(let [chain (ethereum/network->chain-keyword network)
settings (get-in db [:accounts/accounts current-account-id :settings])
settings (get account :settings)
new-settings (update-in settings [:wallet :visible-tokens chain] #(toggle-checked % symbol checked?))]
(accounts/update-settings new-settings cofx))))

View File

@ -91,10 +91,8 @@
(fn track-handler
[context]
(let [new-db (get-coeffect context :db)
current-account-id (:accounts/current-account-id new-db)]
(when (get-in new-db [:accounts/accounts
current-account-id
:sharing-usage-data?])
current-account-id (get-in new-db [:account/account :address])]
(when (get-in new-db [:account/account :sharing-usage-data?])
(let [event (get-coeffect context :event)
offline? (or (= :offline (:network-status new-db))
(= :offline (:sync-state new-db)))

View File

@ -21,8 +21,7 @@
(is (not fx))))
(testing "initialising console without existing account and console chat not initialisated"
(let [fresh-db {:chats {}
:accounts/current-account-id nil}
(let [fresh-db {:chats {}}
{:keys [db dispatch-n]} (chat-events/init-console-chat {:db fresh-db})]
(is (= (:current-chat-id db)
(:chat-id console-chat/chat)))
@ -30,8 +29,7 @@
const/console-chat-id))))
(testing "initialising console with existing account and console chat not initialisated"
(let [fresh-db {:chats {}
:accounts/current-account-id (:whisper-identity contact)}
(let [fresh-db {:chats {}}
{:keys [db dispatch-n]} (chat-events/init-console-chat {:db fresh-db})]
(is (= (:current-chat-id db)
(:chat-id console-chat/chat)))