From ef13b4d210cb5837ac81d860dcf00d3d5aaa3ec3 Mon Sep 17 00:00:00 2001 From: Andrea Maria Piana Date: Wed, 26 May 2021 10:28:22 +0200 Subject: [PATCH] [Fixes: #12061] Add unread mentions count Fixes: #2061 This commit adds an unread mentions count in 3 places: 1) Public chats 2) Communities in home 3) Communities chats The logic is that if you have unread mentions, it will show you the count of messages with unread mentions, while if you have only unread messages, you will see a blue dot. Signed-off-by: Andrea Maria Piana --- src/status_im/chat/models.cljs | 1 + src/status_im/chat/models/loading.cljs | 4 +- src/status_im/chat/models/message_seen.cljs | 5 ++- src/status_im/contact/block.cljs | 2 + src/status_im/data_store/chats.cljs | 40 +----------------- src/status_im/data_store/chats_test.cljs | 41 ------------------- src/status_im/subs.cljs | 12 ++++++ src/status_im/transport/message/core.cljs | 24 +++++++---- .../ui/screens/communities/community.cljs | 4 +- .../ui/screens/communities/views.cljs | 23 +++++++---- .../ui/screens/home/views/inner_item.cljs | 12 +++++- status-go-version.json | 6 +-- 12 files changed, 70 insertions(+), 104 deletions(-) diff --git a/src/status_im/chat/models.cljs b/src/status_im/chat/models.cljs index 1b1c3f2f6d..a2607326dd 100644 --- a/src/status_im/chat/models.cljs +++ b/src/status_im/chat/models.cljs @@ -143,6 +143,7 @@ (update-in [:chats chat-id] merge {:last-message nil :unviewed-messages-count 0 + :unviewed-mentions-count 0 :deleted-at-clock-value last-message-clock-value}))})) (fx/defn clear-history-handler diff --git a/src/status_im/chat/models/loading.cljs b/src/status_im/chat/models/loading.cljs index 6d8a8bc3f8..d6a10e5122 100644 --- a/src/status_im/chat/models/loading.cljs +++ b/src/status_im/chat/models/loading.cljs @@ -47,7 +47,9 @@ (fx/defn handle-mark-all-read-successful {:events [::mark-all-read-successful]} [{:keys [db]} chat-id] - {:db (assoc-in db [:chats chat-id :unviewed-messages-count] 0)}) + {:db (update-in db [:chats chat-id] assoc + :unviewed-messages-count 0 + :unviewed-mentions-count 0)}) (fx/defn handle-mark-all-read {:events [:chat.ui/mark-all-read-pressed :chat/mark-all-as-read]} diff --git a/src/status_im/chat/models/message_seen.cljs b/src/status_im/chat/models/message_seen.cljs index 3f06294f98..2eb1598619 100644 --- a/src/status_im/chat/models/message_seen.cljs +++ b/src/status_im/chat/models/message_seen.cljs @@ -10,6 +10,9 @@ [{: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 @@ -27,4 +30,4 @@ db loaded-unviewed-ids)} (messages-store/mark-messages-seen chat-id loaded-unviewed-ids nil) - (update-chats-unviewed-messages-count {:chat-id chat-id}))))) \ No newline at end of file + (update-chats-unviewed-messages-count {:chat-id chat-id}))))) diff --git a/src/status_im/contact/block.cljs b/src/status_im/contact/block.cljs index 075e427d03..895e12f394 100644 --- a/src/status_im/contact/block.cljs +++ b/src/status_im/contact/block.cljs @@ -12,6 +12,7 @@ public-key {:keys [chat-id unviewed-messages-count + unviewed-mentions-count last-message]}] (let [removed-messages-ids (keep (fn [[message-id {:keys [from]}]] @@ -25,6 +26,7 @@ (update-in [:chats chat-id] assoc :unviewed-messages-count unviewed-messages-count + :unviewed-mentions-count unviewed-mentions-count :last-message last-message))] {:db (assoc-in db [:message-lists chat-id] (message-list/add-many nil (vals (get-in db [:messages chat-id]))))})) diff --git a/src/status_im/data_store/chats.cljs b/src/status_im/data_store/chats.cljs index af84d2d296..2b772cb6be 100644 --- a/src/status_im/data_store/chats.cljs +++ b/src/status_im/data_store/chats.cljs @@ -6,16 +6,6 @@ [status-im.utils.fx :as fx] [taoensso.timbre :as log])) -(defn type->rpc [{:keys [chat-type public? group-chat profile-public-key timeline?] :as chat}] - (if chat-type - (assoc chat :chatType chat-type) - (assoc chat :chatType (cond - profile-public-key constants/profile-chat-type - timeline? constants/timeline-chat-type - public? constants/public-chat-type - group-chat constants/private-group-chat-type - :else constants/one-to-one-chat-type)))) - (defn rpc->type [{:keys [chatType name] :as chat}] (cond (or (= constants/public-chat-type chatType) @@ -34,15 +24,6 @@ :group-chat true) :else (assoc chat :public? false :group-chat false))) -(defn- marshal-members [{:keys [admins contacts members-joined chat-type] :as chat}] - (cond-> chat - (= chat-type constants/private-group-chat-type) - (assoc :members (map #(hash-map :id % - :admin (boolean (admins %)) - :joined (boolean (members-joined %))) contacts)) - :always - (dissoc :admins :contacts :members-joined))) - (defn- unmarshal-members [{:keys [members chatType] :as chat}] (cond (= constants/public-chat-type chatType) (assoc chat @@ -68,26 +49,6 @@ :admins #{} :members-joined #{}))) -(defn- ->rpc [chat] - (-> chat - marshal-members - (update :last-message messages/->rpc) - type->rpc - (clojure.set/rename-keys {:chat-id :id - :membership-update-events :membershipUpdateEvents - :synced-from :syncedFrom - :synced-to :syncedTo - :unviewed-messages-count :unviewedMessagesCount - :last-message :lastMessage - :community-id :communityId - :deleted-at-clock-value :deletedAtClockValue - :is-active :active - :last-clock-value :lastClockValue - :profile-public-key :profile}) - (dissoc :public? :group-chat :messages - :chat-type - :contacts :admins :members-joined))) - (defn <-rpc [chat] (-> chat rpc->type @@ -100,6 +61,7 @@ :deletedAtClockValue :deleted-at-clock-value :chatType :chat-type :unviewedMessagesCount :unviewed-messages-count + :unviewedMentionsCount :unviewed-mentions-count :lastMessage :last-message :active :is-active :lastClockValue :last-clock-value diff --git a/src/status_im/data_store/chats_test.cljs b/src/status_im/data_store/chats_test.cljs index 7964fb3f1b..8e251d4ade 100644 --- a/src/status_im/data_store/chats_test.cljs +++ b/src/status_im/data_store/chats_test.cljs @@ -2,47 +2,6 @@ (:require [cljs.test :refer-macros [deftest is testing]] [status-im.data-store.chats :as chats])) -(deftest ->to-rpc - (let [chat {:public? false - :group-chat true - :color "color" - :contacts #{"a" "b" "c" "d"} - :last-clock-value 10 - :chat-type 3 - :admins #{"a" "b"} - :members-joined #{"a" "c"} - :name "name" - :membership-update-events :events - :unviewed-messages-count 2 - :is-active true - :chat-id "chat-id" - :timestamp 2} - expected-chat {:id "chat-id" - :color "color" - :name "name" - :chatType 3 - :lastMessage nil - :members #{{:id "a" - :admin true - :joined true} - {:id "b" - :admin true - :joined false} - {:id "c" - :admin false - :joined true} - {:id "d" - :admin false - :joined false}} - :lastClockValue 10 - :membershipUpdateEvents :events - :unviewedMessagesCount 2 - :active true - :timestamp 2}] - (testing "marshaling chat" - (is (= expected-chat (-> (#'status-im.data-store.chats/->rpc chat) - (update :members #(into #{} %)))))))) - (deftest normalize-chat-test (let [chat {:id "chat-id" :color "color" diff --git a/src/status_im/subs.cljs b/src/status_im/subs.cljs index d7b18ee91d..f645c60164 100644 --- a/src/status_im/subs.cljs +++ b/src/status_im/subs.cljs @@ -285,6 +285,18 @@ 0 chats))) +(re-frame/reg-sub + :communities/unviewed-counts + (fn [[_ community-id]] + [(re-frame/subscribe [:chats/by-community-id community-id])]) + (fn [[chats]] + (reduce (fn [acc {:keys [unviewed-mentions-count unviewed-messages-count]}] + {:unviewed-messages-count (+ (:unviewed-messages-count acc) (or unviewed-messages-count 0)) + :unviewed-mentions-count (+ (:unviewed-mentions-count acc) (or unviewed-mentions-count 0))}) + {:unviewed-messages-count 0 + :unviewed-mentions-count 0} + chats))) + (re-frame/reg-sub :communities/requests-to-join-for-community :<- [:communities/requests-to-join] diff --git a/src/status_im/transport/message/core.cljs b/src/status_im/transport/message/core.cljs index 99e5144cda..3985b6cf7a 100644 --- a/src/status_im/transport/message/core.cljs +++ b/src/status_im/transport/message/core.cljs @@ -49,8 +49,10 @@ (process-next response-js sync-handler) (models.chat/ensure-chats (map #(-> % (data-store.chats/<-rpc) - ;;TODO why here? - (dissoc :unviewed-messages-count)) + ;; We dissoc this fields as they are handled by status-react and + ;; not status-go, as there might be requests in-flight that change + ;; this value + (dissoc :unviewed-messages-count :unviewed-mentions-count)) (types/js->clj chats))))) (seq messages) @@ -118,9 +120,15 @@ (let [chat-id (.-localChatId message-js) message-type (.-messageType message-js) from (.-from message-js) + mentioned (.-mentioned message-js) + profile (models.chat/profile-chat? {:db db} chat-id) new (.-new message-js) current (= current-chat-id chat-id) - profile (models.chat/profile-chat? {:db db} chat-id) + should-update-unviewed? (and (not current) + new + (not profile) + (not (= message-type constants/message-type-private-group-system-message)) + (not (= from (multiaccounts.model/current-public-key {:db db})))) tx-hash (and (.-commandParameters message-js) (.-commandParameters.transactionHash message-js))] (cond-> acc current @@ -130,13 +138,13 @@ (update :statuses conj message-js) ;;update counter - (and (not current) - new - (not profile) - (not (= message-type constants/message-type-private-group-system-message)) - (not (= from (multiaccounts.model/current-public-key {:db db})))) + should-update-unviewed? (update-in [:db :chats chat-id :unviewed-messages-count] inc) + (and should-update-unviewed? + mentioned) + (update-in [:db :chats chat-id :unviewed-mentions-count] inc) + ;;conj incoming transaction for :watch-tx (not (string/blank? tx-hash)) (update :transactions conj tx-hash) diff --git a/src/status_im/ui/screens/communities/community.cljs b/src/status_im/ui/screens/communities/community.cljs index 17e5e760fc..328dd72975 100644 --- a/src/status_im/ui/screens/communities/community.cljs +++ b/src/status_im/ui/screens/communities/community.cljs @@ -124,7 +124,9 @@ (defn community-chat-item [{:keys [chat-id] :as home-item}] [inner-item/home-list-item - home-item + ;; We want communities to behave as public chats when it comes to + ;; unread indicator + (assoc home-item :public? true) {:on-press (fn [] (re-frame/dispatch [:dismiss-keyboard]) (re-frame/dispatch [:chat.ui/navigate-to-chat chat-id]) diff --git a/src/status_im/ui/screens/communities/views.cljs b/src/status_im/ui/screens/communities/views.cljs index 452000ab38..a8b1d1eaaf 100644 --- a/src/status_im/ui/screens/communities/views.cljs +++ b/src/status_im/ui/screens/communities/views.cljs @@ -11,6 +11,7 @@ [status-im.ui.components.topbar :as topbar] [status-im.ui.components.colors :as colors] [status-im.ui.components.toolbar :as toolbar] + [status-im.ui.components.badge :as badge] [status-im.ui.components.react :as react] [status-im.ui.screens.communities.icon :as communities.icon] [quo.design-system.colors :as quo.colors])) @@ -20,14 +21,19 @@ (>evt event)) (defn community-unviewed-count [id] - (when-not (zero? (' instead", "owner": "status-im", "repo": "status-go", - "version": "v0.79.5", - "commit-sha1": "dfd46680879954fed29f18863354139d3d2ff34b", - "src-sha256": "0v5v32zvazcr1fnv8j08pknwz7567i6brhyr51x42al1an9jdkqj" + "version": "v0.79.6", + "commit-sha1": "c74c9eedfd16ba06d6076b79f87e4401b19ab621", + "src-sha256": "1g0z0inr8qp2964xkas4365d8v9shh9c8mwrbhhrkdd1yl6ggy4b" }