Confirm pairing messages

Partially addresses https://github.com/status-im/status-react/issues/7513
This commit adds confirmation to pairing messages, which previously were
not confirmed.

Signed-off-by: Igor Mandrigin <i@mandrigin.ru>
This commit is contained in:
Andrea Maria Piana 2019-02-18 16:31:46 +01:00 committed by Igor Mandrigin
parent d6bd685703
commit 71654a6829
No known key found for this signature in database
GPG Key ID: 4A0EDDE26E66BC8B
2 changed files with 27 additions and 14 deletions

View File

@ -35,6 +35,11 @@
device-type utils.platform/os] device-type utils.platform/os]
(protocol/send (transport.pairing/PairInstallation. installation-id device-type installation-name fcm-token) nil cofx))) (protocol/send (transport.pairing/PairInstallation. installation-id device-type installation-name fcm-token) nil cofx)))
(fx/defn confirm-message-processed
[{:keys [db]} raw-message]
{:transport/confirm-messages-processed [{:web3 (:web3 db)
:js-obj raw-message}]})
(defn has-paired-installations? [cofx] (defn has-paired-installations? [cofx]
(->> (->>
(get-in cofx [:db :pairing/installations]) (get-in cofx [:db :pairing/installations])
@ -86,13 +91,15 @@
:on-cancel #(re-frame/dispatch [:pairing.ui/prompt-dismissed]) :on-cancel #(re-frame/dispatch [:pairing.ui/prompt-dismissed])
:on-accept #(re-frame/dispatch [:pairing.ui/prompt-accepted])}})) :on-accept #(re-frame/dispatch [:pairing.ui/prompt-accepted])}}))
(fx/defn upsert-installation [{:keys [db]} {:keys [installation-id] :as new-installation}] (fx/defn upsert-installation [{:keys [db] :as cofx} {:keys [installation-id] :as new-installation}]
(let [old-installation (get-in db [:pairing/installations installation-id]) (let [success-event [:message/messages-persisted [(or (:dedup-id cofx) (:js-obj cofx))]]
old-installation (get-in db [:pairing/installations installation-id])
updated-installation (merge old-installation new-installation)] updated-installation (merge old-installation new-installation)]
{:db (assoc-in db {:db (assoc-in db
[:pairing/installations installation-id] [:pairing/installations installation-id]
updated-installation) updated-installation)
:data-store/tx [(data-store.installations/save updated-installation)]})) :data-store/tx [{:transaction (data-store.installations/save updated-installation)
:success-event success-event}]}))
(defn handle-bundles-added [{:keys [db] :as cofx} bundle] (defn handle-bundles-added [{:keys [db] :as cofx} bundle]
(let [installation-id (:installationID bundle) (let [installation-id (:installationID bundle)
@ -234,31 +241,37 @@
contacts)) contacts))
(defn handle-sync-installation [{:keys [db] :as cofx} {:keys [contacts account chat]} sender] (defn handle-sync-installation [{:keys [db] :as cofx} {:keys [contacts account chat]} sender]
(when (= sender (accounts.db/current-public-key cofx)) (if (= sender (accounts.db/current-public-key cofx))
(let [new-contacts (vals (merge-contacts (:contacts/contacts db) (ensure-photo-path contacts))) (let [success-event [:message/messages-persisted [(or (:dedup-id cofx) (:js-obj cofx))]]
new-contacts (vals (merge-contacts (:contacts/contacts db) (ensure-photo-path contacts)))
new-account (merge-account (:account/account db) account) new-account (merge-account (:account/account db) account)
contacts-fx (mapv contact/upsert-contact new-contacts)] contacts-fx (mapv contact/upsert-contact new-contacts)]
(apply fx/merge (apply fx/merge
cofx cofx
(concat (concat
[{:db (assoc db :account/account new-account) [{:db (assoc db :account/account new-account)
:data-store/base-tx [(data-store.accounts/save-account-tx new-account)]} :data-store/base-tx [{:transaction (data-store.accounts/save-account-tx new-account)
:success-event success-event}]}
#(when (:public? chat) #(when (:public? chat)
(models.chat/start-public-chat % (:chat-id chat) {:dont-navigate? true}))] (models.chat/start-public-chat % (:chat-id chat) {:dont-navigate? true}))]
contacts-fx))))) contacts-fx)))
(confirm-message-processed cofx (or (:dedup-id cofx)
(:js-obj cofx)))))
(defn handle-pair-installation [{:keys [db] :as cofx} {:keys [name (defn handle-pair-installation [{:keys [db] :as cofx} {:keys [name
fcm-token fcm-token
installation-id installation-id
device-type]} timestamp sender] device-type]} timestamp sender]
(when (and (= sender (accounts.db/current-public-key cofx)) (if (and (= sender (accounts.db/current-public-key cofx))
(not= (get-in db [:account/account :installation-id]) installation-id)) (not= (get-in db [:account/account :installation-id]) installation-id))
(let [installation {:installation-id installation-id (let [installation {:installation-id installation-id
:name name :name name
:fcm-token fcm-token :fcm-token fcm-token
:device-type device-type :device-type device-type
:last-paired timestamp}] :last-paired timestamp}]
(upsert-installation cofx installation)))) (upsert-installation cofx installation))
(confirm-message-processed cofx (or (:dedup-id cofx)
(:js-obj cofx)))))
(fx/defn set-name [{:keys [db] :as cofx} installation-name] (fx/defn set-name [{:keys [db] :as cofx} installation-name]
(let [new-account (assoc (get-in cofx [:db :account/account]) :installation-name installation-name)] (let [new-account (assoc (get-in cofx [:db :account/account]) :installation-name installation-name)]

View File

@ -168,7 +168,7 @@
:photo-path "generated") :photo-path "generated")
"contact-5" (assoc local-contact-5 :device-info nil)}] "contact-5" (assoc local-contact-5 :device-info nil)}]
(testing "not coming from us" (testing "not coming from us"
(is (not (pairing/handle-sync-installation cofx sync-message "not-us")))) (is (not (:db (pairing/handle-sync-installation cofx sync-message "not-us")))))
(testing "coming from us" (testing "coming from us"
(is (= expected (get-in (is (= expected (get-in
(pairing/handle-sync-installation cofx sync-message "us") (pairing/handle-sync-installation cofx sync-message "us")
@ -212,7 +212,7 @@
:name "name" :name "name"
:installation-id "1"}] :installation-id "1"}]
(testing "not coming from us" (testing "not coming from us"
(is (not (pairing/handle-pair-installation cofx pair-message 1 "not-us")))) (is (not (:db (pairing/handle-pair-installation cofx pair-message 1 "not-us")))))
(testing "coming from us" (testing "coming from us"
(is (= {"1" {:has-bundle? true (is (= {"1" {:has-bundle? true
:installation-id "1" :installation-id "1"