diff --git a/src/status_im/models/pending_messages.cljs b/src/status_im/models/pending_messages.cljs index 093df19a35..d91e9ce0bc 100644 --- a/src/status_im/models/pending_messages.cljs +++ b/src/status_im/models/pending_messages.cljs @@ -14,14 +14,12 @@ (defn get-pending-messages! [] (->> (r/get-all :account :pending-message) r/realm-collection->list - (map (fn [{:keys [message-id] :as message}] - (-> message - (update :topics reader/read-string) - (assoc :id message-id)))))) + (map (fn [message] + (update message :topics reader/read-string))))) (defn- get-id [message-id to] - (let [to' (if (and to (str/starts-with? "0x" to)) + (let [to' (if (and to (str/starts-with? to "0x")) (subs to 2) to) to'' (when to' (subs to' 0 7)) diff --git a/src/status_im/protocol/core.cljs b/src/status_im/protocol/core.cljs index 05ff4c9d04..f1207e75cc 100644 --- a/src/status_im/protocol/core.cljs +++ b/src/status_im/protocol/core.cljs @@ -65,6 +65,7 @@ {:pre [(valid? ::options options)]} (debug :init-whisper) (stop-watching-all!) + (d/reset-all-pending-messages!) (let [web3 (u/make-web3 rpc-url) listener-options {:web3 web3 :identity identity}] diff --git a/src/status_im/protocol/discoveries.cljs b/src/status_im/protocol/discoveries.cljs index 9a377d385b..22667337b0 100644 --- a/src/status_im/protocol/discoveries.cljs +++ b/src/status_im/protocol/discoveries.cljs @@ -63,6 +63,7 @@ (defn contact-request! [{:keys [web3 message]}] {:pre [(valid? :contact-request/message message)]} + (debug :send-command-request!) (d/add-pending-message! web3 (assoc message :type :contact-request diff --git a/src/status_im/protocol/handlers.cljs b/src/status_im/protocol/handlers.cljs index bc4891ebdf..455f14edd1 100644 --- a/src/status_im/protocol/handlers.cljs +++ b/src/status_im/protocol/handlers.cljs @@ -23,8 +23,8 @@ :identity public-key :groups groups :callback #(dispatch [:incoming-message %1 %2]) - :ack-not-received-s-interval 17 - :default-ttl 15 + :ack-not-received-s-interval 125 + :default-ttl 120 :send-online-s-interval 180 :ttl {} :max-attempts-number 3 @@ -51,8 +51,12 @@ (case type :message (dispatch [:received-protocol-message! message]) :group-message (dispatch [:received-protocol-message! message]) - :ack (when (#{:message :group-message} (:type payload)) - (dispatch [:message-delivered message])) + :ack (cond + (#{:message :group-message} (:type payload)) + (dispatch [:message-delivered message]) + + (= :contact-request (:type payload)) + (dispatch [:pending-message-remove message])) :seen (dispatch [:message-seen message]) :group-invitation (dispatch [:group-chat-invite-received message]) :leave-group (dispatch [:participant-left-group message]) diff --git a/src/status_im/protocol/web3/delivery.cljs b/src/status_im/protocol/web3/delivery.cljs index 25d33faeee..545d87bf5e 100644 --- a/src/status_im/protocol/web3/delivery.cljs +++ b/src/status_im/protocol/web3/delivery.cljs @@ -73,29 +73,44 @@ ::attempts ::was-sent?])) (defn add-prepeared-pending-message! - [web3 {:keys [id to] :as pending-message}] + [web3 {:keys [message-id to] :as pending-message}] {:pre [(valid? :delivery/pending-message pending-message)]} (debug :add-prepeared-pending-message!) (let [message (select-keys pending-message [:from :to :topics :payload]) - pending-message' (assoc pending-message :message message)] - (swap! messages assoc-in [web3 id to] pending-message') + pending-message' (assoc pending-message :message message + :id message-id)] + (swap! messages assoc-in [web3 message-id to] pending-message') (when to (swap! recipient->pending-message - update to set/union #{[web3 id to]})))) + update to set/union #{[web3 message-id to]})))) (defn remove-pending-message! [web3 id to] - (swap! messages update-in [web3 id] dissoc to) + (swap! messages update web3 + (fn [messages] + (when messages + (let [message (messages id) + message' (dissoc message to)] + (if (seq message') + (assoc messages id message') + (dissoc messages id)))))) (when to (swap! recipient->pending-message update to set/difference #{[web3 id to]}))) (defn message-was-sent! [web3 id to] - (let [messages' (swap! messages update-in [web3 id to] - (fn [message] - (assoc message :was-sent? true - :attemps 1)))] + (let [messages' (swap! messages update web3 + (fn [messages] + (let [message (get-in messages [id to]) + message' (when message + (assoc message :was-sent? true + :attemps 1))] + (if message' + (assoc-in messages [id to] message') + messages))))] (when @pending-mesage-callback - (@pending-mesage-callback :sent (get-in messages' [web3 id to]))))) + (let [message (get-in messages' [web3 id to])] + (when message + (@pending-mesage-callback :sent message)))))) (defn attempt-was-made! [web3 id to] (debug :attempt-was-made id) @@ -191,3 +206,6 @@ (update-in messages key assoc :last-attempt 0 :attempts 0)))))) + +(defn reset-all-pending-messages! [] + (reset! messages {})) diff --git a/src/status_im/protocol/web3/transport.cljs b/src/status_im/protocol/web3/transport.cljs index ff00d4fcf1..d913869370 100644 --- a/src/status_im/protocol/web3/transport.cljs +++ b/src/status_im/protocol/web3/transport.cljs @@ -13,5 +13,5 @@ (defn post-message! [web3 message callback] {:pre [(valid? :shh/message message)]} - (debug :post-message web3 message) + (debug :post-message message) (.post (u/shh web3) (clj->js message) callback))