Skip to content

Commit

Permalink
fix hidden community appears on "Opened" Tab (#21238)
Browse files Browse the repository at this point in the history
  • Loading branch information
Parveshdhull authored Sep 18, 2024
1 parent 6966432 commit 2f3d3fc
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 56 deletions.
65 changes: 36 additions & 29 deletions src/status_im/contexts/communities/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:require
[clojure.string :as string]
[legacy.status-im.data-store.communities :as data-store.communities]
[oops.core :as oops]
[schema.core :as schema]
[status-im.constants :as constants]
[status-im.contexts.chat.messenger.messages.link-preview.events :as link-preview.events]
Expand All @@ -25,7 +26,7 @@
token-permissions-check id last-opened-at]
:as community} (data-store.communities/<-rpc community-js)
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
[:communities id]
(assoc community :last-opened-at (max last-opened-at previous-last-opened-at)))
Expand Down Expand Up @@ -223,14 +224,14 @@
:fx [[:dispatch [:communities/check-permissions-to-join-community community-id]]]})))

(defn community-fetched
[{:keys [db]} [community-id community]]
(when community
[{:keys [db]} [community-id community-js]]
(when community-js
{: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/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)

Expand All @@ -253,18 +254,19 @@
(not (get-in db [:communities community-id]))
(not (get-in db [:communities/fetching-communities community-id])))
{:db (assoc-in db [:communities/fetching-communities community-id] true)
:json-rpc/call [{:method "wakuext_fetchCommunity"
:params [{:CommunityKey community-id
:TryDatabase true
:WaitForResponse true}]
:on-success (fn [community]
(if community
(rf/dispatch [:chat.ui/community-fetched community-id
community])
(failed-to-fetch-community
community-id
"community wasn't found at the store node")))
:on-error (partial failed-to-fetch-community community-id)}]}))
:json-rpc/call [{:method "wakuext_fetchCommunity"
:params [{:CommunityKey community-id
:TryDatabase true
:WaitForResponse true}]
:js-response true
:on-success (fn [community]
(if community
(rf/dispatch [:chat.ui/community-fetched community-id
community])
(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
[:=>
Expand All @@ -283,25 +285,30 @@
(rf/reg-event-fx :communities/fetch-community fetch-community)

(defn spectate-community-success
[{:keys [db]} [{:keys [communities]}]]
(when-let [community (first communities)]
{:db (-> db
(assoc-in [:communities (:id community) :spectated] true))
:fx [[:dispatch [:communities/handle-community community]]]}))
[{:keys [db]} [response-js]]
(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
(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)

(defn spectate-community
[{:keys [db]} [community-id]]
(let [{:keys [spectated joined]} (get-in db [:communities community-id])]
(when (and (not joined) (not spectated))
{:json-rpc/call [{:method "wakuext_spectateCommunity"
:params [community-id]
:on-success [:chat.ui/spectate-community-success]
:on-error (fn [err]
(log/error {:message
"Failed to spectate community"
:error err}))}]})))
{:json-rpc/call [{:method "wakuext_spectateCommunity"
:params [community-id]
:js-response true
:on-success [:chat.ui/spectate-community-success]
:on-error (fn [err]
(log/error {:message
"Failed to spectate community"
:error err}))}]})))

(schema/=> spectate-community
[:=>
Expand Down
59 changes: 36 additions & 23 deletions src/status_im/contexts/communities/events_test.cljs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns status-im.contexts.communities.events-test
(:require [cljs.test :refer [deftest is testing]]
[legacy.status-im.data-store.communities :as data-store.communities]
matcher-combinators.test
[status-im.contexts.chat.messenger.messages.link-preview.events :as link-preview.events]
[status-im.contexts.communities.events :as events]))
Expand Down Expand Up @@ -51,7 +52,7 @@
[:dispatch [:chat.ui/spectate-community community-id]]
[:dispatch
[: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))))))
(testing "given a joined community"
(let [cofx {:db {:communities/fetching-communities {community-id true}}}
Expand All @@ -62,7 +63,7 @@
[:dispatch [:chat.ui/spectate-community community-id]]
[:dispatch
[: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))))))
(testing "given a token-gated community"
(let [cofx {:db {:communities/fetching-communities {community-id true}}}
Expand All @@ -73,7 +74,7 @@
[:dispatch [:chat.ui/spectate-community community-id]]
[:dispatch
[: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))))))
(testing "given nil community"
(testing "do nothing"
Expand Down Expand Up @@ -103,26 +104,38 @@
(events/spectate-community {:db {:communities {community-id {}}}} [community-id]))))))

(deftest spectate-community-success-test
(testing "given communities"
(testing "mark first community spectated true"
(is (match?
{:db {:communities {community-id {:spectated true}}}}
(events/spectate-community-success {} [{:communities [{:id community-id}]}]))))
(testing "dispatch fxs for first community"
(is (match?
{:fx [[:dispatch [:communities/handle-community {:id community-id}]]
]}
(events/spectate-community-success {} [{:communities [{:id community-id}]}])))))
(testing "given empty community"
(testing "do nothing"
(is (match?
nil
(events/spectate-community-success {} [{:communities []}])))))
(testing "given nil community"
(testing "do nothing"
(is (match?
nil
(events/spectate-community-success {} []))))))
(let [community-id-1 1
community-id-2 2]
(testing "given communities"
(testing "mark first community spectated true"
(is (match?
{:db {:communities {community-id-1 {:spectated true}}}}
(events/spectate-community-success {}
(clj->js [{:communities [{:id community-id-1}
{:id community-id-2}]}])))))
(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?
{:fx [[:dispatch [:communities/handle-community {:id community-id-1}]]]}
(let [result (events/spectate-community-success {}
(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 "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
(let [community {:id community-id}]
Expand Down
8 changes: 5 additions & 3 deletions src/utils/transforms.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
(defn <-js-map
"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
keys and/or values are transformed in one loop.
Expand All @@ -33,15 +33,17 @@
([^js m]
(<-js-map m nil))
([^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!
(reduce (fn [r k]
(let [v (oops/oget+ m k)
new-key (if key-fn (key-fn k v) k)
new-val (if val-fn (val-fn k v) v)]
(assoc! r new-key new-val)))
(transient {})
(js-keys m))))))
(js-keys m)))
m)))

(defn js-stringify
[js-object spaces]
Expand Down
2 changes: 1 addition & 1 deletion src/utils/transforms_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
(are [expected m]
(is (equals-as-json expected (sut/<-js-map m)))
nil nil
nil #js []
{:a 1} {:a 1}
#js {} #js {}
#js {"a" 1 "b" 2} #js {"a" 1 "b" 2}))

Expand Down

0 comments on commit 2f3d3fc

Please sign in to comment.