Skip to content

Commit

Permalink
invitations messages
Browse files Browse the repository at this point in the history
  • Loading branch information
flexsurfer committed Aug 13, 2020
1 parent acea09b commit 210cd4a
Show file tree
Hide file tree
Showing 15 changed files with 264 additions and 87 deletions.
19 changes: 0 additions & 19 deletions src/status_im/chat/models.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -133,25 +133,6 @@
chats))}
(transport.filters/load-chats filtered-chats))))

(fx/defn add-group-chat-from-link
"Add chat to db and update"
[{:keys [db] :as cofx} link-params]
(let [[admin-pk chat-name chat-id] (string/split link-params #"&")]
;;TODO not sure if chat already exists should we just do nothing?
;;TODO save chat to status-go
{:db (assoc-in db [:chats chat-id] {:chat-name chat-name
:identicon ""
:color "#FE8F59" ;; TODO where to get color?
:last-clock-value 0 ;;TODO?
:name chat-name
:unviewed-messages-count 0
:is-active true
:group-chat-status :link-opened
:group-chat true
:public? false
:chat-id chat-id
:deleted-at-clock-value 0})}))

(fx/defn upsert-chat
"Upsert chat when not deleted"
[{:keys [db] :as cofx} {:keys [chat-id] :as chat-props}]
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
7 changes: 7 additions & 0 deletions src/status_im/data_store/invitations.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(ns status-im.data-store.invitations)

(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_createGroupChatFromInvitationLink" {}
"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_createGroupChatFromInvitationLink" {}
"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
15 changes: 12 additions & 3 deletions src/status_im/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@
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]
[status-im.group-chats.core :as models.group]))

;; init module
(handlers/register-handler-fx
Expand Down Expand Up @@ -776,14 +778,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 [(models.group/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 +809,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
35 changes: 34 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]
[clojure.string :as string]))

(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
[{: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 %])}]}))

(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 Down Expand Up @@ -104,3 +113,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
14 changes: 14 additions & 0 deletions src/status_im/subs.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,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 @@ -807,6 +808,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
17 changes: 15 additions & 2 deletions src/status_im/transport/message/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
[status-im.data-store.reactions :as data-store.reactions]
[status-im.data-store.contacts :as data-store.contacts]
[status-im.data-store.chats :as data-store.chats]
[status-im.data-store.invitations :as data-store.invitations]
[status-im.group-chats.core :as models.group]
[status-im.utils.fx :as fx]
[status-im.utils.types :as types]))

Expand All @@ -24,14 +26,18 @@
(fx/defn handle-reactions [cofx reactions]
(models.reactions/receive-signal cofx reactions))

(fx/defn handle-invitations [cofx invitations]
(models.group/handle-invitations cofx invitations))

(fx/defn process-response
{:events [::process]}
[cofx ^js response-js]
(let [^js chats (.-chats response-js)
^js contacts (.-contacts response-js)
^js installations (.-installations response-js)
^js messages (.-messages response-js)
^js emoji-reactions (.-emojiReactions response-js)]
^js emoji-reactions (.-emojiReactions response-js)
^js invitations (.-invitations response-js)]
(cond
(seq installations)
(let [installations-clj (types/js->clj installations)]
Expand Down Expand Up @@ -68,7 +74,14 @@
(js-delete response-js "emojiReactions")
(fx/merge cofx
{:utils/dispatch-later [{:ms 20 :dispatch [::process response-js]}]}
(handle-reactions (map data-store.reactions/<-rpc reactions)))))))
(handle-reactions (map data-store.reactions/<-rpc reactions))))

(seq invitations)
(let [invitations (types/js->clj invitations)]
(js-delete response-js "invitations")
(fx/merge cofx
{:utils/dispatch-later [{:ms 20 :dispatch [::process response-js]}]}
(handle-invitations (map data-store.invitations/<-rpc invitations)))))))

(fx/defn remove-hash
[{:keys [db] :as cofx} envelope-hash]
Expand Down
Loading

0 comments on commit 210cd4a

Please sign in to comment.