Skip to content

Commit

Permalink
[#10179] Create invite link for group chat
Browse files Browse the repository at this point in the history
  • Loading branch information
flexsurfer committed Aug 17, 2020
1 parent c2277cb commit 9e0de02
Show file tree
Hide file tree
Showing 20 changed files with 430 additions and 72 deletions.
10 changes: 6 additions & 4 deletions src/status_im/chat/models.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,12 @@

(defn map-chats [{:keys [db] :as cofx}]
(fn [val]
(merge
(or (get (:chats db) (:chat-id val))
(create-new-chat (:chat-id val) cofx))
val)))
(assoc
(merge
(or (get (:chats db) (:chat-id val))
(create-new-chat (:chat-id val) cofx))
val)
:invitation-admin (:invitation-admin val))))

(defn filter-chats [db]
(fn [val]
Expand Down
3 changes: 2 additions & 1 deletion src/status_im/data_store/chats.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@
:unviewedMessagesCount :unviewed-messages-count
:lastMessage :last-message
:active :is-active
:lastClockValue :last-clock-value})
:lastClockValue :last-clock-value
:invitationAdmin :invitation-admin})
(update :last-message #(when % (messages/<-rpc %)))
(dissoc :chatType :members)))

Expand Down
8 changes: 8 additions & 0 deletions src/status_im/data_store/invitations.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(ns status-im.data-store.invitations
(:require clojure.set))

(defn <-rpc [message]
(-> message
(clojure.set/rename-keys {:chatId :chat-id
:introductionMessage :introduction-message
:messageType :message-type})))
10 changes: 10 additions & 0 deletions src/status_im/ethereum/json_rpc.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@
"shhext_addMembersToGroupChat" {}
"shhext_removeMemberFromGroupChat" {}
"shhext_leaveGroupChat" {}
"shhext_removeGroupChat" {}
"shhext_changeGroupChatName" {}
"shhext_createGroupChatWithMembers" {}
"shhext_createGroupChatFromInvitationLink" {}
"shhext_reSendChatMessage" {}
"shhext_getOurInstallations" {}
"shhext_setInstallationMetadata" {}
Expand Down Expand Up @@ -89,6 +91,9 @@
"shhext_sendTransaction" {}
"shhext_acceptRequestTransaction" {}
"shhext_signMessageWithChatKey" {}
"shhext_sendGroupChatInvitationRequest" {}
"shhext_sendGroupChatInvitationRejection" {}
"shhext_getGroupChatInvitations" {}
"wakuext_post" {}
"wakuext_startMessenger" {}
"wakuext_sendPairInstallation" {}
Expand All @@ -104,8 +109,10 @@
"wakuext_addMembersToGroupChat" {}
"wakuext_removeMemberFromGroupChat" {}
"wakuext_leaveGroupChat" {}
"wakuext_removeGroupChat" {}
"wakuext_changeGroupChatName" {}
"wakuext_createGroupChatWithMembers" {}
"wakuext_createGroupChatFromInvitationLink" {}
"wakuext_reSendChatMessage" {}
"wakuext_getOurInstallations" {}
"wakuext_setInstallationMetadata" {}
Expand Down Expand Up @@ -144,6 +151,9 @@
"wakuext_sendTransaction" {}
"wakuext_acceptRequestTransaction" {}
"wakuext_signMessageWithChatKey" {}
"wakuext_sendGroupChatInvitationRequest" {}
"wakuext_sendGroupChatInvitationRejection" {}
"wakuext_getGroupChatInvitations" {}
"status_chats" {}
"wallet_getTransfers" {}
"wallet_getTokensBalances" {}
Expand Down
14 changes: 11 additions & 3 deletions src/status_im/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
status-im.search.core
status-im.ui.screens.profile.events
status-im.chat.models.images
status-im.ui.screens.privacy-and-security-settings.events))
status-im.ui.screens.privacy-and-security-settings.events
[status-im.data-store.invitations :as data-store.invitations]))

;; init module
(handlers/register-handler-fx
Expand Down Expand Up @@ -776,14 +777,16 @@
(fn [_ [_ err]]
(log/error :send-status-message-error err)))

