Fix reconciliation for new filter type :all (#14401)

This commit is contained in:
Icaro Motta 2022-11-18 06:15:39 -03:00 committed by GitHub
parent 73bbfa7d31
commit f4d9162ff8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 103 additions and 211 deletions

View File

@ -38,8 +38,7 @@
~excessively~ big, this implementation will probably need to be revisited." ~excessively~ big, this implementation will probably need to be revisited."
[db-notifications new-notifications] [db-notifications new-notifications]
(reduce (fn [acc {:keys [id type read] :as notification}] (reduce (fn [acc {:keys [id type read] :as notification}]
(let [filter-status (if read :read :unread) (let [remove-notification (fn [data]
remove-notification (fn [data]
(remove #(= id (:id %)) data)) (remove #(= id (:id %)) data))
insert-and-sort (fn [data] insert-and-sort (fn [data]
(->> notification (->> notification
@ -47,14 +46,17 @@
(sort-by (juxt :timestamp :id)) (sort-by (juxt :timestamp :id))
reverse))] reverse))]
(as-> acc $ (as-> acc $
(update-in $ [type :read :data] remove-notification) (update-in $ [type :all :data] remove-notification)
(update-in $ [types/no-type :all :data] remove-notification)
(update-in $ [type :unread :data] remove-notification) (update-in $ [type :unread :data] remove-notification)
(update-in $ [types/no-type :read :data] remove-notification)
(update-in $ [types/no-type :unread :data] remove-notification) (update-in $ [types/no-type :unread :data] remove-notification)
(if (or (:dismissed notification) (:accepted notification)) (if (or (:dismissed notification) (:accepted notification))
$ $
(-> $ (update-in [type filter-status :data] insert-and-sort) (cond-> (-> $
(update-in [types/no-type filter-status :data] insert-and-sort)))))) (update-in [type :all :data] insert-and-sort)
(update-in [types/no-type :all :data] insert-and-sort))
(not read) (update-in [type :unread :data] insert-and-sort)
(not read) (update-in [types/no-type :unread :data] insert-and-sort))))))
db-notifications db-notifications
new-notifications)) new-notifications))
@ -137,13 +139,11 @@
(and (some? cursor) (and (some? cursor)
(not= cursor start-or-end-cursor))) (not= cursor start-or-end-cursor)))
(def ^:const status-read 1)
(def ^:const status-unread 2) (def ^:const status-unread 2)
(def ^:const status-all 3) (def ^:const status-all 3)
(defn status [filter-status] (defn status [filter-status]
(case filter-status (case filter-status
:read status-read
:unread status-unread :unread status-unread
:all status-all :all status-all
99)) 99))

View File

