Fix unbounded number of calls to fetch notifications (#14250)

This commit is contained in:
Icaro Motta 2022-10-27 16:21:41 -03:00 committed by GitHub
parent 69303cd7d1
commit 91b237d3d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 512 additions and 527 deletions

View File

@ -5,25 +5,17 @@
[status-im.constants :as const] [status-im.constants :as const]
[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]))
(defn setup [] (defn setup []
(h/register-helper-events) (h/register-helper-events)
(rf/dispatch [:init/app-started])) (rf/dispatch [:init/app-started]))
(defn remove-color-key
"Remove `:color` key from notifications because they have random values that are
inconvenient to assert against."
[grouped-notifications {:keys [type status]}]
(update-in grouped-notifications
[type status :data]
(fn [old _]
(map #(dissoc % :color) old))
nil))
;;;; Contact verification ;;;; Contact verification
(deftest contact-verification-decline-test (deftest contact-verification-decline-test
(with-redefs [config/new-activity-center-enabled? true]
(testing "successfully declines and reconciles returned notification" (testing "successfully declines and reconciles returned notification"
(rf-test/run-test-sync (rf-test/run-test-sync
(setup) (setup)
@ -86,12 +78,7 @@
const/activity-center-notification-type-contact-verification const/activity-center-notification-type-contact-verification
{:read {:data [expected-notification]} {:read {:data [expected-notification]}
:unread {:data []}}} :unread {:data []}}}
(-> (h/db) (get-in (h/db) [:activity-center :notifications]))))))
(get-in [:activity-center :notifications])
(remove-color-key {:type const/activity-center-notification-type-no-type
:status :read})
(remove-color-key {:type const/activity-center-notification-type-contact-verification
:status :read})))))))
(testing "logs failure" (testing "logs failure"
(rf-test/run-test-sync (rf-test/run-test-sync
@ -107,11 +94,12 @@
{:contact-verification-id contact-verification-id {:contact-verification-id contact-verification-id
:error :fake-error}] :error :fake-error}]
:level :warn} :level :warn}
(last @logs))))))))) (last @logs))))))))))
;;;; Notification reconciliation ;;;; Notification reconciliation
(deftest notifications-reconcile-test (deftest notifications-reconcile-test
(with-redefs [config/new-activity-center-enabled? true]
(testing "does nothing when there are no new notifications" (testing "does nothing when there are no new notifications"
(rf-test/run-test-sync (rf-test/run-test-sync
(setup) (setup)
@ -350,11 +338,12 @@
:read false :read false
:type const/activity-center-notification-type-one-to-one-chat :type const/activity-center-notification-type-one-to-one-chat
:timestamp 50}]}}} :timestamp 50}]}}}
(get-in (h/db) [:activity-center :notifications])))))) (get-in (h/db) [:activity-center :notifications])))))))
;;;; Notifications fetching and pagination ;;;; Notifications fetching and pagination
(deftest notifications-fetch-test (deftest notifications-fetch-test
(with-redefs [config/new-activity-center-enabled? true]
(testing "fetches first page" (testing "fetches first page"
(rf-test/run-test-sync (rf-test/run-test-sync
(setup) (setup)
@ -387,9 +376,7 @@
:read false :read false
:reply-message nil :reply-message nil
:type const/activity-center-notification-type-one-to-one-chat}]}}} :type const/activity-center-notification-type-one-to-one-chat}]}}}
(remove-color-key (get-in (h/db) [:activity-center :notifications]) (get-in (h/db) [:activity-center :notifications]))))))
{:status :unread
:type const/activity-center-notification-type-one-to-one-chat}))))))
(testing "does not fetch next page when pagination cursor reached the end" (testing "does not fetch next page when pagination cursor reached the end"
(rf-test/run-test-sync (rf-test/run-test-sync
@ -461,9 +448,7 @@
:read false :read false
:reply-message nil :reply-message nil
:type const/activity-center-notification-type-mention}]}}} :type const/activity-center-notification-type-mention}]}}}
(remove-color-key (get-in (h/db) [:activity-center :notifications]) (get-in (h/db) [:activity-center :notifications]))))))
{:status :unread
:type const/activity-center-notification-type-mention}))))))
(testing "does not fetch next page while it is still loading" (testing "does not fetch next page while it is still loading"
(rf-test/run-test-sync (rf-test/run-test-sync
@ -504,4 +489,4 @@
const/activity-center-notification-type-one-to-one-chat const/activity-center-notification-type-one-to-one-chat
:unread :unread
:fake-error] :fake-error]
(:args (last @spy-queue)))))))) (:args (last @spy-queue)))))))))

