fix delivery bugs

This commit is contained in:
Roman Volosovskyi 2016-09-19 12:11:34 +03:00
parent 8ca1d645a3
commit 20fda59dd9
6 changed files with 42 additions and 20 deletions

View File

@ -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))

View File

@ -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}]

View File

@ -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

View File

@ -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])

View File

@ -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 {}))

View File

@ -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))