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)
@ -253,18 +254,19 @@
(not (get-in db [:communities community-id])) (not (get-in db [:communities community-id]))
(not (get-in db [:communities/fetching-communities community-id]))) (not (get-in db [:communities/fetching-communities community-id])))
{:db (assoc-in db [:communities/fetching-communities community-id] true) {:db (assoc-in db [:communities/fetching-communities community-id] true)
:json-rpc/call [{:method "wakuext_fetchCommunity" :json-rpc/call [{:method "wakuext_fetchCommunity"
:params [{:CommunityKey community-id :params [{:CommunityKey community-id
:TryDatabase true :TryDatabase true
:WaitForResponse true}] :WaitForResponse true}]
:on-success (fn [community] :js-response true
(if community :on-success (fn [community]
(rf/dispatch [:chat.ui/community-fetched community-id (if community
community]) (rf/dispatch [:chat.ui/community-fetched community-id
(failed-to-fetch-community community])
community-id (failed-to-fetch-community
"community wasn't found at the store node"))) community-id
:on-error (partial failed-to-fetch-community community-id)}]})) "community wasn't found at the store node")))
:on-error (partial failed-to-fetch-community community-id)}]}))
(schema/=> fetch-community (schema/=> fetch-community
[:=> [:=>
@ -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
{:db (-> db (let [communities (oops/oget response-js "communities")
(assoc-in [:communities (:id community) :spectated] true)) community (first communities)
:fx [[:dispatch [:communities/handle-community community]]]})) community-id (when community (oops/oget community "id"))]
(when community
{:db (-> db
(assoc-in [:communities community-id :spectated] true))
: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)
@ -295,13 +301,14 @@
[{:keys [db]} [community-id]] [{:keys [db]} [community-id]]
(let [{:keys [spectated joined]} (get-in db [:communities community-id])] (let [{:keys [spectated joined]} (get-in db [:communities community-id])]
(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]
:on-success [:chat.ui/spectate-community-success] :js-response true
:on-error (fn [err] :on-success [:chat.ui/spectate-community-success]
(log/error {:message :on-error (fn [err]
"Failed to spectate community" (log/error {:message
:error err}))}]}))) "Failed to spectate community"
:error err}))}]})))
(schema/=> spectate-community (schema/=> spectate-community
[:=> [:=>

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
(testing "given communities" (let [community-id-1 1
(testing "mark first community spectated true" community-id-2 2]
(is (match? (testing "given communities"
{:db {:communities {community-id {:spectated true}}}} (testing "mark first community spectated true"
(events/spectate-community-success {} [{:communities [{:id community-id}]}])))) (is (match?
(testing "dispatch fxs for first community" {:db {:communities {community-id-1 {:spectated true}}}}
(is (match? (events/spectate-community-success {}
{:fx [[:dispatch [:communities/handle-community {:id community-id}]] (clj->js [{:communities [{:id community-id-1}
]} {:id community-id-2}]}])))))
(events/spectate-community-success {} [{:communities [{:id community-id}]}]))))) (testing "dispatch fxs for first community"
(testing "given empty community" ;; Convert the JavaScript object back to a Clojure map because `match?` does not work well
(testing "do nothing" ;; with JavaScript objects.
(is (match? (is (match?
nil {:fx [[:dispatch [:communities/handle-community {:id community-id-1}]]]}
(events/spectate-community-success {} [{:communities []}]))))) (let [result (events/spectate-community-success {}
(testing "given nil community" (clj->js [{:communities
(testing "do nothing" [{:id community-id-1}
(is (match? {:id community-id-2}]}]))]
nil (update-in result [:fx 0 1] #(js->clj % :keywordize-keys true)))))))
(events/spectate-community-success {} [])))))) (testing "given empty community"
(testing "do nothing"
(is (match?
nil
(events/spectate-community-success {}
(clj->js [{:communities []}]))))))
(testing "given nil community"
(testing "do nothing"
(is (match?
nil
(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}))