Upgraded to re-frame 0.9.4

Simplify handlers chaining
Moved to reg-sub usage
Use reg-sub 3-arity to remove subscribe calls
This commit is contained in:
Julien Eluard 2017-07-02 01:21:07 +02:00 committed by Roman Volosovskyi
parent f846cb8a92
commit 6e5e6200ab
34 changed files with 577 additions and 637 deletions

View File

@ -6,7 +6,7 @@
:dependencies [[org.clojure/clojure "1.9.0-alpha13"] :dependencies [[org.clojure/clojure "1.9.0-alpha13"]
[org.clojure/clojurescript "1.9.671"] [org.clojure/clojurescript "1.9.671"]
[reagent "0.6.0" :exclusions [cljsjs/react cljsjs/react-dom cljsjs/react-dom-server]] [reagent "0.6.0" :exclusions [cljsjs/react cljsjs/react-dom cljsjs/react-dom-server]]
[re-frame "0.7.0"] [re-frame "0.9.4"]
[natal-shell "0.3.0"] [natal-shell "0.3.0"]
[com.andrewmcveigh/cljs-time "0.4.0"] [com.andrewmcveigh/cljs-time "0.4.0"]
[tailrecursion/cljs-priority-map "1.2.0"] [tailrecursion/cljs-priority-map "1.2.0"]

View File

@ -1,7 +1,7 @@
(ns status-im.accounts.handlers (ns status-im.accounts.handlers
(:require [status-im.data-store.accounts :as accounts-store] (:require [status-im.data-store.accounts :as accounts-store]
[status-im.data-store.processed-messages :as processed-messages] [status-im.data-store.processed-messages :as processed-messages]
[re-frame.core :refer [register-handler after dispatch dispatch-sync debug]] [re-frame.core :refer [reg-event-db after dispatch dispatch-sync debug]]
[taoensso.timbre :as log] [taoensso.timbre :as log]
[status-im.protocol.core :as protocol] [status-im.protocol.core :as protocol]
[status-im.components.status :as status] [status-im.components.status :as status]
@ -22,18 +22,20 @@
[status-im.protocol.message-cache :as cache] [status-im.protocol.message-cache :as cache]
[status-im.navigation.handlers :as nav])) [status-im.navigation.handlers :as nav]))
(defn save-account (defn save-account
[{:keys [network]} [{:keys [network]} [_ account]]
[_ account]]
(accounts-store/save (assoc account :network network) true)) (accounts-store/save (assoc account :network network) true))
(register-handler (defn update-account
[{:keys [network] :as db} [_ {:keys [address] :as account}]]
(let [account' (assoc account :network network)]
(update db :accounts assoc address account')))
(reg-event-db
:add-account :add-account
((after save-account) (u/handlers->
(fn [{:keys [network] :as db} [_ {:keys [address] :as account}]] update-account
(let [account' (assoc account :network network)] save-account ))
(update db :accounts assoc address account')))))
(defn account-created [result password] (defn account-created [result password]
(let [data (json->clj result) (let [data (json->clj result)
@ -55,7 +57,7 @@
(dispatch [:add-account account]) (dispatch [:add-account account])
(dispatch [:login-account address password true])))) (dispatch [:login-account address password true]))))
(register-handler :create-account (reg-event-db :create-account
(u/side-effect! (u/side-effect!
(fn [_ [_ password]] (fn [_ [_ password]]
(dispatch [:set :creating-account? true]) (dispatch [:set :creating-account? true])
@ -100,7 +102,7 @@
:payload {:keypair {:public updates-public-key :payload {:keypair {:public updates-public-key
:private updates-private-key}}}})))) :private updates-private-key}}}}))))
(register-handler (reg-event-db
:check-status-change :check-status-change
(u/side-effect! (u/side-effect!
(fn [{:keys [current-account-id accounts]} [_ status]] (fn [{:keys [current-account-id accounts]} [_ status]]
@ -118,23 +120,27 @@
account (merge (get accounts current-account-id) data)] account (merge (get accounts current-account-id) data)]
(assoc-in db [:accounts current-account-id] account))) (assoc-in db [:accounts current-account-id] account)))
(register-handler (defn account-update-keys
[db _]
(let [{:keys [public private]} (protocol/new-keypair!)]
(account-update db {:updates-public-key public
:updates-private-key private})))
(reg-event-db
:account-update :account-update
(-> (fn [db [_ data]] (u/handlers->
(account-update db data)) account-update
((after save-account!)) save-account!
((after broadcast-account-update)))) broadcast-account-update))
(register-handler (reg-event-db
:account-update-keys :account-update-keys
(-> (fn [db] (u/handlers->
(let [{:keys [public private]} (protocol/new-keypair!)] save-account!
(account-update db {:updates-public-key public send-keys-update
:updates-private-key private}))) account-update-keys))
((after save-account!))
((after send-keys-update))))
(register-handler (reg-event-db
:send-account-update-if-needed :send-account-update-if-needed
(u/side-effect! (u/side-effect!
(fn [{:keys [current-account-id accounts]} _] (fn [{:keys [current-account-id accounts]} _]
@ -151,7 +157,7 @@
(assoc db :current-account-id address (assoc db :current-account-id address
:current-public-key key))) :current-public-key key)))
(register-handler :set-current-account set-current-account) (reg-event-db :set-current-account set-current-account)
(defn load-accounts! [db _] (defn load-accounts! [db _]
(let [accounts (->> (accounts-store/get-all) (let [accounts (->> (accounts-store/get-all)
@ -165,7 +171,7 @@
:view-id view :view-id view
:navigation-stack (list view)))) :navigation-stack (list view))))
(register-handler :load-accounts load-accounts!) (reg-event-db :load-accounts load-accounts!)
(defn console-create-account [db _] (defn console-create-account [db _]
(let [message-id (random/id)] (let [message-id (random/id)]
@ -179,9 +185,9 @@
:to "me"}]) :to "me"}])
db)) db))
(register-handler :console-create-account console-create-account) (reg-event-db :console-create-account console-create-account)
(register-handler (reg-event-db
:load-processed-messages :load-processed-messages
(u/side-effect! (u/side-effect!
(fn [_] (fn [_]

View File

@ -18,8 +18,7 @@
(register-handler :set-login-from-qr set-login-from-qr) (register-handler :set-login-from-qr set-login-from-qr)
(register-handler (register-handler :open-login
:open-login
(after #(dispatch [:navigate-to :login])) (after #(dispatch [:navigate-to :login]))
(fn [db [_ address photo-path name]] (fn [db [_ address photo-path name]]
(update db :login assoc :address address :photo-path photo-path :name name))) (update db :login assoc :address address :photo-path photo-path :name name)))
@ -38,8 +37,7 @@
(dispatch [:navigate-to-clean :chat-list]) (dispatch [:navigate-to-clean :chat-list])
(dispatch [:navigate-to :chat-list])))) (dispatch [:navigate-to :chat-list]))))
(register-handler (register-handler :change-account
:change-account
(u/side-effect! (u/side-effect!
(fn [_ [_ address new-account? callback]] (fn [_ [_ address new-account? callback]]
(status/clear-web-data) (status/clear-web-data)
@ -59,8 +57,7 @@
(log/debug "Logged in: " (:view-id db) is-login-screen? new-account?) (log/debug "Logged in: " (:view-id db) is-login-screen? new-account?)
(dispatch [:change-account address new-account? on-account-changed]))) (dispatch [:change-account address new-account? on-account-changed])))
(register-handler (register-handler :login-account
:login-account
(after (after
(fn [db [_ address password]] (fn [db [_ address password]]
(status/login address password (status/login address password

View File

@ -1,5 +1,5 @@
(ns status-im.accounts.recover.handlers (ns status-im.accounts.recover.handlers
(:require [re-frame.core :refer [register-handler after dispatch dispatch-sync]] (:require [re-frame.core :refer [reg-event-db after dispatch dispatch-sync]]
[status-im.components.status :as status] [status-im.components.status :as status]
[status-im.utils.types :refer [json->clj]] [status-im.utils.types :refer [json->clj]]
[status-im.utils.identicon :refer [identicon]] [status-im.utils.identicon :refer [identicon]]
@ -37,7 +37,7 @@
password password
account-recovered)) account-recovered))
(register-handler :recover-account (u/side-effect! recover-account)) (reg-event-db :recover-account (u/side-effect! recover-account))
(defmethod nav/preload-data! :recover (defmethod nav/preload-data! :recover
[db] [db]

View File

@ -37,15 +37,13 @@
:path [name] :path [name]
:result %}])))}))))) :result %}])))})))))
(u/register-handler (u/register-handler :set-in-bot-db
:set-in-bot-db
(re-frame/after check-subscriptions) (re-frame/after check-subscriptions)
(fn [{:keys [current-chat-id] :as db} [_ {:keys [bot path value]}]] (fn [{:keys [current-chat-id] :as db} [_ {:keys [bot path value]}]]
(let [bot (or bot current-chat-id)] (let [bot (or bot current-chat-id)]
(assoc-in db (concat [:bot-db bot] path) value)))) (assoc-in db (concat [:bot-db bot] path) value))))
(u/register-handler (u/register-handler :register-bot-subscription
:register-bot-subscription
(fn [db [_ {:keys [bot subscriptions] :as opts}]] (fn [db [_ {:keys [bot subscriptions] :as opts}]]
(reduce (reduce
(fn [db [sub-name sub-path]] (fn [db [sub-name sub-path]]
@ -56,8 +54,7 @@
db db
subscriptions))) subscriptions)))
(u/register-handler (u/register-handler ::calculated-subscription
::calculated-subscription
(u/side-effect! (u/side-effect!
(fn [_ [_ {:keys [bot path] (fn [_ [_ {:keys [bot path]
{:keys [error result]} :result {:keys [error result]} :result
@ -69,14 +66,12 @@
:value returned}] :value returned}]
(re-frame/dispatch [:set-in-bot-db opts])))))) (re-frame/dispatch [:set-in-bot-db opts]))))))
(u/register-handler (u/register-handler :update-bot-db
:update-bot-db
(fn [{:keys [current-chat-id] :as app-db} [_ {:keys [bot db]}]] (fn [{:keys [current-chat-id] :as app-db} [_ {:keys [bot db]}]]
(let [bot (or bot current-chat-id)] (let [bot (or bot current-chat-id)]
(update-in app-db [:bot-db bot] merge db)))) (update-in app-db [:bot-db bot] merge db))))
(u/register-handler (u/register-handler :clear-bot-db
:clear-bot-db
(fn [{:keys [current-chat-id] :as app-db} [_ {:keys [bot]}]] (fn [{:keys [current-chat-id] :as app-db} [_ {:keys [bot]}]]
(let [bot (or bot current-chat-id)] (let [bot (or bot current-chat-id)]
(assoc-in app-db [:bot-db bot] nil)))) (assoc-in app-db [:bot-db bot] nil))))

View File

@ -1,15 +1,14 @@
(ns status-im.bots.subs (ns status-im.bots.subs
(:require-macros [reagent.ratom :refer [reaction]]) (:require [re-frame.core :refer [reg-sub subscribe]]))
(:require [re-frame.core :as re-frame]))
(re-frame/register-sub (reg-sub
:bot-subscription :bot-subscription
(fn [db [_ path]] (fn [db [_ path]]
(let [chat-id (re-frame/subscribe [:get-current-chat-id])] (let [chat-id (subscribe [:get-current-chat-id])]
(reaction (get-in @db (concat [:bot-db @chat-id] path)))))) (get-in db (concat [:bot-db @chat-id] path)))))
(re-frame/register-sub (reg-sub
:current-bot-db :current-bot-db
(fn [db] (fn [db]
(let [chat-id (re-frame/subscribe [:get-current-chat-id])] (let [chat-id (subscribe [:get-current-chat-id])]
(reaction (get-in @db [:bot-db @chat-id]))))) (get-in db [:bot-db @chat-id]))))

