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 Sep 7, 2020
1 parent 4304a5d commit d02d849
Show file tree
Hide file tree
Showing 24 changed files with 550 additions and 104 deletions.
26 changes: 3 additions & 23 deletions src/status_im/acquisition/install_referrer.cljs
Original file line number Diff line number Diff line change
@@ -1,32 +1,12 @@
(ns status-im.acquisition.install-referrer
(:require [taoensso.timbre :as log]
[clojure.string :as cstr]
["react-native-device-info" :refer [getInstallReferrer]]))

(defn- split-param [param]
(->
(cstr/split param #"=")
(concat (repeat ""))
(->>
(take 2))))

(defn- url-decode
[string]
(some-> string str (cstr/replace #"\+" "%20") (js/decodeURIComponent)))

(defn- query->map
[qstr]
(when-not (cstr/blank? qstr)
(some->> (cstr/split qstr #"&")
seq
(mapcat split-param)
(map url-decode)
(apply hash-map))))
["react-native-device-info" :refer [getInstallReferrer]]
[status-im.utils.http :as http]))

(defn parse-referrer
"Google return query params for referral with all utm tags"
[referrer]
(-> referrer query->map (get "referrer")))
(-> referrer http/query->map (get "referrer")))

(defn get-referrer [cb]
(-> (getInstallReferrer)
Expand Down
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
5 changes: 5 additions & 0 deletions src/status_im/constants.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
emoji-reaction-sad (:sad resources/reactions)
emoji-reaction-angry (:angry resources/reactions)})

(def invitation-state-unknown 0)
(def invitation-state-requested 1)
(def invitation-state-rejected 2)
(def invitation-state-approved 3)

(def message-type-one-to-one 1)
(def message-type-public-group 2)
(def message-type-private-group 3)
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})))
8 changes: 8 additions & 0 deletions src/status_im/ethereum/json_rpc.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"shhext_leaveGroupChat" {}
"shhext_changeGroupChatName" {}
"shhext_createGroupChatWithMembers" {}
"shhext_createGroupChatFromInvitation" {}
"shhext_reSendChatMessage" {}
"shhext_getOurInstallations" {}
"shhext_setInstallationMetadata" {}
Expand Down Expand Up @@ -89,6 +90,9 @@
"shhext_sendTransaction" {}
"shhext_acceptRequestTransaction" {}
"shhext_signMessageWithChatKey" {}
"shhext_sendGroupChatInvitationRequest" {}
"shhext_sendGroupChatInvitationRejection" {}
"shhext_getGroupChatInvitations" {}
"wakuext_post" {}
"wakuext_startMessenger" {}
"wakuext_sendPairInstallation" {}
Expand All @@ -106,6 +110,7 @@
"wakuext_leaveGroupChat" {}
"wakuext_changeGroupChatName" {}
"wakuext_createGroupChatWithMembers" {}
"wakuext_createGroupChatFromInvitation" {}
"wakuext_reSendChatMessage" {}
"wakuext_getOurInstallations" {}
"wakuext_setInstallationMetadata" {}
Expand Down Expand Up @@ -144,6 +149,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.http.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
66 changes: 65 additions & 1 deletion src/status_im/group_chats/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
[status-im.transport.filters.core :as transport.filters]
[status-im.navigation :as navigation]
[status-im.utils.fx :as fx]
[status-im.waku.core :as waku]))
[status-im.waku.core :as waku]
[status-im.constants :as constants]))

(fx/defn remove-member
"Format group update message and sign membership"
Expand Down Expand Up @@ -71,6 +72,14 @@
: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]}]
(if (get-in cofx [:db :chats chat-id :is-active])
(models.chat/navigate-to-chat cofx chat-id)
{::json-rpc/call [{:method (json-rpc/call-ext-method (waku/enabled? cofx) "createGroupChatFromInvitation")
: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 +93,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 (assoc-in db [:group-chat/invitations id :state] constants/invitation-state-approved)
::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 +110,16 @@
:params [nil chat-id true]
:on-success #(re-frame/dispatch [::chat-updated %])}]})

(fx/defn remove
"Remove chat"
{:events [:group-chats.ui/remove-chat-confirmed]}
[cofx chat-id]
(fx/merge cofx
(models.chat/deactivate-chat chat-id)
(models.chat/upsert-chat {:chat-id chat-id
:is-active false})
(navigation/navigate-to-cofx :home {})))

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

Expand All @@ -104,3 +132,39 @@
::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 membership-retry
{:events [:group-chats.ui/membership-retry]}
[{{:keys [current-chat-id] :as db} :db}]
{:db (assoc-in db [:chat/memberships current-chat-id :retry?] true)})

(fx/defn membership-message
{:events [:group-chats.ui/update-membership-message]}
[{{:keys [current-chat-id] :as db} :db} message]
{:db (assoc-in db [:chat/memberships current-chat-id :message] message)})

(fx/defn send-group-chat-membership-request
"Send group chat membership request"
{:events [:send-group-chat-membership-request]}
[{{:keys [current-chat-id chats] :as db} :db :as cofx}]
(let [{:keys [invitation-admin]} (get chats current-chat-id)
message (get-in db [:chat/memberships current-chat-id :message])]
{:db (assoc-in db [:chat/memberships current-chat-id] nil)
::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 @@ -190,6 +205,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
7 changes: 6 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 Expand Up @@ -79,6 +83,7 @@
[cofx {:keys [type] :as data}]
(case type
:public-chat (handle-public-chat cofx data)
:group-chat (handle-group-chat cofx data)
:private-chat (handle-private-chat cofx data)
:contact (handle-view-profile cofx data)
:browser (handle-browse cofx data)
Expand Down
Loading

0 comments on commit d02d849

Please sign in to comment.