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"]
[org.clojure/clojurescript "1.9.671"]
[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"]
[com.andrewmcveigh/cljs-time "0.4.0"]
[tailrecursion/cljs-priority-map "1.2.0"]

View File

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

View File

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

View File

@ -1,5 +1,5 @@
(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.utils.types :refer [json->clj]]
[status-im.utils.identicon :refer [identicon]]
@ -37,7 +37,7 @@
password
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
[db]

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -115,13 +115,13 @@
(defn actions-list-view []
(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])
status-bar-height (get-in platform-specific [:component-styles :status-bar :default :height])]
(fn []
(when-let [actions (if @group-chat
(group-chat-items @members @public?)
(user-chat-items @chat-id))]
(when-let [actions (if group-chat
(group-chat-items @members public?)
(user-chat-items chat-id))]
[view (merge
(st/actions-wrapper status-bar-height)
(get-in platform-specific [:component-styles :actions-list-view]))

View File

@ -53,22 +53,22 @@
[text {:style st/author} name]))
(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])]
(fn [{:keys [messages-count content datemark]}]
(let [{:keys [status]} (if @group-chat
(let [{:keys [status]} (if group-chat
{:photo-path nil
:status nil
:last-online 0}
(first @members))]
[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
:font :default
:number-of-lines 1}
(if (str/blank? @name)
(if (str/blank? name)
(generate-gfy)
(or (get-contact-translated @chat-id :name @name)
(or (get-contact-translated chat-id :name name)
(label :t/chat-name)))]
(when (or status content)
[text {:style st/status-text

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,6 +1,6 @@
(ns status-im.navigation.handlers
(: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]]))
(defn push-view [db view-id]
@ -81,14 +81,17 @@
(register-handler :navigate-to-clean navigate-to-clean)
(register-handler :navigate-to-tab
(->
(fn [db [_ view-id]]
(defn store-prev-tab
[db [_ view-id]]
(-> db
(assoc :prev-tab-view-id (:view-id db))
(assoc :prev-view-id (:view-id db))))
((enrich navigate-to-clean))
((enrich preload-data!))))
(register-handler :navigate-to-tab
(u/handlers->
store-prev-tab
navigate-to-clean
preload-data!))
(register-handler :on-navigated-to-tab
(enrich preload-data!)

View File

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

View File

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

View File

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

View File

@ -1,6 +1,6 @@
(ns status-im.profile.handlers
(: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.utils.image-processing :refer [img->base64]]
[status-im.i18n :refer [label]]
@ -50,10 +50,10 @@
(defn open-edit-profile [_ _]
(dispatch [:navigate-to :edit-my-profile]))
(register-handler
:open-edit-my-profile
(-> prepare-edit-profile
((after open-edit-profile))))
(register-handler :open-edit-my-profile
(u/handlers->
prepare-edit-profile
open-edit-profile))
(defmethod nav/preload-data! :qr-code-view
[{:keys [current-account-id] :as db} [_ _ {:keys [contact qr-source amount?]}]]

View File

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

View File

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

View File

@ -1,25 +1,24 @@
(ns status-im.transactions.subs
(:require-macros [reagent.ratom :refer [reaction]])
(:require [re-frame.core :refer [register-sub subscribe]]
(:require [re-frame.core :refer [reg-sub subscribe]]
[status-im.utils.hex :as i]))
(register-sub :transactions
(reg-sub :transactions
(fn [db]
(reaction (vals (:transactions @db)))))
(vals (:transactions db))))
(register-sub :contacts-by-address
(reg-sub :contacts-by-address
(fn [db]
(reaction (into {} (map (fn [[_ {:keys [address] :as contact}]]
(into {} (map (fn [[_ {:keys [address] :as contact}]]
(when address
[address contact]))
(:contacts @db))))))
(:contacts db)))))
(register-sub :contact-by-address
(fn [_ [_ address]]
(let [contacts (subscribe [:contacts-by-address])
address' (when address
(reg-sub :contact-by-address
:<- [:contacts-by-address]
(fn [contacts [_ address]]
(let [address' (when address
(i/normalize-hex address))]
(reaction (@contacts address')))))
(contacts address'))))
(register-sub :wrong-password?
(fn [db] (reaction (:wrong-password? @db))))
(reg-sub :wrong-password?
(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
(: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]
[taoensso.timbre :as log]
[cljs.spec.alpha :as s]))
[cljs.spec.alpha :as s])
(:require-macros status-im.utils.handlers))
(defn side-effect!
"Middleware for handlers that will not affect db."
@ -11,31 +13,32 @@
(handler db params)
db))
(defn debug-handlers-names
"Middleware which logs debug information to js/console for each event.
Includes a clojure.data/diff of the db, before vs after, showing the changes
caused by the event."
[handler]
(fn debug-handler
[db v]
(log/debug "Handling re-frame event: " (first v))
(let [new-db (handler db v)]
new-db)))
(def debug-handlers-names
"Interceptor which logs debug information to js/console for each event."
(->interceptor
:id :debug-handlers-names
:before (fn debug-handlers-names-before
[context]
(log/debug "Handling re-frame event: " (first (get-coeffect context :event)))
context)))
(defn check-spec
(def check-spec
"throw an exception if db doesn't match the spec"
[handler]
(->interceptor
:id check-spec
:before
(fn check-handler
[db v]
(let [new-db (handler db v)]
[context]
(let [new-db (get-coeffect context :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)) {})))
new-db)))
context))))
(defn register-handler
([name handler] (register-handler name nil 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]
(if status

View File

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