Skip to content

Commit

Permalink
feat: re-fetch community info in community overview and channel
Browse files Browse the repository at this point in the history
Signed-off-by: yqrashawn <namy.19@gmail.com>
  • Loading branch information
yqrashawn committed Jan 12, 2024
1 parent 5cc027a commit 0160bfa
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@
[rn/touchable-opacity
{:on-press #(do
(rf/dispatch [:pop-to-root :shell-stack])
(rf/dispatch [:navigate-to :community-overview (:id community)])
(rf/dispatch [:navigate-to-community-overview (:id community)])
(rf/dispatch [:chat/close]))}
[rn/text
{:style {:text-align :center
Expand Down
2 changes: 1 addition & 1 deletion src/status_im/common/router.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@
(defn community-route-type
[route-params]
(if (string/starts-with? (:community-id route-params) "z")
:desktop-community
:serialized-community
:community))

(defn handle-uri
Expand Down
49 changes: 23 additions & 26 deletions src/status_im/common/universal_links.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,23 @@
(log/info "universal-links: handling community request " community-id)
(navigation/navigate-to cofx :community-requests-to-join {:community-id community-id}))

(rf/defn handle-community
[cofx {:keys [community-id]}]
(log/info "universal-links: handling community" community-id)
(navigation/navigate-to cofx :community {:community-id community-id}))

(rf/defn handle-navigation-to-desktop-community-from-mobile
{:events [:handle-navigation-to-desktop-community-from-mobile]}
[cofx deserialized-key]
(rf/merge
cofx
{:dispatch [:navigate-to :community-overview deserialized-key]}
(navigation/pop-to-root :shell-stack)))

(rf/defn handle-desktop-community
(rf/defn handle-serialized-community
[_ {:keys [community-id]}]
(native-module/deserialize-and-compress-key
community-id
(fn [deserialized-key]
(rf/dispatch [:chat.ui/fetch-community (str deserialized-key)])
(rf/dispatch [:handle-navigation-to-desktop-community-from-mobile (str deserialized-key)]))))
(rf/dispatch [:navigate-to-community-overview (str deserialized-key)]))))

(rf/defn navigate-to-community-overview
{:events [:navigate-to-community-overview]}
[cofx deserialized-key]
(if (string/starts-with? deserialized-key "z")
(handle-serialized-community cofx deserialized-key)
(rf/merge
cofx
{:fx [[:chat.ui/fetch-community deserialized-key]
[:dispatch [:navigate-to :community-overview deserialized-key]]]}
(navigation/pop-to-root :shell-stack))))

(rf/defn handle-community-chat
[cofx {:keys [chat-id]}]
Expand Down Expand Up @@ -145,16 +142,16 @@
{:events [::match-value]}
[cofx url {:keys [type] :as data}]
(case type
:group-chat (handle-group-chat cofx data)
:private-chat (handle-private-chat cofx data)
:community-requests (handle-community-requests cofx data)
:community (handle-community cofx data)
:desktop-community (handle-desktop-community cofx data)
:community-chat (handle-community-chat cofx data)
:contact (handle-view-profile cofx data)
:browser (handle-browse cofx data)
:eip681 (handle-eip681 cofx data)
:wallet-account (handle-wallet-account cofx data)
:group-chat (handle-group-chat cofx data)
:private-chat (handle-private-chat cofx data)
:community-requests (handle-community-requests cofx data)
:community (navigate-to-community-overview cofx data)
:serialized-community (handle-serialized-community cofx data)
:community-chat (handle-community-chat cofx data)
:contact (handle-view-profile cofx data)
:browser (handle-browse cofx data)
:eip681 (handle-eip681 cofx data)
:wallet-account (handle-wallet-account cofx data)
(handle-not-found url)))

(rf/defn route-url
Expand Down
4 changes: 3 additions & 1 deletion src/status_im/contexts/chat/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@
{:events [:chat/navigate-to-chat]}
[{db :db :as cofx} chat-id animation]
(rf/merge cofx
{:dispatch [(if animation :shell/navigate-to :navigate-to) :chat chat-id animation]}
{:fx [[:dispatch [(if animation :shell/navigate-to :navigate-to) :chat chat-id animation]]
(when-let [community-id (get-in db [:chats chat-id :community-id])]
[:dispatch [:chat.ui/fetch-community community-id]])]}
(close-chat)
(force-close-chat chat-id)
(fn [{:keys [db]}]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,34 @@
(boolean enabled?)
{}))

(defn community-resolved
(defn community-fetched
[{:keys [db]} [community-id community]]
(when community
{:db (update db :communities/resolve-community-info dissoc community-id)
{:db (update db :communities/fetching-community dissoc community-id)
:fx [[:dispatch [:communities/handle-community community]]
[:dispatch
[:chat.ui/cache-link-preview-data (community-link community-id) community]]]}))

(rf/reg-event-fx :chat.ui/community-resolved community-resolved)
(rf/reg-event-fx :chat.ui/community-fetched community-fetched)

(defn community-failed-to-resolve
(defn community-failed-to-fetch
[{:keys [db]} [community-id]]
{:db (update db :communities/resolve-community-info dissoc community-id)})
{:db (update db :communities/fetching-community dissoc community-id)})

(rf/reg-event-fx :chat.ui/community-failed-to-resolve community-failed-to-resolve)
(rf/reg-event-fx :chat.ui/community-failed-to-fetch community-failed-to-fetch)

(defn fetch-community
[{:keys [db]} [community-id]]
(when community-id
{:db (assoc-in db [:communities/resolve-community-info community-id] true)
(when (and community-id (not (get-in db [:communities/fetching-community community-id])))
{:db (assoc-in db [:communities/fetching-community community-id] true)
:json-rpc/call [{:method "wakuext_fetchCommunity"
:params [{:CommunityKey community-id
:TryDatabase true
:WaitForResponse true}]
:on-success (fn [community]
(rf/dispatch [:chat.ui/community-resolved community-id community]))
(rf/dispatch [:chat.ui/community-fetched community-id community]))
:on-error (fn [err]
(rf/dispatch [:chat.ui/community-failed-to-resolve community-id])
(rf/dispatch [:chat.ui/community-failed-to-fetch community-id])
(log/error {:message
"Failed to request community info from mailserver"
:error err}))}]}))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

(t/deftest fetch-community
(t/testing "with community id"
(t/testing "update resolving indicator in db"
(t/testing "update fetching indicator in db"
(t/is (match?
{:db {:communities/resolve-community-info {"community-id" true}}}
{:db {:communities/fetching-community {"community-id" true}}}
(sut/fetch-community {} ["community-id"]))))
(t/testing "call the fetch community rpc method with correct community id"
(t/is (match?
Expand All @@ -23,58 +23,58 @@
nil
(sut/fetch-community {} []))))))

(t/deftest community-failed-to-resolve
(t/deftest community-failed-to-fetch
(t/testing "given a community id"
(t/testing "remove community id from resolving indicator in db"
(t/testing "remove community id from fetching indicator in db"
(t/is (match?
nil
(get-in (sut/community-failed-to-resolve {:db {:communities/resolve-community-info
{"community-id" true}}}
["community-id"])
[:db :communities/resolve-community-info "community-id"]))))))
(get-in (sut/community-failed-to-fetch {:db {:communities/fetching-community
{"community-id" true}}}
["community-id"])
[:db :communities/fetching-community "community-id"]))))))

(t/deftest community-resolved
(t/deftest community-fetched
(with-redefs [sut/community-link (fn [id] (str "community-link+" id))]
(t/testing "given a community"
(let [cofx {:db {:communities/resolve-community-info {"community-id" true}}}
(let [cofx {:db {:communities/fetching-community {"community-id" true}}}
arg ["community-id" {:id "community-id"}]]
(t/testing "remove community id from resolving indicator in db"
(t/testing "remove community id from fetching indicator in db"
(t/is (match?
nil
(get-in (sut/community-resolved cofx arg)
[:db :communities/resolve-community-info "community-id"]))))
(get-in (sut/community-fetched cofx arg)
[:db :communities/fetching-community "community-id"]))))
(t/testing "dispatch fxs"
(t/is (match?
{:fx [[:dispatch [:communities/handle-community {:id "community-id"}]]
[:dispatch
[:chat.ui/cache-link-preview-data "community-link+community-id"
{:id "community-id"}]]]}
(sut/community-resolved cofx arg))))))
(sut/community-fetched cofx arg))))))
(t/testing "given a joined community"
(let [cofx {:db {:communities/resolve-community-info {"community-id" true}}}
(let [cofx {:db {:communities/fetching-community {"community-id" true}}}
arg ["community-id" {:id "community-id" :joined true}]]
(t/testing "dispatch fxs, do not spectate community"
(t/is (match?
{:fx [[:dispatch [:communities/handle-community {:id "community-id"}]]
[:dispatch
[:chat.ui/cache-link-preview-data "community-link+community-id"
{:id "community-id"}]]]}
(sut/community-resolved cofx arg))))))
(sut/community-fetched cofx arg))))))
(t/testing "given a token-gated community"
(let [cofx {:db {:communities/resolve-community-info {"community-id" true}}}
(let [cofx {:db {:communities/fetching-community {"community-id" true}}}
arg ["community-id" {:id "community-id" :tokenPermissions [1]}]]
(t/testing "dispatch fxs, do not spectate community"
(t/is (match?
{:fx [[:dispatch [:communities/handle-community {:id "community-id"}]]
[:dispatch
[:chat.ui/cache-link-preview-data "community-link+community-id"
{:id "community-id"}]]]}
(sut/community-resolved cofx arg))))))
(sut/community-fetched cofx arg))))))
(t/testing "given nil community"
(t/testing "do nothing"
(t/is (match?
nil
(sut/community-resolved {} ["community-id" nil])))))))
(sut/community-fetched {} ["community-id" nil])))))))

