Skip to content

Commit

Permalink
Fix navigation to chat when PN is tapped while signed off. Fixes #3488
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Pombeiro committed Jan 8, 2019
1 parent 5520dfd commit 248609b
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 58 deletions.
4 changes: 2 additions & 2 deletions doc/codebase-structure-and-guidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ These guidelines make db.cljs namespaces the place to go when making changes to
- events must only contain a function call defined in a module
```clojure
(handlers/register-handler-fx
:notifications/handle-push-notification
:notifications/handle-push-notification-open
(fn [cofx [_ event]]
(notifications/handle-push-notification event cofx)))
(notifications/handle-push-notification-open event cofx)))
```
- events must use synthetic namespaces:

Expand Down
4 changes: 2 additions & 2 deletions src/status_im/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -814,9 +814,9 @@
;; notifications module

(handlers/register-handler-fx
:notifications/notification-event-received
:notifications/notification-open-event-received
(fn [cofx [_ event]]
(notifications/handle-push-notification cofx event)))
(notifications/handle-push-notification-open cofx event)))

(handlers/register-handler-fx
:notifications.callback/get-fcm-token-success
Expand Down
35 changes: 18 additions & 17 deletions src/status_im/init/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,12 @@
(= view-id :create-account)
(assoc-in [:accounts/create :step] :enter-name))}))

(defn login-only-events [cofx address]
(defn login-only-events [cofx address stored-pns]
(fx/merge cofx
{:notifications/request-notifications-permissions nil}
(navigation/navigate-to-cofx :home nil)
(universal-links/process-stored-event)
(notifications/process-stored-event address)
(notifications/process-stored-event address stored-pns)
(when platform/desktop?
(chat-model/update-dock-badge-label))))

Expand All @@ -213,22 +213,23 @@
(= (get-in cofx [:db :view-id])
:hardwallet-success))

(fx/defn initialize-account [cofx address]
(fx/merge cofx
{:notifications/get-fcm-token nil}
(initialize-account-db address)
(contact/load-contacts)
(pairing/load-installations)
#(when (dev-mode? %)
(models.dev-server/start))
(browser/initialize-browsers)
(fx/defn initialize-account [{:keys [db] :as cofx} address]
(let [stored-pns (:push-notifications/stored db)]
(fx/merge cofx
{:notifications/get-fcm-token nil}
(initialize-account-db address)
(contact/load-contacts)
(pairing/load-installations)
#(when (dev-mode? %)
(models.dev-server/start))
(browser/initialize-browsers)

(browser/initialize-dapp-permissions)
(extensions.registry/initialize)
(accounts.update/update-sign-in-time)
#(when-not (or (creating-account? %)
(finishing-hardwallet-setup? %))
(login-only-events % address))))
(browser/initialize-dapp-permissions)
(extensions.registry/initialize)
(accounts.update/update-sign-in-time)
#(when-not (or (creating-account? %)
(finishing-hardwallet-setup? %))
(login-only-events % address stored-pns)))))

(re-frame/reg-fx
:init/init-store
Expand Down
41 changes: 23 additions & 18 deletions src/status_im/notifications/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -125,19 +125,20 @@
:to to
:from from}})))

(fx/defn handle-push-notification
[{:keys [db] :as cofx} {:keys [from to] :as event}]
(let [current-public-key (accounts.db/current-public-key cofx)]
(fx/defn handle-push-notification-open
[{:keys [db] :as cofx} {:keys [from to stored?] :as event}]
(let [current-public-key (accounts.db/current-public-key cofx)
nav-opts (when stored? {:navigation-reset? true})]
(if (= to current-public-key)
(fx/merge cofx
{:db (update db :push-notifications/stored dissoc to)}
(chat-model/navigate-to-chat from nil))
(chat-model/navigate-to-chat from nav-opts))
{:db (assoc-in db [:push-notifications/stored to] from)})))

(defn handle-notification-event [event] ;; https://github.com/invertase/react-native-firebase/blob/adcbeac3d11585dd63922ef178ff6fd886d5aa9b/src/modules/notifications/Notification.js#L13
(defn handle-notification-open-event [event] ;; https://github.com/invertase/react-native-firebase/blob/adcbeac3d11585dd63922ef178ff6fd886d5aa9b/src/modules/notifications/Notification.js#L13
(let [payload (get-notification-payload (.. event -notification))]
(when payload
(re-frame/dispatch [:notifications/notification-event-received payload]))))
(re-frame/dispatch [:notifications/notification-open-event-received payload]))))

