Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
alwx committed Feb 7, 2024
1 parent aa90878 commit cd3b934
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 70 deletions.
8 changes: 6 additions & 2 deletions src/legacy/status_im/mobile_sync_settings/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
[legacy.status-im.multiaccounts.update.core :as multiaccounts.update]
[legacy.status-im.utils.mobile-sync :as utils]
[legacy.status-im.wallet.core :as wallet]
[status-im.contexts.chat.home.add-new-contact.events :as add-new-contact]
[status-im.navigation.events :as navigation]
[taoensso.timbre :as log]
[utils.re-frame :as rf]))

(rf/defn set-new-identity-reconnected
[{:keys [db]}]
(let [input (get-in db [:contacts/new-identity :input])]
(rf/dispatch [:contacts/set-new-identity {:input input}])))

(rf/defn sheet-defaults
[{:keys [db]}]
(let [remember-choice? (get-in db [:profile/profile :remember-syncing-choice?])]
Expand All @@ -36,7 +40,7 @@
[(mailserver/process-next-messages-request)
(bottom-sheet/hide-bottom-sheet-old)
(wallet/restart-wallet-service nil)
(add-new-contact/set-new-identity-reconnected)]
(set-new-identity-reconnected)]

logged-in?
[(mailserver/process-next-messages-request)
Expand Down
2 changes: 1 addition & 1 deletion src/legacy/status_im/profile/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,6 @@
(assoc :contacts/ens-name ens-name))
:dispatch [:contacts/build-contact {:pubkey identity
:ens ens-name
:success-fn (fn [contact]
:success-fn (fn [_]
{:dispatch [:open-modal :profile]})}]}
{:dispatch [:navigate-to :my-profile]})))
129 changes: 72 additions & 57 deletions src/status_im/contexts/chat/home/add_new_contact/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
[status-im.common.validators :as validators]
[status-im.contexts.chat.contacts.events :as data-store.contacts]
status-im.contexts.chat.home.add-new-contact.effects
[status-im.navigation.events :as navigation]
[utils.ens.stateofus :as stateofus]
[utils.ethereum.chain :as chain]
[utils.re-frame :as rf]
Expand Down Expand Up @@ -88,68 +87,63 @@

(def validate-contact (comp ->state ->type ->id))

(declare build-contact)

(defn set-new-identity
[{:keys [db]} [{:keys [input build-success-fn]}]]
[{:keys [db]} [{:keys [input build-success-fn failure-fn]}]]
(let [user-public-key (get-in db [:profile/profile :public-key])
{:keys [input id ens state]
:as contact} (-> {:user-public-key user-public-key
:input input}
:input input
:scanned input}
init-contact
validate-contact)]
(case state
:empty {:db (dissoc db :contacts/new-identity)}
:empty {:db (dissoc db :contacts/new-identity)}
(:valid :invalid) {:db (assoc db :contacts/new-identity contact)}
:decompress-key {:db (assoc db :contacts/new-identity contact)
:serialization/decompress-public-key
{:compressed-key id
:on-success
#(rf/dispatch [:contacts/set-new-identity-success input % build-success-fn])
:on-error
#(rf/dispatch [:contacts/set-new-identity-error input %])}}
:resolve-ens {:db (assoc db :contacts/new-identity contact)
:effects.contacts/resolve-public-key-from-ens
{:chain-id (chain/chain-id db)
:ens ens
:on-success
#(rf/dispatch [:contacts/set-new-identity-success input % build-success-fn])
:on-error
#(rf/dispatch [:contacts/set-new-identity-error input %])}})))
:decompress-key {:db (assoc db :contacts/new-identity contact)
:serialization/decompress-public-key
{:compressed-key id
:on-success
#(re-frame/dispatch [:contacts/set-new-identity-success
{:input input
:pubkey %
:build-success-fn build-success-fn}])
:on-error
#(re-frame/dispatch [:contacts/set-new-identity-error
{:input input
:pubkey %
:failure-fn failure-fn}])}}
:resolve-ens {:db (assoc db :contacts/new-identity contact)
:effects.contacts/resolve-public-key-from-ens
{:chain-id (chain/chain-id db)
:ens ens
:on-success
#(re-frame/dispatch [:contacts/set-new-identity-success
{:input input
:pubkey %
:build-success-fn build-success-fn}])
:on-error
#(re-frame/dispatch [:contacts/set-new-identity-error
{:input input
:pubkey %
:failure-fn failure-fn}])}})))

(re-frame/reg-event-fx :contacts/set-new-identity set-new-identity)

(rf/defn build-contact
{:events [:contacts/build-contact]}
[_ {:keys [pubkey ens success-fn]}]
(js/console.log "ALWX ens" ens)
{:json-rpc/call [{:method "wakuext_buildContact"
:params [{:publicKey pubkey
:ENSName ens}]
:js-response true
:on-success #(rf/dispatch [:contacts/build-contact-success
{:pubkey pubkey
:contact (data-store.contacts/<-rpc-js %)
:success-fn success-fn}])}]})

(rf/defn build-contact-success
{:events [:contacts/build-contact-success]}
[{:keys [db]} {:keys [pubkey contact success-fn]}]
(merge {:db (assoc-in db [:contacts/contacts pubkey] contact)}
(when success-fn
(success-fn contact))))

