From cfc16ee008226ab5d72bf438ac6f7934a272ddb7 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Tue, 20 Sep 2016 19:17:37 +0300 Subject: [PATCH] Fixes delivery status issues: use :ack-of-message instead of :message-id check if message was already set to :seen --- src/status_im/models/pending_messages.cljs | 6 ++- src/status_im/protocol/ack.cljs | 16 ++++--- src/status_im/protocol/handlers.cljs | 51 ++++++++++++---------- 3 files changed, 41 insertions(+), 32 deletions(-) diff --git a/src/status_im/models/pending_messages.cljs b/src/status_im/models/pending_messages.cljs index d91e9ce0bc..04db12a9d0 100644 --- a/src/status_im/models/pending_messages.cljs +++ b/src/status_im/models/pending_messages.cljs @@ -46,10 +46,12 @@ (r/create :account :pending-message message' true))))) (defn remove-pending-message! - [{{:keys [message-id]} :payload}] + [{{:keys [message-id ack-of-message]} :payload}] (r/write :account (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] (r/write diff --git a/src/status_im/protocol/ack.cljs b/src/status_im/protocol/ack.cljs index 8d8a38b4a5..c77320c448 100644 --- a/src/status_im/protocol/ack.cljs +++ b/src/status_im/protocol/ack.cljs @@ -1,22 +1,24 @@ (ns status-im.protocol.ack (: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! [web3 from - {:keys [type requires-ack? message-id ack? group-id]} + {:keys [type requires-ack? message-id ack? group-id ack-of-message]} identity] (when (and requires-ack? (not ack?)) (let [message {:from identity :to from - :message-id message-id + :message-id (random/id) :topics [f/status-topic] :type type :ack? true - :payload {:type type - :ack? true - :group-id group-id}}] + :payload {:type type + :ack? true + :ack-of-message message-id + :group-id group-id}}] (d/add-pending-message! web3 message))) (when ack? - (d/remove-pending-message! web3 message-id from))) + (d/remove-pending-message! web3 ack-of-message from))) diff --git a/src/status_im/protocol/handlers.cljs b/src/status_im/protocol/handlers.cljs index fe00fea927..1da61f730b 100644 --- a/src/status_im/protocol/handlers.cljs +++ b/src/status_im/protocol/handlers.cljs @@ -180,32 +180,35 @@ (defn save-message-status! [status] (fn [_ [_ - {:keys [from] - {:keys [message-id group-id]} :payload}]] - (when-let [message (messages/get-message message-id)] - (let [group? (boolean group-id) - message (if (and group? (not= status :sent)) - (update-in message - [:user-statuses from] - (fn [{old-status :status}] - {:id (random/id) - :whisper-identity from - :status (if (= (keyword old-status) :seen) - old-status - status)})) - (assoc message :message-status status))] - (messages/update-message! message))))) + {:keys [from] + {:keys [message-id ack-of-message group-id]} :payload}]] + (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) + message (if (and group? (not= status :sent)) + (update-in message + [:user-statuses from] + (fn [{old-status :status}] + {:id (random/id) + :whisper-identity from + :status (if (= (keyword old-status) :seen) + old-status + status)})) + (assoc message :message-status status))] + (messages/update-message! message))))))) (defn update-message-status [status] (fn [db - [_ {:keys [from] - {:keys [message-id group-id]} :payload}]] + [_ {:keys [from] + {:keys [message-id ack-of-message group-id]} :payload}]] (if (chats/is-active? (or group-id from)) - (let [group? (boolean group-id) - status-path (if (and group? (not= status :sent)) - [:message-user-statuses message-id from] - [:message-statuses message-id]) + (let [message-id' (or ack-of-message message-id) + group? (boolean group-id) + status-path (if (and group? (not= status :sent)) + [:message-user-statuses message-id' from] + [:message-statuses message-id']) current-status (get-in db status-path)] (if-not (= :seen current-status) (assoc-in db status-path {:whisper-identity from @@ -232,8 +235,10 @@ (register-handler :message-seen [(after (save-message-status! :seen)) - (after (fn [_ [_ {:keys [chat-id]}]] - (dispatch [:remove-unviewed-messages chat-id])))] + (after (fn [_ [_ {:keys [from] + {:keys [group-id]} :payload}]] + (when-not group-id + (dispatch [:remove-unviewed-messages from]))))] (update-message-status :seen)) (register-handler :pending-message-upsert