From a56968716152c5d4434bcd05dc88c3faf8ddb641 Mon Sep 17 00:00:00 2001 From: Andrea Maria Piana Date: Mon, 23 Aug 2021 13:02:00 +0200 Subject: [PATCH] [Fixes: #12448] Fix unread indicator in chat on message delete --- src/status_im/chat/models.cljs | 10 ++++++ src/status_im/chat/models/message.cljs | 35 +++++++++------------ src/status_im/chat/models/message_seen.cljs | 33 ------------------- status-go-version.json | 6 ++-- 4 files changed, 27 insertions(+), 57 deletions(-) delete mode 100644 src/status_im/chat/models/message_seen.cljs diff --git a/src/status_im/chat/models.cljs b/src/status_im/chat/models.cljs index 0bfb0ca083..ed4c4ec7fa 100644 --- a/src/status_im/chat/models.cljs +++ b/src/status_im/chat/models.cljs @@ -431,3 +431,13 @@ :on-accept #(do (re-frame/dispatch [:bottom-sheet/hide]) (re-frame/dispatch [:chat.ui/remove-chat chat-id]))}}) + +(fx/defn decrease-unviewed-count + {:events [:chat/decrease-unviewed-count]} + [{:keys [db]} chat-id unseen-messages] + {:db (update-in db [:chats chat-id :unviewed-messages-count] + (fn [count] + ;; There might be some other requests being fired, + ;; so we need to make sure the count has not been set to + ;; 0 in the meantime + (max (- count unseen-messages) 0)))}) diff --git a/src/status_im/chat/models/message.cljs b/src/status_im/chat/models/message.cljs index 4ad210dadf..cf1d0ae8c3 100644 --- a/src/status_im/chat/models/message.cljs +++ b/src/status_im/chat/models/message.cljs @@ -1,5 +1,6 @@ (ns status-im.chat.models.message (:require [status-im.chat.models :as chat-model] + [re-frame.core :as re-frame] [status-im.chat.models.message-list :as message-list] [status-im.constants :as constants] [status-im.data-store.messages :as data-store.messages] @@ -195,29 +196,21 @@ [cofx messages] (protocol/send-chat-messages cofx messages)) -(defn flip-args [f] - (fn [x y] (f y x))) - -(defn message-ids->message-id-chat-id-map - "Determine the chat ids of a seq of message-ids" - [db message-ids] - (->> db - :messages ; get messages map - seq ; convert it to seq - (map second) ; get values of messages map -> message-id : message-obj - (into {}) ; convert message-id : message-obj seq to map - ((flip-args select-keys) message-ids) ; select the message objects of the ids in question - vals ; keep only the values of required messages - (map #(select-keys % [:chat-id :message-id])))) ; return the chat-id and message-id of required messages - (fx/defn handle-removed-messages {:events [::handle-removed-messages]} - [{:keys [db]} removed-messages] - (let [mcids (message-ids->message-id-chat-id-map db removed-messages)] - {:db (reduce (fn [acc current] - (update-in acc [:messages (:chat-id current)] dissoc (:message-id current))) - db mcids) - :dispatch [:get-activity-center-notifications]})) + [{:keys [db] :as cofx} removed-messages] + (let [mark-as-seen-fx (mapv (fn [removed-message] + (let [chat-id (:chatId removed-message) + message-id (:messageId removed-message)] + (data-store.messages/mark-messages-seen chat-id + [message-id] + #(re-frame/dispatch [:chat/decrease-unviewed-count chat-id %3])))) removed-messages) + remove-messages-fx (fn [{:keys [db]}] + {:db (reduce (fn [acc current] + (update-in acc [:messages (:chatId current)] dissoc (:messageId current))) + db removed-messages) + :dispatch [:get-activity-center-notifications]})] + (apply fx/merge cofx (conj mark-as-seen-fx remove-messages-fx)))) (comment (handle-removed-messages diff --git a/src/status_im/chat/models/message_seen.cljs b/src/status_im/chat/models/message_seen.cljs deleted file mode 100644 index 2eb1598619..0000000000 --- a/src/status_im/chat/models/message_seen.cljs +++ /dev/null @@ -1,33 +0,0 @@ -(ns status-im.chat.models.message-seen - (:require [status-im.utils.fx :as fx] - [status-im.data-store.messages :as messages-store])) - -(defn subtract-seen-messages - [old-count new-seen-messages-ids] - (max 0 (- old-count (count new-seen-messages-ids)))) - -(fx/defn update-chats-unviewed-messages-count - [{:keys [db]} {:keys [chat-id _]}] - (let [{:keys [loaded-unviewed-messages-ids unviewed-messages-count]} - (get-in db [:chats chat-id])] - ;; We currently only use this for private group chats and one-to-ones - ;; but this method would have to be changed if we showed unviewed-mentions-count - ;; in one to ones and private group chats as well - {:db (update-in db [:chats chat-id] assoc - :unviewed-messages-count (subtract-seen-messages - unviewed-messages-count - loaded-unviewed-messages-ids))})) - -(fx/defn mark-messages-seen - "Marks all unviewed loaded messages as seen in particular chat" - [{:keys [db] :as cofx} chat-id] - (let [loaded-unviewed-ids (get-in db [:chats chat-id :loaded-unviewed-messages-ids])] - (when (seq loaded-unviewed-ids) - (fx/merge cofx - {:db (reduce (fn [acc message-id] - (assoc-in acc [:messages chat-id message-id :seen] - true)) - db - loaded-unviewed-ids)} - (messages-store/mark-messages-seen chat-id loaded-unviewed-ids nil) - (update-chats-unviewed-messages-count {:chat-id chat-id}))))) diff --git a/status-go-version.json b/status-go-version.json index 333e85407f..38ea548f49 100644 --- a/status-go-version.json +++ b/status-go-version.json @@ -2,7 +2,7 @@ "_comment": "DO NOT EDIT THIS FILE BY HAND. USE 'scripts/update-status-go.sh ' instead", "owner": "status-im", "repo": "status-go", - "version": "v0.83.18", - "commit-sha1": "0e54d04e67fee94276b69a9b20338cf4e7624cc5", - "src-sha256": "091g3xqsswvfi58jnr9xcwx8xhzcch7d5yky00gjxmw70hyxwigw" + "version": "v0.84.1", + "commit-sha1": "83394e0ed17265cb20ab3e56fe3de13ca4057267", + "src-sha256": "0xhg4n5i8ifv2z3vydi0878n6mv53cajmsr54fpaxqr4lvhrkzza" }