diff --git a/src/status_im/events.cljs b/src/status_im/events.cljs index 3dfaec2b111..0e700bcccc8 100644 --- a/src/status_im/events.cljs +++ b/src/status_im/events.cljs @@ -813,15 +813,12 @@ (handlers/register-handler-fx :transport/message-sent (fn [cofx [_ response messages-count]] - (let [{:keys [localChatId id messageType]} (-> response :messages first)] - (fx/merge cofx - (handle-update response) - (transport.message/set-message-envelope-hash localChatId id messageType messages-count))))) - -(handlers/register-handler-fx - :transport/contact-message-sent - (fn [cofx [_ chat-id envelope-hash]] - (transport.message/set-contact-message-envelope-hash cofx chat-id envelope-hash))) + (let [set-hash-fxs (map (fn [{:keys [localChatId id messageType]}] + (transport.message/set-message-envelope-hash localChatId id messageType messages-count)) + (:messages response))] + (apply fx/merge cofx + (conj set-hash-fxs + (handle-update response)))))) (handlers/register-handler-fx :transport.callback/node-info-fetched diff --git a/src/status_im/transport/message/core.cljs b/src/status_im/transport/message/core.cljs index 81385ff1127..f905aabdc8f 100644 --- a/src/status_im/transport/message/core.cljs +++ b/src/status_im/transport/message/core.cljs @@ -95,37 +95,37 @@ confirmations)})))) (fx/defn update-envelope-status - [{:keys [db] :as cofx} envelope-hash status] - (let [{:keys [chat-id message-type message-id]} - (get-in db [:transport/message-envelopes envelope-hash])] - (case message-type - :contact-message - (when (= :sent status) - (remove-hash cofx envelope-hash)) - - (when-let [{:keys [from]} (get-in db [:chats chat-id :messages message-id])] - (check-confirmations cofx status chat-id message-id))))) + [{:keys [db] :as cofx} message-id status] + (if-let [{:keys [chat-id]} + (get-in db [:transport/message-envelopes message-id])] + (when-let [{:keys [from]} (get-in db [:chats chat-id :messages message-id])] + (check-confirmations cofx status chat-id message-id)) + ;; We don't have a message-envelope for this, might be that the confirmation + ;; came too early + {:db (update-in db [:transport/message-confirmations message-id] conj status)})) (fx/defn update-envelopes-status - [{:keys [db] :as cofx} envelope-hashes status] - (apply fx/merge cofx (map #(update-envelope-status % status) envelope-hashes))) - -(fx/defn set-contact-message-envelope-hash - [{:keys [db] :as cofx} chat-id envelope-hash] - {:db (assoc-in db [:transport/message-envelopes envelope-hash] - {:chat-id chat-id - :message-type :contact-message})}) + [{:keys [db] :as cofx} message-id status] + (apply fx/merge cofx (map #(update-envelope-status % status) message-id))) (fx/defn set-message-envelope-hash "message-type is used for tracking" [{:keys [db] :as cofx} chat-id message-id message-type messages-count] - {:db (-> db - (assoc-in [:transport/message-envelopes message-id] - {:chat-id chat-id - :message-id message-id - :message-type message-type}) - (update-in [:transport/message-ids->confirmations message-id] - #(or % {:pending-confirmations messages-count})))}) + ;; Check first if the confirmation has already arrived + (let [statuses (get-in db [:transport/message-confirmations message-id]) + check-confirmations-fx (map + #(check-confirmations % chat-id message-id) + statuses) + + add-envelope-data (fn [{:keys [db]}] + {:db (-> db + (update :transport/message-confirmations dissoc message-id) + (assoc-in [:transport/message-envelopes message-id] + {:chat-id chat-id + :message-type message-type}) + (update-in [:transport/message-ids->confirmations message-id] + #(or % {:pending-confirmations messages-count})))})] + (apply fx/merge cofx (conj check-confirmations-fx add-envelope-data)))) (defn- own-info [db] (let [{:keys [name photo-path address]} (:multiaccount db)] diff --git a/test/cljs/status_im/test/transport/core.cljs b/test/cljs/status_im/test/transport/core.cljs index f2d9a505843..867371ca411 100644 --- a/test/cljs/status_im/test/transport/core.cljs +++ b/test/cljs/status_im/test/transport/core.cljs @@ -67,7 +67,6 @@ (let [cofx (message/set-message-envelope-hash initial-cofx chat-id message-id :message-type 1)] (testing "it sets the message-infos" (is (= {:chat-id chat-id - :message-id message-id :message-type :message-type} (get-in cofx [:db :transport/message-envelopes message-id])))) (testing "the message is sent" @@ -75,7 +74,6 @@ (get-in (message/update-envelope-status cofx message-id :sent) [:db :chats chat-id :messages message-id :outgoing-status])))) - (testing "the message is not sent" (is (= :not-sent (get-in @@ -107,6 +105,22 @@ (get-in cofx [:db :chats chat-id :messages message-id :outgoing-status])))))) + (testing "order of events is reversed" + (let [cofx (fx/merge + initial-cofx + (message/update-envelope-status message-id :sent) + (message/update-envelope-status message-id :sent) + (message/update-envelope-status message-id :sent) + (message/set-message-envelope-hash chat-id message-id :message-type 3))] + (testing "it removes the confirmations" + (is (not (get-in cofx [:db :transport/message-confirmations message-id]))) + (is (not (get-in cofx [:db :transport/message-ids->confirmations message-id])))) + + (testing "the message is sent" + (is (= :sent + (get-in + cofx + [:db :chats chat-id :messages message-id :outgoing-status])))))) (testing "message not sent" (let [cofx (fx/merge initial-cofx