View File

@ -2,7 +2,8 @@
(:require [clojure.set :as set] (:require [clojure.set :as set]
[quo.design-system.colors :as colors] [quo.design-system.colors :as colors]
[status-im.constants :as constants] [status-im.constants :as constants]
[status-im.data-store.messages :as messages])) [status-im.data-store.messages :as messages]
[status-im.utils.config :as config]))
(defn- rpc->type [{:keys [type name] :as chat}] (defn- rpc->type [{:keys [type name] :as chat}]
(case type (case type
@ -33,14 +34,15 @@
chat)) chat))
(defn <-rpc [item] (defn <-rpc [item]
(-> item (cond-> (-> item
rpc->type rpc->type
(set/rename-keys {:lastMessage :last-message (set/rename-keys {:lastMessage :last-message
:replyMessage :reply-message :replyMessage :reply-message
:chatId :chat-id :chatId :chat-id
:contactVerificationStatus :contact-verification-status}) :contactVerificationStatus :contact-verification-status})
(assoc :color (rand-nth colors/chat-colors))
(update :last-message #(when % (messages/<-rpc %))) (update :last-message #(when % (messages/<-rpc %)))
(update :message #(when % (messages/<-rpc %))) (update :message #(when % (messages/<-rpc %)))
(update :reply-message #(when % (messages/<-rpc %))) (update :reply-message #(when % (messages/<-rpc %)))
(dissoc :chatId))) (dissoc :chatId))
(not config/new-activity-center-enabled?)
(assoc :color (rand-nth colors/chat-colors))))

View File

@ -1,8 +1,8 @@
(ns status-im.data-store.activities-test (ns status-im.data-store.activities-test
(:require [cljs.test :refer [deftest is testing]] (:require [cljs.test :refer [deftest is testing]]
[quo.design-system.colors :as colors]
[status-im.constants :as constants] [status-im.constants :as constants]
[status-im.data-store.activities :as store])) [status-im.data-store.activities :as store]
[status-im.utils.config :as config]))
(def chat-id (def chat-id
"0x04c66155") "0x04c66155")
@ -18,16 +18,14 @@
:replyMessage {}}) :replyMessage {}})
(deftest <-rpc-test (deftest <-rpc-test
(testing "assocs random chat color" (with-redefs [config/new-activity-center-enabled? true]
(is (contains? (set colors/chat-colors) (:color (store/<-rpc raw-notification)))))
(testing "renames keys" (testing "renames keys"
(is (= {:name chat-name (is (= {:name chat-name
:chat-id chat-id :chat-id chat-id
:contact-verification-status constants/contact-verification-state-pending} :contact-verification-status constants/contact-verification-state-pending}
(-> raw-notification (-> raw-notification
store/<-rpc store/<-rpc
(dissoc :color :last-message :message :reply-message))))) (dissoc :last-message :message :reply-message)))))
(testing "transforms messages from RPC response" (testing "transforms messages from RPC response"
(is (= {:last-message {:quoted-message nil (is (= {:last-message {:quoted-message nil
@ -98,4 +96,4 @@
(-> raw-notification (-> raw-notification
(assoc :type constants/activity-center-notification-type-one-to-one-chat) (assoc :type constants/activity-center-notification-type-one-to-one-chat)
store/<-rpc store/<-rpc
(select-keys [:name :chat-type :chat-name :public? :group-chat])))))) (select-keys [:name :chat-type :chat-name :public? :group-chat])))))))