(rf/defn set-new-identity-success
{:events [:contacts/set-new-identity-success]}
[{:keys [db]} input pubkey build-success-fn]
(defn set-new-identity-success
[{:keys [db]} [{:keys [input pubkey build-success-fn]}]]
(let [contact (get-in db [:contacts/new-identity])]
(when (= (:input contact) input)
(rf/merge {:db (assoc db :contacts/new-identity (->state (assoc contact :public-key pubkey)))}
(build-contact {:pubkey pubkey
:ens (:ens contact)
:success-fn build-success-fn})))))

(rf/defn set-new-identity-error
{:events [:contacts/set-new-identity-error]}
[{:keys [db]} input err]
(merge {:db (assoc db :contacts/new-identity (->state (assoc contact :public-key pubkey)))}
(build-contact {:pubkey pubkey
:ens (:ens contact)
:success-fn build-success-fn})))))

(re-frame/reg-event-fx :contacts/set-new-identity-success set-new-identity-success)

(defn set-new-identity-error
[{:keys [db]} [{:keys [input err failure-fn]}]]
(let [contact (get-in db [:contacts/new-identity])]
(when (= (:input contact) input)
(let [state (cond
Expand All @@ -160,14 +154,35 @@
(string/includes? (:message err) "no such host")))
{:state :invalid :msg :t/lost-connection}
:else {:state :invalid})]
{:db (assoc db :contacts/new-identity (merge contact state))}))))
(merge {:db (assoc db :contacts/new-identity (merge contact state))}
(when failure-fn
(failure-fn)))))))

(re-frame/reg-event-fx :contacts/set-new-identity-error set-new-identity-error)

(rf/defn clear-new-identity
{:events [:contacts/clear-new-identity :contacts/new-chat-focus]}
(defn build-contact
[{:keys [pubkey ens success-fn]}]
{:json-rpc/call [{:method "wakuext_buildContact"
:params [{:publicKey pubkey
:ENSName ens}]
:js-response true
:on-success #(re-frame/dispatch [:contacts/build-contact-success
{:pubkey pubkey
:contact (data-store.contacts/<-rpc-js %)
:success-fn success-fn}])}]})

(re-frame/reg-event-fx :contacts/build-contact (fn [_ [contact-data]] (build-contact contact-data)))

(defn build-contact-success
[{:keys [db]} [{:keys [pubkey contact success-fn]}]]
(merge {:db (assoc-in db [:contacts/contacts pubkey] contact)}
(when success-fn
(success-fn contact))))

(re-frame/reg-event-fx :contacts/build-contact-success build-contact-success)

(defn clear-new-identity
[{:keys [db]}]
{:db (dissoc db :contacts/new-identity)})

(rf/defn set-new-identity-reconnected
[{:keys [db]}]
(let [input (get-in db [:contacts/new-identity :input])]
(rf/dispatch [:contacts/set-new-identity {:input input}])))
(re-frame/reg-event-fx :contacts/clear-new-identity clear-new-identity)
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@

(defn new-contact
[]
(let [{:keys [public-key ens state msg]} (rf/sub [:contacts/new-identity])
(let [{:keys [public-key ens state msg] :as i} (rf/sub [:contacts/new-identity])
customization-color (rf/sub [:profile/customization-color])]
[rn/keyboard-avoiding-view {:style {:flex 1}}
[rn/touchable-without-feedback {:on-press rn/dismiss-keyboard!}
Expand Down
31 changes: 22 additions & 9 deletions src/status_im/contexts/shell/qr_reader/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
[status-im.common.scan-qr-code.view :as scan-qr-code]
[status-im.contexts.communities.events]
[status-im.contexts.wallet.common.validation :as wallet-validation]
[status-im.navigation.events :as navigation]
[utils.debounce :as debounce]
[utils.i18n :as i18n]))

(def invalid-qr-toast {:type :negative
:theme :dark
:text (i18n/label :t/invalid-qr)})

(defn- text-for-path? [text path]
(some #(string/starts-with? text %) (router/path-urls path)))

Expand All @@ -28,20 +31,33 @@
[scanned-text]
false)

(defn load-and-show-profile [address]
(debounce/debounce-and-dispatch
[:contacts/set-new-identity
{:input address
:build-success-fn (fn [{:keys [public-key ens-name]}]
{:dispatch-n [[:chat.ui/show-profile public-key ens-name]
[:contacts/clear-new-identity]]})
:failure-fn (fn []
{:dispatch [:toasts/upsert invalid-qr-toast]})}]
300))

(defn show-invalid-qr-toast []
(debounce/debounce-and-dispatch
[:toasts/upsert invalid-qr-toast] 300))

(defn on-qr-code-scanned [scanned-text]
(let [address (extract-id scanned-text)]
(cond
(text-for-path? scanned-text router/community-with-data-path)
nil
;;(debounce/debounce-and-dispatch [:communities/navigate-to-community-overview address] 300)
nil

(text-for-path? scanned-text router/channel-path)
nil

(text-for-path? scanned-text router/user-with-data-path)
(debounce/debounce-and-dispatch [:contacts/set-new-identity {:input address
:build-success-fn (fn [{:keys [public-key ens-name] :as contact}]
{:dispatch-n [[:chat.ui/show-profile public-key ens-name]]})}] 300)
(load-and-show-profile address)

(legacy-eth-address? scanned-text)
;; :wallet/scan-address-success
Expand All @@ -52,10 +68,7 @@
nil

:else
(debounce/debounce-and-dispatch [:toasts/upsert {:type :negative
:theme :dark
:text (i18n/label :t/invalid-qr)}]
300))))
(show-invalid-qr-toast))))

(defn- f-internal-view
[]
Expand Down

0 comments on commit cd3b934

Please sign in to comment.