(t/deftest spectate-community
(t/testing "given a joined community"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
[utils.re-frame :as rf]))

(defn token-requirements
[id]
(rf/dispatch [:communities/check-permissions-to-join-community id])
(fn []
[]
(fn [id]
(let [{:keys [can-request-access?
number-of-hold-tokens tokens]} (rf/sub [:community/token-gated-overview id])]
[rn/view {:style {:padding-horizontal 12}}
Expand Down
8 changes: 4 additions & 4 deletions src/status_im/contexts/communities/discover/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
[quo/community-card-view-item
{:community (assoc item :cover cover)
:width width
:on-press #(rf/dispatch [:navigate-to :community-overview (:id item)])}]
:on-press #(rf/dispatch [:navigate-to-community-overview (:id item)])}]
[quo/community-list-item
{:on-press (fn []
(rf/dispatch [:dismiss-keyboard])
(rf/dispatch [:navigate-to :community-overview (:id item)]))
(rf/dispatch [:navigate-to-community-overview (:id item)]))
:on-long-press #(rf/dispatch
[:show-bottom-sheet
{:content (fn []
Expand Down Expand Up @@ -142,12 +142,12 @@
(if (= view-type :card-view)
[quo/community-card-view-item
{:community (assoc community :cover cover)
:on-press #(rf/dispatch [:navigate-to :community-overview (:id community)])}]
:on-press #(rf/dispatch [:navigate-to-community-overview (:id community)])}]

[quo/community-list-item
{:on-press (fn []
(rf/dispatch [:dismiss-keyboard])
(rf/dispatch [:navigate-to :community-overview (:id community)]))
(rf/dispatch [:navigate-to-community-overview (:id community)]))
:on-long-press #(js/alert "TODO: to be implemented")}
community])]))
(if communities communities communities-ids))
Expand Down
20 changes: 15 additions & 5 deletions src/status_im/contexts/communities/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,24 @@
(when community-js
(let [{:keys [token-permissions
token-permissions-check joined id]
:as community} (<-rpc community-js)
has-token-permissions? (not (seq token-permissions))]
:as community} (<-rpc community-js)
has-perm-to-join? (fn [id-perm-tuple]
(let [{:keys [type]} (second id-perm-tuple)]
(or (= type constants/community-token-permission-become-admin)
(= type constants/community-token-permission-become-member)
(= type constants/community-token-permission-become-token-owner))))
has-channel-perm? (fn [id-perm-tuple]
(let [{:keys [type]} (second id-perm-tuple)]
(or (= type constants/community-token-permission-can-view-channel)
(=
type
constants/community-token-permission-can-view-and-post-channel))))]
{:db (assoc-in db [:communities id] community)
:fx [(when (and has-token-permissions? (not joined))
:fx [(when (not joined)
[:dispatch [:chat.ui/spectate-community id]])
(when (and has-token-permissions? (nil? token-permissions-check))
(when (and (nil? token-permissions-check) (some has-perm-to-join? token-permissions))
[:dispatch [:communities/check-permissions-to-join-community id]])
(when (and has-token-permissions? (not (get-in db [:community-channels-permissions id])))
(when (some has-channel-perm? token-permissions)
[:dispatch [:communities/check-all-community-channels-permissions id]])]}))))

(rf/defn handle-removed-chats
Expand Down
2 changes: 1 addition & 1 deletion src/status_im/contexts/communities/home/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
[quo/communities-membership-list-item
{:customization-color customization-color
:style {:padding-horizontal 20}
:on-press #(debounce/dispatch-and-chill [:navigate-to :community-overview id] 500)
:on-press #(debounce/dispatch-and-chill [:navigate-to-community-overview id] 500)
:on-long-press #(rf/dispatch
[:show-bottom-sheet
{:content (fn []
Expand Down
4 changes: 2 additions & 2 deletions src/status_im/contexts/communities/overview/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@
[quo/icon :i/info {:no-color true}]]])

(defn token-gates
[{:keys [id]}]
(rf/dispatch [:communities/check-permissions-to-join-community id])
[]
(fn [{:keys [id color]}]
(let [{:keys [can-request-access?
number-of-hold-tokens tokens]} (rf/sub [:community/token-gated-overview id])]
Expand Down Expand Up @@ -376,6 +375,7 @@

(defn overview
[id]
(rf/dispatch [:chat.ui/fetch-community id])
(let [id (or id (rf/sub [:get-screen-params :community-overview]))
customization-color (rf/sub [:profile/customization-color])]
[rn/view {:style style/community-overview-container}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
[gesture/touchable-without-feedback
{:on-press (fn []
(rf/dispatch [:navigate-back])
(rf/dispatch [:navigate-to :community-overview community-id]))}
(rf/dispatch [:navigate-to-community-overview community-id]))}
[quo/activity-log
{:title header-text
:customization-color customization-color
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@
(rf/dispatch [:chat/navigate-to-chat channel-id])

(= card-type shell.constants/community-card)
(rf/dispatch [:navigate-to :community-overview id])))
(rf/dispatch [:navigate-to-community-overview id])))

(defn calculate-card-position-and-open-screen
[card-ref card-type id channel-id]
Expand Down
2 changes: 1 addition & 1 deletion src/status_im/subs/communities.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

(re-frame/reg-sub
:communities/fetching-community
:<- [:communities/resolve-community-info]
:<- [:communities/fetching-community]
(fn [info [_ id]]
(get info id)))

Expand Down
2 changes: 1 addition & 1 deletion src/status_im/subs/root.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
(reg-root-key-sub :communities/channels-permissions :community-channels-permissions)
(reg-root-key-sub :communities/requests-to-join :communities/requests-to-join)
(reg-root-key-sub :communities/community-id-input :communities/community-id-input)
(reg-root-key-sub :communities/resolve-community-info :communities/resolve-community-info)
(reg-root-key-sub :communities/fetching-community :communities/fetching-community)
(reg-root-key-sub :communities/my-pending-requests-to-join :communities/my-pending-requests-to-join)
(reg-root-key-sub :communities/collapsed-categories :communities/collapsed-categories)
(reg-root-key-sub :communities/selected-tab :communities/selected-tab)
Expand Down

0 comments on commit 0160bfa

Please sign in to comment.