From f36d5d64cc52ccd6bb06fb8743ae9ed535b21d0d Mon Sep 17 00:00:00 2001 From: Andrea Maria Piana Date: Tue, 3 Jul 2018 16:59:19 +0200 Subject: [PATCH] [Fixes #4944] Enable universal/deep links Signed-off-by: Andrea Maria Piana --- .env.prod | 1 - CHANGELOG.md | 1 + src/status_im/chat/events.cljs | 12 +++++------ .../ui/screens/accounts/login/events.cljs | 8 +++++--- src/status_im/ui/screens/events.cljs | 4 +--- src/status_im/ui/screens/navigation.cljs | 19 +++++++++++------- src/status_im/utils/universal_links/core.cljs | 4 ++-- test/cljs/status_im/test/chat/events.cljs | 20 +++++++++++++++++++ .../test/utils/universal_links/core.cljs | 3 +++ 9 files changed, 50 insertions(+), 22 deletions(-) diff --git a/.env.prod b/.env.prod index 03acd57be3..d0204dd8ae 100644 --- a/.env.prod +++ b/.env.prod @@ -20,4 +20,3 @@ GROUP_CHATS_ENABLED=0 USE_SYM_KEY=0 MAINNET_WARNING_ENABLED=1 SPAM_BUTTON_DETECTION_ENABLED=1 -UNIVERSAL_LINKS_ENABLED=0 \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 4cfcb10bf2..e7daaf929e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Spam moderation - Collectibles support (CryptoKitties, CryptoStrikers and Etheremon) - Added more dapps +- Universal and deep links for public chats, browsing dapps, viewing profiles ### Fixed - Fixed mailservers connectivity issue diff --git a/src/status_im/chat/events.cljs b/src/status_im/chat/events.cljs index 0d5671eab9..7a8c5dbde2 100644 --- a/src/status_im/chat/events.cljs +++ b/src/status_im/chat/events.cljs @@ -386,16 +386,16 @@ (navigate-to-chat random-id {}) (transport.message/send (group-chat/GroupAdminUpdate. chat-name selected-contacts) random-id))))) -(defn show-profile [identity {:keys [db] :as cofx}] - (handlers-macro/merge-fx cofx - {:db (assoc db :contacts/identity identity)} - (navigation/navigate-forget :profile))) +(defn show-profile [identity keep-navigation? {:keys [db] :as cofx}] + (cond->> {:db (assoc db :contacts/identity identity)} + keep-navigation? (navigation/navigate-to-cofx :profile nil) + :else (navigation/navigate-forget :profile))) (handlers/register-handler-fx :show-profile [re-frame/trim-v] - (fn [cofx [identity]] - (show-profile identity cofx))) + (fn [cofx [identity keep-navigation?]] + (show-profile identity keep-navigation? cofx))) (handlers/register-handler-fx :resend-message diff --git a/src/status_im/ui/screens/accounts/login/events.cljs b/src/status_im/ui/screens/accounts/login/events.cljs index 69cd7938f5..8da79970af 100644 --- a/src/status_im/ui/screens/accounts/login/events.cljs +++ b/src/status_im/ui/screens/accounts/login/events.cljs @@ -8,7 +8,8 @@ [status-im.native-module.core :as status] [status-im.utils.config :as config] [status-im.utils.keychain.core :as keychain] - [status-im.utils.utils :as utils])) + [status-im.utils.utils :as utils] + [status-im.utils.universal-links.core :as universal-links])) ;;;; FX @@ -149,10 +150,11 @@ (register-handler-fx :change-account-handler - (fn [{{:keys [view-id] :as db} :db} [_ address]] + (fn [{{:keys [view-id] :as db} :db :as cofx} [_ address]] {:db (cond-> (dissoc db :accounts/login) (= view-id :create-account) (assoc-in [:accounts/create :step] :enter-name)) :dispatch [:initialize-account address (when (not= view-id :create-account) - [[:navigate-to-clean :home]])]})) + [[:navigate-to-clean :home] + (universal-links/stored-url-event cofx)])]})) diff --git a/src/status_im/ui/screens/events.cljs b/src/status_im/ui/screens/events.cljs index ff3ae92e85..a8bb460b16 100644 --- a/src/status_im/ui/screens/events.cljs +++ b/src/status_im/ui/screens/events.cljs @@ -13,7 +13,6 @@ status-im.ui.screens.group.chat-settings.events status-im.ui.screens.group.events [status-im.ui.screens.navigation :as navigation] - [status-im.utils.universal-links.core :as universal-links] status-im.utils.universal-links.events [status-im.chat.commands.core :as commands] status-im.ui.screens.add-new.new-chat.navigation @@ -375,8 +374,7 @@ [:update-transactions] [:get-fcm-token] [:update-sign-in-time] - [:show-mainnet-is-default-alert] - (universal-links/stored-url-event cofx)] + [:show-mainnet-is-default-alert]] (seq events-after) (into events-after))})) (handlers/register-handler-fx diff --git a/src/status_im/ui/screens/navigation.cljs b/src/status_im/ui/screens/navigation.cljs index 33c4497e5c..ca197287ae 100644 --- a/src/status_im/ui/screens/navigation.cljs +++ b/src/status_im/ui/screens/navigation.cljs @@ -50,17 +50,22 @@ (dissoc db :was-modal?) ;;TODO check how it worked with this bug (apply preload-data! db args))) +(defn navigate-to-cofx [go-to-view-id screen-params {:keys [db]}] + (let [view-id (:view-id db) + db (cond-> db + (seq screen-params) + (assoc-in [:navigation/screen-params go-to-view-id] screen-params))] + {:db (if (= view-id go-to-view-id) + db + (push-view db go-to-view-id))})) + (defn navigate-to - "Navigates to particular view" + "DEPRECATED, use navigate-to-cofx above. + Navigates to particular view" ([db go-to-view-id] (navigate-to db go-to-view-id nil)) ([{:keys [view-id] :as db} go-to-view-id screen-params] - (let [db (cond-> db - (seq screen-params) - (assoc-in [:navigation/screen-params go-to-view-id] screen-params))] - (if (= view-id go-to-view-id) - db - (push-view db go-to-view-id))))) + (:db (navigate-to-cofx go-to-view-id screen-params {:db db})))) (def unload-data-interceptor (re-frame/->interceptor diff --git a/src/status_im/utils/universal_links/core.cljs b/src/status_im/utils/universal_links/core.cljs index 5ae486a955..c6dddcbb97 100644 --- a/src/status_im/utils/universal_links/core.cljs +++ b/src/status_im/utils/universal_links/core.cljs @@ -19,7 +19,7 @@ (defn universal-link? [url] (boolean - (re-matches #"((^https?://get.status.im/)|(^status-im://)).*$" url))) + (re-matches #"((^https?://get.status.im/)|(^status-im://))[\x00-\x7F]+$" url))) (defn open! [url] (log/info "universal-links: opening " url) @@ -39,7 +39,7 @@ (defn handle-view-profile [profile-id cofx] (log/info "universal-links: handling view profile" profile-id) - (chat.events/show-profile profile-id cofx)) + (chat.events/show-profile profile-id true cofx)) (defn handle-not-found [full-url] (log/info "universal-links: no handler for " full-url)) diff --git a/test/cljs/status_im/test/chat/events.cljs b/test/cljs/status_im/test/chat/events.cljs index 27a0b7b7cf..d1b36c675a 100644 --- a/test/cljs/status_im/test/chat/events.cljs +++ b/test/cljs/status_im/test/chat/events.cljs @@ -83,3 +83,23 @@ (is (= #{"4" "5" "6"} (set (get-in (chat-events/mark-messages-seen "1-1" {:db test-db}) [:shh/post 0 :message :payload :message-ids])))))) + +(deftest show-profile-test + (testing "default behaviour" + (testing "it navigates to profile but forgets the navigation" + (let [{:keys [db]} (chat-events/show-profile + "a" + false + {:db {:navigation-stack '(:home)}})] + (is (= "a" (:contacts/identity db))) + (is (= '(:home) (:navigation-stack db))) + (is (= :profile (:view-id db)))))) + (testing "keep-navigation? on" + (testing "it navigates to profile and keeps the navigation" + (let [{:keys [db]} (chat-events/show-profile + "a" + true + {:db {:navigation-stack '(:home)}})] + (is (= "a" (:contacts/identity db))) + (is (= '(:profile :home) (:navigation-stack db))) + (is (= :profile (:view-id db))))))) diff --git a/test/cljs/status_im/test/utils/universal_links/core.cljs b/test/cljs/status_im/test/utils/universal_links/core.cljs index edf2e16b5e..cecbcd71eb 100644 --- a/test/cljs/status_im/test/utils/universal_links/core.cljs +++ b/test/cljs/status_im/test/utils/universal_links/core.cljs @@ -62,6 +62,9 @@ (testing "https://get.status.im/blah" (testing "it returns true" (is (links/universal-link? "https://get.status.im/blah")))) + (testing "unicode characters" + (testing "it returns false" + (is (not (links/universal-link? "https://get.status.im/browse/www.аррӏе.com"))))) (testing "not-status-im://blah" (testing "it returns false" (is (not (links/universal-link? "https://not.status.im/blah")))))