From 04a75134879136285b044d58f888f89604dcf3ef Mon Sep 17 00:00:00 2001 From: Gheorghe Pinzaru Date: Fri, 7 Aug 2020 11:16:42 +0300 Subject: [PATCH] QA fixes --- src/status_im/qr_scanner/core.cljs | 14 ++- src/status_im/router/core.cljs | 113 ++++++++++-------- src/status_im/router/core_test.cljs | 7 +- src/status_im/utils/universal_links/core.cljs | 12 +- translations/en.json | 1 + 5 files changed, 85 insertions(+), 62 deletions(-) diff --git a/src/status_im/qr_scanner/core.cljs b/src/status_im/qr_scanner/core.cljs index d3279d98beef..bf599c667ef7 100644 --- a/src/status_im/qr_scanner/core.cljs +++ b/src/status_im/qr_scanner/core.cljs @@ -47,7 +47,8 @@ :content (i18n/label :t/can-not-add-yourself)}})) (fx/defn handle-public-chat [cofx {:keys [topic]}] - (chat/start-public-chat cofx topic {})) + (when (seq topic) + (chat/start-public-chat cofx topic {}))) (fx/defn handle-view-profile [{:keys [db] :as cofx} {:keys [public-key]}] @@ -58,13 +59,18 @@ public-key (navigation/navigate-to-cofx (assoc-in cofx [:db :contacts/identity] public-key) :tabs - {:screen :profile-stack - :params {:screen :profile}}))) + {:screen :chat-stack + :params {:screen :profile}}) + + :else + {:utils/show-popup {:title (i18n/label :t/unable-to-read-this-code) + :content (i18n/label :t/ens-name-not-found) + :on-dismiss #(re-frame/dispatch [:navigate-to :home])}})) (fx/defn handle-eip681 [cofx data] (fx/merge cofx {:dispatch [:wallet/parse-eip681-uri-and-resolve-ens data]} - (navigation/navigate-to-cofx :tabs {:screen :wallet} nil))) + (navigation/navigate-to-cofx :tabs {:screen :wallet}))) (fx/defn match-scan {:events [::match-scanned-value]} diff --git a/src/status_im/router/core.cljs b/src/status_im/router/core.cljs index 100e7608051c..8ff4fa785c49 100644 --- a/src/status_im/router/core.cljs +++ b/src/status_im/router/core.cljs @@ -2,6 +2,7 @@ (:require [re-frame.core :as re-frame] [clojure.string :as string] [bidi.bidi :as bidi] + [taoensso.timbre :as log] [status-im.ui.screens.add-new.new-public-chat.db :as public-chat.db] [status-im.utils.security :as security] [status-im.ethereum.eip681 :as eip681] @@ -22,10 +23,13 @@ (def handled-schemes (set (into uri-schemes web-urls))) +(def browser-extractor {[#"(.*)" :domain ] {"" :browser + "/" :browser}}) + (def routes ["" {handled-schemes {["" :chat-id] :public-chat "chat" {["/public/" :chat-id] :public-chat} - ["b/" :domain] :browse - ["browse/" :domain] :browse + "b/" browser-extractor + "browser/" browser-extractor ["p/" :chat-id] :private-chat ["u/" :user-id] :user ["user/" :user-id] :user @@ -48,7 +52,7 @@ ens-name (ens-name-parse contact-identity)] (resolver/pubkey registry ens-name cb))) -(defn match-contact +(defn match-contact-async [chain {:keys [user-id]} callback] (let [public-key? (and (string? user-id) (string/starts-with? user-id "0x")) @@ -62,38 +66,44 @@ (and (not public-key?) (string? user-id)) (let [registry (get ens/ens-registries chain) ens-name (ens-name-parse user-id) - on-success #(match-contact chain {:user-id %} callback)] + on-success #(match-contact-async chain {:user-id %} callback)] (resolver/pubkey registry ens-name on-success)) :else (callback {:type :contact :error :not-found})))) -(defn match-public-chat [{:keys [chat-id]} cb] +(defn match-public-chat [{:keys [chat-id]}] (if (public-chat.db/valid-topic? chat-id) - (cb {:type :public-chat - :topic chat-id}) - (cb {:type :public-chat - :error :invalid-topic}))) - -(defn match-private-chat [chain {:keys [chat-id]} cb] - (match-contact chain {:user-id chat-id} - (fn [{:keys [public-key]}] - (if public-key - (cb {:type :private-chat - :chat-id public-key}) - (cb {:type :private-chat - :error :invalid-chat-id}))))) - -(defn match-browser [{:keys [domain]} cb] - (if (security/safe-link? domain) - (cb {:type :browser - :url domain}) - (cb {:type :browser - :error :unsafe-link}))) + {:type :public-chat + :topic chat-id} + {:type :public-chat + :error :invalid-topic})) + +(defn match-private-chat-async [chain {:keys [chat-id]} cb] + (match-contact-async chain + {:user-id chat-id} + (fn [{:keys [public-key]}] + (if public-key + (cb {:type :private-chat + :chat-id public-key}) + (cb {:type :private-chat + :error :invalid-chat-id}))))) + +(defn match-browser [uri {:keys [domain]}] + ;; NOTE: We rebuild domain from original URI and matched domain + (let [domain (->> (string/split uri domain) + second + (str domain))] + (prn domain uri) + (if (security/safe-link? domain) + {:type :browser + :url domain} + {:type :browser + :error :unsafe-link}))) ;; NOTE(Ferossgp): Better to handle eip681 also with router instead of regexp. -(defn match-eip681 [uri cb] +(defn match-eip681 [uri] (if-let [message (eip681/parse-uri uri)] (let [{:keys [paths ens-names]} (reduce (fn [acc path] @@ -107,45 +117,46 @@ [[:address] [:function-arguments :address]])] (if (empty? ens-names) ;; if there are no ens-names, we dispatch request-uri-parsed immediately - (cb {:type :eip681 - :message message - :uri uri}) - (cb {:type :eip681 - :uri uri - :message message - :paths paths - :ens-names ens-names}))) - (cb {:type :eip681 - :uri uri - :error :cannot-parse}))) - -(defn match-referral [{:keys [referrer]} cb] - (cb {:type :referrals - :referrer referrer})) + {:type :eip681 + :message message + :uri uri} + {:type :eip681 + :uri uri + :message message + :paths paths + :ens-names ens-names})) + {:type :eip681 + :uri uri + :error :cannot-parse})) + +(defn match-referral [{:keys [referrer]}] + {:type :referrals + :referrer referrer}) (defn handle-uri [chain uri cb] (let [{:keys [handler route-params]} (match-uri uri)] + (log/info "[router] uri " uri " matched " handler " with " route-params) (cond (= handler :public-chat) - (match-public-chat route-params cb) + (cb (match-public-chat route-params)) - (= handler :private-chat) - (match-private-chat chain route-params cb) + (= handler :browser) + (cb (match-browser uri route-params)) - (= handler :browse) - (match-browser route-params cb) + (= handler :ethereum) + (cb (match-eip681 uri)) (= handler :user) - (match-contact chain route-params cb) + (match-contact-async chain route-params cb) - (= handler :ethereum) - (match-eip681 uri cb) + (= handler :private-chat) + (match-private-chat-async chain route-params cb) (spec/valid? :global/public-key uri) - (match-contact chain {:user-id uri} cb) + (match-contact-async chain {:user-id uri} cb) (= handler :referrals) - (match-referral route-params cb) + (cb (match-referral route-params)) :else (cb {:type :undefined diff --git a/src/status_im/router/core_test.cljs b/src/status_im/router/core_test.cljs index 43fc543f4185..b4efb7cb5fc8 100644 --- a/src/status_im/router/core_test.cljs +++ b/src/status_im/router/core_test.cljs @@ -15,7 +15,7 @@ (str "status-im://user/" public-key) [:user {:user-id public-key}] - "status-im://b/www.cryptokitties.co" [:browse {:domain "www.cryptokitties.co"}] + "status-im://b/www.cryptokitties.co" [:browser {:domain "www.cryptokitties.c"}] "https://join.status.im/status" [:public-chat {:chat-id "status"}] @@ -23,6 +23,9 @@ (str "https://join.status.im/user/" public-key) [:user {:user-id public-key}] - "https://join.status.im/b/www.cryptokitties.co" [:browse {:domain "www.cryptokitties.co"}] + ;; Last char removed by: https://github.com/juxt/bidi/issues/104 + "https://join.status.im/b/www.cryptokitties.co" [:browser {:domain "www.cryptokitties.c"}] + + "https://join.status.im/b/https://www.google.com/" [:browser {:domain "https://www.google.co"}] "ethereum:0x89205a3a3b2a69de6dbf7f01ed13b2108b2c43e7" [:ethereum {:path "0x89205a3a3b2a69de6dbf7f01ed13b2108b2c43e7"}])) diff --git a/src/status_im/utils/universal_links/core.cljs b/src/status_im/utils/universal_links/core.cljs index 23220d4ffa28..1b0b82710e24 100644 --- a/src/status_im/utils/universal_links/core.cljs +++ b/src/status_im/utils/universal_links/core.cljs @@ -46,14 +46,16 @@ (fx/defn handle-private-chat [{:keys [db] :as cofx} {:keys [chat-id]}] (log/info "universal-links: handling private chat" chat-id) - (if-not (new-chat.db/own-public-key? db chat-id) - (chat/start-chat cofx chat-id) - {:utils/show-popup {:title (i18n/label :t/unable-to-read-this-code) - :content (i18n/label :t/can-not-add-yourself)}})) + (when chat-id + (if-not (new-chat.db/own-public-key? db chat-id) + (chat/start-chat cofx chat-id) + {:utils/show-popup {:title (i18n/label :t/unable-to-read-this-code) + :content (i18n/label :t/can-not-add-yourself)}}))) (fx/defn handle-public-chat [cofx {:keys [topic]}] (log/info "universal-links: handling public chat" topic) - (chat/start-public-chat cofx topic {})) + (when (seq topic) + (chat/start-public-chat cofx topic {}))) (fx/defn handle-view-profile [{:keys [db] :as cofx} {:keys [public-key]}] diff --git a/translations/en.json b/translations/en.json index 2454a62d3181..302dbb8610c1 100644 --- a/translations/en.json +++ b/translations/en.json @@ -413,6 +413,7 @@ "ens-username-connected-with-different-key": "Continuing will require a transaction to connect the username with your current chat key.", "ens-username-owned-continue": "Continuing will connect this username with your chat key.", "ens-username-taken": "Username already taken :(", + "ens-name-not-found": "Cannot resolve ENS name", "enter-12-words": "Enter the 12 words of your seed phrase, separated by single spaces", "enter-a-private-key": "Enter a private key", "enter-a-seed-phrase": "Enter a seed phrase",