Sort communities by currently active tab (#18812)

This commit is contained in:
Ibrahem Khalil 2024-03-06 13:13:58 +02:00 committed by GitHub
parent ffcca1aea3
commit f1834c6a15
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 60 additions and 23 deletions

View File

@ -81,7 +81,9 @@
(fn [] (fn []
(when-not cached-preview-data (when-not cached-preview-data
(let [community-id (community-id-from-link community-link)] (let [community-id (community-id-from-link community-link)]
(rf/dispatch [:communities/fetch-community community-id])))) (rf/dispatch [:communities/fetch-community
{:community-id community-id
:update-last-opened-at? false}]))))
:reagent-render :reagent-render
(fn [] (fn []
(when cached-preview-data (when cached-preview-data

View File

@ -22,15 +22,18 @@
[{:keys [db]} [community-js]] [{:keys [db]} [community-js]]
(when community-js (when community-js
(let [{:keys [token-permissions (let [{:keys [token-permissions
token-permissions-check joined id] token-permissions-check joined id last-opened-at]
:as community} (data-store.communities/<-rpc community-js) :as community} (data-store.communities/<-rpc community-js)
has-channel-perm? (fn [id-perm-tuple] has-channel-perm? (fn [id-perm-tuple]
(let [{:keys [type]} (second id-perm-tuple)] (let [{:keys [type]} (second id-perm-tuple)]
(or (= type constants/community-token-permission-can-view-channel) (or (= type constants/community-token-permission-can-view-channel)
(= (=
type type
constants/community-token-permission-can-view-and-post-channel))))] constants/community-token-permission-can-view-and-post-channel))))
{:db (assoc-in db [:communities id] community) previous-last-opened-at (get-in db [:communities id :last-opened-at])]
{:db (assoc-in db
[:communities id]
(assoc community :last-opened-at (max last-opened-at previous-last-opened-at)))
:fx [[:dispatch [:communities/initialize-permission-addresses id]] :fx [[:dispatch [:communities/initialize-permission-addresses id]]
(when (not joined) (when (not joined)
[:dispatch [:chat.ui/spectate-community id]]) [:dispatch [:chat.ui/spectate-community id]])
@ -299,6 +302,7 @@
(when community (when community
{:db (update db :communities/fetching-community dissoc community-id) {:db (update db :communities/fetching-community dissoc community-id)
:fx [[:dispatch [:communities/handle-community community]] :fx [[:dispatch [:communities/handle-community community]]
[:dispatch [:communities/update-last-opened-at 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]]]})) community]]]}))
@ -312,7 +316,7 @@
(rf/reg-event-fx :chat.ui/community-failed-to-fetch community-failed-to-fetch) (rf/reg-event-fx :chat.ui/community-failed-to-fetch community-failed-to-fetch)
(defn fetch-community (defn fetch-community
[{:keys [db]} [community-id]] [{:keys [db]} [{:keys [community-id update-last-opened-at?]}]]
(when (and community-id (not (get-in db [:communities/fetching-community community-id]))) (when (and community-id (not (get-in db [:communities/fetching-community community-id])))
{:db (assoc-in db [:communities/fetching-community community-id] true) {:db (assoc-in db [:communities/fetching-community community-id] true)
:json-rpc/call [{:method "wakuext_fetchCommunity" :json-rpc/call [{:method "wakuext_fetchCommunity"
@ -320,6 +324,8 @@
:TryDatabase true :TryDatabase true
:WaitForResponse true}] :WaitForResponse true}]
:on-success (fn [community] :on-success (fn [community]
(when update-last-opened-at?
(rf/dispatch [:communities/update-last-opened-at community-id]))
(rf/dispatch [:chat.ui/community-fetched community-id community])) (rf/dispatch [:chat.ui/community-fetched community-id community]))
:on-error (fn [err] :on-error (fn [err]
(rf/dispatch [:chat.ui/community-failed-to-fetch community-id]) (rf/dispatch [:chat.ui/community-failed-to-fetch community-id])
@ -332,7 +338,11 @@
[:catn [:catn
[:cofx :schema.re-frame/cofx] [:cofx :schema.re-frame/cofx]
[:args [:args
[:schema [:catn [:community-id [:? :string]]]]]] [:schema
[:catn
[:map
[:community-id [:? :string]]
[:update-last-opened-at? [:? :boolean]]]]]]]
[:maybe [:maybe
[:map [:map
[:db map?] [:db map?]
@ -400,17 +410,21 @@
(navigate-to-serialized-community cofx deserialized-key) (navigate-to-serialized-community cofx deserialized-key)
(rf/merge (rf/merge
cofx cofx
{:fx [[:dispatch [:communities/fetch-community deserialized-key]] {:fx [[:dispatch
[:dispatch [:navigate-to :community-overview deserialized-key]] [:communities/fetch-community
[:dispatch [:communities/update-last-opened-at deserialized-key]]]} {:community-id deserialized-key
:update-last-opened-at? true}]]
[:dispatch [:navigate-to :community-overview deserialized-key]]]}
(navigation/pop-to-root :shell-stack))))) (navigation/pop-to-root :shell-stack)))))
(rf/reg-event-fx :communities/navigate-to-community-chat (rf/reg-event-fx :communities/navigate-to-community-chat
(fn [{:keys [db]} [chat-id pop-to-root?]] (fn [{:keys [db]} [chat-id pop-to-root?]]
(let [{:keys [community-id]} (get-in db [:chats chat-id])] (let [{:keys [community-id]} (get-in db [:chats chat-id])]
{:fx [(when community-id {:fx [(when community-id
[:dispatch [:communities/fetch-community community-id]] [:dispatch
[:dispatch [:communities/update-last-opened-at community-id]]) [:communities/fetch-community
{:community-id community-id
:update-last-opened-at? true}]])
(if pop-to-root? (if pop-to-root?
[:dispatch [:chat/pop-to-root-and-navigate-to-chat chat-id]] [:dispatch [:chat/pop-to-root-and-navigate-to-chat chat-id]]
[:dispatch [:chat/navigate-to-chat chat-id]])]}))) [:dispatch [:chat/navigate-to-chat chat-id]])]})))

View File

@ -106,14 +106,14 @@
(testing "update fetching indicator in db" (testing "update fetching indicator in db"
(is (match? (is (match?
{:db {:communities/fetching-community {community-id true}}} {:db {:communities/fetching-community {community-id true}}}
(events/fetch-community {} [community-id])))) (events/fetch-community {} [{:community-id community-id}]))))
(testing "call the fetch community rpc method with correct community id" (testing "call the fetch community rpc method with correct community id"
(is (match? (is (match?
{: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}]}]}
(events/fetch-community {} [community-id]))))) (events/fetch-community {} [{:community-id community-id}])))))
(testing "with no community id" (testing "with no community id"
(testing "do nothing" (testing "do nothing"
(is (match? (is (match?
@ -143,6 +143,7 @@
(testing "dispatch fxs" (testing "dispatch fxs"
(is (match? (is (match?
{:fx [[:dispatch [:communities/handle-community {:id community-id}]] {:fx [[:dispatch [:communities/handle-community {:id community-id}]]
[:dispatch [:communities/update-last-opened-at 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}]]]} {:id community-id}]]]}
@ -153,6 +154,7 @@
(testing "dispatch fxs, do not spectate community" (testing "dispatch fxs, do not spectate community"
(is (match? (is (match?
{:fx [[:dispatch [:communities/handle-community {:id community-id}]] {:fx [[:dispatch [:communities/handle-community {:id community-id}]]
[:dispatch [:communities/update-last-opened-at 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}]]]} {:id community-id}]]]}
@ -163,6 +165,7 @@
(testing "dispatch fxs, do not spectate community" (testing "dispatch fxs, do not spectate community"
(is (match? (is (match?
{:fx [[:dispatch [:communities/handle-community {:id community-id}]] {:fx [[:dispatch [:communities/handle-community {:id community-id}]]
[:dispatch [:communities/update-last-opened-at 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}]]]} {:id community-id}]]]}

View File

@ -94,6 +94,19 @@
(def memo-communities-stack-items (atom nil)) (def memo-communities-stack-items (atom nil))
(defn- merge-opened-communities
[{:keys [joined pending] :as assorted-communities}]
(update assorted-communities :opened concat joined pending))
(defn- group-communities-by-status
[requests
{:keys [id]
:as community}]
(cond
(:joined community) :joined
(boolean (get requests id)) :pending
:else :opened))
(re-frame/reg-sub (re-frame/reg-sub
:communities/grouped-by-status :communities/grouped-by-status
:<- [:view-id] :<- [:view-id]
@ -104,16 +117,21 @@
;; in app-db. Result map has form: {:joined [id1, id2] :pending [id3, id5] :opened [id4]}" ;; in app-db. Result map has form: {:joined [id1, id2] :pending [id3, id5] :opened [id4]}"
(fn [[view-id communities requests]] (fn [[view-id communities requests]]
(if (or (empty? @memo-communities-stack-items) (= view-id :communities-stack)) (if (or (empty? @memo-communities-stack-items) (= view-id :communities-stack))
(let [grouped-communities (reduce (fn [acc community] (let [grouped-communities (->> communities
(let [joined? (:joined community) vals
community-id (:id community) (group-by #(group-communities-by-status requests %))
pending? (boolean (get requests community-id))] merge-opened-communities
(cond (map (fn [[k v]]
joined? (update acc :joined conj community) {k (sort-by (fn [{:keys [requested-to-join-at last-opened-at
pending? (update acc :pending conj community) joined-at]}]
:else (update acc :opened conj community)))) (condp = k
{:joined [] :pending [] :opened []} :joined joined-at
(vals communities))] :pending requested-to-join-at
:opened last-opened-at
last-opened-at))
#(compare %2 %1)
v)}))
(into {}))]
(reset! memo-communities-stack-items grouped-communities) (reset! memo-communities-stack-items grouped-communities)
grouped-communities) grouped-communities)
@memo-communities-stack-items))) @memo-communities-stack-items)))