(defn handle-initial-push-notification
"This method handles pending push notifications. It is only needed to handle PNs from legacy clients (which use firebase.notifications API)"
Expand All @@ -147,7 +148,7 @@
getInitialNotification
(then (fn [event]
(when event
(handle-notification-event event))))))
(handle-notification-open-event event))))))

(defn setup-token-refresh-callback []
(.onTokenRefresh (.messaging firebase)
Expand Down Expand Up @@ -182,7 +183,7 @@
(defn setup-on-notification-opened-callback []
(.. firebase
notifications
(onNotificationOpened handle-notification-event)))
(onNotificationOpened handle-notification-open-event)))

(defn init []
(setup-token-refresh-callback)
Expand All @@ -192,17 +193,21 @@
(when platform/android?
(create-notification-channel))))

(fx/defn process-stored-event [cofx address]
(fx/defn process-stored-event [cofx address stored-pns]
(when-not platform/desktop?
(let [current-account (get-in cofx [:db :account/account])
current-address (:address current-account)
to (:public-key current-account)
from (get-in cofx [:db :push-notifications/stored to])]
(when (and from
(= address current-address))
(handle-push-notification cofx
{:from from
:to to})))))
(if (accounts.db/logged-in? cofx)
(let [current-account (get-in cofx [:db :account/account])
current-address (:address current-account)
to (:public-key current-account)
from (get stored-pns to)]
(log/debug "process-stored-event" "address" address "from" from "to" to)
(when (and from
(= address current-address))
(handle-push-notification-open cofx
{:from from
:to to
:stored? true})))
(log/error "process-stored-event called without user being logged in!"))))

(re-frame/reg-fx
:notifications/display-notification
Expand Down
4 changes: 2 additions & 2 deletions src/status_im/utils/universal_links/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@
(re-frame/dispatch [:handle-universal-link url])))

(fx/defn handle-browse [cofx url]
(log/info "universal-links: handling browse " url)
(log/info "universal-links: handling browse" url)
{:browser/show-browser-selection url})

(fx/defn handle-public-chat [cofx public-chat]
(log/info "universal-links: handling public chat " public-chat)
(log/info "universal-links: handling public chat" public-chat)
(chat/start-public-chat cofx public-chat {:navigation-reset? true}))

(fx/defn handle-view-profile [{:keys [db] :as cofx} public-key]
Expand Down
43 changes: 26 additions & 17 deletions test/cljs/status_im/test/notifications/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,43 @@
(:require [cljs.test :refer-macros [deftest is testing]]
[status-im.notifications.core :as notifications]))

