diff --git a/src/status_im/contexts/chat/messenger/messages/link_preview/view.cljs b/src/status_im/contexts/chat/messenger/messages/link_preview/view.cljs index 788b994f92..8bf0b081d1 100644 --- a/src/status_im/contexts/chat/messenger/messages/link_preview/view.cljs +++ b/src/status_im/contexts/chat/messenger/messages/link_preview/view.cljs @@ -81,7 +81,9 @@ (fn [] (when-not cached-preview-data (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 (fn [] (when cached-preview-data diff --git a/src/status_im/contexts/communities/events.cljs b/src/status_im/contexts/communities/events.cljs index 67ece5168c..a645449608 100644 --- a/src/status_im/contexts/communities/events.cljs +++ b/src/status_im/contexts/communities/events.cljs @@ -22,15 +22,18 @@ [{:keys [db]} [community-js]] (when community-js (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) 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) + constants/community-token-permission-can-view-and-post-channel)))) + 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]] (when (not joined) [:dispatch [:chat.ui/spectate-community id]]) @@ -299,6 +302,7 @@ (when community {:db (update db :communities/fetching-community dissoc community-id) :fx [[:dispatch [:communities/handle-community community]] + [:dispatch [:communities/update-last-opened-at community-id]] [:dispatch [:chat.ui/cache-link-preview-data (link-preview.events/community-link community-id) community]]]})) @@ -312,7 +316,7 @@ (rf/reg-event-fx :chat.ui/community-failed-to-fetch community-failed-to-fetch) (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]))) {:db (assoc-in db [:communities/fetching-community community-id] true) :json-rpc/call [{:method "wakuext_fetchCommunity" @@ -320,6 +324,8 @@ :TryDatabase true :WaitForResponse true}] :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])) :on-error (fn [err] (rf/dispatch [:chat.ui/community-failed-to-fetch community-id]) @@ -332,7 +338,11 @@ [:catn [:cofx :schema.re-frame/cofx] [:args - [:schema [:catn [:community-id [:? :string]]]]]] + [:schema + [:catn + [:map + [:community-id [:? :string]] + [:update-last-opened-at? [:? :boolean]]]]]]] [:maybe [:map [:db map?] @@ -400,17 +410,21 @@ (navigate-to-serialized-community cofx deserialized-key) (rf/merge cofx - {:fx [[:dispatch [:communities/fetch-community deserialized-key]] - [:dispatch [:navigate-to :community-overview deserialized-key]] - [:dispatch [:communities/update-last-opened-at deserialized-key]]]} + {:fx [[:dispatch + [:communities/fetch-community + {:community-id deserialized-key + :update-last-opened-at? true}]] + [:dispatch [:navigate-to :community-overview deserialized-key]]]} (navigation/pop-to-root :shell-stack))))) (rf/reg-event-fx :communities/navigate-to-community-chat (fn [{:keys [db]} [chat-id pop-to-root?]] (let [{:keys [community-id]} (get-in db [:chats chat-id])] {:fx [(when community-id - [:dispatch [:communities/fetch-community community-id]] - [:dispatch [:communities/update-last-opened-at community-id]]) + [:dispatch + [:communities/fetch-community + {:community-id community-id + :update-last-opened-at? true}]]) (if pop-to-root? [:dispatch [:chat/pop-to-root-and-navigate-to-chat chat-id]] [:dispatch [:chat/navigate-to-chat chat-id]])]}))) diff --git a/src/status_im/contexts/communities/events_test.cljs b/src/status_im/contexts/communities/events_test.cljs index 25dffdc49b..eee2f3a60f 100644 --- a/src/status_im/contexts/communities/events_test.cljs +++ b/src/status_im/contexts/communities/events_test.cljs @@ -106,14 +106,14 @@ (testing "update fetching indicator in db" (is (match? {: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" (is (match? {:json-rpc/call [{:method "wakuext_fetchCommunity" :params [{:CommunityKey community-id :TryDatabase true :WaitForResponse true}]}]} - (events/fetch-community {} [community-id]))))) + (events/fetch-community {} [{:community-id community-id}]))))) (testing "with no community id" (testing "do nothing" (is (match? @@ -143,6 +143,7 @@ (testing "dispatch fxs" (is (match? {:fx [[:dispatch [:communities/handle-community {:id community-id}]] + [:dispatch [:communities/update-last-opened-at community-id]] [:dispatch [:chat.ui/cache-link-preview-data "community-link+community-id" {:id community-id}]]]} @@ -153,6 +154,7 @@ (testing "dispatch fxs, do not spectate community" (is (match? {:fx [[:dispatch [:communities/handle-community {:id community-id}]] + [:dispatch [:communities/update-last-opened-at community-id]] [:dispatch [:chat.ui/cache-link-preview-data "community-link+community-id" {:id community-id}]]]} @@ -163,6 +165,7 @@ (testing "dispatch fxs, do not spectate community" (is (match? {:fx [[:dispatch [:communities/handle-community {:id community-id}]] + [:dispatch [:communities/update-last-opened-at community-id]] [:dispatch [:chat.ui/cache-link-preview-data "community-link+community-id" {:id community-id}]]]} diff --git a/src/status_im/subs/communities.cljs b/src/status_im/subs/communities.cljs index 8e75587c11..ecc0000468 100644 --- a/src/status_im/subs/communities.cljs +++ b/src/status_im/subs/communities.cljs @@ -94,6 +94,19 @@ (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 :communities/grouped-by-status :<- [:view-id] @@ -104,16 +117,21 @@ ;; in app-db. Result map has form: {:joined [id1, id2] :pending [id3, id5] :opened [id4]}" (fn [[view-id communities requests]] (if (or (empty? @memo-communities-stack-items) (= view-id :communities-stack)) - (let [grouped-communities (reduce (fn [acc community] - (let [joined? (:joined community) - community-id (:id community) - pending? (boolean (get requests community-id))] - (cond - joined? (update acc :joined conj community) - pending? (update acc :pending conj community) - :else (update acc :opened conj community)))) - {:joined [] :pending [] :opened []} - (vals communities))] + (let [grouped-communities (->> communities + vals + (group-by #(group-communities-by-status requests %)) + merge-opened-communities + (map (fn [[k v]] + {k (sort-by (fn [{:keys [requested-to-join-at last-opened-at + joined-at]}] + (condp = k + :joined joined-at + :pending requested-to-join-at + :opened last-opened-at + last-opened-at)) + #(compare %2 %1) + v)})) + (into {}))] (reset! memo-communities-stack-items grouped-communities) grouped-communities) @memo-communities-stack-items)))