From 947b1c2b2e88a3a4cf6da130d22cbd489ee9af54 Mon Sep 17 00:00:00 2001 From: Andrea Maria Piana Date: Mon, 16 Mar 2020 09:46:58 +0100 Subject: [PATCH] Correctly load messages when on tab While on tabs we want a slightly different behavior: Unread counter should increase, message should be loaded in the chat On moving to the chat tab from one of these tabs, it should mark the messages as seen. Signed-off-by: Andrea Maria Piana --- src/status_im/chat/models/loading.cljs | 27 +++++++++++++------ src/status_im/chat/models/message.cljs | 18 +++++++------ .../status_im/test/chat/models/message.cljs | 4 +++ 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/status_im/chat/models/loading.cljs b/src/status_im/chat/models/loading.cljs index 82f01bc9b7..e8aad6e0e7 100644 --- a/src/status_im/chat/models/loading.cljs +++ b/src/status_im/chat/models/loading.cljs @@ -41,13 +41,16 @@ (fx/defn offload-all-messages [{:keys [db] :as cofx}] (when-let [current-chat-id (:current-chat-id db)] - {:db (update-in db [:chats current-chat-id] + {:db + (-> db + (dissoc :loaded-chat-id) + (update-in [:chats current-chat-id] assoc :all-loaded? false :cursor nil :messages-initialized? false :messages {} - :message-list nil)})) + :message-list nil))})) (fx/defn handle-chat-visibility-changed {:events [:chat.ui/message-visibility-changed]} @@ -155,10 +158,18 @@ (fx/defn load-messages [{:keys [db now] :as cofx}] (when-let [current-chat-id (:current-chat-id db)] - (when-not (get-in db [:chats current-chat-id :messages-initialized?]) - ; reset chat viewable-items state - (chat.state/reset) - (fx/merge cofx - {:db (assoc-in db [:chats current-chat-id :messages-initialized?] now)} - (load-more-messages))))) + (if-not (get-in db [:chats current-chat-id :messages-initialized?]) + (do + ; reset chat viewable-items state + (chat.state/reset) + (fx/merge cofx + {:db (-> db + ;; We keep track of whether there's a loaded chat + ;; which will be reset only if we hit home + (assoc :loaded-chat-id current-chat-id) + (assoc-in [:chats current-chat-id :messages-initialized?] now))} + (chat-model/mark-messages-seen current-chat-id) + (load-more-messages))) + ;; We mark messages as seen in case we received them while on a different tab + (chat-model/mark-messages-seen cofx current-chat-id)))) diff --git a/src/status_im/chat/models/message.cljs b/src/status_im/chat/models/message.cljs index ced7a237a4..c15bd2ff3a 100644 --- a/src/status_im/chat/models/message.cljs +++ b/src/status_im/chat/models/message.cljs @@ -80,11 +80,11 @@ (fx/defn add-message [{:keys [db] :as cofx} {{:keys [chat-id message-id replace timestamp from] :as message} :message - :keys [current-chat?]}] + :keys [seen-by-user?]}] (let [current-public-key (multiaccounts.model/current-public-key cofx) message-to-be-removed (when replace (get-in db [:chats chat-id :messages replace])) - prepared-message (prepare-message message chat-id current-chat?)] + prepared-message (prepare-message message chat-id seen-by-user?)] (fx/merge cofx (when message-to-be-removed (hide-message chat-id message-to-be-removed)) @@ -97,7 +97,7 @@ (update-in [:chats chat-id :messages] assoc message-id prepared-message) (update-in [:chats chat-id :message-list] message-list/add prepared-message)) - (and (not current-chat?) + (and (not seen-by-user?) (not= from current-public-key)) (update-in [:chats chat-id :loaded-unviewed-messages-ids] (fnil conj #{}) message-id))})))) @@ -109,10 +109,11 @@ chat-id clock-value content] :as message}] - (let [{:keys [current-chat-id view-id]} db + (let [{:keys [loaded-chat-id + view-id + current-chat-id]} db cursor-clock-value (get-in db [:chats current-chat-id :cursor-clock-value]) - current-chat? (and (= :chat view-id) - (= current-chat-id chat-id))] + current-chat? (= chat-id loaded-chat-id)] (when (and current-chat? (or (not cursor-clock-value) (<= cursor-clock-value clock-value))) @@ -122,7 +123,8 @@ (<= (:clock-value @view.state/viewable-item) clock-value)) (add-message cofx {:message message - :current-chat? current-chat?}) + :seen-by-user? (and current-chat? + (= view-id :chat))}) ;; Not in the current view, offload to db and update cursor if necessary (when (and (< clock-value cursor-clock-value) @@ -226,7 +228,7 @@ {:events [:messages/system-messages-saved]} [cofx messages] (apply fx/merge cofx (map #(add-message {:message % - :current-chat? true}) + :seen-by-user? true}) messages))) (fx/defn send-message diff --git a/test/cljs/status_im/test/chat/models/message.cljs b/test/cljs/status_im/test/chat/models/message.cljs index dc0ae7388e..106860ed31 100644 --- a/test/cljs/status_im/test/chat/models/message.cljs +++ b/test/cljs/status_im/test/chat/models/message.cljs @@ -45,6 +45,7 @@ (deftest add-own-received-message (let [db {:multiaccount {:public-key "me"} :view-id :chat + :loaded-chat-id "chat-id" :current-chat-id "chat-id" :chats {"chat-id" {:messages {}}}}] (testing "a message coming from you!" @@ -66,6 +67,7 @@ (let [cofx {:db {:chats {"chat-id" {:contacts #{"present"} :members-joined #{"a"}}} :multiaccount {:public-key "a"} + :loaded-chat-id "chat-id" :current-chat-id "chat-id" :view-id :chat}} cofx-without-member (update-in cofx [:db :chats "chat-id" :members-joined] disj "a") @@ -102,6 +104,7 @@ (deftest receive-public-chats (let [cofx {:db {:chats {"chat-id" {:public? true}} :multiaccount {:public-key "a"} + :loaded-chat-id "chat-id" :current-chat-id "chat-id" :view-id :chat}} valid-message {:chat-id "chat-id" @@ -130,6 +133,7 @@ (let [cofx {:db {:chats {"matching" {}} :multiaccount {:public-key "me"} :current-chat-id "matching" + :loaded-chat-id "matching" :view-id :chat}} valid-message {:chat-id "matching" :from "matching"