Merge pull request #251 from status-im/bug/#248

Delivery status issues #248

Former-commit-id: ebd09dcc77
This commit is contained in:
Roman Volosovskyi 2016-09-22 17:17:56 +03:00 committed by GitHub
commit d4f51d06ad
3 changed files with 41 additions and 32 deletions

View File

@ -46,10 +46,12 @@
(r/create :account :pending-message message' true))))) (r/create :account :pending-message message' true)))))
(defn remove-pending-message! (defn remove-pending-message!
[{{:keys [message-id]} :payload}] [{{:keys [message-id ack-of-message]} :payload}]
(r/write :account (r/write :account
(fn [] (fn []
(r/delete :account (r/get-by-field :account :pending-message :message-id message-id))))) (r/delete :account
(r/get-by-field :account :pending-message
:message-id (or ack-of-message message-id))))))
(defn remove-all-by-chat [chat-id] (defn remove-all-by-chat [chat-id]
(r/write (r/write

View File

@ -1,22 +1,24 @@
(ns status-im.protocol.ack (ns status-im.protocol.ack
(:require [status-im.protocol.web3.delivery :as d] (:require [status-im.protocol.web3.delivery :as d]
[status-im.protocol.web3.filtering :as f])) [status-im.protocol.web3.filtering :as f]
[status-im.utils.random :as random]))
(defn check-ack! (defn check-ack!
[web3 [web3
from from
{:keys [type requires-ack? message-id ack? group-id]} {:keys [type requires-ack? message-id ack? group-id ack-of-message]}
identity] identity]
(when (and requires-ack? (not ack?)) (when (and requires-ack? (not ack?))
(let [message {:from identity (let [message {:from identity
:to from :to from
:message-id message-id :message-id (random/id)
:topics [f/status-topic] :topics [f/status-topic]
:type type :type type
:ack? true :ack? true
:payload {:type type :payload {:type type
:ack? true :ack? true
:ack-of-message message-id
:group-id group-id}}] :group-id group-id}}]
(d/add-pending-message! web3 message))) (d/add-pending-message! web3 message)))
(when ack? (when ack?
(d/remove-pending-message! web3 message-id from))) (d/remove-pending-message! web3 ack-of-message from)))

View File

@ -181,8 +181,10 @@
(defn save-message-status! [status] (defn save-message-status! [status]
(fn [_ [_ (fn [_ [_
{:keys [from] {:keys [from]
{:keys [message-id group-id]} :payload}]] {:keys [message-id ack-of-message group-id]} :payload}]]
(when-let [message (messages/get-message message-id)] (let [message-id' (or ack-of-message message-id)]
(when-let [{:keys [message-status] :as message} (messages/get-message message-id')]
(when-not (= (keyword message-status) :seen)
(let [group? (boolean group-id) (let [group? (boolean group-id)
message (if (and group? (not= status :sent)) message (if (and group? (not= status :sent))
(update-in message (update-in message
@ -194,18 +196,19 @@
old-status old-status
status)})) status)}))
(assoc message :message-status status))] (assoc message :message-status status))]
(messages/update-message! message))))) (messages/update-message! message)))))))
(defn update-message-status [status] (defn update-message-status [status]
(fn [db (fn [db
[_ {:keys [from] [_ {:keys [from]
{:keys [message-id group-id]} :payload}]] {:keys [message-id ack-of-message group-id]} :payload}]]
(if (chats/is-active? (or group-id from)) (if (chats/is-active? (or group-id from))
(let [group? (boolean group-id) (let [message-id' (or ack-of-message message-id)
group? (boolean group-id)
status-path (if (and group? (not= status :sent)) status-path (if (and group? (not= status :sent))
[:message-user-statuses message-id from] [:message-user-statuses message-id' from]
[:message-statuses message-id]) [:message-statuses message-id'])
current-status (get-in db status-path)] current-status (get-in db status-path)]
(if-not (= :seen current-status) (if-not (= :seen current-status)
(assoc-in db status-path {:whisper-identity from (assoc-in db status-path {:whisper-identity from
@ -232,8 +235,10 @@
(register-handler :message-seen (register-handler :message-seen
[(after (save-message-status! :seen)) [(after (save-message-status! :seen))
(after (fn [_ [_ {:keys [chat-id]}]] (after (fn [_ [_ {:keys [from]
(dispatch [:remove-unviewed-messages chat-id])))] {:keys [group-id]} :payload}]]
(when-not group-id
(dispatch [:remove-unviewed-messages from]))))]
(update-message-status :seen)) (update-message-status :seen))
(register-handler :pending-message-upsert (register-handler :pending-message-upsert