fix hidden community appears on "Opened" Tab (#21238)

This commit is contained in:
Parvesh Monu 2024-09-18 20:17:44 +05:30 committed by GitHub
parent 69664322b8
commit 2f3d3fc7f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 78 additions and 56 deletions

View File

@ -2,6 +2,7 @@
(:require (:require
[clojure.string :as string] [clojure.string :as string]
[legacy.status-im.data-store.communities :as data-store.communities] [legacy.status-im.data-store.communities :as data-store.communities]
[oops.core :as oops]
[schema.core :as schema] [schema.core :as schema]
[status-im.constants :as constants] [status-im.constants :as constants]
[status-im.contexts.chat.messenger.messages.link-preview.events :as link-preview.events] [status-im.contexts.chat.messenger.messages.link-preview.events :as link-preview.events]
@ -25,7 +26,7 @@
token-permissions-check id last-opened-at] token-permissions-check id last-opened-at]
:as community} (data-store.communities/<-rpc community-js) :as community} (data-store.communities/<-rpc community-js)
previous-last-opened-at (get-in db [:communities id :last-opened-at])] previous-last-opened-at (get-in db [:communities id :last-opened-at])]
(when (>= clock (get-in db [:communities id :clock])) (when (and id (>= clock (get-in db [:communities id :clock])))
{:db (assoc-in db {:db (assoc-in db
[:communities id] [:communities id]
(assoc community :last-opened-at (max last-opened-at previous-last-opened-at))) (assoc community :last-opened-at (max last-opened-at previous-last-opened-at)))
@ -223,14 +224,14 @@
:fx [[:dispatch [:communities/check-permissions-to-join-community community-id]]]}))) :fx [[:dispatch [:communities/check-permissions-to-join-community community-id]]]})))
(defn community-fetched (defn community-fetched
[{:keys [db]} [community-id community]] [{:keys [db]} [community-id community-js]]
(when community (when community-js
{:db (update db :communities/fetching-communities dissoc community-id) {:db (update db :communities/fetching-communities dissoc community-id)
:fx [[:dispatch [:communities/handle-community community]] :fx [[:dispatch [:communities/handle-community community-js]]
[:dispatch [:chat.ui/spectate-community community-id]] [:dispatch [:chat.ui/spectate-community community-id]]
[:dispatch [:dispatch
[:chat.ui/cache-link-preview-data (link-preview.events/community-link community-id) [:chat.ui/cache-link-preview-data (link-preview.events/community-link community-id)
community]]]})) (data-store.communities/<-rpc community-js)]]]}))
(rf/reg-event-fx :chat.ui/community-fetched community-fetched) (rf/reg-event-fx :chat.ui/community-fetched community-fetched)
@ -257,6 +258,7 @@
:params [{:CommunityKey community-id :params [{:CommunityKey community-id
:TryDatabase true :TryDatabase true
:WaitForResponse true}] :WaitForResponse true}]
:js-response true
:on-success (fn [community] :on-success (fn [community]
(if community (if community
(rf/dispatch [:chat.ui/community-fetched community-id (rf/dispatch [:chat.ui/community-fetched community-id
@ -283,11 +285,15 @@
(rf/reg-event-fx :communities/fetch-community fetch-community) (rf/reg-event-fx :communities/fetch-community fetch-community)
(defn spectate-community-success (defn spectate-community-success
[{:keys [db]} [{:keys [communities]}]] [{:keys [db]} [response-js]]
(when-let [community (first communities)] (when response-js
(let [communities (oops/oget response-js "communities")
community (first communities)
community-id (when community (oops/oget community "id"))]
(when community
{:db (-> db {:db (-> db
(assoc-in [:communities (:id community) :spectated] true)) (assoc-in [:communities community-id :spectated] true))
:fx [[:dispatch [:communities/handle-community community]]]})) :fx [[:dispatch [:communities/handle-community community]]]}))))
(rf/reg-event-fx :chat.ui/spectate-community-success spectate-community-success) (rf/reg-event-fx :chat.ui/spectate-community-success spectate-community-success)
@ -297,6 +303,7 @@
(when (and (not joined) (not spectated)) (when (and (not joined) (not spectated))
{:json-rpc/call [{:method "wakuext_spectateCommunity" {:json-rpc/call [{:method "wakuext_spectateCommunity"
:params [community-id] :params [community-id]
:js-response true
:on-success [:chat.ui/spectate-community-success] :on-success [:chat.ui/spectate-community-success]
:on-error (fn [err] :on-error (fn [err]
(log/error {:message (log/error {:message

View File

@ -1,5 +1,6 @@
(ns status-im.contexts.communities.events-test (ns status-im.contexts.communities.events-test
(:require [cljs.test :refer [deftest is testing]] (:require [cljs.test :refer [deftest is testing]]
[legacy.status-im.data-store.communities :as data-store.communities]
matcher-combinators.test matcher-combinators.test
[status-im.contexts.chat.messenger.messages.link-preview.events :as link-preview.events] [status-im.contexts.chat.messenger.messages.link-preview.events :as link-preview.events]
[status-im.contexts.communities.events :as events])) [status-im.contexts.communities.events :as events]))
@ -51,7 +52,7 @@
[:dispatch [:chat.ui/spectate-community community-id]] [:dispatch [:chat.ui/spectate-community community-id]]
[:dispatch [:dispatch
[:chat.ui/cache-link-preview-data "community-link+community-id" [:chat.ui/cache-link-preview-data "community-link+community-id"
{:id community-id}]]]} (data-store.communities/<-rpc #js {"id" community-id})]]]}
(events/community-fetched cofx arg)))))) (events/community-fetched cofx arg))))))
(testing "given a joined community" (testing "given a joined community"
(let [cofx {:db {:communities/fetching-communities {community-id true}}} (let [cofx {:db {:communities/fetching-communities {community-id true}}}
@ -62,7 +63,7 @@
[:dispatch [:chat.ui/spectate-community community-id]] [:dispatch [:chat.ui/spectate-community community-id]]
[:dispatch [:dispatch
[:chat.ui/cache-link-preview-data "community-link+community-id" [:chat.ui/cache-link-preview-data "community-link+community-id"
{:id community-id}]]]} (data-store.communities/<-rpc #js {"id" community-id})]]]}
(events/community-fetched cofx arg)))))) (events/community-fetched cofx arg))))))
(testing "given a token-gated community" (testing "given a token-gated community"
(let [cofx {:db {:communities/fetching-communities {community-id true}}} (let [cofx {:db {:communities/fetching-communities {community-id true}}}
@ -73,7 +74,7 @@
[:dispatch [:chat.ui/spectate-community community-id]] [:dispatch [:chat.ui/spectate-community community-id]]
[:dispatch [:dispatch
[:chat.ui/cache-link-preview-data "community-link+community-id" [:chat.ui/cache-link-preview-data "community-link+community-id"
{:id community-id}]]]} (data-store.communities/<-rpc #js {"id" community-id})]]]}
(events/community-fetched cofx arg)))))) (events/community-fetched cofx arg))))))
(testing "given nil community" (testing "given nil community"
(testing "do nothing" (testing "do nothing"
@ -103,26 +104,38 @@
(events/spectate-community {:db {:communities {community-id {}}}} [community-id])))))) (events/spectate-community {:db {:communities {community-id {}}}} [community-id]))))))
(deftest spectate-community-success-test (deftest spectate-community-success-test
(let [community-id-1 1
community-id-2 2]
(testing "given communities" (testing "given communities"
(testing "mark first community spectated true" (testing "mark first community spectated true"
(is (match? (is (match?
{:db {:communities {community-id {:spectated true}}}} {:db {:communities {community-id-1 {:spectated true}}}}
(events/spectate-community-success {} [{:communities [{:id community-id}]}])))) (events/spectate-community-success {}
(clj->js [{:communities [{:id community-id-1}
{:id community-id-2}]}])))))
(testing "dispatch fxs for first community" (testing "dispatch fxs for first community"
;; Convert the JavaScript object back to a Clojure map because `match?` does not work well
;; with JavaScript objects.
(is (match? (is (match?
{:fx [[:dispatch [:communities/handle-community {:id community-id}]] {:fx [[:dispatch [:communities/handle-community {:id community-id-1}]]]}
]} (let [result (events/spectate-community-success {}
(events/spectate-community-success {} [{:communities [{:id community-id}]}]))))) (clj->js [{:communities
[{:id community-id-1}
{:id community-id-2}]}]))]
(update-in result [:fx 0 1] #(js->clj % :keywordize-keys true)))))))
(testing "given empty community" (testing "given empty community"
(testing "do nothing" (testing "do nothing"
(is (match? (is (match?
nil nil
(events/spectate-community-success {} [{:communities []}]))))) (events/spectate-community-success {}
(clj->js [{:communities []}]))))))
(testing "given nil community" (testing "given nil community"
(testing "do nothing" (testing "do nothing"
(is (match? (is (match?
nil nil
(events/spectate-community-success {} [])))))) (events/spectate-community-success {}
(clj->js []))))))))
(deftest get-revealed-accounts-test (deftest get-revealed-accounts-test
(let [community {:id community-id}] (let [community {:id community-id}]

View File

@ -18,7 +18,7 @@
(defn <-js-map (defn <-js-map
"Shallowly transforms JS Object keys/values with `key-fn`/`val-fn`. "Shallowly transforms JS Object keys/values with `key-fn`/`val-fn`.
Returns nil if `m` is not an instance of `js/Object`. Returns m if `m` is not an instance of `js/Object`.
Implementation taken from `js->clj`, but with the ability to customize how Implementation taken from `js->clj`, but with the ability to customize how
keys and/or values are transformed in one loop. keys and/or values are transformed in one loop.
@ -33,7 +33,8 @@
([^js m] ([^js m]
(<-js-map m nil)) (<-js-map m nil))
([^js m {:keys [key-fn val-fn]}] ([^js m {:keys [key-fn val-fn]}]
(when (identical? (type m) js/Object) (assert (or (nil? m) (identical? (type m) js/Object) (map? m)))
(if (identical? (type m) js/Object)
(persistent! (persistent!
(reduce (fn [r k] (reduce (fn [r k]
(let [v (oops/oget+ m k) (let [v (oops/oget+ m k)
@ -41,7 +42,8 @@
new-val (if val-fn (val-fn k v) v)] new-val (if val-fn (val-fn k v) v)]
(assoc! r new-key new-val))) (assoc! r new-key new-val)))
(transient {}) (transient {})
(js-keys m)))))) (js-keys m)))
m)))
(defn js-stringify (defn js-stringify
[js-object spaces] [js-object spaces]

View File

@ -13,7 +13,7 @@
(are [expected m] (are [expected m]
(is (equals-as-json expected (sut/<-js-map m))) (is (equals-as-json expected (sut/<-js-map m)))
nil nil nil nil
nil #js [] {:a 1} {:a 1}
#js {} #js {} #js {} #js {}
#js {"a" 1 "b" 2} #js {"a" 1 "b" 2})) #js {"a" 1 "b" 2} #js {"a" 1 "b" 2}))