From 874e88fc3835a6acebcf075aa5b3898bae100834 Mon Sep 17 00:00:00 2001 From: janherich Date: Thu, 24 May 2018 13:25:38 +0200 Subject: [PATCH] Fix seen statuses Signed-off-by: Igor Mandrigin --- src/status_im/chat/core.cljs | 32 +++++++++++-------- src/status_im/chat/models/message.cljs | 4 +-- src/status_im/data_store/core.cljs | 4 +-- src/status_im/transport/handlers.cljs | 16 ++++------ .../transport/message/v1/protocol.cljs | 3 +- src/status_im/utils/handlers_macro.cljs | 2 +- 6 files changed, 32 insertions(+), 29 deletions(-) diff --git a/src/status_im/chat/core.cljs b/src/status_im/chat/core.cljs index 2a1b700f87..0d38b11117 100644 --- a/src/status_im/chat/core.cljs +++ b/src/status_im/chat/core.cljs @@ -1,20 +1,26 @@ -(ns status-im.chat.core) +(ns status-im.chat.core + (:require [status-im.data-store.messages :as messages-store])) ;; Seen messages (defn receive-seen - [chat-id sender {:keys [message-ids js-obj]} {:keys [db]}] + [chat-id sender {:keys [message-ids]} {:keys [db js-obj]}] (merge - {:confirm-message-processed [{:web3 (:web3 db) - :js-obj js-obj}]} + {:confirm-messages-processed [{:web3 (:web3 db) + :js-obj js-obj}]} (when-let [seen-messages-ids (-> (get-in db [:chats chat-id :messages]) (select-keys message-ids) keys)] - {:db (reduce - (fn [new-db message-id] - (assoc-in new-db - [:chats chat-id - :messages message-id - :user-statuses sender] - :seen)) - db - seen-messages-ids)}))) + (let [new-db (reduce + (fn [new-db message-id] + (assoc-in new-db + [:chats chat-id + :messages message-id + :user-statuses sender] + :seen)) + db + seen-messages-ids)] + {:db new-db + :data-store/tx [(messages-store/update-messages-tx + (map #(select-keys (get-in db [:chats chat-id :messages %]) + [:message-id :user-statuses]) + seen-messages-ids))]})))) diff --git a/src/status_im/chat/models/message.cljs b/src/status_im/chat/models/message.cljs index 5f7e8bbd50..7e18df11c6 100644 --- a/src/status_im/chat/models/message.cljs +++ b/src/status_im/chat/models/message.cljs @@ -128,8 +128,8 @@ request-command) add-message-fn (if batch? add-batch-message add-single-message)] (handlers-macro/merge-fx cofx - {:confirm-message-processed [{:web3 web3 - :js-obj js-obj}]} + {:confirm-messages-processed [{:web3 web3 + :js-obj js-obj}]} (add-message-fn (cond-> message public-key (assoc :user-statuses {public-key (if current-chat? :seen :received)}) diff --git a/src/status_im/data_store/core.cljs b/src/status_im/data_store/core.cljs index 849621f714..322cb32b52 100644 --- a/src/status_im/data_store/core.cljs +++ b/src/status_im/data_store/core.cljs @@ -21,9 +21,7 @@ (data-source/change-account address new-account? encryption-key handler)) (defn- perform-transactions [raw-transactions realm] - (let [success-events (->> raw-transactions - (map :success-event) - (filter identity)) + (let [success-events (keep :success-event raw-transactions) transactions (map (fn [{:keys [transaction] :as f}] (or transaction f)) raw-transactions)] (data-source/write realm #(doseq [transaction transactions] diff --git a/src/status_im/transport/handlers.cljs b/src/status_im/transport/handlers.cljs index f9a33904f7..e112998a3a 100644 --- a/src/status_im/transport/handlers.cljs +++ b/src/status_im/transport/handlers.cljs @@ -29,15 +29,14 @@ (let [{:keys [payload sig timestamp ttl]} (js->clj js-message :keywordize-keys true) status-message (-> payload transport.utils/to-utf8 - transit/deserialize - (assoc :js-obj js-message))] + transit/deserialize)] (when (and sig status-message) (handlers-macro/merge-fx - cofx + (assoc cofx :js-obj js-message) (message/receive status-message (or chat-id sig) sig) (update-last-received-from-inbox now-in-s timestamp ttl))))) -(defn- js-array->list [array] +(defn- js-array->seq [array] (for [i (range (.-length array))] (aget array i))) @@ -47,7 +46,7 @@ cofx (fn [message temp-cofx] (receive-message temp-cofx now-in-s chat-id message)) - (js-array->list js-messages)))) + (js-array->seq js-messages)))) (handlers/register-handler-fx :protocol/receive-whisper-message @@ -173,9 +172,8 @@ fx)))) (re-frame/reg-fx - ;; TODO(rasom): confirmMessagesProcessed should be called after :data-store/tx - ;; effect, so this effect should be rewritten/removed - :confirm-message-processed + ;; TODO(janherich): this should be called after `:data-store/tx` actually + :confirm-messages-processed (fn [messages] (let [{:keys [web3]} (first messages) js-messages (->> messages @@ -186,4 +184,4 @@ js-messages (fn [err resp] (when err - (log/info "Confirming message processed failed")))))))) + (log/info "Confirming messages processed failed")))))))) diff --git a/src/status_im/transport/message/v1/protocol.cljs b/src/status_im/transport/message/v1/protocol.cljs index 84e9956957..875df1b4ce 100644 --- a/src/status_im/transport/message/v1/protocol.cljs +++ b/src/status_im/transport/message/v1/protocol.cljs @@ -101,7 +101,8 @@ :message-id (transport.utils/message-id this) :show? true :chat-id chat-id - :from signature)]})) + :from signature + :js-obj (:js-obj cofx))]})) (defrecord MessagesSeen [message-ids] message/StatusMessage diff --git a/src/status_im/utils/handlers_macro.cljs b/src/status_im/utils/handlers_macro.cljs index 2aab1c9436..cb077f60cc 100644 --- a/src/status_im/utils/handlers_macro.cljs +++ b/src/status_im/utils/handlers_macro.cljs @@ -10,7 +10,7 @@ (def ^:private mergable-keys #{:data-store/tx :data-store/base-tx :chat-received-message/add-fx :shh/add-new-sym-keys :shh/get-new-sym-keys :shh/post - :confirm-message-processed :call-jail}) + :confirm-messages-processed :call-jail}) (defn safe-merge [fx new-fx] (if (:merging-fx-with-common-keys fx)