Skip to content

Commit

Permalink
fix: handle unknown contract community
Browse files Browse the repository at this point in the history
  • Loading branch information
yqrashawn committed May 28, 2024
1 parent 3d1c0c6 commit 42c45e0
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/status_im/common/signals/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@
(local-notifications/process cofx (transforms/js->clj event-js))

"community.found"
(link-preview/cache-community-preview-data (transforms/js->clj event-js))
(let [community (transforms/js->clj event-js)]
{:fx [[:dispatch [:chat.ui/cache-link-preview-data-by-community community]]
[:dispatch [:discover-community/maybe-found-unknown-contract-community community]]]})

"status.updates.timedout"
(visibility-status-updates/handle-visibility-status-updates cofx (transforms/js->clj event-js))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
data)))]
{:db (assoc-in db [:profile/profile :link-previews-cache] link-previews-cache)})))

(rf/reg-event-fx :chat.ui/cache-link-preview-data-by-community
(fn [_ [community]]
{:fx [[:dispatch [:chat.ui/cache-link-preview-data (community-link (:id community)) community]]]}))

(rf/reg-event-fx :chat.ui/load-link-preview-data
(fn [{{:profile/keys [profile]} :db} [link]]
(let [{:keys [error] :as cache-data} (get-in profile [:link-previews-cache link])]
Expand Down Expand Up @@ -46,10 +50,6 @@
:on-success [:chat.ui/link-preview-whitelist-received]
:on-error #(log/error "Failed to get link preview whitelist")})))

(defn cache-community-preview-data
[{:keys [id] :as community}]
(rf/dispatch [:chat.ui/cache-link-preview-data (community-link id) community]))

(rf/reg-event-fx :chat.ui/enable-link-previews
(fn [{{:profile/keys [profile]} :db} [site enabled?]]
(let [enabled-sites (if enabled?
Expand Down
32 changes: 27 additions & 5 deletions src/status_im/contexts/communities/discover/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,38 @@
{}
m))

(rf/defn handle-contract-communities
{:events [:fetched-contract-communities]}
[{:keys [db]} contract-communities]
(defn maybe-found-unknown-contract-community
[{:keys [db]} [{:keys [id] :as community}]]
(let [{:keys [contract-communities]} db
{:keys [unknown-featured unknown-other]} contract-communities]
{:db (cond-> db
((set unknown-featured) id)
(assoc-in [:contract-communities :featured id] community)
((set unknown-featured) id)
(assoc-in [:contract-communities :unknown-featured] (remove #{id} unknown-featured))

((set unknown-other) id)
(assoc-in [:contract-communities :other id] community)
((set unknown-other) id)
(assoc-in [:contract-communities :unknown-other] (remove #{id} unknown-other)))}))

(rf/reg-event-fx :discover-community/maybe-found-unknown-contract-community
maybe-found-unknown-contract-community)

(defn handle-contract-communities
[{:keys [db]} [contract-communities]]
(let [cc (rename-contract-community-keys contract-communities)
featured (:contract-featured-communities cc)
unknown (:unknown-communities cc)
other (remove (set featured) (:contract-communities cc))]
{:db (assoc db
:contract-communities
{:featured (select-keys (:communities cc) featured)
:other (select-keys (:communities cc) other)})}))
{:featured (select-keys (:communities cc) featured)
:unknown-featured (filter (set unknown) featured)
:unknown-other (filter (set unknown) other)
:other (select-keys (:communities cc) other)})}))

(rf/reg-event-fx :fetched-contract-communities handle-contract-communities)

(rf/defn fetch-contract-communities
{:events [:fetch-contract-communities]}
Expand Down
30 changes: 30 additions & 0 deletions src/status_im/contexts/communities/discover/events_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,33 @@
"0x0490d2bb47388504e4b615052566e5830662bf202eb179251e9118587ce628c6c76e1f4550f9cd52058cf9dbdb5b788eea10b7c765cd7565675daa5f822acab8f4"
{}}}}
:unknown-communities nil}))

(deftest handle-contract-communities
(are [input-contract-communities
expected-contract-communities-in-db]
(match?
(get-in (events/handle-contract-communities {:db nil} [input-contract-communities])
[:db :contract-communities])
expected-contract-communities-in-db)

{:communities {}
:contractFeaturedCommunities []
:contractCommunities []
:unknownCommunities []}

{:featured {}
:other {}
:unknown-featured '()
:unknown-other '()}


{:communities {"a" {:id "a"}}
:contractFeaturedCommunities ["a" "b"]
:contractCommunities ["a" "b" "c"]
:unknownCommunities ["b" "c"]}


{:featured {"a" {:id "a"}}
:other {}
:unknown-featured '("b")
:unknown-other '("c")}))

0 comments on commit 42c45e0

Please sign in to comment.