View File

@ -204,14 +204,16 @@
(dissoc :messages)))) (dissoc :messages))))
(defn load-commands! (defn load-commands!
[{:keys [current-chat-id]}] [{:keys [current-chat-id]} _]
(dispatch [:load-commands! current-chat-id])) (dispatch [:load-commands! current-chat-id]))
(register-handler :init-chat (register-handler :init-chat
(after #(dispatch [:load-requests!])) (after #(dispatch [:load-requests!]))
(-> load-messages!
((enrich init-chat)) (u/handlers->
((after load-commands!)))) load-messages!
init-chat
load-commands!))
(defn compare-chats (defn compare-chats
[{timesatmp1 :timestamp} {timestamp2 :timestamp}] [{timesatmp1 :timestamp} {timestamp2 :timestamp}]
@ -241,7 +243,9 @@
(register-handler :initialize-chats (register-handler :initialize-chats
[(after #(dispatch [:load-unviewed-messages!])) [(after #(dispatch [:load-unviewed-messages!]))
(after #(dispatch [:load-default-contacts!]))] (after #(dispatch [:load-default-contacts!]))]
((enrich initialize-chats) load-chats!)) (u/handlers->
initialize-chats
load-chats!))
(register-handler :reload-chats (register-handler :reload-chats
(fn [{:keys [chats] :as db} _] (fn [{:keys [chats] :as db} _]
@ -335,10 +339,11 @@
(dispatch [(or navigation-type :navigate-to) :chat chat-id])) (dispatch [(or navigation-type :navigate-to) :chat chat-id]))
(register-handler ::start-chat! (register-handler ::start-chat!
(-> add-new-chat (u/handlers->
((enrich add-chat)) add-new-chat
((after save-new-chat!)) add-chat
((after open-chat!)))) save-new-chat!
open-chat!))
(register-handler :start-chat (register-handler :start-chat
(u/side-effect! (u/side-effect!
@ -350,9 +355,10 @@
(dispatch [::start-chat! contact-id options navigation-type])))))) (dispatch [::start-chat! contact-id options navigation-type]))))))
(register-handler :add-chat (register-handler :add-chat
(-> add-new-chat (u/handlers->
((enrich add-chat)) add-new-chat
((after save-new-chat!)))) add-chat
save-new-chat!))
(defn update-chat! (defn update-chat!
[_ [_ {:keys [name] :as chat}]] [_ [_ {:keys [name] :as chat}]]
@ -417,10 +423,11 @@
(dispatch [:remove-chat current-chat-id])))) (dispatch [:remove-chat current-chat-id]))))
(register-handler :remove-chat (register-handler :remove-chat
(-> remove-chat (u/handlers->
((after delete-messages!)) remove-chat
((after remove-pending-messages!)) delete-messages!
((after delete-chat!)))) remove-pending-messages!
delete-chat!))
(defn send-seen! (defn send-seen!
[{:keys [web3 current-public-key chats contacts]} [{:keys [web3 current-public-key chats contacts]}

View File

@ -5,21 +5,18 @@
[status-im.utils.platform :as platform] [status-im.utils.platform :as platform]
[taoensso.timbre :as log])) [taoensso.timbre :as log]))
(handlers/register-handler (handlers/register-handler :set-expandable-height
:set-expandable-height
(fn [{:keys [current-chat-id] :as db} [_ key value]] (fn [{:keys [current-chat-id] :as db} [_ key value]]
(-> db (-> db
(assoc-in [:chat-animations current-chat-id key :height] value) (assoc-in [:chat-animations current-chat-id key :height] value)
(update-in [:chat-animations current-chat-id key :changes-counter] inc)))) (update-in [:chat-animations current-chat-id key :changes-counter] inc))))
(handlers/register-handler (handlers/register-handler :hide-expandable
:hide-expandable
(handlers/side-effect! (handlers/side-effect!
(fn [_ [_ key]] (fn [_ [_ key]]
(dispatch [:set-expandable-height key 1])))) (dispatch [:set-expandable-height key 1]))))
(handlers/register-handler (handlers/register-handler :choose-predefined-expandable-height
:choose-predefined-expandable-height
(handlers/side-effect! (handlers/side-effect!
(fn [{:keys [current-chat-id chat-ui-props layout-height] :as db} [_ key preset]] (fn [{:keys [current-chat-id chat-ui-props layout-height] :as db} [_ key preset]]
(if (= preset :max) (if (= preset :max)
@ -34,8 +31,7 @@
(input-utils/default-container-area-height bottom layout-height))] (input-utils/default-container-area-height bottom layout-height))]
(dispatch [:set-expandable-height key height])))))) (dispatch [:set-expandable-height key height]))))))
(handlers/register-handler (handlers/register-handler :fix-expandable-height
:fix-expandable-height
(handlers/side-effect! (handlers/side-effect!
(fn [{:keys [current-chat-id chats chat-ui-props layout-height] :as db} [_ vy current key]] (fn [{:keys [current-chat-id chats chat-ui-props layout-height] :as db} [_ vy current key]]
(let [input-height (get-in chat-ui-props [current-chat-id :input-height]) (let [input-height (get-in chat-ui-props [current-chat-id :input-height])

View File

@ -41,10 +41,9 @@
#(received-message (label :t/faucet-success)) #(received-message (label :t/faucet-success))
#(received-message (label :t/faucet-error))))) #(received-message (label :t/faucet-error)))))
(register-handler (register-handler :open-faucet
:open-faucet (u/side-effect!
(u/side-effect! (fn [{:keys [accounts current-account-id]} [_ faucet-name _]]
(fn [{:keys [accounts current-account-id]} [_ faucet-name _]] (if-let [faucet (faucet-by-name faucet-name)]
(if-let [faucet (faucet-by-name faucet-name)] (let [current-address (get-in accounts [current-account-id :address])]
(let [current-address (get-in accounts [current-account-id :address])] (open-faucet faucet-name current-address faucet))))))
(open-faucet faucet-name current-address faucet))))))

View File

@ -13,8 +13,7 @@
[status-im.i18n :as i18n] [status-im.i18n :as i18n]
[clojure.string :as str])) [clojure.string :as str]))
(handlers/register-handler (handlers/register-handler :set-chat-input-text
:set-chat-input-text
(fn [{:keys [current-chat-id chats chat-ui-props] :as db} [_ text chat-id]] (fn [{:keys [current-chat-id chats chat-ui-props] :as db} [_ text chat-id]]
(let [chat-id (or chat-id current-chat-id) (let [chat-id (or chat-id current-chat-id)
ends-with-space? (input-model/text-ends-with-space? text)] ends-with-space? (input-model/text-ends-with-space? text)]
@ -34,15 +33,13 @@
(input-model/text->emoji) (input-model/text->emoji)
(assoc-in db [:chats chat-id :input-text])))))) (assoc-in db [:chats chat-id :input-text]))))))
(handlers/register-handler (handlers/register-handler :add-to-chat-input-text
:add-to-chat-input-text
(handlers/side-effect! (handlers/side-effect!
(fn [{:keys [chats current-chat-id]} [_ text-to-add]] (fn [{:keys [chats current-chat-id]} [_ text-to-add]]
(let [input-text (get-in chats [current-chat-id :input-text])] (let [input-text (get-in chats [current-chat-id :input-text])]
(dispatch [:set-chat-input-text (str input-text text-to-add)]))))) (dispatch [:set-chat-input-text (str input-text text-to-add)])))))
(handlers/register-handler (handlers/register-handler :select-chat-input-command
:select-chat-input-command
(handlers/side-effect! (handlers/side-effect!
(fn [{:keys [current-chat-id chat-ui-props] :as db} (fn [{:keys [current-chat-id chat-ui-props] :as db}
[_ {:keys [prefill prefill-bot-db sequential-params name] :as command} metadata prevent-auto-focus?]] [_ {:keys [prefill prefill-bot-db sequential-params name] :as command} metadata prevent-auto-focus?]]
@ -69,14 +66,12 @@
(when-not prevent-auto-focus? (when-not prevent-auto-focus?
(dispatch [:chat-input-focus :input-ref])))))) (dispatch [:chat-input-focus :input-ref]))))))
(handlers/register-handler (handlers/register-handler :set-chat-input-metadata
:set-chat-input-metadata
(fn [{:keys [current-chat-id] :as db} [_ data chat-id]] (fn [{:keys [current-chat-id] :as db} [_ data chat-id]]
(let [chat-id (or chat-id current-chat-id)] (let [chat-id (or chat-id current-chat-id)]
(assoc-in db [:chats chat-id :input-metadata] data)))) (assoc-in db [:chats chat-id :input-metadata] data))))
(handlers/register-handler (handlers/register-handler :set-command-argument
:set-command-argument
(handlers/side-effect! (handlers/side-effect!
(fn [{:keys [current-chat-id] :as db} [_ [index arg move-to-next?]]] (fn [{:keys [current-chat-id] :as db} [_ [index arg move-to-next?]]]
(let [command (-> (get-in db [:chats current-chat-id :input-text]) (let [command (-> (get-in db [:chats current-chat-id :input-text])
@ -98,8 +93,7 @@
(= index (dec (count command-args)))) (= index (dec (count command-args))))
const/spacing-char))]))))))) const/spacing-char))])))))))
(handlers/register-handler (handlers/register-handler :chat-input-focus
:chat-input-focus
(handlers/side-effect! (handlers/side-effect!
(fn [{:keys [current-chat-id chat-ui-props] :as db} [_ ref]] (fn [{:keys [current-chat-id chat-ui-props] :as db} [_ ref]]
(try (try
@ -108,8 +102,7 @@
(catch :default e (catch :default e
(log/debug "Cannot focus the reference")))))) (log/debug "Cannot focus the reference"))))))
(handlers/register-handler (handlers/register-handler :chat-input-blur
:chat-input-blur
(handlers/side-effect! (handlers/side-effect!
(fn [{:keys [current-chat-id chat-ui-props] :as db} [_ ref]] (fn [{:keys [current-chat-id chat-ui-props] :as db} [_ ref]]
(try (try
@ -118,8 +111,7 @@
(catch :default e (catch :default e
(log/debug "Cannot blur the reference")))))) (log/debug "Cannot blur the reference"))))))
(handlers/register-handler (handlers/register-handler :update-suggestions
:update-suggestions
(fn [{:keys [current-chat-id] :as db} [_ chat-id text]] (fn [{:keys [current-chat-id] :as db} [_ chat-id text]]
(let [chat-id (or chat-id current-chat-id) (let [chat-id (or chat-id current-chat-id)
chat-text (str/trim (or text (get-in db [:chats chat-id :input-text]) "")) chat-text (str/trim (or text (get-in db [:chats chat-id :input-text]) ""))
@ -139,8 +131,7 @@
(assoc-in [:chats chat-id :request-suggestions] requests) (assoc-in [:chats chat-id :request-suggestions] requests)
(assoc-in [:chats chat-id :command-suggestions] all-commands))))) (assoc-in [:chats chat-id :command-suggestions] all-commands)))))
(handlers/register-handler (handlers/register-handler :load-chat-parameter-box
:load-chat-parameter-box
(handlers/side-effect! (handlers/side-effect!
(fn [{:keys [current-chat-id bot-db current-account-id] :as db} (fn [{:keys [current-chat-id bot-db current-account-id] :as db}
[_ {:keys [name type bot owner-id] :as command}]] [_ {:keys [name type bot owner-id] :as command}]]
@ -175,8 +166,7 @@
:parameter-index parameter-index} :parameter-index parameter-index}
%])}))))))) %])})))))))
(handlers/register-handler (handlers/register-handler ::send-message
::send-message
(handlers/side-effect! (handlers/side-effect!
(fn [{:keys [current-public-key current-account-id] :as db} [_ command-message chat-id]] (fn [{:keys [current-public-key current-account-id] :as db} [_ command-message chat-id]]
(let [text (get-in db [:chats chat-id :input-text]) (let [text (get-in db [:chats chat-id :input-text])
@ -194,8 +184,7 @@
(not (str/blank? text)) (not (str/blank? text))
(dispatch [:prepare-message data])))))) (dispatch [:prepare-message data]))))))
(handlers/register-handler (handlers/register-handler :proceed-command
:proceed-command
(handlers/side-effect! (handlers/side-effect!
(fn [db [_ command chat-id]] (fn [db [_ command chat-id]]
(let [jail-id (or (get-in command [:command :bot]) chat-id)] (let [jail-id (or (get-in command [:command :bot]) chat-id)]
@ -222,8 +211,7 @@
(dispatch [::request-command-data validation-params])))))) (dispatch [::request-command-data validation-params]))))))
(handlers/register-handler (handlers/register-handler ::proceed-validation-messages
::proceed-validation-messages
(handlers/side-effect! (handlers/side-effect!
(fn [db [_ command chat-id {:keys [markup validationHandler parameters]} proceed-fn]] (fn [db [_ command chat-id {:keys [markup validationHandler parameters]} proceed-fn]]
(let [set-errors #(do (dispatch [:set-chat-ui-props {:validation-messages % (let [set-errors #(do (dispatch [:set-chat-ui-props {:validation-messages %
@ -239,15 +227,13 @@
:default :default
(proceed-fn)))))) (proceed-fn))))))
(handlers/register-handler (handlers/register-handler ::execute-validation-handler
::execute-validation-handler
(handlers/side-effect! (handlers/side-effect!
(fn [_ [_ name params set-errors proceed]] (fn [_ [_ name params set-errors proceed]]
(when-let [validator (input-model/validation-handler name)] (when-let [validator (input-model/validation-handler name)]
(validator params set-errors proceed))))) (validator params set-errors proceed)))))
(handlers/register-handler (handlers/register-handler ::send-command
::send-command
(handlers/side-effect! (handlers/side-effect!
(fn [db [_ on-send {{:keys [fullscreen]} :command :as command} chat-id]] (fn [db [_ on-send {{:keys [fullscreen]} :command :as command} chat-id]]
(if on-send (if on-send
@ -264,8 +250,7 @@
:data-type :preview :data-type :preview
:after #(dispatch [::send-message % chat-id])}]))))) :after #(dispatch [::send-message % chat-id])}])))))
(handlers/register-handler (handlers/register-handler ::request-command-data
::request-command-data
(handlers/side-effect! (handlers/side-effect!
(fn [{:keys [contacts bot-db] :as db} (fn [{:keys [contacts bot-db] :as db}
[_ {{:keys [command [_ {{:keys [command
@ -298,8 +283,7 @@
:on-requested #(after command-message %)}] :on-requested #(after command-message %)}]
(dispatch [:request-command-data request-data data-type]))))) (dispatch [:request-command-data request-data data-type])))))
(handlers/register-handler (handlers/register-handler :send-current-message
:send-current-message
(handlers/side-effect! (handlers/side-effect!
(fn [{:keys [current-chat-id] :as db} [_ chat-id]] (fn [{:keys [current-chat-id] :as db} [_ chat-id]]
(dispatch [:set-chat-ui-props {:sending-in-progress? true}]) (dispatch [:set-chat-ui-props {:sending-in-progress? true}])
@ -321,8 +305,7 @@
(dispatch [:set-chat-input-text (str text const/spacing-char)])))) (dispatch [:set-chat-input-text (str text const/spacing-char)]))))
(dispatch [::send-message nil chat-id])))))) (dispatch [::send-message nil chat-id]))))))
(handlers/register-handler (handlers/register-handler ::check-dapp-suggestions
::check-dapp-suggestions
(handlers/side-effect! (handlers/side-effect!
(fn [{:keys [current-account-id] :as db} [_ chat-id text]] (fn [{:keys [current-account-id] :as db} [_ chat-id text]]
(let [data (get-in db [:local-storage chat-id])] (let [data (get-in db [:local-storage chat-id])]
@ -333,16 +316,14 @@
:context {:data data :context {:data data
:from current-account-id}}))))) :from current-account-id}})))))
(handlers/register-handler (handlers/register-handler :clear-seq-arguments
:clear-seq-arguments
(fn [{:keys [current-chat-id chats] :as db} [_ chat-id]] (fn [{:keys [current-chat-id chats] :as db} [_ chat-id]]
(let [chat-id (or chat-id current-chat-id)] (let [chat-id (or chat-id current-chat-id)]
(-> db (-> db
(assoc-in [:chats chat-id :seq-arguments] []) (assoc-in [:chats chat-id :seq-arguments] [])
(assoc-in [:chats chat-id :seq-argument-input-text] nil))))) (assoc-in [:chats chat-id :seq-argument-input-text] nil)))))
(handlers/register-handler (handlers/register-handler :update-seq-arguments
:update-seq-arguments
(fn [{:keys [current-chat-id chats] :as db} [_ chat-id]] (fn [{:keys [current-chat-id chats] :as db} [_ chat-id]]
(let [chat-id (or chat-id current-chat-id) (let [chat-id (or chat-id current-chat-id)
text (get-in chats [chat-id :seq-argument-input-text])] text (get-in chats [chat-id :seq-argument-input-text])]
@ -350,8 +331,7 @@
(update-in [:chats chat-id :seq-arguments] #(into [] (conj % text))) (update-in [:chats chat-id :seq-arguments] #(into [] (conj % text)))
(assoc-in [:chats chat-id :seq-argument-input-text] nil))))) (assoc-in [:chats chat-id :seq-argument-input-text] nil)))))
(handlers/register-handler (handlers/register-handler :send-seq-argument
:send-seq-argument
(handlers/side-effect! (handlers/side-effect!
(fn [{:keys [current-chat-id chats] :as db} [_ chat-id]] (fn [{:keys [current-chat-id chats] :as db} [_ chat-id]]
(let [chat-id (or chat-id current-chat-id) (let [chat-id (or chat-id current-chat-id)
@ -370,14 +350,12 @@
:after #(dispatch [::proceed-validation-messages :after #(dispatch [::proceed-validation-messages
command chat-id %2 after-validation])}]))))) command chat-id %2 after-validation])}])))))
(handlers/register-handler (handlers/register-handler :set-chat-seq-arg-input-text
:set-chat-seq-arg-input-text
(fn [{:keys [current-chat-id] :as db} [_ text chat-id]] (fn [{:keys [current-chat-id] :as db} [_ text chat-id]]
(let [chat-id (or chat-id current-chat-id)] (let [chat-id (or chat-id current-chat-id)]
(assoc-in db [:chats chat-id :seq-argument-input-text] text)))) (assoc-in db [:chats chat-id :seq-argument-input-text] text))))
(handlers/register-handler (handlers/register-handler :update-text-selection
:update-text-selection
(handlers/side-effect! (handlers/side-effect!
(fn [{:keys [current-chat-id] :as db} [_ selection]] (fn [{:keys [current-chat-id] :as db} [_ selection]]
(let [input-text (get-in db [:chats current-chat-id :input-text]) (let [input-text (get-in db [:chats current-chat-id :input-text])
@ -390,8 +368,7 @@
(dispatch [:set-chat-ui-props {:selection selection}]) (dispatch [:set-chat-ui-props {:selection selection}])
(dispatch [:load-chat-parameter-box (:command command)]))))) (dispatch [:load-chat-parameter-box (:command command)])))))
(handlers/register-handler (handlers/register-handler :select-prev-argument
:select-prev-argument
(handlers/side-effect! (handlers/side-effect!
(fn [{:keys [chat-ui-props current-chat-id] :as db} _] (fn [{:keys [chat-ui-props current-chat-id] :as db} _]
(let [input-text (get-in db [:chats current-chat-id :input-text]) (let [input-text (get-in db [:chats current-chat-id :input-text])
@ -412,8 +389,7 @@
(.setNativeProps ref (clj->js {:selection {:start new-sel :end new-sel}})) (.setNativeProps ref (clj->js {:selection {:start new-sel :end new-sel}}))
(dispatch [:update-text-selection new-sel]))))))))) (dispatch [:update-text-selection new-sel])))))))))
(handlers/register-handler (handlers/register-handler :select-next-argument
:select-next-argument
(handlers/side-effect! (handlers/side-effect!
(fn [{:keys [chat-ui-props current-chat-id] :as db} _] (fn [{:keys [chat-ui-props current-chat-id] :as db} _]
(let [arg-pos (input-model/argument-position db current-chat-id)] (let [arg-pos (input-model/argument-position db current-chat-id)]

View File

@ -1,6 +1,5 @@
(ns status-im.chat.subs (ns status-im.chat.subs
(:require-macros [reagent.ratom :refer [reaction]]) (:require [re-frame.core :refer [reg-sub dispatch subscribe path]]
(:require [re-frame.core :refer [register-sub dispatch subscribe path]]
[status-im.data-store.chats :as chats] [status-im.data-store.chats :as chats]
[status-im.chat.constants :as const] [status-im.chat.constants :as const]
[status-im.chat.models.input :as input-model] [status-im.chat.models.input :as input-model]
@ -14,231 +13,213 @@
[taoensso.timbre :as log] [taoensso.timbre :as log]
[clojure.string :as str])) [clojure.string :as str]))
(register-sub (reg-sub
:chat-properties :chat-properties
(fn [db [_ properties]] (fn [db [_ properties]]
(->> properties (->> properties
(map (fn [k] (map (fn [k]
[k (-> @db [k (get-in db[:chats (:current-chat-id db) k])
(get-in [:chats (:current-chat-id @db) k]) ]))
(reaction))])) (into {}))))
(into {}))))
(register-sub (reg-sub
:chat-ui-props :chat-ui-props
(fn [db [_ ui-element chat-id]] (fn [db [_ ui-element chat-id]]
(let [current-chat-id (subscribe [:get-current-chat-id])] (let [current-chat-id (subscribe [:get-current-chat-id])]
(reaction (get-in @db [:chat-ui-props (or chat-id @current-chat-id) ui-element]))))) (get-in db [:chat-ui-props (or chat-id @current-chat-id) ui-element]))))
(register-sub (reg-sub
:chat-input-margin :chat-input-margin
(fn [] :<- [:get :keyboard-height]
(let [kb-height (subscribe [:get :keyboard-height])] (fn [kb-height]
(reaction (if ios? kb-height 0)))
(if ios? @kb-height 0)))))
(register-sub (reg-sub
:chat :chat
(fn [db [_ k chat-id]] (fn [db [_ k chat-id]]
(-> @db (get-in db [:chats (or chat-id (:current-chat-id db)) k])))
(get-in [:chats (or chat-id (:current-chat-id @db)) k])
(reaction))))
(register-sub (reg-sub
:get-current-chat-id :get-current-chat-id
(fn [db _] (fn [db ]
(reaction (:current-chat-id @db)))) (:current-chat-id db)))
(register-sub (reg-sub
:get-chat-by-id :get-chat-by-id
(fn [_ [_ chat-id]] (fn [_ [_ chat-id]]
(reaction (chats/get-by-id chat-id)))) (chats/get-by-id chat-id)))
(register-sub :get-bots-suggestions (reg-sub :get-bots-suggestions
(fn [db _] (fn [db]
(let [chat-id (subscribe [:get-current-chat-id])] (let [chat-id (subscribe [:get-current-chat-id])]
(reaction (get-in @db [:bots-suggestions @chat-id]))))) (get-in db [:bots-suggestions @chat-id]))))
(register-sub :get-commands (reg-sub :get-commands
(fn [db [_ chat-id]] (fn [db [_ chat-id]]
(let [current-chat (or chat-id (@db :current-chat-id))] (let [current-chat (or chat-id (db :current-chat-id))]
(reaction (or (get-in @db [:contacts current-chat :commands]) {}))))) (or (get-in db [:contacts current-chat :commands]) {}))))
(register-sub (reg-sub
:get-responses :get-responses
(fn [db [_ chat-id]] (fn [db [_ chat-id]]
(let [current-chat (or chat-id (@db :current-chat-id))] (let [current-chat (or chat-id (db :current-chat-id))]
(reaction (or (get-in @db [:contacts current-chat :responses]) {}))))) (or (get-in db [:contacts current-chat :responses]) {}))))
(register-sub (reg-sub :get-commands-and-responses
:get-commands-and-responses
(fn [db [_ chat-id]] (fn [db [_ chat-id]]
(reaction (let [{:keys [chats contacts]} db]
(let [{:keys [chats contacts]} @db] (->> (get-in chats [chat-id :contacts])
(->> (get-in chats [chat-id :contacts]) (filter :is-in-chat)
(filter :is-in-chat) (mapv (fn [{:keys [identity]}]
(mapv (fn [{:keys [identity]}] (let [{:keys [commands responses]} (get contacts identity)]
(let [{:keys [commands responses]} (get contacts identity)] (merge responses commands))))
(merge responses commands)))) (apply merge)))))
(apply merge))))))
(register-sub (reg-sub :possible-chat-actions
:possible-chat-actions
(fn [db [_ chat-id]] (fn [db [_ chat-id]]
"Returns a vector of [command message-id] values. `message-id` can be `:any`. "Returns a vector of [command message-id] values. `message-id` can be `:any`.
Example: [[browse-command :any] [debug-command :any] [phone-command '1489161286111-58a2cd...']]" Example: [[browse-command :any] [debug-command :any] [phone-command '1489161286111-58a2cd...']]"
(let [chat-id (or chat-id (@db :current-chat-id))] (let [chat-id (or chat-id (db :current-chat-id))]
(reaction (input-model/possible-chat-actions db chat-id))))
(input-model/possible-chat-actions @db chat-id)))))
(register-sub (reg-sub
:selected-chat-command :selected-chat-command
(fn [db [_ chat-id]] (fn [db [_ chat-id]]
(let [current-chat-id (subscribe [:get :current-chat-id]) (let [current-chat-id (subscribe [:get :current-chat-id])
input-text (subscribe [:chat :input-text])] input-text (subscribe [:chat :input-text])]
(reaction (input-model/selected-chat-command db (or chat-id @current-chat-id) @input-text))))
(input-model/selected-chat-command @db (or chat-id @current-chat-id) @input-text)))))
(register-sub (reg-sub
:current-chat-argument-position :current-chat-argument-position
(fn [db] (fn [db]
(let [command (subscribe [:selected-chat-command]) (let [command (subscribe [:selected-chat-command])
input-text (subscribe [:chat :input-text]) input-text (subscribe [:chat :input-text])
seq-arguments (subscribe [:chat :seq-arguments]) seq-arguments (subscribe [:chat :seq-arguments])
selection (subscribe [:chat-ui-props :selection])] selection (subscribe [:chat-ui-props :selection])]
(reaction (input-model/current-chat-argument-position @command @input-text @selection @seq-arguments))))
(input-model/current-chat-argument-position @command @input-text @selection @seq-arguments)))))
(register-sub (reg-sub
:chat-parameter-box :chat-parameter-box
(fn [db] (fn [db]
(let [chat-id (subscribe [:get-current-chat-id]) (let [chat-id (subscribe [:get-current-chat-id])
command (subscribe [:selected-chat-command]) command (subscribe [:selected-chat-command])
index (subscribe [:current-chat-argument-position])] index (subscribe [:current-chat-argument-position])]
(reaction (cond
(cond (and @command (not= @index input-model/*no-argument-error*))
(and @command (not= @index input-model/*no-argument-error*)) (let [command-name (get-in @command [:command :name])]
(let [command-name (get-in @command [:command :name])] (get-in db [:chats @chat-id :parameter-boxes command-name @index]))
(get-in @db [:chats @chat-id :parameter-boxes command-name @index]))
(not @command) (not @command)
(get-in @db [:chats @chat-id :parameter-boxes :message]) (get-in db [:chats @chat-id :parameter-boxes :message])
:default :default
nil))))) nil))))
(register-sub (reg-sub
:show-parameter-box? :show-parameter-box?
(fn [db _] :<- [:chat-parameter-box]
(let [chat-parameter-box (subscribe [:chat-parameter-box]) :<- [:show-suggestions?]
show-suggestions? (subscribe [:show-suggestions?]) :<- [:chat :input-text]
input-text (subscribe [:chat :input-text]) :<- [:chat-ui-props :validation-messages]
validation-messages (subscribe [:chat-ui-props :validation-messages])] (fn [[chat-parameter-box show-suggestions? input-text validation-messages]]
(reaction (and @chat-parameter-box (and chat-parameter-box
(not @validation-messages) (not validation-messages)
(not @show-suggestions?)))))) (not show-suggestions?))))
(register-sub (reg-sub
:command-completion :command-completion
(fn [db [_ chat-id]] (fn [db [_ chat-id]]
(reaction (input-model/command-completion db chat-id)))
(input-model/command-completion @db chat-id))))
(register-sub (reg-sub
:show-suggestions? :show-suggestions?
(fn [db [_ chat-id]] (fn [db [_ chat-id]]
(let [chat-id (or chat-id (@db :current-chat-id)) (let [chat-id (or chat-id (db :current-chat-id))
show-suggestions? (subscribe [:chat-ui-props :show-suggestions? chat-id]) show-suggestions? (subscribe [:chat-ui-props :show-suggestions? chat-id])
input-text (subscribe [:chat :input-text chat-id]) input-text (subscribe [:chat :input-text chat-id])
selected-command (subscribe [:selected-chat-command chat-id]) selected-command (subscribe [:selected-chat-command chat-id])
requests (subscribe [:chat :request-suggestions chat-id]) requests (subscribe [:chat :request-suggestions chat-id])
commands (subscribe [:chat :command-suggestions chat-id])] commands (subscribe [:chat :command-suggestions chat-id])]
(reaction (and (or @show-suggestions? (chat-utils/starts-as-command? (str/trim (or @input-text ""))))
(and (or @show-suggestions? (chat-utils/starts-as-command? (str/trim (or @input-text "")))) (not (:command @selected-command))
(not (:command @selected-command)) (or (not-empty @requests)
(or (not-empty @requests) (not-empty @commands))))))
(not-empty @commands)))))))
(register-sub :get-current-chat (reg-sub :get-current-chat
(fn [db _] (fn [db]
(let [current-chat-id (:current-chat-id @db)] (let [current-chat-id (:current-chat-id db)]
(reaction (get-in @db [:chats current-chat-id]))))) (get-in db [:chats current-chat-id]))))
(register-sub :get-chat (reg-sub :get-chat
(fn [db [_ chat-id]] (fn [db [_ chat-id]]
(reaction (get-in @db [:chats chat-id])))) (get-in db [:chats chat-id])))
(register-sub :get-response (reg-sub :get-response
(fn [db [_ n]] (fn [db [_ n]]
(let [chat-id (subscribe [:get-current-chat-id])] (let [chat-id (subscribe [:get-current-chat-id])]
(reaction (get-in @db [:contacts @chat-id :responses n]))))) (get-in db [:contacts @chat-id :responses n]))))
(register-sub :is-request-answered? (reg-sub :is-request-answered?
(fn [_ [_ message-id]] :<- [:chat :requests]
(let [requests (subscribe [:chat :requests])] (fn [requests [_ message-id]]
(reaction (not-any? #(= message-id (:message-id %)) @requests))))) (not-any? #(= message-id (:message-id %)) requests)))
(register-sub :unviewed-messages-count (reg-sub :unviewed-messages-count
(fn [db [_ chat-id]] (fn [db [_ chat-id]]
(reaction (get-in @db [:unviewed-messages chat-id :count])))) (get-in db [:unviewed-messages chat-id :count])))
(register-sub :web-view-extra-js (reg-sub :web-view-extra-js
(fn [db] (fn [db]
(let [chat-id (subscribe [:get-current-chat-id])] (let [chat-id (subscribe [:get-current-chat-id])]
(reaction (get-in @db [:web-view-extra-js @chat-id]))))) (get-in db [:web-view-extra-js @chat-id]))))
(register-sub :all-messages-loaded? (reg-sub :all-messages-loaded?
(fn [db] (fn [db]
(let [chat-id (subscribe [:get-current-chat-id])] (let [chat-id (subscribe [:get-current-chat-id])]
(reaction (get-in @db [:chats @chat-id :all-loaded?]))))) (get-in db [:chats @chat-id :all-loaded?]))))
(register-sub :photo-path (reg-sub :photo-path
(fn [_ [_ id]] :<- [:get :contacts]
(let [contacts (subscribe [:get :contacts])] (fn [contacts [_ id]]
(reaction (:photo-path (@contacts id)))))) (:photo-path (contacts id))))
(register-sub :get-last-message (reg-sub :get-last-message
(fn [db [_ chat-id]] (fn [db [_ chat-id]]
(reaction (let [{:keys [last-message messages]} (get-in db [:chats chat-id])]
(let [{:keys [last-message messages]} (get-in @db [:chats chat-id])] (->> (conj messages last-message)
(->> (conj messages last-message) (sort-by :clock-value > )
(sort-by :clock-value >)
(filter :show?) (filter :show?)
(first)))))) (first)))))
(register-sub :get-last-message-short-preview (reg-sub :get-last-message-short-preview
(fn [db [_ chat-id]] (fn [db [_ chat-id]]
(let [last-message (subscribe [:get-last-message chat-id])] (let [last-message (subscribe [:get-last-message chat-id])]
(reaction (get-in db [:message-data :short-preview (:message-id @last-message)]))))
(get-in @db [:message-data :short-preview (:message-id @last-message)])))))
(register-sub :get-default-container-area-height (reg-sub :get-default-container-area-height
(fn [db] :<- [:chat-ui-props :input-height]
(reaction :<- [:get :layout-height]
(let [input-height (subscribe [:chat-ui-props :input-height]) :<- [:chat-input-margin]
layout-height (subscribe [:get :layout-height]) (fn [[input-height layout-height chat-input-margin]]
chat-input-margin (subscribe [:chat-input-margin]) (let [bottom (+ input-height chat-input-margin)]
bottom (+ @input-height @chat-input-margin)] (input-utils/default-container-area-height bottom layout-height))))
(input-utils/default-container-area-height bottom @layout-height)))))
(register-sub :get-max-container-area-height (reg-sub :get-max-container-area-height
(fn [db] :<- [:chat-ui-props :input-height]
(reaction :<- [:get :layout-height]
(let [input-height (subscribe [:chat-ui-props :input-height]) :<- [:chat-input-margin]
layout-height (subscribe [:get :layout-height]) (fn [[input-height layout-height chat-input-margin]]
chat-input-margin (subscribe [:chat-input-margin]) (let [bottom (+ input-height chat-input-margin)]
bottom (+ @input-height @chat-input-margin)] (input-utils/max-container-area-height bottom layout-height))))
(input-utils/max-container-area-height bottom @layout-height)))))
(register-sub :chat-animations (reg-sub :chat-animations
(fn [db [_ key type]] (fn [db [_ key type]]
(let [chat-id (subscribe [:get-current-chat-id])] (let [chat-id (subscribe [:get-current-chat-id])]
(reaction (get-in @db [:chat-animations @chat-id key type]))))) (get-in db [:chat-animations @chat-id key type]))))
(register-sub :get-chat-last-outgoing-message (reg-sub :get-chat-last-outgoing-message
(fn [db [_ chat-id]] (fn [db [_ chat-id]]
(reaction (->> (:messages (get-in db [:chats chat-id]))
(->> (:messages (get-in @db [:chats chat-id])) (filter :outgoing)
(filter :outgoing) (sort-by :clock-value >)
(sort-by :clock-value >) (first))))
(first)))))

View File

@ -115,13 +115,13 @@
(defn actions-list-view [] (defn actions-list-view []
(let [{:keys [group-chat chat-id public?]} (let [{:keys [group-chat chat-id public?]}
(subscribe [:chat-properties [:group-chat :chat-id :public?]]) @(subscribe [:chat-properties [:group-chat :chat-id :public?]])
members (subscribe [:current-chat-contacts]) members (subscribe [:current-chat-contacts])
status-bar-height (get-in platform-specific [:component-styles :status-bar :default :height])] status-bar-height (get-in platform-specific [:component-styles :status-bar :default :height])]
(fn [] (fn []
(when-let [actions (if @group-chat (when-let [actions (if group-chat
(group-chat-items @members @public?) (group-chat-items @members public?)
(user-chat-items @chat-id))] (user-chat-items chat-id))]
[view (merge [view (merge
(st/actions-wrapper status-bar-height) (st/actions-wrapper status-bar-height)
(get-in platform-specific [:component-styles :actions-list-view])) (get-in platform-specific [:component-styles :actions-list-view]))

View File

@ -53,22 +53,22 @@
[text {:style st/author} name])) [text {:style st/author} name]))
(defn message-content-status [_] (defn message-content-status [_]
(let [{:keys [chat-id group-chat name color]} (subscribe [:chat-properties [:chat-id :group-chat :name :color]]) (let [{:keys [chat-id group-chat name color]} @(subscribe [:chat-properties [:chat-id :group-chat :name :color]])
members (subscribe [:current-chat-contacts])] members (subscribe [:current-chat-contacts])]
(fn [{:keys [messages-count content datemark]}] (fn [{:keys [messages-count content datemark]}]
(let [{:keys [status]} (if @group-chat (let [{:keys [status]} (if group-chat
{:photo-path nil {:photo-path nil
:status nil :status nil
:last-online 0} :last-online 0}
(first @members))] (first @members))]
[view st/status-container [view st/status-container
[chat-icon-message-status @chat-id @group-chat @name @color false] [chat-icon-message-status chat-id group-chat name color false]
[text {:style st/status-from [text {:style st/status-from
:font :default :font :default
:number-of-lines 1} :number-of-lines 1}
(if (str/blank? @name) (if (str/blank? name)
(generate-gfy) (generate-gfy)
(or (get-contact-translated @chat-id :name @name) (or (get-contact-translated chat-id :name name)
(label :t/chat-name)))] (label :t/chat-name)))]
(when (or status content) (when (or status content)
[text {:style st/status-text [text {:style st/status-text

View File

@ -67,29 +67,29 @@
contacts contacts
chat-id chat-id
public?]} public?]}
(subscribe [:chat-properties [:group-chat :name :contacts :chat-id :public?]]) @(subscribe [:chat-properties [:group-chat :name :contacts :chat-id :public?]])
show-actions? (subscribe [:chat-ui-props :show-actions?]) show-actions? (subscribe [:chat-ui-props :show-actions?])
accounts (subscribe [:get :accounts]) accounts (subscribe [:get :accounts])
contact (subscribe [:get-in [:contacts @chat-id]]) contact (subscribe [:get-in [:contacts chat-id]])
sync-state (subscribe [:get :sync-state]) sync-state (subscribe [:get :sync-state])
creating? (subscribe [:get :creating-account?])] creating? (subscribe [:get :creating-account?])]
(fn [] (fn []
[view (st/chat-name-view (or (empty? @accounts) [view (st/chat-name-view (or (empty? @accounts)
@show-actions? @show-actions?
@creating?)) @creating?))
(let [chat-name (if (str/blank? @name) (let [chat-name (if (str/blank? name)
(generate-gfy) (generate-gfy)
(or (get-contact-translated @chat-id :name @name) (or (get-contact-translated chat-id :name name)
(label :t/chat-name)))] (label :t/chat-name)))]
[text {:style st/chat-name-text [text {:style st/chat-name-text
:number-of-lines 1 :number-of-lines 1
:font :toolbar-title} :font :toolbar-title}
(if @public? (if public?
(str "#" chat-name) (str "#" chat-name)
chat-name)]) chat-name)])
(if @group-chat (if group-chat
[group-last-activity {:contacts @contacts [group-last-activity {:contacts contacts
:public? @public? :public? public?
:sync-state @sync-state}] :sync-state @sync-state}]
[last-activity {:online-text (online-text @contact @chat-id) [last-activity {:online-text (online-text @contact chat-id)
:sync-state @sync-state}])]))) :sync-state @sync-state}])])))

View File

@ -1,6 +1,5 @@
(ns status-im.chats-list.subs (ns status-im.chats-list.subs
(:require-macros [reagent.ratom :refer [reaction]]) (:require [re-frame.core :refer [reg-sub subscribe]]
(:require [re-frame.core :refer [register-sub subscribe]]
[clojure.string :as str])) [clojure.string :as str]))
(defn search-filter [text item] (defn search-filter [text item]
@ -9,11 +8,10 @@
text (str/lower-case text)] text (str/lower-case text)]
(not= (str/index-of name text) nil))) (not= (str/index-of name text) nil)))
(register-sub :filtered-chats (reg-sub :filtered-chats
(fn [_ _] :<- [:get :chats]
(let [chats (subscribe [:get :chats]) :<- [:get-in [:toolbar-search :text]]
search-text (subscribe [:get-in [:toolbar-search :text]])] (fn [[chats search-text]]
(reaction (if search-text
(if @search-text (filter #(search-filter search-text (second %)) chats)
(filter #(search-filter @search-text (second %)) @chats) chats)))
@chats)))))

View File

@ -1,6 +1,5 @@
(ns status-im.components.main-tabs (ns status-im.components.main-tabs
(:require-macros [reagent.ratom :refer [reaction]] (:require-macros [status-im.utils.views :refer [defview]]
[status-im.utils.views :refer [defview]]
[cljs.core.async.macros :as am]) [cljs.core.async.macros :as am])
(:require [re-frame.core :refer [subscribe dispatch dispatch-sync]] (:require [re-frame.core :refer [subscribe dispatch dispatch-sync]]
[reagent.core :as r] [reagent.core :as r]

View File

@ -284,9 +284,10 @@
(dispatch [:start-chat whisper-identity {} :navigation-replace]))))) (dispatch [:start-chat whisper-identity {} :navigation-replace])))))
(register-handler ::prepare-contact (register-handler ::prepare-contact
(-> add-new-contact (u/handlers->
((after save-contact)) add-new-contact
((after send-contact-request)))) save-contact
send-contact-request))
(register-handler ::update-pending-contact (register-handler ::update-pending-contact
(after save-contact) (after save-contact)
@ -310,8 +311,7 @@
(register-handler :set-contact-identity-from-qr set-contact-identity-from-qr) (register-handler :set-contact-identity-from-qr set-contact-identity-from-qr)
(register-handler (register-handler :contact-update-received
:contact-update-received
(u/side-effect! (u/side-effect!
(fn [{:keys [chats current-public-key] :as db} [_ {:keys [from payload]}]] (fn [{:keys [chats current-public-key] :as db} [_ {:keys [from payload]}]]
(when (not= current-public-key from) (when (not= current-public-key from)
@ -329,8 +329,7 @@
(dispatch [:update-chat! {:chat-id from (dispatch [:update-chat! {:chat-id from
:name name}]))))))))) :name name}])))))))))
(register-handler (register-handler :update-keys-received
:update-keys-received
(u/side-effect! (u/side-effect!
(fn [db [_ {:keys [from payload]}]] (fn [db [_ {:keys [from payload]}]]
(let [{{:keys [public private]} :keypair (let [{{:keys [public private]} :keypair
@ -345,8 +344,7 @@
:keys-last-updated timestamp}] :keys-last-updated timestamp}]
(dispatch [:update-contact! contact]))))))) (dispatch [:update-contact! contact])))))))
(register-handler (register-handler :contact-online-received
:contact-online-received
(u/side-effect! (u/side-effect!
(fn [db [_ {:keys [from] (fn [db [_ {:keys [from]
{{:keys [timestamp]} :content} :payload}]] {{:keys [timestamp]} :content} :payload}]]
@ -384,8 +382,7 @@
db) db)
db))) db)))
(register-handler (register-handler :open-contact-menu
:open-contact-menu
(u/side-effect! (u/side-effect!
(fn [_ [_ list-selection-fn {:keys [name] :as contact}]] (fn [_ [_ list-selection-fn {:keys [name] :as contact}]]
(list-selection-fn {:title name (list-selection-fn {:title name
@ -396,8 +393,7 @@
:default)) :default))
:cancel-text (label :t/cancel)})))) :cancel-text (label :t/cancel)}))))
(register-handler (register-handler :open-contact-toggle-list
:open-contact-toggle-list
(after #(dispatch [:navigate-to :contact-toggle-list])) (after #(dispatch [:navigate-to :contact-toggle-list]))
(fn [db [_ group-type]] (fn [db [_ group-type]]
(-> (->

View File

@ -1,20 +1,16 @@
(ns status-im.contacts.subs (ns status-im.contacts.subs
(:require-macros [reagent.ratom :refer [reaction]]) (:require [re-frame.core :refer [reg-sub subscribe]]
(:require [re-frame.core :refer [register-sub subscribe]]
[status-im.utils.identicon :refer [identicon]] [status-im.utils.identicon :refer [identicon]]
[clojure.string :as str] [clojure.string :as str]
[status-im.bots.constants :as bots-constants])) [status-im.bots.constants :as bots-constants]))
(register-sub :current-contact (reg-sub :current-contact
(fn [db [_ k]] (fn [db [_ k]]
(-> @db (get-in db [:contacts (:current-chat-id db) k])))
(get-in [:contacts (:current-chat-id @db) k])
(reaction))))
(register-sub :get-contacts (reg-sub :get-contacts
(fn [db _] (fn [db _]
(let [contacts (reaction (:contacts @db))] (:contacts db)))
(reaction @contacts))))
(defn sort-contacts [contacts] (defn sort-contacts [contacts]
(sort (fn [c1 c2] (sort (fn [c1 c2]
@ -25,71 +21,69 @@
(vals contacts))) (vals contacts)))
(register-sub :all-added-contacts (reg-sub :all-added-contacts
(fn [db _] (fn [db]
(let [contacts (reaction (:contacts @db))] (let [contacts (:contacts db)]
(->> (remove (fn [[_ {:keys [pending? whisper-identity]}]] (->> (remove (fn [[_ {:keys [pending? whisper-identity]}]]
(or (true? pending?) (or (true? pending?)
(bots-constants/hidden-bots whisper-identity))) @contacts) (bots-constants/hidden-bots whisper-identity))) contacts)
(sort-contacts) (sort-contacts)))))
(reaction)))))
(register-sub :all-added-people-contacts (reg-sub :all-added-people-contacts
(fn [_ _] :<- [:all-added-contacts]
(let [contacts (subscribe [:all-added-contacts])] (fn [contacts]
(reaction (remove #(true? (:dapp? %)) @contacts))))) (remove #(true? (:dapp? %)) contacts)))
(register-sub :people-in-current-chat (reg-sub :people-in-current-chat
(fn [{:keys [current-chat-id]} _] (fn [{:keys [current-chat-id]} _]
(let [contacts (subscribe [:current-chat-contacts])] (let [contacts (subscribe [:current-chat-contacts])]
(reaction (remove #(true? (:dapp? %)) @contacts))))) (remove #(true? (:dapp? %)) @contacts))))
(defn filter-group-contacts [group-contacts contacts] (defn filter-group-contacts [group-contacts contacts]
(filter #(group-contacts (:whisper-identity %)) contacts)) (filter #(group-contacts (:whisper-identity %)) contacts))
(register-sub :all-added-group-contacts (reg-sub :all-added-group-contacts
(fn [db [_ group-id]] (fn [db [_ group-id]]
(let [contacts (subscribe [:all-added-contacts]) (let [contacts (subscribe [:all-added-contacts])
group-contacts (reaction (into #{} (map #(:identity %) group-contacts (into #{} (map #(:identity %)
(get-in @db [:contact-groups group-id :contacts]))))] (get-in db [:contact-groups group-id :contacts])))]
(reaction (filter-group-contacts @group-contacts @contacts))))) (filter-group-contacts group-contacts @contacts))))
(defn filter-not-group-contacts [group-contacts contacts] (defn filter-not-group-contacts [group-contacts contacts]
(remove #(group-contacts (:whisper-identity %)) contacts)) (remove #(group-contacts (:whisper-identity %)) contacts))
(register-sub :all-not-added-group-contacts (reg-sub :all-not-added-group-contacts
(fn [db [_ group-id]] (fn [db [_ group-id]]
(let [contacts (subscribe [:all-added-contacts]) (let [contacts (subscribe [:all-added-contacts])
group-contacts (reaction (into #{} (map #(:identity %) group-contacts (into #{} (map #(:identity %)
(get-in @db [:contact-groups group-id :contacts]))))] (get-in db [:contact-groups group-id :contacts])))]
(reaction (filter-not-group-contacts @group-contacts @contacts))))) (filter-not-group-contacts group-contacts @contacts))))
(register-sub :all-added-group-contacts-with-limit (reg-sub :all-added-group-contacts-with-limit
(fn [db [_ group-id limit]] (fn [db [_ group-id limit]]
(let [contacts (subscribe [:all-added-group-contacts group-id])] (let [contacts (subscribe [:all-added-group-contacts group-id])]
(reaction (take limit @contacts))))) (take limit @contacts))))
(register-sub :all-added-group-contacts-count (reg-sub :all-added-group-contacts-count
(fn [_ [_ group-id]] (fn [_ [_ group-id]]
(let [contacts (subscribe [:all-added-group-contacts group-id])] (let [contacts (subscribe [:all-added-group-contacts group-id])]
(reaction (count @contacts))))) (count @contacts))))
(register-sub :get-added-contacts-with-limit (reg-sub :get-added-contacts-with-limit
(fn [_ [_ limit]] :<- [:all-added-contacts]
(let [contacts (subscribe [:all-added-contacts])] (fn [contacts [_ limit]]
(reaction (take limit @contacts))))) (take limit contacts)))
(register-sub :added-contacts-count (reg-sub :added-contacts-count
(fn [_ _] :<- [:all-added-contacts]
(let [contacts (subscribe [:all-added-contacts])] (fn [contacts]
(reaction (count @contacts))))) (count contacts)))
(register-sub :all-added-groups (reg-sub :all-added-groups
(fn [db _] (fn [db]
(let [groups (reaction (vals (:contact-groups @db)))] (let [groups (vals (:contact-groups db))]
(->> (remove :pending? @groups) (->> (remove :pending? groups)
(sort-by :order >) (sort-by :order >)))))
(reaction)))))
(defn get-contact-letter [contact] (defn get-contact-letter [contact]
(when-let [letter (first (:name contact))] (when-let [letter (first (:name contact))]
@ -103,94 +97,90 @@
(defn search-filter-reaction [contacts] (defn search-filter-reaction [contacts]
(let [text (subscribe [:get-in [:toolbar-search :text]])] (let [text (subscribe [:get-in [:toolbar-search :text]])]
(reaction (if @text
(if @text (filter #(search-filter @text %) @contacts)
(filter #(search-filter @text %) @contacts) @contacts)))
@contacts))))
(register-sub :all-added-group-contacts-filtered (reg-sub :all-added-group-contacts-filtered
(fn [_ [_ group-id]] (fn [_ [_ group-id]]
(let [contacts (if group-id (let [contacts (if group-id
(subscribe [:all-added-group-contacts group-id]) (subscribe [:all-added-group-contacts group-id])
(subscribe [:all-added-contacts]))] (subscribe [:all-added-contacts]))]
(search-filter-reaction contacts)))) (search-filter-reaction contacts))))
(register-sub :all-group-not-added-contacts-filtered (reg-sub :all-group-not-added-contacts-filtered
(fn [db _] (fn [db]
(let [contact-group-id (:contact-group-id @db) (let [contact-group-id (:contact-group-id db)
contacts (subscribe [:all-not-added-group-contacts contact-group-id])] contacts (subscribe [:all-not-added-group-contacts contact-group-id])]
(search-filter-reaction contacts)))) (search-filter-reaction contacts))))
(register-sub :contacts-filtered (reg-sub :contacts-filtered
(fn [db [_ subscription-id]] (fn [_ [_ subscription-id]]
(let [contacts (subscribe [subscription-id])] (let [contacts (subscribe [subscription-id])]
(search-filter-reaction contacts)))) (search-filter-reaction contacts))))
(register-sub :contacts-with-letters (reg-sub :contacts-with-letters
(fn [db _] (fn [db]
(let [contacts (reaction (:contacts @db))] (let [contacts (:contacts db)]
(reaction (let [ordered (sort-contacts contacts)]
(let [ordered (sort-contacts @contacts)] (reduce (fn [prev cur]
(reduce (fn [prev cur] (let [prev-letter (get-contact-letter (last prev))
(let [prev-letter (get-contact-letter (last prev)) cur-letter (get-contact-letter cur)]
cur-letter (get-contact-letter cur)] (conj prev
(conj prev (if (not= prev-letter cur-letter)
(if (not= prev-letter cur-letter) (assoc cur :letter cur-letter)
(assoc cur :letter cur-letter) cur))))
cur)))) [] ordered)))))
[] ordered))))))
(defn contacts-by-chat [fn db chat-id] (defn contacts-by-chat [fn db chat-id]
(let [chat (reaction (get-in @db [:chats chat-id])) (let [chat (get-in db [:chats chat-id])
contacts (reaction (:contacts @db))] contacts (:contacts db)]
(reaction (when chat
(when @chat (let [current-participants (->> chat
(let [current-participants (->> @chat :contacts
:contacts (map :identity)
(map :identity) set)]
set)] (fn #(current-participants (:whisper-identity %))
(fn #(current-participants (:whisper-identity %)) (vals contacts))))))
(vals @contacts)))))))
(defn contacts-by-current-chat [fn db] (defn contacts-by-current-chat [fn db]
(let [current-chat-id (:current-chat-id @db)] (let [current-chat-id (:current-chat-id db)]
(contacts-by-chat fn db current-chat-id))) (contacts-by-chat fn db current-chat-id)))
(register-sub :contact (reg-sub :contact
(fn [db _] (fn [db]
(let [identity (:contact-identity @db)] (let [identity (:contact-identity db)]
(reaction (get-in @db [:contacts identity]))))) (get-in db [:contacts identity]))))
(register-sub :contact-by-identity (reg-sub :contact-by-identity
(fn [db [_ identity]] (fn [db [_ identity]]
(reaction (get-in @db [:contacts identity])))) (get-in db [:contacts identity])))
(register-sub :contact-name-by-identity (reg-sub :contact-name-by-identity
(fn [db [_ identity]] :<- [:get-contacts]
(let [contacts (subscribe [:get-contacts])] (fn [contacts [_ identity]]
(reaction (:name (@contacts identity)))))) (:name (contacts identity))))
(register-sub :all-new-contacts (reg-sub :all-new-contacts
(fn [db _] (fn [db]
(contacts-by-current-chat remove db))) (contacts-by-current-chat remove db)))
(register-sub :current-chat-contacts (reg-sub :current-chat-contacts
(fn [db _] (fn [db]
(contacts-by-current-chat filter db))) (contacts-by-current-chat filter db)))
(register-sub :chat-photo (reg-sub :chat-photo
(fn [db [_ chat-id]] (fn [db [_ chat-id]]
(let [chat-id (or chat-id (:current-chat-id @db)) (let [chat-id (or chat-id (:current-chat-id db))
chat (reaction (get-in @db [:chats chat-id])) chat (get-in db [:chats chat-id])
contacts (contacts-by-chat filter db chat-id)] contacts (contacts-by-chat filter db chat-id)]
(reaction (when (and chat (not (:group-chat chat)))
(when (and @chat (not (:group-chat @chat))) (cond
(cond (:photo-path chat)
(:photo-path @chat) (:photo-path chat)
(:photo-path @chat)
(pos? (count @contacts)) (pos? (count contacts))
(:photo-path (first @contacts)) (:photo-path (first contacts))
:else :else
(identicon chat-id))))))) (identicon chat-id))))))

View File

@ -125,12 +125,12 @@
(into {})))) (into {}))))
(register-handler :add-discover (register-handler :add-discover
(-> add-discover (u/handlers->
((after save-discover!)) add-discover
((enrich reload-tags!)))) save-discover!
reload-tags!))
(register-handler (register-handler :remove-old-discoveries!
:remove-old-discoveries!
(u/side-effect! (u/side-effect!
(fn [_ _] (fn [_ _]
(discoveries/delete :created-at :asc 1000 200)))) (discoveries/delete :created-at :asc 1000 200))))

View File

@ -1,6 +1,5 @@
(ns status-im.discover.subs (ns status-im.discover.subs
(:require-macros [reagent.ratom :refer [reaction]]) (:require [re-frame.core :refer [reg-sub]]
(:require [re-frame.core :refer [register-sub]]
[status-im.utils.datetime :as time])) [status-im.utils.datetime :as time]))
(defn- calculate-priority [{:keys [chats contacts current-public-key]} (defn- calculate-priority [{:keys [chats contacts current-public-key]}
@ -24,31 +23,28 @@
tags') tags')
(vals discoveries)))) (vals discoveries))))
(register-sub :get-popular-discoveries (reg-sub :get-popular-discoveries
(fn [db [_ limit tags]] (fn [db [_ limit tags]]
(let [discoveries (reaction (:discoveries @db)) (let [discoveries (:discoveries db)
current-tag (reaction (:current-tag @db)) current-tag (:current-tag db)
search-tags (reaction (:discover-search-tags @db))] search-tags (:discover-search-tags db)]
(reaction (let [discoveries (->> (get-discoveries-by-tags discoveries current-tag (or tags search-tags))
(let [discoveries (->> (get-discoveries-by-tags @discoveries @current-tag (or tags @search-tags)) (map #(assoc % :priority (calculate-priority db %)))
(map #(assoc % :priority (calculate-priority @db %))) (sort-by :priority >))]
(sort-by :priority >))] {:discoveries (take limit discoveries)
{:discoveries (take limit discoveries) :total (count discoveries)}))))
:total (count discoveries)})))))
(register-sub :get-recent-discoveries (reg-sub :get-recent-discoveries
(fn [db] (fn [db]
(->> (:discoveries @db) (vals (:discoveries db))))
(vals)
(reaction))))
(register-sub :get-popular-tags (reg-sub :get-popular-tags
(fn [db [_ limit]] (fn [db [_ limit]]
(reaction (take limit (:tags @db))))) (take limit (:tags db))))
(register-sub :get-discover-search-results (reg-sub :get-discover-search-results
(fn [db _] (fn [db]
(let [discoveries (reaction (:discoveries @db)) (let [discoveries (:discoveries db)
current-tag (reaction (:current-tag @db)) current-tag (:current-tag db)
tags (reaction (:discover-search-tags @db))] tags (:discover-search-tags db)]
(reaction (get-discoveries-by-tags @discoveries @current-tag @tags))))) (get-discoveries-by-tags discoveries current-tag tags))))

View File

@ -1,6 +1,6 @@
(ns status-im.group-settings.handlers (ns status-im.group-settings.handlers
(:require [re-frame.core :refer [debug dispatch after enrich]] (:require [re-frame.core :refer [dispatch after enrich]]
[status-im.utils.handlers :refer [register-handler]] [status-im.utils.handlers :refer [register-handler] :as u]
[status-im.chat.handlers :refer [delete-messages!]] [status-im.chat.handlers :refer [delete-messages!]]
[status-im.protocol.core :as protocol] [status-im.protocol.core :as protocol]
[status-im.utils.random :as random] [status-im.utils.random :as random]
@ -133,18 +133,15 @@
(register-handler :remove-participants (register-handler :remove-participants
;; todo check if user have rights to add/remove participants ;; todo check if user have rights to add/remove participants
;; todo order of operations tbd ;; todo order of operations tb
(-> remove-members (u/handlers->
;; todo shouldn't this be done only after receiving of the "ack message" remove-members
;; about the api call that removes participants from the group? remove-members-from-chat!
((after remove-members-from-chat!)) notify-about-removing!
;; todo uncomment create-removing-messages!
((after notify-about-removing!)) deselect-members))
((after create-removing-messages!))
((enrich deselect-members))
debug))
(defn add-memebers (defn add-members
[{:keys [current-chat-id selected-participants] :as db} _] [{:keys [current-chat-id selected-participants] :as db} _]
(let [new-identities (map #(hash-map :identity %) selected-participants)] (let [new-identities (map #(hash-map :identity %) selected-participants)]
(update-in db [:chats current-chat-id :contacts] concat new-identities))) (update-in db [:chats current-chat-id :contacts] concat new-identities)))
@ -194,8 +191,8 @@
:message-id (random/id)}})))) :message-id (random/id)}}))))
(register-handler :add-new-participants (register-handler :add-new-participants
;; todo order of operations tbd (u/handlers->
(-> add-memebers add-members
((after add-members-to-chat!)) add-members-to-chat!
((after notify-about-new-members!)) notify-about-new-members!
((enrich deselect-members)))) deselect-members))

View File

@ -1,13 +1,11 @@
(ns status-im.group-settings.subs (ns status-im.group-settings.subs
(:require-macros [reagent.ratom :refer [reaction]]) (:require [re-frame.core :refer [reg-sub]]
(:require [re-frame.core :refer [register-sub]]
[status-im.constants :refer [max-chat-name-length]])) [status-im.constants :refer [max-chat-name-length]]))
(register-sub :selected-participant (reg-sub :selected-participant
(fn [db _] (fn [db]
(reaction (let [identity (first (:selected-participants db))]
(let [identity (first (:selected-participants @db))] (get-in db [:contacts identity]))))
(get-in @db [:contacts identity])))))
(defn get-chat-name-validation-messages [chat-name] (defn get-chat-name-validation-messages [chat-name]
(filter some? (filter some?
@ -16,12 +14,12 @@
(when (< max-chat-name-length (count chat-name)) (when (< max-chat-name-length (count chat-name))
"Chat name is too long")))) "Chat name is too long"))))
(register-sub :new-chat-name-validation-messages (reg-sub :new-chat-name-validation-messages
(fn [db [_]] (fn [db]
(let [chat-name (reaction (:new-chat-name @db))] (let [chat-name (:new-chat-name db)]
(reaction (get-chat-name-validation-messages @chat-name))))) (get-chat-name-validation-messages chat-name))))
(register-sub :new-chat-name-valid? (reg-sub :new-chat-name-valid?
(fn [db [_]] (fn [db]
(let [chat-name (reaction (:new-chat-name @db))] (let [chat-name (:new-chat-name db)]
(reaction (zero? (count (get-chat-name-validation-messages @chat-name))))))) (zero? (count (get-chat-name-validation-messages chat-name))))))

View File

@ -1,7 +1,5 @@
(ns status-im.models.commands (ns status-im.models.commands
(:require [status-im.db :as db] (:require [status-im.bots.constants :as bots-constants]))
[tailrecursion.priority-map :refer [priority-map-by]]
[status-im.bots.constants :as bots-constants]))
(defn parse-command-message-content (defn parse-command-message-content
[commands global-commands content] [commands global-commands content]

View File

@ -1,6 +1,6 @@
(ns status-im.navigation.handlers (ns status-im.navigation.handlers
(:require [re-frame.core :refer [dispatch subscribe debug enrich after]] (:require [re-frame.core :refer [dispatch subscribe debug enrich after]]
[status-im.utils.handlers :refer [register-handler]] [status-im.utils.handlers :refer [register-handler] :as u]
[status-im.constants :refer [console-chat-id]])) [status-im.constants :refer [console-chat-id]]))
(defn push-view [db view-id] (defn push-view [db view-id]
@ -81,14 +81,17 @@
(register-handler :navigate-to-clean navigate-to-clean) (register-handler :navigate-to-clean navigate-to-clean)
(defn store-prev-tab
[db [_ view-id]]
(-> db
(assoc :prev-tab-view-id (:view-id db))
(assoc :prev-view-id (:view-id db))))
(register-handler :navigate-to-tab (register-handler :navigate-to-tab
(-> (u/handlers->
(fn [db [_ view-id]] store-prev-tab
(-> db navigate-to-clean
(assoc :prev-tab-view-id (:view-id db)) preload-data!))
(assoc :prev-view-id (:view-id db))))
((enrich navigate-to-clean))
((enrich preload-data!))))
(register-handler :on-navigated-to-tab (register-handler :on-navigated-to-tab
(enrich preload-data!) (enrich preload-data!)

View File

@ -1,7 +1,7 @@
(ns status-im.new-group.handlers (ns status-im.new-group.handlers
(:require [status-im.protocol.core :as protocol] (:require [status-im.protocol.core :as protocol]
[re-frame.core :refer [after dispatch debug enrich]] [re-frame.core :refer [after dispatch debug enrich]]
[status-im.utils.handlers :refer [register-handler]] [status-im.utils.handlers :refer [register-handler] :as u]
[status-im.components.styles :refer [default-chat-color]] [status-im.components.styles :refer [default-chat-color]]
[status-im.data-store.chats :as chats] [status-im.data-store.chats :as chats]
[status-im.data-store.contact-groups :as groups] [status-im.data-store.contact-groups :as groups]
@ -85,7 +85,7 @@
(dispatch [:navigate-to :chat (:chat-id new-chat)])) (dispatch [:navigate-to :chat (:chat-id new-chat)]))
(defn start-listen-group! (defn start-listen-group!
[{:keys [new-chat web3 current-public-key]}] [{:keys [new-chat web3 current-public-key]} _]
(let [{:keys [chat-id public-key private-key contacts name]} new-chat (let [{:keys [chat-id public-key private-key contacts name]} new-chat
identities (mapv :identity contacts)] identities (mapv :identity contacts)]
(protocol/invite-to-group! (protocol/invite-to-group!
@ -108,11 +108,12 @@
:callback #(dispatch [:incoming-message %1 %2])}))) :callback #(dispatch [:incoming-message %1 %2])})))
(register-handler :create-new-group-chat (register-handler :create-new-group-chat
(-> prepare-chat (u/handlers->
((enrich add-chat)) prepare-chat
((after create-chat!)) add-chat
((after show-chat!)) create-chat!
((after start-listen-group!)))) show-chat!
start-listen-group!))
(register-handler :create-new-public-group (register-handler :create-new-public-group
(after (fn [_ [_ topic]] (after (fn [_ [_ topic]]
@ -200,31 +201,30 @@
[{:keys [new-group] :as db} _] [{:keys [new-group] :as db} _]
(update db :contact-groups merge {(:group-id new-group) new-group})) (update db :contact-groups merge {(:group-id new-group) new-group}))
(defn create-group! (defn save-group!
[{:keys [new-group]} _] [{:keys [new-group]} _]
(groups/save new-group)) (groups/save new-group))
(defn update-group!
[{:keys [new-group] :as db} _]
(groups/save new-group))
(defn show-contact-list! (defn show-contact-list!
[_ _] [_ _]
(dispatch [:navigate-to-clean :contact-list])) (dispatch [:navigate-to-clean :contact-list]))
(register-handler (register-handler :create-new-group
:create-new-group (u/handlers->
(-> prepare-group prepare-group
((enrich add-group)) add-group
((after create-group!)) save-group!
((after show-contact-list!)))) show-contact-list!))
(register-handler (defn update-new-group
:update-group [db [_ new-group]]
(-> (fn [db [_ new-group]] (assoc db :new-group new-group))
(assoc db :new-group new-group))
((enrich update-group)) (register-handler :update-group
((after update-group!)))) (u/handlers->
update-new-group
update-group
save-group!))
(defn save-groups! [{:keys [new-groups]} _] (defn save-groups! [{:keys [new-groups]} _]
(groups/save-all new-groups)) (groups/save-all new-groups))
@ -326,8 +326,7 @@
[{:keys [contact-group-id selected-contacts]} _] [{:keys [contact-group-id selected-contacts]} _]
(groups/add-contacts contact-group-id selected-contacts)) (groups/add-contacts contact-group-id selected-contacts))
(register-handler (register-handler :add-selected-contacts-to-group
:add-selected-contacts-to-group
(after add-selected-contacts-to-group!) (after add-selected-contacts-to-group!)
add-selected-contacts-to-group) add-selected-contacts-to-group)
@ -343,8 +342,7 @@
(when (get-in db [:contact-groups group-id]) (when (get-in db [:contact-groups group-id])
(groups/add-contacts group-id contacts))) (groups/add-contacts group-id contacts)))
(register-handler (register-handler :add-contacts-to-group
:add-contacts-to-group
(after add-contacts-to-group!) (after add-contacts-to-group!)
add-contacts-to-group) add-contacts-to-group)

View File

@ -1,37 +1,34 @@
(ns status-im.new-group.subs (ns status-im.new-group.subs
(:require-macros [reagent.ratom :refer [reaction]]) (:require [re-frame.core :refer [reg-sub subscribe]]
(:require [re-frame.core :refer [register-sub subscribe]]
[status-im.utils.subs :as u])) [status-im.utils.subs :as u]))
(register-sub :is-contact-selected? (reg-sub :is-contact-selected?
(u/contains-sub :selected-contacts)) (u/contains-sub :selected-contacts))
(defn filter-selected-contacts [selected-contacts contacts] (defn filter-selected-contacts [selected-contacts contacts]
(remove #(true? (:pending? (contacts %))) selected-contacts)) (remove #(true? (:pending? (contacts %))) selected-contacts))
(register-sub :selected-contacts-count (reg-sub :selected-contacts-count
(fn [_ _] :<- [:get :selected-contacts]
(let [selected-contacts (subscribe [:get :selected-contacts]) :<- [:get :contacts]
contacts (subscribe [:get :contacts])] (fn [[selected-contacts contacts]]
;TODO temporary, contact should be deleted from group after contact deletion from contacts ;TODO temporary, contact should be deleted from group after contact deletion from contacts
(reaction (count (filter-selected-contacts @selected-contacts @contacts)))))) (count (filter-selected-contacts selected-contacts contacts))))
(register-sub :selected-participants-count (reg-sub :selected-participants-count
(fn [_ _] :<- [:get :selected-participants]
(let [selected-participants (subscribe [:get :selected-participants])] (fn [selected-participants]
(reaction (count @selected-participants))))) (count selected-participants)))
(defn filter-contacts [selected-contacts added-contacts] (defn filter-contacts [selected-contacts added-contacts]
(filter #(selected-contacts (:whisper-identity %)) added-contacts)) (filter #(selected-contacts (:whisper-identity %)) added-contacts))
(register-sub :selected-group-contacts (reg-sub :selected-group-contacts
(fn [_ _] :<- [:get :selected-contacts]
(let [selected-contacts (subscribe [:get :selected-contacts]) :<- [:all-added-contacts]
added-contacts (subscribe [:all-added-contacts])] (fn [[selected-contacts added-contacts]]
(reaction (filter-contacts @selected-contacts @added-contacts))))) (filter-contacts selected-contacts added-contacts)))
(register-sub :get-contact-group (reg-sub :get-contact-group
(fn [db _] (fn [db]
(let [contact-groups (reaction (:contact-groups @db)) ((:contact-groups db) (:contact-group-id db))))
contact-group-id (reaction (:contact-group-id @db))]
(reaction (@contact-groups @contact-group-id)))))

View File

@ -1,7 +1,6 @@
(ns status-im.participants.subs (ns status-im.participants.subs
(:require-macros [reagent.ratom :refer [reaction]]) (:require [re-frame.core :refer [reg-sub]]
(:require [re-frame.core :refer [register-sub]]
[status-im.utils.subs :as u])) [status-im.utils.subs :as u]))
(register-sub :is-participant-selected? (reg-sub :is-participant-selected?
(u/contains-sub :selected-participants)) (u/contains-sub :selected-participants))

View File

@ -1,6 +1,6 @@
(ns status-im.profile.handlers (ns status-im.profile.handlers
(:require [re-frame.core :refer [subscribe dispatch after]] (:require [re-frame.core :refer [subscribe dispatch after]]
[status-im.utils.handlers :refer [register-handler]] [status-im.utils.handlers :refer [register-handler] :as u]
[status-im.components.react :refer [show-image-picker]] [status-im.components.react :refer [show-image-picker]]
[status-im.utils.image-processing :refer [img->base64]] [status-im.utils.image-processing :refer [img->base64]]
[status-im.i18n :refer [label]] [status-im.i18n :refer [label]]
@ -14,17 +14,17 @@
(dispatch [:navigation-replace :chat identity]))) (dispatch [:navigation-replace :chat identity])))
(register-handler :open-image-picker (register-handler :open-image-picker
(u/side-effect! (u/side-effect!
(fn [_ _] (fn [_ _]
(show-image-picker (show-image-picker
(fn [image] (fn [image]
(let [path (get (js->clj image) "path") (let [path (get (js->clj image) "path")
_ (log/debug path) _ (log/debug path)
on-success (fn [base64] on-success (fn [base64]
(dispatch [:set-in [:profile-edit :photo-path] (str "data:image/jpeg;base64," base64)])) (dispatch [:set-in [:profile-edit :photo-path] (str "data:image/jpeg;base64," base64)]))
on-error (fn [type error] on-error (fn [type error]
(.log js/console type error))] (.log js/console type error))]
(img->base64 path on-success on-error))))))) (img->base64 path on-success on-error)))))))
(register-handler :phone-number-change-requested (register-handler :phone-number-change-requested
;; Switch user to the console issuing the !phone command automatically to let him change his phone number. ;; Switch user to the console issuing the !phone command automatically to let him change his phone number.
@ -50,10 +50,10 @@
(defn open-edit-profile [_ _] (defn open-edit-profile [_ _]
(dispatch [:navigate-to :edit-my-profile])) (dispatch [:navigate-to :edit-my-profile]))
(register-handler (register-handler :open-edit-my-profile
:open-edit-my-profile (u/handlers->
(-> prepare-edit-profile prepare-edit-profile
((after open-edit-profile)))) open-edit-profile))
(defmethod nav/preload-data! :qr-code-view (defmethod nav/preload-data! :qr-code-view
[{:keys [current-account-id] :as db} [_ _ {:keys [contact qr-source amount?]}]] [{:keys [current-account-id] :as db} [_ _ {:keys [contact qr-source amount?]}]]

View File

@ -41,9 +41,13 @@
(update :qr-codes dissoc context) (update :qr-codes dissoc context)
(dissoc :current-qr-context))) (dissoc :current-qr-context)))
(defn navigate-back!
[{:keys [view-id]} _]
(when (= :qr-scanner view-id)
(dispatch [:navigate-back])))
(register-handler :set-qr-code (register-handler :set-qr-code
(-> (u/side-effect! handle-qr-request) (u/handlers->
((enrich clear-qr-request)) handle-qr-request
((after (fn [{:keys [view-id]}] clear-qr-request
(when (= :qr-scanner view-id) navigate-back!))
(dispatch [:navigate-back])))))))

View File

@ -1,6 +1,5 @@
(ns status-im.subs (ns status-im.subs
(:require-macros [reagent.ratom :refer [reaction]]) (:require [re-frame.core :refer [reg-sub subscribe]]
(:require [re-frame.core :refer [register-sub subscribe]]
status-im.chat.subs status-im.chat.subs
status-im.chats-list.subs status-im.chats-list.subs
status-im.group-settings.subs status-im.group-settings.subs
@ -11,32 +10,32 @@
status-im.transactions.subs status-im.transactions.subs
status-im.bots.subs)) status-im.bots.subs))
(register-sub :get (reg-sub :get
(fn [db [_ k]] (fn [db [_ k]]
(reaction (k @db)))) (k db)))
(register-sub :get-current-account (reg-sub :get-current-account
(fn [db [_ _]] (fn [db]
(reaction (let [current-account-id (:current-account-id @db)] (let [current-account-id (:current-account-id db)]
(get-in @db [:accounts current-account-id]))))) (get-in db [:accounts current-account-id]))))
(register-sub :get-in (reg-sub :get-in
(fn [db [_ path]] (fn [db [_ path]]
(reaction (get-in @db path)))) (get-in db path)))
(register-sub :signed-up? (reg-sub :signed-up?
(fn [] :<- [:get :current-account-id]
(let [account-id (subscribe [:get :current-account-id]) :<- [:get :accounts]
accounts (subscribe [:get :accounts])] (fn [[account-id accounts]]
(reaction (when (and @accounts @account-id) (when (and accounts account-id)
(get-in @accounts [@account-id :signed-up?])))))) (get-in accounts [account-id :signed-up?]))))
(register-sub :tabs-hidden? (reg-sub :tabs-hidden?
(fn [] :<- [:get-in [:toolbar-search :show]]
(let [search-mode? (subscribe [:get-in [:toolbar-search :show]]) :<- [:get-in [:chat-list-ui-props :edit?]]
chats-edit-mode? (subscribe [:get-in [:chat-list-ui-props :edit?]]) :<- [:get-in [:contacts-ui-props :edit?]]
contacts-edit-mode? (subscribe [:get-in [:contacts-ui-props :edit?]]) :<- [:get :view-id]
view-id (subscribe [:get :view-id])] (fn [[search-mode? chats-edit-mode? contacts-edit-mode? view-id]]
(reaction (or @search-mode? (or search-mode?
(and (= @view-id :chat-list) @chats-edit-mode?) (and (= view-id :chat-list) chats-edit-mode?)
(and (= @view-id :contact-list) @contacts-edit-mode?)))))) (and (= view-id :contact-list) contacts-edit-mode?))))

View File

@ -1,25 +1,24 @@
(ns status-im.transactions.subs (ns status-im.transactions.subs
(:require-macros [reagent.ratom :refer [reaction]]) (:require [re-frame.core :refer [reg-sub subscribe]]
(:require [re-frame.core :refer [register-sub subscribe]]
[status-im.utils.hex :as i])) [status-im.utils.hex :as i]))
(register-sub :transactions (reg-sub :transactions
(fn [db] (fn [db]
(reaction (vals (:transactions @db))))) (vals (:transactions db))))
(register-sub :contacts-by-address (reg-sub :contacts-by-address
(fn [db] (fn [db]
(reaction (into {} (map (fn [[_ {:keys [address] :as contact}]] (into {} (map (fn [[_ {:keys [address] :as contact}]]
(when address (when address
[address contact])) [address contact]))
(:contacts @db)))))) (:contacts db)))))
(register-sub :contact-by-address (reg-sub :contact-by-address
(fn [_ [_ address]] :<- [:contacts-by-address]
(let [contacts (subscribe [:contacts-by-address]) (fn [contacts [_ address]]
address' (when address (let [address' (when address
(i/normalize-hex address))] (i/normalize-hex address))]
(reaction (@contacts address'))))) (contacts address'))))
(register-sub :wrong-password? (reg-sub :wrong-password?
(fn [db] (reaction (:wrong-password? @db)))) (fn [db] (:wrong-password? db)))

View File

@ -0,0 +1,12 @@
(ns status-im.utils.handlers)
(defmacro handlers->
"Help thread multiple handler functions.
All functions are expected to accept [db event] as parameters.
If one handler returns a modified db it will be used as parameters for subsequent handlers."
[& forms]
(let [db (gensym "db")
event (gensym "event")]
`(fn [~db ~event]
(let [~@(interleave (repeat db) (map #(list 'or (list % db event) db) forms))]
~db))))

View File

@ -1,8 +1,10 @@
(ns status-im.utils.handlers (ns status-im.utils.handlers
(:require [re-frame.core :refer [after dispatch debug] :as re-core] (:require [re-frame.core :refer [reg-event-db]]
[re-frame.interceptor :refer [->interceptor get-coeffect]]
[clojure.string :as str] [clojure.string :as str]
[taoensso.timbre :as log] [taoensso.timbre :as log]
[cljs.spec.alpha :as s])) [cljs.spec.alpha :as s])
(:require-macros status-im.utils.handlers))
(defn side-effect! (defn side-effect!
"Middleware for handlers that will not affect db." "Middleware for handlers that will not affect db."
@ -11,31 +13,32 @@
(handler db params) (handler db params)
db)) db))
(defn debug-handlers-names (def debug-handlers-names
"Middleware which logs debug information to js/console for each event. "Interceptor which logs debug information to js/console for each event."
Includes a clojure.data/diff of the db, before vs after, showing the changes (->interceptor
caused by the event." :id :debug-handlers-names
[handler] :before (fn debug-handlers-names-before
(fn debug-handler [context]
[db v] (log/debug "Handling re-frame event: " (first (get-coeffect context :event)))
(log/debug "Handling re-frame event: " (first v)) context)))
(let [new-db (handler db v)]
new-db)))
(defn check-spec (def check-spec
"throw an exception if db doesn't match the spec" "throw an exception if db doesn't match the spec"
[handler] (->interceptor
(fn check-handler :id check-spec
[db v] :before
(let [new-db (handler db v)] (fn check-handler
(when-not (s/valid? :status-im.specs/db new-db) [context]
(throw (ex-info (str "spec check failed on: " (first v) "\n " (s/explain-str :status-im.specs/db new-db)) {}))) (let [new-db (get-coeffect context :db)
new-db))) v (get-coeffect context :event)]
(when-not (s/valid? :status-im.specs/db new-db)
(throw (ex-info (str "spec check failed on: " (first v) "\n " (s/explain-str :status-im.specs/db new-db)) {})))
context))))
(defn register-handler (defn register-handler
([name handler] (register-handler name nil handler)) ([name handler] (register-handler name nil handler))
([name middleware handler] ([name middleware handler]
(re-core/register-handler name [debug-handlers-names (when js/goog.DEBUG check-spec) middleware] handler))) (reg-event-db name [debug-handlers-names (when js/goog.DEBUG check-spec) middleware] handler)))
(defn get-hashtags [status] (defn get-hashtags [status]
(if status (if status

View File

@ -1,10 +1,8 @@
(ns status-im.utils.subs (ns status-im.utils.subs)
(:require-macros [reagent.ratom :refer [reaction]]))
(defn contains-sub (defn contains-sub
"Creates subscrition that cheks if collection (map or set) contains element" "Creates subscrition that cheks if collection (map or set) contains element"
[collection] [collection]
(fn [db [_ element]] (fn [db [_ element]]
(-> (collection @db) (-> (collection db)
(contains? element) (contains? element))))
(reaction))))