@ -7,7 +7,7 @@
[status-im.ethereum.json-rpc :as json-rpc] [status-im.ethereum.json-rpc :as json-rpc]
status-im.events status-im.events
[status-im.test-helpers :as h] [status-im.test-helpers :as h]
[status-im.utils.config :as config])) [status-im2.setup.config :as config]))
(defn setup [] (defn setup []
(h/register-helper-events) (h/register-helper-events)
@ -85,10 +85,10 @@
(rf/dispatch event) (rf/dispatch event)
(is (= {types/no-type (is (= {types/no-type
{:read {:data [contact-verification-expected-notification]} {:all {:data [contact-verification-expected-notification]}
:unread {:data []}} :unread {:data []}}
types/contact-verification types/contact-verification
{:read {:data [contact-verification-expected-notification]} {:all {:data [contact-verification-expected-notification]}
:unread {:data []}}} :unread {:data []}}}
(get-in (h/db) [:activity-center :notifications]))) (get-in (h/db) [:activity-center :notifications])))
@ -158,12 +158,12 @@
(rf-test/run-test-sync (rf-test/run-test-sync
(setup) (setup)
(let [notifications {types/one-to-one-chat (let [notifications {types/one-to-one-chat
{:read {:cursor "" {:all {:cursor ""
:data [{:id "0x1" :data [{:id "0x1"
:read true :read true
:type types/one-to-one-chat} :type types/one-to-one-chat}
{:id "0x2" {:id "0x2"
:read true :read false
:type types/one-to-one-chat}]} :type types/one-to-one-chat}]}
:unread {:cursor "" :unread {:cursor ""
:data [{:id "0x3" :data [{:id "0x3"
@ -183,216 +183,112 @@
(testing "removes dismissed or accepted notifications" (testing "removes dismissed or accepted notifications"
(rf-test/run-test-sync (rf-test/run-test-sync
(setup) (setup)
(rf/dispatch [:test/assoc-in [:activity-center :notifications] (let [notif-1 {:id "0x1" :read true :type types/one-to-one-chat}
{types/one-to-one-chat notif-2 {:id "0x2" :read false :type types/one-to-one-chat}
{:read {:cursor "" notif-3 {:id "0x3" :read false :type types/one-to-one-chat}
:data [{:id "0x1" :read true :type types/one-to-one-chat} notif-4 {:id "0x4" :read false :type types/private-group-chat}
{:id "0x2" :read true :type types/one-to-one-chat}]} notif-5 {:id "0x5" :read true :type types/private-group-chat}
:unread {:cursor "" notif-6 {:id "0x6" :read false :type types/private-group-chat}]
:data [{:id "0x3" :read false :type types/one-to-one-chat}]}} (rf/dispatch [:test/assoc-in [:activity-center :notifications]
types/private-group-chat {types/one-to-one-chat
{:unread {:cursor "" {:all {:cursor "" :data [notif-1 notif-2]}
:data [{:id "0x4" :read false :type types/private-group-chat} :unread {:cursor "" :data [notif-3]}}
{:id "0x6" :read false :type types/private-group-chat}]}}}]) types/private-group-chat
{:unread {:cursor "" :data [notif-4 notif-6]}}}])
(rf/dispatch [:activity-center.notifications/reconcile (rf/dispatch [:activity-center.notifications/reconcile
[{:id "0x1" [(assoc notif-1 :dismissed true)
:read true (assoc notif-3 :accepted true)
:type types/one-to-one-chat (assoc notif-4 :dismissed true)
:dismissed true} notif-5]])
{:id "0x3"
:read false
:type types/one-to-one-chat
:accepted true}
{:id "0x4"
:read false
:type types/private-group-chat
:dismissed true}
{:id "0x5"
:read false
:type types/private-group-chat
:accepted true}]])
(is (= {types/no-type (is (= {types/no-type
{:read {:data []} {:all {:data [notif-5]}
:unread {:data []}} :unread {:data []}}
types/one-to-one-chat
types/one-to-one-chat {:all {:cursor "" :data [notif-2]}
{:read {:cursor "" :unread {:cursor "" :data []}}
:data [{:id "0x2" types/private-group-chat
:read true {:all {:data [notif-5]}
:type types/one-to-one-chat}]} :unread {:cursor "" :data [notif-6]}}}
:unread {:cursor "" (get-in (h/db) [:activity-center :notifications]))))))
:data []}}
types/private-group-chat
{:read {:data []}
:unread {:cursor ""
:data [{:id "0x6"
:read false
:type types/private-group-chat}]}}}
(get-in (h/db) [:activity-center :notifications])))))
(testing "replaces old notifications with newly arrived ones" (testing "replaces old notifications with newly arrived ones"
(rf-test/run-test-sync (rf-test/run-test-sync
(setup) (setup)
(rf/dispatch [:test/assoc-in [:activity-center :notifications] (let [notif-1 {:id "0x1" :read true :type types/one-to-one-chat}
{types/no-type notif-4 {:id "0x4" :read false :type types/private-group-chat}
{:read {:cursor "" notif-6 {:id "0x6" :read false :type types/private-group-chat}
:data [{:id "0x1" new-notif-1 (assoc notif-1 :last-message {})
:read true new-notif-4 (assoc notif-4 :author "0xabc")]
:type types/one-to-one-chat}]} (rf/dispatch [:test/assoc-in [:activity-center :notifications]
:unread {:cursor "" {types/no-type
:data [{:id "0x4" {:all {:cursor "" :data [notif-1]}
:read false :unread {:cursor "" :data [notif-4 notif-6]}}
:type types/private-group-chat} types/one-to-one-chat
{:id "0x6" {:all {:cursor "" :data [notif-1]}}
:read false types/private-group-chat
:type types/private-group-chat}]}} {:unread {:cursor "" :data [notif-4 notif-6]}}}])
types/one-to-one-chat
{:read {:cursor ""
:data [{:id "0x1"
:read true
:type types/one-to-one-chat}]}}
types/private-group-chat
{:unread {:cursor ""
:data [{:id "0x4"
:read false
:type types/private-group-chat}
{:id "0x6"
:read false
:type types/private-group-chat}]}}}])
(rf/dispatch [:activity-center.notifications/reconcile (rf/dispatch [:activity-center.notifications/reconcile [new-notif-1 new-notif-4 notif-6]])
[{:id "0x1"
:read true
:type types/one-to-one-chat
:last-message {}}
{:id "0x4"
:read false
:type types/private-group-chat
:author "0xabc"}
{:id "0x6"
:read false
:type types/private-group-chat}]])
(is (= {types/no-type (is (= {types/no-type
{:read {:cursor "" {:all {:cursor "" :data [notif-6 new-notif-4 new-notif-1]}
:data [{:id "0x1" :unread {:cursor "" :data [notif-6 new-notif-4]}}
:read true types/one-to-one-chat
:type types/one-to-one-chat {:all {:cursor "" :data [new-notif-1]}
:last-message {}}]} :unread {:data []}}
:unread {:cursor "" types/private-group-chat
:data [{:id "0x6" {:all {:data [notif-6 new-notif-4]}
:read false :unread {:cursor "" :data [notif-6 new-notif-4]}}}
:type types/private-group-chat} (get-in (h/db) [:activity-center :notifications]))))))
{:id "0x4"
:read false
:type types/private-group-chat
:author "0xabc"}]}}
types/one-to-one-chat
{:read {:cursor ""
:data [{:id "0x1"
:read true
:type types/one-to-one-chat
:last-message {}}]}
:unread {:data []}}
types/private-group-chat
{:read {:data []}
:unread {:cursor ""
:data [{:id "0x6"
:read false
:type types/private-group-chat}
{:id "0x4"
:read false
:type types/private-group-chat
:author "0xabc"}]}}}
(get-in (h/db) [:activity-center :notifications])))))
(testing "reconciles notifications that switched their read/unread status" (testing "reconciles notifications that switched their read/unread status"
(rf-test/run-test-sync (rf-test/run-test-sync
(setup) (setup)
(rf/dispatch [:test/assoc-in [:activity-center :notifications] (let [notif-1 {:id "0x1" :read true :type types/one-to-one-chat}
{types/one-to-one-chat new-notif-1 (assoc notif-1 :read false)]
{:read {:cursor "" (rf/dispatch [:test/assoc-in [:activity-center :notifications]
:data [{:id "0x1" {types/one-to-one-chat
:read true {:all {:cursor "" :data [notif-1]}}}])
:type types/one-to-one-chat}]}}}])
(rf/dispatch [:activity-center.notifications/reconcile (rf/dispatch [:activity-center.notifications/reconcile [new-notif-1]])
[{:id "0x1"
:read false
:type types/one-to-one-chat}]])
(is (= {types/no-type (is (= {types/no-type
{:read {:data []} {:all {:data [new-notif-1]}
:unread {:data [{:id "0x1" :unread {:data [new-notif-1]}}
:read false
:type types/one-to-one-chat}]}}
types/one-to-one-chat types/one-to-one-chat
{:read {:cursor "" {:all {:cursor "" :data [new-notif-1]}
:data []} :unread {:data [new-notif-1]}}}
:unread {:data [{:id "0x1" (get-in (h/db) [:activity-center :notifications]))))))
:read false
:type types/one-to-one-chat}]}}}
(get-in (h/db) [:activity-center :notifications])))))
;; Sorting by timestamp and ID is compatible with what the backend does when ;; Sorting by timestamp and ID is compatible with what the backend does when
;; returning paginated results. ;; returning paginated results.
(testing "sorts notifications by timestamp and id in descending order" (testing "sorts notifications by timestamp and id in descending order"
(rf-test/run-test-sync (rf-test/run-test-sync
(setup) (setup)
(rf/dispatch [:test/assoc-in [:activity-center :notifications] (let [notif-1 {:id "0x1" :read true :type types/one-to-one-chat :timestamp 1}
{types/one-to-one-chat notif-2 {:id "0x2" :read true :type types/one-to-one-chat :timestamp 1}
{:read {:cursor "" notif-3 {:id "0x3" :read false :type types/one-to-one-chat :timestamp 50}
:data [{:id "0x1" :read true :type types/one-to-one-chat :timestamp 1} notif-4 {:id "0x4" :read false :type types/one-to-one-chat :timestamp 100}
{:id "0x2" :read true :type types/one-to-one-chat :timestamp 1}]} notif-5 {:id "0x5" :read false :type types/one-to-one-chat :timestamp 100}
:unread {:cursor "" new-notif-1 (assoc notif-1 :last-message {})
:data [{:id "0x3" :read false :type types/one-to-one-chat :timestamp 50} new-notif-4 (assoc notif-4 :last-message {})]
{:id "0x4" :read false :type types/one-to-one-chat :timestamp 100} (rf/dispatch [:test/assoc-in [:activity-center :notifications]
{:id "0x5" :read false :type types/one-to-one-chat :timestamp 100}]}}}]) {types/one-to-one-chat
{:all {:cursor "" :data [notif-1 notif-2]}
:unread {:cursor "" :data [notif-3 notif-4 notif-5]}}}])
(rf/dispatch [:activity-center.notifications/reconcile (rf/dispatch [:activity-center.notifications/reconcile [new-notif-1 new-notif-4]])
[{:id "0x1" :read true :type types/one-to-one-chat :timestamp 1 :last-message {}}
{:id "0x4" :read false :type types/one-to-one-chat :timestamp 100 :last-message {}}]])
(is (= {types/no-type (is (= {types/no-type
{:read {:data [{:id "0x1" {:all {:data [new-notif-4 new-notif-1]}
:read true :unread {:data [new-notif-4]}}
:type types/one-to-one-chat types/one-to-one-chat
:timestamp 1 {:all {:cursor "" :data [new-notif-4 notif-2 new-notif-1]}
:last-message {}}]} :unread {:cursor "" :data [notif-5 new-notif-4 notif-3]}}}
:unread {:data [{:id "0x4" (get-in (h/db) [:activity-center :notifications]))))))))
:read false
:type types/one-to-one-chat
:timestamp 100
:last-message {}}]}}
types/one-to-one-chat
{:read {:cursor ""
:data [{:id "0x2"
:read true
:type types/one-to-one-chat
:timestamp 1}
{:id "0x1"
:read true
:type types/one-to-one-chat
:timestamp 1
:last-message {}}]}
:unread {:cursor ""
:data [{:id "0x5"
:read false
:type types/one-to-one-chat
:timestamp 100}
{:id "0x4"
:read false
:type types/one-to-one-chat
:timestamp 100
:last-message {}}
{:id "0x3"
:read false
:type types/one-to-one-chat
:timestamp 50}]}}}
(get-in (h/db) [:activity-center :notifications])))))))
;;;; Notifications fetching and pagination ;;;; Notifications fetching and pagination
@ -510,12 +406,12 @@
(let [spy-queue (atom [])] (let [spy-queue (atom [])]
(h/spy-fx spy-queue ::json-rpc/call) (h/spy-fx spy-queue ::json-rpc/call)
(rf/dispatch [:test/assoc-in [:activity-center :filter :status] (rf/dispatch [:test/assoc-in [:activity-center :filter :status]
:read]) :all])
(rf/dispatch [:test/assoc-in [:activity-center :filter :type] (rf/dispatch [:test/assoc-in [:activity-center :filter :type]
types/one-to-one-chat]) types/one-to-one-chat])
(rf/dispatch [:test/assoc-in [:activity-center :notifications types/one-to-one-chat :read :cursor] (rf/dispatch [:test/assoc-in [:activity-center :notifications types/one-to-one-chat :all :cursor]
"10"]) "10"])
(rf/dispatch [:test/assoc-in [:activity-center :notifications types/one-to-one-chat :read :loading?] (rf/dispatch [:test/assoc-in [:activity-center :notifications types/one-to-one-chat :all :loading?]
true]) true])
(rf/dispatch [:activity-center.notifications/fetch-next-page]) (rf/dispatch [:activity-center.notifications/fetch-next-page])

View File

@ -4,7 +4,7 @@
[status-im.constants :as constants] [status-im.constants :as constants]
[status-im.activity-center.notification-types :as notification-types] [status-im.activity-center.notification-types :as notification-types]
[status-im.data-store.messages :as messages] [status-im.data-store.messages :as messages]
[status-im.utils.config :as config])) [status-im2.setup.config :as config]))
(defn- rpc->type [{:keys [type name] :as chat}] (defn- rpc->type [{:keys [type name] :as chat}]
(case type (case type

View File

@ -3,7 +3,7 @@
[status-im.constants :as constants] [status-im.constants :as constants]
[status-im.activity-center.notification-types :as notification-types] [status-im.activity-center.notification-types :as notification-types]
[status-im.data-store.activities :as store] [status-im.data-store.activities :as store]
[status-im.utils.config :as config])) [status-im2.setup.config :as config]))
(def chat-id (def chat-id
"0x04c66155") "0x04c66155")

View File

@ -5,7 +5,7 @@
[status-im.chat.models.pin-message :as models.pin-message] [status-im.chat.models.pin-message :as models.pin-message]
[status-im.chat.models :as models.chat] [status-im.chat.models :as models.chat]
[status-im.chat.models.reactions :as models.reactions] [status-im.chat.models.reactions :as models.reactions]
[status-im.utils.config :as config] [status-im2.setup.config :as config]
[status-im.contact.core :as models.contact] [status-im.contact.core :as models.contact]
[status-im.communities.core :as models.communities] [status-im.communities.core :as models.communities]
[status-im.pairing.core :as models.pairing] [status-im.pairing.core :as models.pairing]

View File

@ -22,7 +22,7 @@
[status-im.ui.components.plus-button :as components.plus-button] [status-im.ui.components.plus-button :as components.plus-button]
[status-im.ui.screens.chat.sheets :as sheets] [status-im.ui.screens.chat.sheets :as sheets]
[status-im.ui.components.invite.views :as invite] [status-im.ui.components.invite.views :as invite]
[status-im.utils.config :as config] [status-im2.setup.config :as config]
[quo2.components.markdown.text :as quo2.text] [quo2.components.markdown.text :as quo2.text]
[status-im.qr-scanner.core :as qr-scanner] [status-im.qr-scanner.core :as qr-scanner]
[status-im.ui.components.chat-icon.screen :as chat-icon.screen] [status-im.ui.components.chat-icon.screen :as chat-icon.screen]

View File

@ -145,7 +145,3 @@
;;TODO for development only should be removed in status 2.0 ;;TODO for development only should be removed in status 2.0
(def new-ui-enabled? true) (def new-ui-enabled? true)
;; TODO: Remove this (highly) temporary flag once the new Activity Center is
;; usable enough to replace the old one **in the new UI**.
(def new-activity-center-enabled? false)