Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle unknown contract community #20198

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)]
(link-preview/cache-community-preview-data community)
{:fx [[:dispatch [:discover-community/maybe-found-unknown-contract-community community]]]})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for the confusion, here you have to dispatch cache-community-preview-data as well as it was in the first iteration of your PR.


"status.updates.timedout"
(visibility-status-updates/handle-visibility-status-updates cofx (transforms/js->clj event-js))
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
71 changes: 71 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,74 @@
"0x0490d2bb47388504e4b615052566e5830662bf202eb179251e9118587ce628c6c76e1f4550f9cd52058cf9dbdb5b788eea10b7c765cd7565675daa5f822acab8f4"
{}}}}
:unknown-communities nil}))

(deftest handle-contract-communities-test
(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")}))

(deftest maybe-found-unknown-contract-community-test
(are [contract-communities-already-in-db
input-found-community
expected-contract-communities-in-db]
(match?
(get-in (events/maybe-found-unknown-contract-community
{:db {:contract-communities contract-communities-already-in-db}}
[input-found-community])
[:db :contract-communities])
expected-contract-communities-in-db)

nil {:id "community-a"} nil

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

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

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