(deftest test-handle-push-notification
(deftest test-handle-push-notification-open
(testing "user's signing in having opened PN while signed out"
(is (= {:db {:account/account {:public-key "0x04d2e59a7501a7bc5bc8bf973a0ab95d06225e2b0f53d5f6be719d857c579bdc1b809bfbe0e8393343f9a5b63a9a90a1a58329c6d1c286d6929f01aaa5472ca9c2"}
:push-notifications/stored {}}
:dispatch [:navigate-to-chat "0x045db1fdb16c4721ddf32e892c5156d9c7a7445482b84ccd41131eb7970f9d623629d86763c5c2a542ae372815125c27eb73535d583d3285bdbfa16ba37f42e2de" {:navigation-reset? true}]}
(notifications/handle-push-notification-open {:db {:push-notifications/stored {}
:account/account {:public-key "0x04d2e59a7501a7bc5bc8bf973a0ab95d06225e2b0f53d5f6be719d857c579bdc1b809bfbe0e8393343f9a5b63a9a90a1a58329c6d1c286d6929f01aaa5472ca9c2"}}}
[:push-notification-opened {:from "0x045db1fdb16c4721ddf32e892c5156d9c7a7445482b84ccd41131eb7970f9d623629d86763c5c2a542ae372815125c27eb73535d583d3285bdbfa16ba37f42e2de"
:to "0x04d2e59a7501a7bc5bc8bf973a0ab95d06225e2b0f53d5f6be719d857c579bdc1b809bfbe0e8393343f9a5b63a9a90a1a58329c6d1c286d6929f01aaa5472ca9c2"
:stored? true}]))))
(testing "user's signed in"
(is (= {:db {:account/account {:public-key "0x04d2e59a7501a7bc5bc8bf973a0ab95d06225e2b0f53d5f6be719d857c579bdc1b809bfbe0e8393343f9a5b63a9a90a1a58329c6d1c286d6929f01aaa5472ca9c2"}
:push-notifications/stored {}}
:dispatch [:navigate-to-chat "0x045db1fdb16c4721ddf32e892c5156d9c7a7445482b84ccd41131eb7970f9d623629d86763c5c2a542ae372815125c27eb73535d583d3285bdbfa16ba37f42e2de"]}
(notifications/handle-push-notification {:db {:push-notifications/stored {}
:account/account {:publi-key "0x04d2e59a7501a7bc5bc8bf973a0ab95d06225e2b0f53d5f6be719d857c579bdc1b809bfbe0e8393343f9a5b63a9a90a1a58329c6d1c286d6929f01aaa5472ca9c2"}}}
[:push-notification-opened {:from "0x045db1fdb16c4721ddf32e892c5156d9c7a7445482b84ccd41131eb7970f9d623629d86763c5c2a542ae372815125c27eb73535d583d3285bdbfa16ba37f42e2de"
:to "0x04d2e59a7501a7bc5bc8bf973a0ab95d06225e2b0f53d5f6be719d857c579bdc1b809bfbe0e8393343f9a5b63a9a90a1a58329c6d1c286d6929f01aaa5472ca9c2"}]))))
(notifications/handle-push-notification-open {:db {:push-notifications/stored {}
:account/account {:public-key "0x04d2e59a7501a7bc5bc8bf973a0ab95d06225e2b0f53d5f6be719d857c579bdc1b809bfbe0e8393343f9a5b63a9a90a1a58329c6d1c286d6929f01aaa5472ca9c2"}}}
[:push-notification-opened {:from "0x045db1fdb16c4721ddf32e892c5156d9c7a7445482b84ccd41131eb7970f9d623629d86763c5c2a542ae372815125c27eb73535d583d3285bdbfa16ba37f42e2de"
:to "0x04d2e59a7501a7bc5bc8bf973a0ab95d06225e2b0f53d5f6be719d857c579bdc1b809bfbe0e8393343f9a5b63a9a90a1a58329c6d1c286d6929f01aaa5472ca9c2"}]))))
(testing "user's signed in into another account"
(is (= {}
(notifications/handle-push-notification {:db {:account/account {:public-key "0x04bc8bf4a91ab726bd98f2c54b3036caacaeea527867945ab839e9ad4e62696856d7f7fa485f68304de357e38a1553eac5592706a16fcf71fd821bbd6c796f9ab3"}}}
[:push-notification-opened {:from "0x045db1fdb16c4721ddf32e892c5156d9c7a7445482b84ccd41131eb7970f9d623629d86763c5c2a542ae372815125c27eb73535d583d3285bdbfa16ba37f42e2de"
:to "0x04d2e59a7501a7bc5bc8bf973a0ab95d06225e2b0f53d5f6be719d857c579bdc1b809bfbe0e8393343f9a5b63a9a90a1a58329c6d1c286d6929f01aaa5472ca9c2"}]))))
(notifications/handle-push-notification-open {:db {:account/account {:public-key "0x04bc8bf4a91ab726bd98f2c54b3036caacaeea527867945ab839e9ad4e62696856d7f7fa485f68304de357e38a1553eac5592706a16fcf71fd821bbd6c796f9ab3"}}}
[:push-notification-opened {:from "0x045db1fdb16c4721ddf32e892c5156d9c7a7445482b84ccd41131eb7970f9d623629d86763c5c2a542ae372815125c27eb73535d583d3285bdbfa16ba37f42e2de"
:to "0x04d2e59a7501a7bc5bc8bf973a0ab95d06225e2b0f53d5f6be719d857c579bdc1b809bfbe0e8393343f9a5b63a9a90a1a58329c6d1c286d6929f01aaa5472ca9c2"}]))))
(testing "user's not signed in"
(is (= {:db {:accounts/accounts {"bd36cd64e2621b054a3b7464ff1b3c4c304880e7" {:address "bd36cd64e2621b054a3b7464ff1b3c4c304880e7"
:photo-path "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAMAAAC7IEhfAAADAFBMVEX"
:name "Bob"
:public-key "0x04d2e59a7501a7bc5bc8bf973a0ab95d06225e2b0f53d5f6be719d857c579bdc1b809bfbe0e8393343f9a5b63a9a90a1a58329c6d1c286d6929f01aaa5472ca9c2"}}
:account/account {:public-key nil}
:account/account {:public-key nil}
:push-notifications/stored {"0x04d2e59a7501a7bc5bc8bf973a0ab95d06225e2b0f53d5f6be719d857c579bdc1b809bfbe0e8393343f9a5b63a9a90a1a58329c6d1c286d6929f01aaa5472ca9c2"
"0x045db1fdb16c4721ddf32e892c5156d9c7a7445482b84ccd41131eb7970f9d623629d86763c5c2a542ae372815125c27eb73535d583d3285bdbfa16ba37f42e2de"}}
:dispatch [:ui/open-login "bd36cd64e2621b054a3b7464ff1b3c4c304880e7" "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAMAAAC7IEhfAAADAFBMVEX" "Bob"]}
(notifications/handle-push-notification {:db {:accounts/accounts {"bd36cd64e2621b054a3b7464ff1b3c4c304880e7" {:address "bd36cd64e2621b054a3b7464ff1b3c4c304880e7"
:photo-path "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAMAAAC7IEhfAAADAFBMVEX"
:name "Bob"
:public-key "0x04d2e59a7501a7bc5bc8bf973a0ab95d06225e2b0f53d5f6be719d857c579bdc1b809bfbe0e8393343f9a5b63a9a90a1a58329c6d1c286d6929f01aaa5472ca9c2"}}
:account/account {:public-key nil}
:push-notifications/stored {}}}
[:push-notification-opened {:from "0x045db1fdb16c4721ddf32e892c5156d9c7a7445482b84ccd41131eb7970f9d623629d86763c5c2a542ae372815125c27eb73535d583d3285bdbfa16ba37f42e2de"
:to "0x04d2e59a7501a7bc5bc8bf973a0ab95d06225e2b0f53d5f6be719d857c579bdc1b809bfbe0e8393343f9a5b63a9a90a1a58329c6d1c286d6929f01aaa5472ca9c2"}])))))
(notifications/handle-push-notification-open {:db {:accounts/accounts {"bd36cd64e2621b054a3b7464ff1b3c4c304880e7" {:address "bd36cd64e2621b054a3b7464ff1b3c4c304880e7"
:photo-path "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAMAAAC7IEhfAAADAFBMVEX"
:name "Bob"
:public-key "0x04d2e59a7501a7bc5bc8bf973a0ab95d06225e2b0f53d5f6be719d857c579bdc1b809bfbe0e8393343f9a5b63a9a90a1a58329c6d1c286d6929f01aaa5472ca9c2"}}
:account/account {:public-key nil}
:push-notifications/stored {}}}
[:push-notification-opened {:from "0x045db1fdb16c4721ddf32e892c5156d9c7a7445482b84ccd41131eb7970f9d623629d86763c5c2a542ae372815125c27eb73535d583d3285bdbfa16ba37f42e2de"
:to "0x04d2e59a7501a7bc5bc8bf973a0ab95d06225e2b0f53d5f6be719d857c579bdc1b809bfbe0e8393343f9a5b63a9a90a1a58329c6d1c286d6929f01aaa5472ca9c2"}])))))

0 comments on commit 248609b

Please sign in to comment.