(fx/defn handle-update [cofx {:keys [chats messages emojiReactions] :as response}]
(fx/defn handle-update [cofx {:keys [chats messages emojiReactions invitations]}]
(let [chats (map data-store.chats/<-rpc chats)
messages (map data-store.messages/<-rpc messages)
message-fxs (map chat.message/receive-one messages)
emoji-reactions (map data-store.reactions/<-rpc emojiReactions)
emoji-react-fxs (map chat.reactions/receive-one emoji-reactions)
invitations-fxs [(group-chats/handle-invitations
(map data-store.invitations/<-rpc invitations))]
chat-fxs (map #(chat/ensure-chat (dissoc % :unviewed-messages-count)) chats)]
(apply fx/merge cofx (concat chat-fxs message-fxs emoji-react-fxs))))
(apply fx/merge cofx (concat chat-fxs message-fxs emoji-react-fxs invitations-fxs))))

(handlers/register-handler-fx
:transport/message-sent
Expand All @@ -805,6 +808,11 @@
[cofx response]
(handle-update cofx response))

(fx/defn invitation-sent
{:events [:transport/invitation-sent]}
[cofx response]
(handle-update cofx response))

(handlers/register-handler-fx
:transport.callback/node-info-fetched
(fn [cofx [_ node-info]]
Expand Down
47 changes: 47 additions & 0 deletions src/status_im/group_chats/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@
:params [nil group-name (into [] selected-contacts)]
:on-success #(re-frame/dispatch [::chat-updated %])}]}))

(fx/defn create-from-link
[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]
{::json-rpc/call [{:method (json-rpc/call-ext-method (waku/enabled? cofx) "addAdminsToGroupChat")
Expand All @@ -84,6 +90,15 @@
:params [nil current-chat-id selected-participants]
:on-success #(re-frame/dispatch [::chat-updated %])}]})

(fx/defn add-members-from-invitation
"Add members to a group chat"
{:events [:group-chats.ui/add-members-from-invitation]}
[{{:keys [current-chat-id] :as db} :db :as cofx} id participant]
{:db (update db :group-chat/invitations dissoc id)
::json-rpc/call [{:method (json-rpc/call-ext-method (waku/enabled? cofx) "addMembersToGroupChat")
:params [nil current-chat-id [participant]]
:on-success #(re-frame/dispatch [::chat-updated %])}]})

(fx/defn leave
"Leave chat"
{:events [:group-chats.ui/leave-chat-confirmed]}
Expand All @@ -92,6 +107,14 @@
:params [nil chat-id true]
:on-success #(re-frame/dispatch [::chat-updated %])}]})

(fx/defn remove
"Remove chat"
{:events [:group-chats.ui/remove-chat-confirmed]}
[{:keys [db] :as cofx} chat-id]
{::json-rpc/call [{:method (json-rpc/call-ext-method (waku/enabled? cofx) "removeGroupChat")
:params [chat-id]
:on-success #(re-frame/dispatch [::chat-updated %])}]})

(defn- valid-name? [name]
(spec/valid? :profile/name name))

Expand All @@ -104,3 +127,27 @@
::json-rpc/call [{:method (json-rpc/call-ext-method (waku/enabled? cofx) "changeGroupChatName")
:params [nil chat-id new-name]
:on-success #(re-frame/dispatch [::chat-updated %])}]}))

(fx/defn send-group-chat-membership-request
"Send group chat membership request"
{:events [:send-group-chat-membership-request]}
[{{:keys [current-chat-id chats]} :db :as cofx} message]
(let [{:keys [invitation-admin]} (get chats current-chat-id)]
{::json-rpc/call [{:method (json-rpc/call-ext-method (waku/enabled? cofx) "sendGroupChatInvitationRequest")
:params [nil current-chat-id invitation-admin message]
:on-success #(re-frame/dispatch [:transport/invitation-sent %])}]}))

(fx/defn send-group-chat-membership-rejection
"Send group chat membership rejection"
{:events [:send-group-chat-membership-rejection]}
[cofx invitation-id]
{::json-rpc/call [{:method (json-rpc/call-ext-method (waku/enabled? cofx) "sendGroupChatInvitationRejection")
:params [nil invitation-id]
:on-success #(re-frame/dispatch [:transport/invitation-sent %])}]})

(fx/defn handle-invitations
[{db :db} invitations]
{:db (update db :group-chat/invitations #(reduce (fn [acc {:keys [id] :as inv}]
(assoc acc id inv))
%
invitations))})
18 changes: 17 additions & 1 deletion src/status_im/multiaccounts/login/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
[status-im.wallet.core :as wallet]
[status-im.wallet.prices :as prices]
[status-im.acquisition.core :as acquisition]
[taoensso.timbre :as log]))
[taoensso.timbre :as log]
[status-im.data-store.invitations :as data-store.invitations]
[status-im.waku.core :as waku]))

(re-frame/reg-fx
::login
Expand Down Expand Up @@ -107,6 +109,14 @@
all-stored-browsers)]
{:db (assoc db :browser/browsers browsers)}))

(fx/defn initialize-invitations
{:events [::initialize-invitations]}
[{:keys [db]} invitations]
{:db (assoc db :group-chat/invitations (reduce (fn [acc {:keys [id] :as inv}]
(assoc acc id (data-store.invitations/<-rpc inv)))
{}
invitations))})

(fx/defn initialize-web3-client-version
{:events [::initialize-web3-client-version]}
[{:keys [db]} node-version]
Expand Down Expand Up @@ -158,6 +168,11 @@
(fx/defn initialize-appearance [cofx]
{::multiaccounts/switch-theme (get-in cofx [:db :multiaccount :appearance])})

(fx/defn get-group-chat-invitations [cofx]
{::json-rpc/call
[{:method (json-rpc/call-ext-method (waku/enabled? cofx) "getGroupChatInvitations")
:on-success #(re-frame/dispatch [::initialize-invitations %])}]})

(fx/defn get-settings-callback
{:events [::get-settings-callback]}
[{:keys [db] :as cofx} settings]
Expand Down Expand Up @@ -189,6 +204,7 @@
(contact/initialize-contacts)
(stickers/init-stickers-packs)
(mobile-network/on-network-status-change)
(get-group-chat-invitations)
(logging/set-log-level (:log-level multiaccount))
(multiaccounts/switch-preview-privacy-mode-flag))))

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
26 changes: 25 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,9 @@
(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 +42,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 +92,22 @@
{:type :public-chat
:error :invalid-topic}))

(defn match-group-chat [uri _]
;;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 +182,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
16 changes: 15 additions & 1 deletion src/status_im/subs.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
(reg-root-key-sub :selected-participants :selected-participants)
(reg-root-key-sub :chat/inputs :chat/inputs)
(reg-root-key-sub :camera-roll-photos :camera-roll-photos)
(reg-root-key-sub :group-chat/invitations :group-chat/invitations)

;;browser
(reg-root-key-sub :browsers :browser/browsers)
Expand Down Expand Up @@ -626,7 +627,7 @@
:<- [:chats/current-raw-chat]
(fn [current-chat]
(select-keys current-chat
[:public? :group-chat :chat-id :chat-name :color])))
[:public? :group-chat :chat-id :chat-name :color :invitation-admin])))

(re-frame/reg-sub
:current-chat/one-to-one-chat?
Expand Down Expand Up @@ -816,6 +817,19 @@
{:joined? (group-chats.db/joined? my-public-key chat)
:inviter-pk (group-chats.db/get-inviter-pk my-public-key chat)}))

(re-frame/reg-sub
:group-chat/invitations-by-chat-id
:<- [:group-chat/invitations]
(fn [invitations [_ chat-id]]
(filter #(= (:chat-id %) chat-id) (vals invitations))))

(re-frame/reg-sub
:group-chat/pending-invitations-by-chat-id
(fn [[_ chat-id] _]
[(re-frame/subscribe [:group-chat/invitations-by-chat-id chat-id])])
(fn [[invitations]]
(remove :rejection invitations)))

(re-frame/reg-sub
:chats/transaction-status
;;TODO address here for transactions
Expand Down
Loading

0 comments on commit 9e0de02

Please sign in to comment.