From 3e95b8540859c00ede1ef3771ff541599efb1131 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Mon, 26 Sep 2016 16:41:22 +0300 Subject: [PATCH] Group chat: stop watching of group's topic & handle new participant (fixes #265 #263) Former-commit-id: 828010849bf5fcf73d516af30f5f11771a7a783d --- env/dev/env/android/main.cljs | 4 +- src/status_im/chat/handlers.cljs | 3 ++ src/status_im/models/pending_messages.cljs | 7 ++- src/status_im/protocol/group.cljs | 2 +- src/status_im/protocol/handlers.cljs | 60 +++++++++++----------- src/status_im/protocol/listeners.cljs | 6 ++- src/status_im/protocol/web3/delivery.cljs | 9 +++- src/status_im/transactions/subs.cljs | 7 ++- src/status_im/utils/hex.cljs | 7 +++ 9 files changed, 61 insertions(+), 44 deletions(-) create mode 100644 src/status_im/utils/hex.cljs diff --git a/env/dev/env/android/main.cljs b/env/dev/env/android/main.cljs index 931ed19ce5..d729fd654f 100644 --- a/env/dev/env/android/main.cljs +++ b/env/dev/env/android/main.cljs @@ -3,9 +3,7 @@ [status-im.android.core :as core] [figwheel.client :as figwheel :include-macros true])) - (enable-console-print!) - -(set! js/console.disableYellowBox true) +(enable-console-print!) (def cnt (r/atom 0)) (defn reloader [] @cnt [core/app-root]) diff --git a/src/status_im/chat/handlers.cljs b/src/status_im/chat/handlers.cljs index 4610c191c6..cf44887111 100644 --- a/src/status_im/chat/handlers.cljs +++ b/src/status_im/chat/handlers.cljs @@ -373,6 +373,9 @@ (u/side-effect! (fn [{:keys [web3 current-chat-id chats current-public-key]} _] (let [{:keys [public-key private-key]} (chats current-chat-id)] + (protocol/stop-watching-group! + {:web3 web3 + :group-id current-chat-id}) (protocol/leave-group-chat! {:web3 web3 :group-id current-chat-id diff --git a/src/status_im/models/pending_messages.cljs b/src/status_im/models/pending_messages.cljs index 04db12a9d0..12a6b5d501 100644 --- a/src/status_im/models/pending_messages.cljs +++ b/src/status_im/models/pending_messages.cljs @@ -9,7 +9,8 @@ [status-im.utils.types :refer [clj->json json->clj]] [status-im.commands.utils :refer [generate-hiccup]] [cljs.reader :as reader] - [clojure.string :as str])) + [clojure.string :as str] + [status-im.utils.hex :as i])) (defn get-pending-messages! [] (->> (r/get-all :account :pending-message) @@ -19,9 +20,7 @@ (defn- get-id [message-id to] - (let [to' (if (and to (str/starts-with? to "0x")) - (subs to 2) - to) + (let [to' (i/normalize-hex to) to'' (when to' (subs to' 0 7)) id' (if to'' (str message-id "-" (subs to'' 0 7)) diff --git a/src/status_im/protocol/group.cljs b/src/status_im/protocol/group.cljs index 0d875a022c..bf7aa870c6 100644 --- a/src/status_im/protocol/group.cljs +++ b/src/status_im/protocol/group.cljs @@ -104,7 +104,7 @@ (defn stop-watching-group! [{:keys [web3 group-id]}] {:pre [(valid? :message/chat-id group-id)]} - (f/remove-filter! web3 [group-id])) + (f/remove-filter! web3 {:topics [group-id]})) (defn start-watching-group! [{:keys [web3 group-id keypair callback identity]}] diff --git a/src/status_im/protocol/handlers.cljs b/src/status_im/protocol/handlers.cljs index 2f59b22136..2b51651e8f 100644 --- a/src/status_im/protocol/handlers.cljs +++ b/src/status_im/protocol/handlers.cljs @@ -18,30 +18,30 @@ updates-private-key]} (get-in db [:accounts current-account-id])] (let [groups (chats/active-group-chats) - w3 (protocol/init-whisper! - {:rpc-url "http://localhost:8545" - :identity public-key - :groups groups - :callback #(dispatch [:incoming-message %1 %2]) - :ack-not-received-s-interval 125 - :default-ttl 120 - :send-online-s-interval 180 - :ttl {} - :max-attempts-number 3 - :delivery-loop-ms-interval 500 - :profile-keypair {:public updates-public-key - :private updates-private-key} - :hashtags (u/get-hashtags status) - :pending-messages (pending-messages/get-pending-messages!) - :contacts (keep (fn [{:keys [whisper-identity - public-key - private-key]}] - (when (and public-key private-key) - {:identity whisper-identity - :keypair {:public public-key - :private private-key}})) + w3 (protocol/init-whisper! + {:rpc-url "http://localhost:8545" + :identity public-key + :groups groups + :callback #(dispatch [:incoming-message %1 %2]) + :ack-not-received-s-interval 125 + :default-ttl 120 + :send-online-s-interval 180 + :ttl {} + :max-attempts-number 3 + :delivery-loop-ms-interval 500 + :profile-keypair {:public updates-public-key + :private updates-private-key} + :hashtags (u/get-hashtags status) + :pending-messages (pending-messages/get-pending-messages!) + :contacts (keep (fn [{:keys [whisper-identity + public-key + private-key]}] + (when (and public-key private-key) + {:identity whisper-identity + :keypair {:public public-key + :private private-key}})) - (contacts/get-contacts))})] + (contacts/get-contacts))})] (assoc db :web3 w3))))) (register-handler :incoming-message @@ -56,6 +56,7 @@ (dispatch [:pending-message-remove message])) :seen (dispatch [:message-seen message]) :group-invitation (dispatch [:group-chat-invite-received message]) + :add-group-identity (dispatch [:participant-invited-to-group message]) :leave-group (dispatch [:participant-left-group message]) :contact-request (dispatch [:contact-request-received message]) :discovery (dispatch [:discovery-response-received message]) @@ -156,11 +157,12 @@ (register-handler :participant-invited-to-group (u/side-effect! - (fn [{:keys [current-public-key]} [action from group-id identity message-id]] - (log/debug action message-id from group-id identity) + (fn [{:keys [current-public-key]} + [_ {:keys [from] + {:keys [group-id identity message-id]} :payload}]] (participant-invited-to-group-message group-id current-public-key identity from message-id) - ;; todo uncomment - #_(dispatch [:add-contact-to-group! group-id identity])))) + (when-not (= current-public-key identity) + (dispatch [:add-contact-to-group! group-id identity]))))) (register-handler :add-contact-to-group! (u/side-effect! @@ -244,9 +246,9 @@ (when (#{:message :group-message} type) (messages/update-message! {:message-id id :delivery-status :pending})))) - (fn [db [_ {:keys [type id to groupd-id]}]] + (fn [db [_ {:keys [type id to group-id]}]] (if (#{:message :group-message} type) - (let [chat-id (or groupd-id to) + (let [chat-id (or group-id to) current-status (get-in db [:message-status chat-id id])] (if-not (= :seen current-status) (assoc-in db [:message-status chat-id id] :pending) diff --git a/src/status_im/protocol/listeners.cljs b/src/status_im/protocol/listeners.cljs index 56afb9b394..e08781e0c0 100644 --- a/src/status_im/protocol/listeners.cljs +++ b/src/status_im/protocol/listeners.cljs @@ -3,7 +3,8 @@ [status-im.protocol.ack :as ack] [status-im.protocol.web3.utils :as u] [status-im.protocol.encryption :as e] - [taoensso.timbre :refer-macros [debug] :as log])) + [taoensso.timbre :refer-macros [debug] :as log] + [status-im.utils.hex :as i])) (defn- parse-payload [payload] (debug :parse-payload) @@ -34,7 +35,8 @@ (debug :message-received) (let [{:keys [from payload to] :as message} (js->clj js-message :keywordize-keys true)] - (when-not (= identity from) + (when-not (= (i/normalize-hex identity) + (i/normalize-hex from)) (let [{:keys [type ack?] :as payload'} (parse-payload payload) diff --git a/src/status_im/protocol/web3/delivery.cljs b/src/status_im/protocol/web3/delivery.cljs index 35ba5f4286..d00d6e3784 100644 --- a/src/status_im/protocol/web3/delivery.cljs +++ b/src/status_im/protocol/web3/delivery.cljs @@ -89,7 +89,14 @@ (fn [messages] (when messages (let [message (messages id) - message' (dissoc message to)] + ;; Message that is send without specified "from" option + ;; is stored in pending "messages" map as + ;; {message-id {nil message}}. + ;; When we receive the first ack for such message it is + ;; removed from pending messages adding of the nil key + ;; to the next dissoc form + ;; todo rewrite handling of ack message in more clear way + message' (dissoc message to nil)] (if (seq message') (assoc messages id message') (dissoc messages id)))))) diff --git a/src/status_im/transactions/subs.cljs b/src/status_im/transactions/subs.cljs index 5240d0575d..43c1e7c8ea 100644 --- a/src/status_im/transactions/subs.cljs +++ b/src/status_im/transactions/subs.cljs @@ -1,7 +1,8 @@ (ns status-im.transactions.subs (:require-macros [reagent.ratom :refer [reaction]]) (:require [re-frame.core :refer [register-sub subscribe]] - [clojure.string :as s])) + [clojure.string :as s] + [status-im.utils.hex :as i])) (register-sub :transactions (fn [db] @@ -19,9 +20,7 @@ (fn [_ [_ address]] (let [contacts (subscribe [:contacts-by-address]) address' (when address - (if (s/starts-with? address "0x") - (subs address 2) - address))] + (i/normalize-hex address))] (reaction (@contacts address'))))) (register-sub :wrong-password? diff --git a/src/status_im/utils/hex.cljs b/src/status_im/utils/hex.cljs new file mode 100644 index 0000000000..9df30967fa --- /dev/null +++ b/src/status_im/utils/hex.cljs @@ -0,0 +1,7 @@ +(ns status-im.utils.hex + (:require [clojure.string :as s])) + +(defn normalize-hex [hex] + (if (and hex (s/starts-with? hex "0x")) + (subs hex 2) + hex))