diff --git a/resources/js/bots/console/bot.js b/resources/js/bots/console/bot.js index 040696a270..15d31112a1 100644 --- a/resources/js/bots/console/bot.js +++ b/resources/js/bots/console/bot.js @@ -429,7 +429,7 @@ function phoneSuggestions(params, context) { suggestions = ph.map(function (phone) { return status.components.touchable( - {onPress: status.components.dispatch([status.events.SET_VALUE, phone.number])}, + {onPress: status.components.dispatch([status.events.SET_COMMAND_ARGUMENT, [0, phone.number]])}, status.components.view(suggestionContainerStyle, [status.components.view(suggestionSubContainerStyle, [ diff --git a/src/status_im/chat/events/console.cljs b/src/status_im/chat/events/console.cljs index 9c599bddf0..8261e0cfc5 100644 --- a/src/status_im/chat/events/console.cljs +++ b/src/status_im/chat/events/console.cljs @@ -87,19 +87,22 @@ (i18n/label :t/faucet-error)))}})) "debug" - (fn [{:keys [random-id db] :as cofx} {:keys [params id]}] + (fn [{:keys [db random-id now] :as cofx} {:keys [params id]}] (let [debug? (= "On" (:mode params))] - (assoc fx :dispatch-n (if debug? - [[:initialize-debugging {:force-start? true}] - [:received-message - {:message-id random-id - :content (i18n/label :t/debug-enabled) - :content-type const/text-content-type - :outgoing false - :chat-id const/console-chat-id - :from const/console-chat-id - :to "me"}]] - [[:stop-debugging]]))))}) + (-> {:db db} + (accounts-events/account-update {:debug? debug? + :last-updated now}) + (assoc :dispatch-n (if debug? + [[:initialize-debugging {:force-start? true}] + [:received-message + {:message-id random-id + :content (i18n/label :t/debug-enabled) + :content-type const/text-content-type + :outgoing false + :chat-id const/console-chat-id + :from const/console-chat-id + :to "me"}]] + [[:stop-debugging]])))))}) (def commands-names (set (keys console-commands->fx))) diff --git a/src/status_im/chat/events/sign_up.cljs b/src/status_im/chat/events/sign_up.cljs index 12f96c0f41..0253a119f3 100644 --- a/src/status_im/chat/events/sign_up.cljs +++ b/src/status_im/chat/events/sign_up.cljs @@ -1,11 +1,13 @@ (ns status-im.chat.events.sign-up (:require [re-frame.core :as re-frame] - [status-im.utils.handlers :as handlers] - [status-im.utils.phone-number :as phone-number-util] [status-im.constants :as const] [status-im.chat.sign-up :as sign-up] + [status-im.utils.handlers :as handlers] + [status-im.utils.phone-number :as phone-number-util] + [status-im.utils.sms-listener :as sms-listener] [status-im.ui.screens.accounts.events :as accounts-events] - [status-im.ui.screens.contacts.events :as contacts-events])) + [status-im.ui.screens.contacts.events :as contacts-events] + [taoensso.timbre :as log])) ;;;; Helpers fns @@ -14,8 +16,7 @@ [db phone-number message-id] (let [current-account-id (:accounts/current-account-id db) {:keys [public-key address]} (get-in db [:accounts/accounts current-account-id])] - {:dispatch sign-up/start-listening-confirmation-code-sms-event - :http-post {:action "sign-up" + {:http-post {:action "sign-up" :data {:phone-number (phone-number-util/format-phone-number phone-number) :whisper-identity public-key :address address} @@ -43,49 +44,83 @@ (sign-up db phone-number message-id))) (defn- message-seen [{:keys [db] :as fx} message-id] - (merge fx - {:db (assoc-in db [:message-data :statuses message-id] {:status :seen}) - :update-message {:message-id message-id - :message-status :seen}})) + (-> fx + (assoc-in [:db :message-data :statuses message-id :status] :seen) + (assoc :update-message {:message-id message-id + :message-status :seen}))) (handlers/register-handler-fx - ::sign-up-success - [re-frame/trim-v (re-frame/inject-cofx :random-id)] - (fn [{:keys [db random-id]} message-id] - (-> {:db db - :dispatch-n [;; create manual way for entering confirmation code - (sign-up/enter-confirmation-code-event random-id) - ;; create automatic way for receiving confirmation code - sign-up/start-listening-confirmation-code-sms-event]} - (message-seen message-id)))) + :start-listening-confirmation-code-sms + [re-frame/trim-v] + (fn [{:keys [db]} [sms-listener]] + {:db (if-not (:confirmation-code-sms-listener db) + (assoc db :confirmation-code-sms-listener sms-listener) + db)})) + +(defn stop-listening-confirmation-code-sms [{:keys [db] :as fx}] + (-> fx + (update db dissoc :confirmation-code-sms-listener) + (update ::remove-sms-listener assoc (:confirmation-code-sms-listener db)))) + +(re-frame/reg-fx + ::remove-sms-listener + (fn [subscription] + (sms-listener/remove-sms-listener subscription))) + +(defn- sms-receive-handler [{confirmation-code :body}] + (when-let [matches (re-matches #"(\d{4})" confirmation-code)] + (re-frame/dispatch [::sign-up-confirm (second matches)]))) + +(def start-listening-confirmation-code-sms-event + [:request-permissions + [:receive-sms] + (fn [] + (let [listener (sms-listener/add-sms-listener sms-receive-handler)] + (re-frame/dispatch [:start-listening-confirmation-code-sms listener])))]) (handlers/register-handler-fx - :start-listening-confirmation-code-sms - [re-frame/trim-v] - (fn [{:keys [db]} [sms-listener]] - {:db (if-not (:confirmation-code-sms-listener db) - (assoc db :confirmation-code-sms-listener sms-listener) - db)})) + ::sign-up-success + [re-frame/trim-v (re-frame/inject-cofx :random-id)] + (fn [{:keys [db random-id]} message-id] + (-> {:db db + :dispatch-n [;; create manual way for entering confirmation code + (sign-up/enter-confirmation-code-event random-id) + ;; create automatic way for receiving confirmation code + start-listening-confirmation-code-sms-event]} + (message-seen message-id)))) + +(defn- extract-last-phone-number [chats] + (let [phone-message (->> (get-in chats ["console" :messages]) + (some (fn [{:keys [type content] :as message}] + (when (and (= type :response) + (= (:command content) "phone")) + message))))] + (get-in phone-message [:content :params :phone]))) (handlers/register-handler-fx ::sign-up-confirm (fn [{:keys [db]} [confirmation-code message-id]] (sign-up-confirm db confirmation-code message-id))) -(defn- sign-up-confirmed [{:keys [db] :as fx}] - (cond-> (update fx :dispatch-n conj [:request-permissions - [:read-contacts] - #(re-frame/dispatch [:sync-contacts (fn [contacts] - [::contacts-synced contacts])])]) - (:confirmation-code-sms-listener db) - (merge {:db (dissoc db :confirmation-code-sms-listener) - :remove-sms-listener (:confirmation-code-sms-listener db)}))) +(defn- sign-up-confirmed [{:keys [db] :as fx} now] + (let [last-phone-number (extract-last-phone-number (:chats db))] + (cond-> + (stop-listening-confirmation-code-sms fx) + + true + (update :dispatch-n conj [:request-permissions [:read-contacts] + #(re-frame/dispatch [:sync-contacts (fn [contacts] + [::contacts-synced contacts])])]) + + last-phone-number + (accounts-events/account-update {:phone last-phone-number + :last-updated now})))) (handlers/register-handler-fx ::sign-up-confirm-response [re-frame/trim-v (re-frame/inject-cofx :random-id)] - (fn [{:keys [db random-id]} [{:keys [message status]} message-id]] - (cond-> {:db db + (fn [{:keys [db random-id now]} [{:keys [message status]} message-id]] + (cond-> {:db db :dispatch-n [[:received-message {:message-id random-id :content message @@ -98,7 +133,7 @@ (message-seen message-id) (= "confirmed" status) - sign-up-confirmed + (sign-up-confirmed now) (= "failed" status) (update :dispatch-n conj (sign-up/incorrect-confirmation-code-event random-id))))) @@ -106,12 +141,11 @@ (handlers/register-handler-fx ::contacts-synced [re-frame/trim-v (re-frame/inject-cofx :random-id)] - (fn [{:keys [db random-id] :as cofx} [contacts]] + (fn [{:keys [db random-id now] :as cofx} [contacts]] (-> {:db db} - (as-> fx - (merge fx - (accounts-events/account-update (assoc cofx :db (:db fx)) {:signed-up? true}) - {:dispatch (sign-up/contacts-synchronised-event random-id)}))))) + (accounts-events/account-update {:signed-up? true + :last-updated now}) + (assoc :dispatch (sign-up/contacts-synchronised-event random-id))))) (handlers/register-handler-fx ::http-request-failure diff --git a/src/status_im/chat/sign_up.cljs b/src/status_im/chat/sign_up.cljs index ae9b697539..d05a2df42d 100644 --- a/src/status_im/chat/sign_up.cljs +++ b/src/status_im/chat/sign_up.cljs @@ -2,8 +2,6 @@ (:require [re-frame.core :refer [subscribe dispatch dispatch-sync]] [status-im.components.styles :refer [default-chat-color]] [status-im.utils.random :as random] - [status-im.utils.sms-listener :refer [add-sms-listener - remove-sms-listener]] [status-im.constants :as const] [status-im.chat.constants :as chat-const] [status-im.i18n :refer [label]] @@ -17,21 +15,14 @@ :content-type const/text-content-type :outgoing true}) -;; todo fn name is not too smart, but... -(defn command-content - [command content] - {:command (name command) - :content content}) - ;; -- Send phone number ---------------------------------------- (defn- confirmation-code-event [correct? message-id] [:received-message {:message-id message-id - :content (command-content - :confirmation-code - (if correct? - (label :t/confirmation-code) - (label :t/incorrect-code))) + :content {:command "confirmation-code" + :content (if correct? + (label :t/confirmation-code) + (label :t/incorrect-code))} :content-type const/content-type-command-request :outgoing false :chat-id const/console-chat-id @@ -41,17 +32,6 @@ (def enter-confirmation-code-event (partial confirmation-code-event true)) (def incorrect-confirmation-code-event (partial confirmation-code-event false)) -(defn- sms-receive-handler [{confirmation-code :body}] - (when-let [matches (re-matches #"(\d{4})" confirmation-code)] - (dispatch [:sign-up-confirm (second matches)]))) - -(def start-listening-confirmation-code-sms-event - [:request-permissions - [:receive-sms] - (fn [] - (let [listener (add-sms-listener sms-receive-handler)] - (dispatch [:start-listening-confirmation-code-sms listener])))]) - ;; -- Send confirmation code and synchronize contacts--------------------------- (defn contacts-synchronised-event [message-id] [:received-message @@ -64,12 +44,10 @@ :to "me"}]) (def start-signup-events - ;; TODO(janherich): show this message again once `/phone` command is working properly again - [#_[:received-message + [[:received-message {:message-id (random/id) - :content (command-content - :phone - (label :t/phone-number-required)) + :content {:command "phone" + :content (label :t/phone-number-required)} :content-type const/content-type-command-request :outgoing false :chat-id const/console-chat-id @@ -98,9 +76,8 @@ (def move-to-internal-failure-event [:received-message {:message-id chat-const/move-to-internal-failure-message-id - :content (command-content - :grant-permissions - (label :t/move-to-internal-failure-message)) + :content {:command "grant-permissions" + :content (label :t/move-to-internal-failure-message)} :content-type const/content-type-command-request :outgoing false :chat-id const/console-chat-id @@ -159,9 +136,8 @@ const/console-chat-id {:chat-id const/console-chat-id :message-id chat-const/intro-message1-id - :content (command-content - :password - (label :t/intro-message1)) + :content {:command "password" + :content (label :t/intro-message1)} :content-type const/content-type-command-request :outgoing false :from const/console-chat-id diff --git a/src/status_im/ui/screens/accounts/events.cljs b/src/status_im/ui/screens/accounts/events.cljs index f00b350ca8..454e9d8ead 100644 --- a/src/status_im/ui/screens/accounts/events.cljs +++ b/src/status_im/ui/screens/accounts/events.cljs @@ -161,29 +161,19 @@ (when (seq hashtags) {:dispatch [:broadcast-status status hashtags]})))))) -(defn- update-account [current-account new-fields now] - (merge current-account (assoc new-fields :last-updated now))) - (defn account-update "Takes map of coeffects containing `:db` and `:now` keys + new account fields, returns all effects necessary for account update." - [{{:keys [network] - :accounts/keys [accounts current-account-id] :as db} :db now :now} new-account-fields] + [{{:accounts/keys [accounts current-account-id] :as db} :db :as fx} new-account-fields] (let [current-account (get accounts current-account-id) - new-account (update-account current-account new-account-fields now)] - {:db (assoc-in db [:accounts/accounts current-account-id] new-account) - ::save-account new-account - ::broadcast-account-update (merge - (select-keys db [:current-public-key :web3]) - (select-keys new-account [:name :photo-path :status - :updates-public-key :updates-private-key]))})) - -;; TODO(janherich): remove this event once it's not used anymore -(handlers/register-handler-fx - :account-update - [re-frame/trim-v] - (fn [cofx [new-account-fields]] - (account-update cofx new-account-fields))) + new-account (merge current-account new-account-fields)] + (-> fx + (assoc-in [:db :accounts/accounts current-account-id] new-account) + (assoc ::save-account new-account) + (assoc ::broadcast-account-update (merge + (select-keys db [:current-public-key :web3]) + (select-keys new-account [:name :photo-path :status + :updates-public-key :updates-private-key])))))) (handlers/register-handler-fx :account-update-keys @@ -192,9 +182,9 @@ (let [{:accounts/keys [accounts current-account-id]} db {:keys [public private]} keypair current-account (get accounts current-account-id) - new-account (update-account current-account {:updates-public-key public - :updates-private-key private} - now)] + new-account (merge current-account {:updates-public-key public + :updates-private-key private + :last-updated now})] {:db (assoc-in db [:accounts/accounts current-account-id] new-account) ::save-account new-account ::send-keys-update (merge @@ -204,14 +194,14 @@ (handlers/register-handler-fx :send-account-update-if-needed - (fn [{{:accounts/keys [accounts current-account-id]} :db now :now :as cofx} _] + (fn [{{:accounts/keys [accounts current-account-id] :as db} :db now :now} _] (let [{:keys [last-updated]} (get accounts current-account-id) needs-update? (> (- now last-updated) time/week)] (log/info "Need to send account-update: " needs-update?) (when needs-update? ;; TODO(janherich): this is very strange and misleading, need to figure out why it'd necessary to update ;; account with network update when last update was more then week ago - (account-update cofx nil))))) + (account-update {:db db} nil))))) (handlers/register-handler-db :set-current-account diff --git a/src/status_im/ui/screens/events.cljs b/src/status_im/ui/screens/events.cljs index 2e836ddae6..cc32bfc4b1 100644 --- a/src/status_im/ui/screens/events.cljs +++ b/src/status_im/ui/screens/events.cljs @@ -26,7 +26,6 @@ [status-im.i18n :as i18n] [status-im.js-dependencies :as dependencies] [status-im.ui.screens.db :refer [app-db]] - [status-im.utils.sms-listener :as sms-listener-util] [status-im.utils.datetime :as time] [status-im.utils.random :as random] [status-im.utils.config :as config] @@ -115,11 +114,6 @@ #(dispatch (success-event-creator %)) #(dispatch (failure-event-creator %))))) -(reg-fx - :remove-sms-listener - (fn [subscription] - (sms-listener-util/remove-sms-listener subscription))) - (reg-fx ::init-store (fn [] diff --git a/src/status_im/ui/screens/network_settings/events.cljs b/src/status_im/ui/screens/network_settings/events.cljs index f8fb1aea1e..474094d97b 100644 --- a/src/status_im/ui/screens/network_settings/events.cljs +++ b/src/status_im/ui/screens/network_settings/events.cljs @@ -31,19 +31,22 @@ (defn network-with-upstream-rpc? [networks network] (get-in networks [network :raw-config :UpstreamConfig :Enabled])) -(defn connect-network [cofx [_ network]] - (merge (accounts-events/account-update cofx {:network network}) - {:close-application nil})) - -(handlers/register-handler-fx ::save-network connect-network) +(handlers/register-handler-fx + ::save-network + (fn [{:keys [db now]} [_ network]] + (accounts-events/account-update {:db db + :close-application nil} + {:network network + :last-updated now}))) (handlers/register-handler-fx :connect-network - (fn [{:keys [db] :as cofx} [_ network]] + (fn [{:keys [db now]} [_ network]] (let [current-network (:network db) networks (:networks/networks db)] (if (network-with-upstream-rpc? networks current-network) - (merge (accounts-events/account-update cofx {:network network}) + (merge (accounts-events/account-update {:db db} {:network network + :last-updated now}) {:dispatch [:navigate-to-clean :accounts] :stop-whisper nil}) {:show-confirmation {:title (i18n/label :t/close-app-title) diff --git a/src/status_im/ui/screens/profile/events.cljs b/src/status_im/ui/screens/profile/events.cljs index 01f2a2f487..6840272378 100644 --- a/src/status_im/ui/screens/profile/events.cljs +++ b/src/status_im/ui/screens/profile/events.cljs @@ -6,6 +6,7 @@ [status-im.constants :refer [console-chat-id]] [status-im.ui.screens.profile.db :as db] [status-im.ui.screens.profile.navigation] + [status-im.ui.screens.accounts.events :as accounts-events] [status-im.utils.gfycat.core :as gfycat] [status-im.utils.handlers :as handlers] [status-im.utils.image-processing :refer [img->base64]] @@ -131,36 +132,39 @@ name (get-in db [:accounts/accounts current-account-id :name])))) -(defn clear-profile [db] - (dissoc db :my-profile/profile :my-profile/drawer :my-profile/default-name)) +(defn clear-profile [fx] + (update fx :db dissoc :my-profile/profile :my-profile/drawer :my-profile/default-name)) (handlers/register-handler-fx :my-profile.drawer/save-name - (fn [{:keys [db]} _] + (fn [{:keys [db now]} _] (let [cleaned-name (clean-name db :my-profile/drawer)] - {:db (clear-profile db) - :dispatch [:account-update {:name cleaned-name}]}))) + (-> (clear-profile {:db db}) + (accounts-events/account-update {:name cleaned-name + :last-updated now}))))) (handlers/register-handler-fx :my-profile.drawer/save-status - (fn [{:keys [db]} _] + (fn [{:keys [db now]} _] (let [status (get-in db [:my-profile/drawer :status]) - new-db (clear-profile db)] + new-fx (clear-profile {:db db})] (if (string/blank? status) - {:db new-db} - {:db new-db - :dispatch-n [[:check-status-change status] - [:account-update {:status status}]]})))) + new-fx + (-> new-fx + (accounts-events/account-update {:status status + :last-updated now}) + (assoc :dispatch-n [[:check-status-change status]])))))) (handlers/register-handler-fx :my-profile/save-profile - (fn [{:keys [db]} _] + (fn [{:keys [db now]} _] (let [{:keys [status photo-path]} (:my-profile/profile db) - cleaned-name (clean-name db :my-profile/profile) - cleaned-edit {:name cleaned-name - :status status - :photo-path photo-path}] - {:db (clear-profile db) - :dispatch-n [[:check-status-change status] - [:account-update cleaned-edit] - [:navigate-back]]}))) + cleaned-name (clean-name db :my-profile/profile) + cleaned-edit {:name cleaned-name + :status status + :photo-path photo-path + :last-updated now}] + (-> (clear-profile {:db db}) + (accounts-events/account-update cleaned-edit) + (assoc :dispatch-n [[:check-status-change status] + [:navigate-back]]))))) diff --git a/src/status_im/ui/screens/profile/views.cljs b/src/status_im/ui/screens/profile/views.cljs index 7a5d2bd5c6..567512706f 100644 --- a/src/status_im/ui/screens/profile/views.cljs +++ b/src/status_im/ui/screens/profile/views.cljs @@ -175,7 +175,7 @@ [info-item-separator] [profile-info-phone-item phone - [{:value #(dispatch [:my-profile/change-phone-number]) + [{:value #(dispatch [:my-profile/update-phone-number]) :text (label :t/edit)}]] [info-item-separator] [network-settings]]) diff --git a/src/status_im/utils/sms_listener.cljs b/src/status_im/utils/sms_listener.cljs index 952088296a..b8bad53d55 100644 --- a/src/status_im/utils/sms_listener.cljs +++ b/src/status_im/utils/sms_listener.cljs @@ -4,6 +4,7 @@ [status-im.react-native.js-dependencies :as rn-dependencies])) ;; Only android is supported! + (defn add-sms-listener "Message format: {:originatingAddress string, :body string}. Returns cancelable subscription." diff --git a/test/cljs/status_im/test/accounts/events.cljs b/test/cljs/status_im/test/accounts/events.cljs index 931fe5a581..424290963f 100644 --- a/test/cljs/status_im/test/accounts/events.cljs +++ b/test/cljs/status_im/test/accounts/events.cljs @@ -88,26 +88,12 @@ (is (= {(:address account-from-realm) account-from-realm (:address new-account) new-account'} @accounts)) - (testing ":account-update event" + (testing ":account-update-keys event" - (let [new-account'' (assoc new-account' - :status "new status" - :name "new name")] + (rf/dispatch [:account-update-keys]) - (rf/dispatch [:set-current-account (:address new-account)]) - - (rf/dispatch [:account-update {:status "new status" :name "new name"}]) - - (is (= {(:address account-from-realm) account-from-realm - (:address new-account) new-account''} - (update @accounts (:address new-account) dissoc :last-updated))) - - (testing ":account-update-keys event" - - (rf/dispatch [:account-update-keys]) - - (is (= {(:address account-from-realm) account-from-realm - (:address new-account) (assoc new-account'' - :updates-private-key "new private" - :updates-public-key "new public")} - (update @accounts (:address new-account) dissoc :last-updated))))))))))) + (is (= {(:address account-from-realm) account-from-realm + (:address new-account) (assoc new-account' + :updates-private-key "new private" + :updates-public-key "new public")} + (update @accounts (:address new-account) dissoc :last-updated)))))))))