From dd4f55e11fd40793796f6d51f8c5f170a234f729 Mon Sep 17 00:00:00 2001 From: michaelr Date: Mon, 11 Apr 2016 22:57:20 +0300 Subject: [PATCH] removed from chat & leave chat --- images/leave.png | Bin 0 -> 1719 bytes src/syng_im/components/chat.cljs | 23 +++++++++------ src/syng_im/handlers.cljs | 32 ++++++++++++++++++++- src/syng_im/models/chats.cljs | 10 ++++++- src/syng_im/persistence/realm.cljs | 19 +----------- src/syng_im/protocol/protocol_handler.cljs | 4 +-- src/syng_im/resources.cljs | 1 + src/syng_im/subs.cljs | 9 ++++-- 8 files changed, 65 insertions(+), 33 deletions(-) create mode 100644 images/leave.png diff --git a/images/leave.png b/images/leave.png new file mode 100644 index 0000000000000000000000000000000000000000..17df8fc16bf09fda32bfbcfafa89f086e86d9e09 GIT binary patch literal 1719 zcmV;o21xmdP)Pe>F|9LK*iYyN9#QOxM13ppDk)J2epK)hs!{#+C#MbhXH zHE5*Ugr#*G0qDot@d;H*emy z?{jp1``&No5bS$(`=O491;D!F&>9@^X6 z@$1(wt8+@p!omX8@`Ax2nVp@5rfG1y-DqxZ=C-98N}8CMARQeY`0?Whi+ps6Mx)fq z@_N0btE=muOS9MO#k+U!$i991xDzGo9f-wZWO#U(jE#-iE(ieD*Vn0(Gd(>`#>dCE z^_?p#E4X;^A_9Q`-AB+Z zNF)qncoX$cCwc$=ee&SJgRBa2I-PL2TvU?UiX=&>t*yntzyPYNt8?zu4<9~k`oz0j zF5J3x3w3pMwhku#R_^!vNl#A?e*gY$>VrJ9g~A$jAt;T)83@ zf}l<8?xSP?n3$L_6@2~rb+oj!6pWm$*eOc>_U#*a@#2MH;Much(bUvbkb*X`OO!k} zH)l93-Me=$Iy*ZVrNlROh?33pMcv)qC@Co^=vK59)}rKKFql3-mT~mxQ5L!66)Z)` zpFVv`8=xo(TrQVrMJZqg(%r*381pF(yF@u8>|*(IR8&;iQs?7 zN>Rc9MK(n(FE3LSB_vTM91cTORjQ(dBnzM|N=UK@nxcdx3!x<{qaR1+N+1v* zpeYEjp-=kXxq9^~PMkPFwciyji6T}A0sy(VxHxSGjYk=fTbV+klk*#R99DL762-uKRT3RYyxNyPL z2O^OO1_uYBsw(mMe54SD=XMGLk~UuK@9!r*pD&{VcJAB>kH>?`%1Z3owTo(U08m_9 z43Ecy=H_M;6%{evcoDE)5Rf#%Ix;eX?(S}U{`}c=0j%z$-oAZnc^%2{AP7jBV6CXA zkZ#_*38&L(S3bI!*)7csl4S0j^CAdHF$=CJ3YVfHkqDJ?%K_JNWYD3!AJ`VCFu~3}t0y($Aki$>Yb5F*7rRNF)Nc+l}`2 zcGTC`b0-K$8iJ=|eSMugefktno;*Py5U?_*lr%LpVRUqq%adp|e*mED3f91&kwE|e N002ovPDHLkV1l#&CsF_a literal 0 HcmV?d00001 diff --git a/src/syng_im/components/chat.cljs b/src/syng_im/components/chat.cljs index 71c78f83e0..18efbb4b3b 100644 --- a/src/syng_im/components/chat.cljs +++ b/src/syng_im/components/chat.cljs @@ -55,16 +55,22 @@ :style {:backgroundColor "white" :height 56 :elevation 2} - :actions [{:title "Add Contact to chat" - :icon res/add-icon - :showWithText true} - {:title "Remove Contact from chat" - :icon res/trash-icon - :showWithText true}] + :actions (when (and (:group-chat @chat) + (:is-active @chat)) + [{:title "Add Contact to chat" + :icon res/add-icon + :showWithText true} + {:title "Remove Contact from chat" + :icon res/trash-icon + :showWithText true} + {:title "Leave Chat" + :icon res/leave-icon + :showWithText true}]) :onActionSelected (fn [position] (case position 0 (dispatch [:show-add-participants navigator]) - 1 (dispatch [:show-remove-participants navigator]))) + 1 (dispatch [:show-remove-participants navigator]) + 2 (dispatch [:leave-group-chat navigator]))) :onIconClicked (fn [] (nav-pop navigator))}]) [list-view {:dataSource datasource @@ -75,4 +81,5 @@ (add-msg-color contact-by-identity))] (r/as-element [chat-message msg]))) :style {:backgroundColor "white"}}] - [chat-message-new]])))) + (when (:is-active @chat) + [chat-message-new])])))) diff --git a/src/syng_im/handlers.cljs b/src/syng_im/handlers.cljs index 882653c19f..a4b6878830 100644 --- a/src/syng_im/handlers.cljs +++ b/src/syng_im/handlers.cljs @@ -20,7 +20,8 @@ [syng-im.models.chats :refer [create-chat chat-add-participants - chat-remove-participants]] + chat-remove-participants + set-chat-active]] [syng-im.models.chat :refer [signal-chat-updated set-current-chat-id current-chat-id @@ -144,6 +145,13 @@ :content (str (or remover-name from) " removed " (or removed-name identity)) :content-type text-content-type}))) +(defn you-removed-from-group-msg [chat-id from msg-id] + (let [remover-name (:name (contacts/contact-by-identity from))] + (save-message chat-id {:from "system" + :msg-id msg-id + :content (str (or remover-name from) " removed you from group chat") + :content-type text-content-type}))) + (defn removed-participant-msg [chat-id identity] (let [contact-name (:name (contacts/contact-by-identity identity))] (save-message chat-id {:from "system" @@ -151,6 +159,12 @@ :content (str "You've removed " (or contact-name identity)) :content-type text-content-type}))) +(defn left-chat-msg [chat-id] + (save-message chat-id {:from "system" + :msg-id (random/id) + :content "You left this chat" + :content-type text-content-type})) + (register-handler :group-chat-invite-acked (fn [db [action from group-id ack-msg-id]] (log/debug action from group-id ack-msg-id) @@ -164,6 +178,13 @@ (participant-removed-from-group-msg group-id identity from msg-id) (signal-chat-updated db group-id))) +(register-handler :you-removed-from-group + (fn [db [action from group-id msg-id]] + (log/debug action msg-id from group-id) + (you-removed-from-group-msg group-id from msg-id) + (set-chat-active group-id false) + (signal-chat-updated db group-id))) + (register-handler :participant-invited-to-group (fn [db [action from group-id identity msg-id]] (log/debug action msg-id from group-id identity) @@ -203,6 +224,15 @@ (save-message chat-id msg) (signal-chat-updated db chat-id)))) +(register-handler :leave-group-chat + (fn [db [action navigator]] + (log/debug action) + (let [chat-id (current-chat-id db)] + (api/leave-group-chat chat-id) + (set-chat-active chat-id false) + (left-chat-msg chat-id) + (signal-chat-updated db chat-id)))) + (register-handler :send-chat-command (fn [db [action chat-id command content]] (log/debug action "chat-id" chat-id "command" command "content" content) diff --git a/src/syng_im/models/chats.cljs b/src/syng_im/models/chats.cljs index 13c36096cd..12caa0ecce 100644 --- a/src/syng_im/models/chats.cljs +++ b/src/syng_im/models/chats.cljs @@ -47,6 +47,7 @@ :background-color background :text-color text}) identities group-chat-colors)] (r/create :chats {:chat-id chat-id + :is-active true :name chat-name :group-chat group-chat? :timestamp (timestamp) @@ -94,11 +95,18 @@ (defn active-group-chats [] (let [results (-> (r/get-all :chats) - (r/filtered "group-chat = true"))] + (r/filtered "group-chat = true && is-active = true"))] (->> (.map results (fn [object index collection] (aget object "chat-id"))) (js->clj)))) + +(defn set-chat-active [chat-id active?] + (r/write (fn [] + (-> (r/get-by-field :chats :chat-id chat-id) + (r/single) + (aset "is-active" active?))))) + (comment (active-group-chats) diff --git a/src/syng_im/persistence/realm.cljs b/src/syng_im/persistence/realm.cljs index 3020efeb31..e6fd6990e5 100644 --- a/src/syng_im/persistence/realm.cljs +++ b/src/syng_im/persistence/realm.cljs @@ -43,6 +43,7 @@ :name "string" :group-chat {:type "bool" :indexed true} + :is-active "bool" :timestamp "int" :contacts {:type "list" :objectType "chat-contact"}}}]}) @@ -130,22 +131,4 @@ (comment - (write #(.create realm "msgs" (clj->js {:msg-id "12" - :content "sdfd" - :from "sdfsd" - :chat-id "56" - :content-type "fg" - :timestamp 2 - :outgoing true - :to "sfs" - :delivery-status "seen"}) true)) - - (.addListener realm "change" (fn [& args] - (log/debug args))) - - ;realm.addListener('change', () => { - ; // Update UI - ; ... - ; }); - ) \ No newline at end of file diff --git a/src/syng_im/protocol/protocol_handler.cljs b/src/syng_im/protocol/protocol_handler.cljs index 2eacd6f4a7..c6556b4864 100644 --- a/src/syng_im/protocol/protocol_handler.cljs +++ b/src/syng_im/protocol/protocol_handler.cljs @@ -36,8 +36,8 @@ (dispatch [:participant-invited-to-group from group-id identity msg-id])) :group-removed-participant (let [{:keys [group-id identity from msg-id]} event] (dispatch [:participant-removed-from-group from group-id identity msg-id])) - ;:removed-from-group (let [{:keys [group-id from]} event] - ; (add-to-chat "group-chat" ":" (str (shorten from) " removed you from group chat"))) + :removed-from-group (let [{:keys [group-id from msg-id]} event] + (dispatch [:you-removed-from-group from group-id msg-id])) ;:participant-left-group (let [{:keys [group-id from]} event] ; (add-to-chat "group-chat" ":" (str (shorten from) " left group chat"))) ;(add-to-chat "chat" ":" (str "Don't know how to handle " event-type)) diff --git a/src/syng_im/resources.cljs b/src/syng_im/resources.cljs index aa3b31e2e1..956fd46c28 100644 --- a/src/syng_im/resources.cljs +++ b/src/syng_im/resources.cljs @@ -15,3 +15,4 @@ (def v (js/require "./images/v.png")) (def add-icon (js/require "./images/add.png")) (def trash-icon (js/require "./images/trash.png")) +(def leave-icon (js/require "./images/leave.png")) diff --git a/src/syng_im/subs.cljs b/src/syng_im/subs.cljs index 7718291632..7239a549b3 100644 --- a/src/syng_im/subs.cljs +++ b/src/syng_im/subs.cljs @@ -60,10 +60,13 @@ (register-sub :get-current-chat (fn [db _] (let [current-chat-id (-> (current-chat-id @db) + (reaction)) + chat-updated (-> (chat-updated? @db @current-chat-id) (reaction))] - (-> (when-let [chat-id @current-chat-id] - (chat-by-id chat-id)) - (reaction))))) + (reaction + (let [_ @chat-updated] + (when-let [chat-id @current-chat-id] + (chat-by-id chat-id))))))) ;; -- User data --------------------------------------------------------------