Skip to content

Commit

Permalink
bidi
Browse files Browse the repository at this point in the history
  • Loading branch information
flexsurfer committed Aug 14, 2020
1 parent 210cd4a commit f9bc183
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
10 changes: 4 additions & 6 deletions src/status_im/group_chats/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,10 @@
:on-success #(re-frame/dispatch [::chat-updated %])}]}))

(fx/defn create-from-link
[{:keys [db] :as cofx} link-params]
(let [[admin-pk encoded-chat-name chat-id] (string/split link-params #"&")
chat-name (js/decodeURI encoded-chat-name)]
{::json-rpc/call [{:method (json-rpc/call-ext-method (waku/enabled? cofx) "createGroupChatFromInvitationLink")
:params [chat-name chat-id admin-pk]
:on-success #(re-frame/dispatch [::chat-updated %])}]}))
[cofx {:keys [chat-id invitation-admin chat-name]}]
{::json-rpc/call [{:method (json-rpc/call-ext-method (waku/enabled? cofx) "createGroupChatFromInvitationLink")
:params [chat-name chat-id invitation-admin]
:on-success #(re-frame/dispatch [::chat-updated %])}]})

(fx/defn make-admin
[{:keys [db] :as cofx} chat-id member]
Expand Down
6 changes: 5 additions & 1 deletion src/status_im/qr_scanner/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
[status-im.utils.utils :as utils]
[status-im.ethereum.core :as ethereum]
[status-im.ui.screens.add-new.new-chat.db :as new-chat.db]
[status-im.utils.fx :as fx]))
[status-im.utils.fx :as fx]
[status-im.group-chats.core :as group-chats]))

(fx/defn scan-qr-code
{:events [::scan-code]}
Expand Down Expand Up @@ -50,6 +51,9 @@
(when (seq topic)
(chat/start-public-chat cofx topic {})))

(fx/defn handle-group-chat [cofx params]
(group-chats/create-from-link cofx params))

(fx/defn handle-view-profile
[{:keys [db] :as cofx} {:keys [public-key]}]
(let [own (new-chat.db/own-public-key? db public-key)]
Expand Down
27 changes: 26 additions & 1 deletion src/status_im/router/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
[status-im.ethereum.resolver :as resolver]
[status-im.ethereum.stateofus :as stateofus]
[cljs.spec.alpha :as spec]
[status-im.ethereum.core :as ethereum]))
[status-im.ethereum.core :as ethereum]
[status-im.utils.db :as utils.db]))

(def ethereum-scheme "ethereum:")

Expand All @@ -27,6 +28,10 @@
(def browser-extractor {[#"(.*)" :domain] {"" :browser
"/" :browser}})

(def group-chat-extractor {[#"(.*)" :params] {"" :group-chat
"/" :group-chat}})


(def eip-extractor {#{[:prefix "-" :address]
[:address]}
{#{["@" :chain-id] ""}
Expand All @@ -38,6 +43,7 @@
"b/" browser-extractor
"browser/" browser-extractor
["p/" :chat-id] :private-chat
"g/" group-chat-extractor
["u/" :user-id] :user
["user/" :user-id] :user
["referral/" :referrer] :referrals}
Expand Down Expand Up @@ -87,6 +93,22 @@
{:type :public-chat
:error :invalid-topic}))

(defn match-group-chat [uri {:keys [params]}]
;;TODO there is a bug in bidi, so we have to parse uri manually here
(let [[_ params] (when (not (string/blank? uri)) (string/split uri #"/g/"))
[admin-pk encoded-chat-name chat-id] (when (not (string/blank? params)) (string/split params #"&"))
chat-id-parts (when (not (string/blank? chat-id)) (string/split chat-id #"-"))
chat-name (when (not (string/blank? encoded-chat-name)) (js/decodeURI encoded-chat-name))]
(if (and (not (string/blank? chat-id)) (not (string/blank? admin-pk)) (not (string/blank? chat-name))
(not (string/blank? (first chat-id-parts)))
(utils.db/valid-public-key? admin-pk)
(utils.db/valid-public-key? (last chat-id-parts)))
{:type :group-chat
:chat-id chat-id
:invitation-admin admin-pk
:chat-name chat-name}
{:error :invalid-group-chat-data})))

(defn match-private-chat-async [chain {:keys [chat-id]} cb]
(match-contact-async chain
{:user-id chat-id}
Expand Down Expand Up @@ -161,6 +183,9 @@
(= handler :private-chat)
(match-private-chat-async chain route-params cb)

(= handler :group-chat)
(cb (match-group-chat uri route-params))

(spec/valid? :global/public-key uri)
(match-contact-async chain {:user-id uri} cb)

Expand Down
11 changes: 6 additions & 5 deletions src/status_im/utils/universal_links/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@

(def links {:public-chat "%s/%s"
:private-chat "%s/p/%s"
:group-chat "%s/g/%s"
:user "%s/u/%s"
:browse "%s/b/%s"
:group "%s/g/%s"})
:browse "%s/b/%s"})

(defn generate-link [link-type domain-type param]
(gstring/format (get links link-type)
Expand All @@ -46,9 +46,9 @@
(log/info "universal-links: handling browse" url)
{:browser/show-browser-selection url})

(fx/defn handle-group [cofx link-params]
(log/info "universal-links: handling group" link-params)
(group-chats/create-from-link cofx link-params))
(fx/defn handle-group-chat [cofx params]
(log/info "universal-links: handling group" params)
(group-chats/create-from-link cofx params))

(fx/defn handle-private-chat [{:keys [db] :as cofx} {:keys [chat-id]}]
(log/info "universal-links: handling private chat" chat-id)
Expand Down Expand Up @@ -99,6 +99,7 @@
{:events [::match-value]}
[cofx url {:keys [type] :as data}]
(case type
:group-chat (handle-group-chat cofx data)
:public-chat (handle-public-chat cofx data)
:private-chat (handle-private-chat cofx data)
:contact (handle-view-profile cofx data)
Expand Down

0 comments on commit f9bc183

Please sign in to comment.