From e002fab376c5e1e57dc196d49a8875f83214ee01 Mon Sep 17 00:00:00 2001 From: Andrea Maria Piana Date: Fri, 8 Mar 2024 15:42:39 +0000 Subject: [PATCH] cache processed description --- .../status_im/data_store/communities.cljs | 12 +- .../data_store/communities_test.cljs | 23 -- .../contexts/communities/discover/events.cljs | 1 + .../contexts/communities/events.cljs | 33 +-- .../contexts/communities/events_test.cljs | 34 +-- .../contexts/communities/overview/events.cljs | 27 -- .../contexts/communities/overview/view.cljs | 10 +- src/status_im/subs/chats.cljs | 9 +- src/status_im/subs/communities.cljs | 50 +--- src/status_im/subs/communities_test.cljs | 253 ++++++------------ src/status_im/subs/root.cljs | 1 - status-go-version.json | 6 +- 12 files changed, 125 insertions(+), 334 deletions(-) delete mode 100644 src/legacy/status_im/data_store/communities_test.cljs diff --git a/src/legacy/status_im/data_store/communities.cljs b/src/legacy/status_im/data_store/communities.cljs index 4ff93d44c5..757788a336 100644 --- a/src/legacy/status_im/data_store/communities.cljs +++ b/src/legacy/status_im/data_store/communities.cljs @@ -4,13 +4,6 @@ [clojure.walk :as walk] [status-im.constants :as constants])) -(defn rpc->channel-permissions - [rpc-channels-permissions] - (update-vals rpc-channels-permissions - (fn [{:keys [viewAndPostPermissions viewOnlyPermissions]}] - {:view-only (set/rename-keys viewOnlyPermissions {:satisfied :satisfied?}) - :view-and-post (set/rename-keys viewAndPostPermissions {:satisfied :satisfied?})}))) - (defn <-revealed-accounts-rpc [accounts] (mapv @@ -34,8 +27,9 @@ (assoc acc (name k) (-> v - (assoc :can-post? (:canPost v)) - (dissoc :canPost) + (assoc :token-gated? (:tokenGated v) + :can-post? (:canPost v)) + (dissoc :canPost :tokenGated) (update :members walk/stringify-keys)))) {} chats)) diff --git a/src/legacy/status_im/data_store/communities_test.cljs b/src/legacy/status_im/data_store/communities_test.cljs deleted file mode 100644 index 21fc398b76..0000000000 --- a/src/legacy/status_im/data_store/communities_test.cljs +++ /dev/null @@ -1,23 +0,0 @@ -(ns legacy.status-im.data-store.communities-test - (:require - [cljs.test :refer [deftest is]] - [legacy.status-im.data-store.communities :as sut])) - -(def permissions - {"community-id-chat-1" - {:viewOnlyPermissions {:satisfied false - :permissions {:token-permission-id-01 {:criteria [false]}}} - :viewAndPostPermissions {:satisfied true :permissions {}}} - "community-id-chat-2" - {:viewOnlyPermissions {:satisfied true :permissions {}} - :viewAndPostPermissions {:satisfied true :permissions {}}}}) - -(deftest rpc->channel-permissions-test - (is (= {"community-id-chat-1" - {:view-only {:satisfied? false - :permissions {:token-permission-id-01 {:criteria [false]}}} - :view-and-post {:satisfied? true :permissions {}}} - "community-id-chat-2" - {:view-only {:satisfied? true :permissions {}} - :view-and-post {:satisfied? true :permissions {}}}} - (sut/rpc->channel-permissions permissions)))) diff --git a/src/status_im/contexts/communities/discover/events.cljs b/src/status_im/contexts/communities/discover/events.cljs index 060259b741..3cc62eeeba 100644 --- a/src/status_im/contexts/communities/discover/events.cljs +++ b/src/status_im/contexts/communities/discover/events.cljs @@ -14,6 +14,7 @@ :canManageUsers :can-manage-users? :categoryID :category-id :canPost :can-post? + :tokenGated :token-gated? :isControlNode :is-control-node? :pinMessageAllMembersEnabled :pin-message-all-members-enabled :isMember :is-member? diff --git a/src/status_im/contexts/communities/events.cljs b/src/status_im/contexts/communities/events.cljs index a645449608..2d24fc9635 100644 --- a/src/status_im/contexts/communities/events.cljs +++ b/src/status_im/contexts/communities/events.cljs @@ -21,28 +21,21 @@ (defn handle-community [{:keys [db]} [community-js]] (when community-js - (let [{:keys [token-permissions + (let [{:keys [clock 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)))) + :as community} (data-store.communities/<-rpc community-js) 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]]) - (when (nil? token-permissions-check) - [:dispatch [:communities/check-permissions-to-join-community id]]) - (when (some has-channel-perm? token-permissions) - [:dispatch [:communities/check-all-community-channels-permissions id]]) - (when joined - [:dispatch [:communities/get-revealed-accounts id]])]}))) + (when (> clock (get-in db [:communities id :clock])) + {: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]]) + (when (nil? token-permissions-check) + [:dispatch [:communities/check-permissions-to-join-community id]]) + (when joined + [:dispatch [:communities/get-revealed-accounts id]])]})))) (rf/reg-event-fx :communities/handle-community handle-community) diff --git a/src/status_im/contexts/communities/events_test.cljs b/src/status_im/contexts/communities/events_test.cljs index eee2f3a60f..43f545c172 100644 --- a/src/status_im/contexts/communities/events_test.cljs +++ b/src/status_im/contexts/communities/events_test.cljs @@ -2,7 +2,6 @@ (:require [cljs.test :refer [deftest is testing]] [legacy.status-im.mailserver.core :as mailserver] matcher-combinators.test - [status-im.constants :as constants] [status-im.contexts.chat.messenger.messages.link-preview.events :as link-preview.events] [status-im.contexts.communities.events :as events])) @@ -262,7 +261,7 @@ (-> effects :json-rpc/call first (select-keys [:method :params])))))))) (deftest handle-community - (let [community {:id community-id}] + (let [community {:id community-id :clock 2}] (testing "given a unjoined community" (let [effects (events/handle-community {} [community])] (is (match? community-id @@ -287,29 +286,10 @@ [[:dispatch [:communities/initialize-permission-addresses community-id]] [:dispatch [:chat.ui/spectate-community community-id]]] (filter some? (:fx effects)))))) - (testing "given a community with view channel permission" - (let [community (assoc community - :token-permissions - [["perm-id" {:type constants/community-token-permission-can-view-channel}]]) + (testing "given a community with lower clock" + (let [effects (events/handle-community {:db {:communities {community-id {:clock 3}}}} [community])] + (is (nil? effects)))) + (testing "given a community without clock" + (let [community (dissoc community :clock) effects (events/handle-community {} [community])] - (is (match? - [[:dispatch [:communities/initialize-permission-addresses community-id]] - [:dispatch [:chat.ui/spectate-community community-id]] - [:dispatch [:communities/check-permissions-to-join-community community-id]] - [:dispatch - [:communities/check-all-community-channels-permissions community-id]]] - (filter some? (:fx effects)))))) - - (testing "given a community with post in channel permission" - (let [community (assoc community - :token-permissions - [["perm-id" - {:type constants/community-token-permission-can-view-and-post-channel}]]) - effects (events/handle-community {} [community])] - (is (match? - [[:dispatch [:communities/initialize-permission-addresses community-id]] - [:dispatch [:chat.ui/spectate-community community-id]] - [:dispatch [:communities/check-permissions-to-join-community community-id]] - [:dispatch - [:communities/check-all-community-channels-permissions community-id]]] - (filter some? (:fx effects)))))))) + (is (nil? effects)))))) diff --git a/src/status_im/contexts/communities/overview/events.cljs b/src/status_im/contexts/communities/overview/events.cljs index 68a0387d20..24b1d1a5fa 100644 --- a/src/status_im/contexts/communities/overview/events.cljs +++ b/src/status_im/contexts/communities/overview/events.cljs @@ -1,37 +1,10 @@ (ns status-im.contexts.communities.overview.events (:require - [legacy.status-im.data-store.communities :as data-store] [taoensso.timbre :as log] [utils.i18n :as i18n] [utils.re-frame :as rf])) -(rf/reg-event-fx :communities/check-all-community-channels-permissions-success - (fn [{:keys [db]} [community-id response]] - {:db (-> db - (assoc-in [:community-channels-permissions community-id] - (data-store/rpc->channel-permissions (:channels response))) - (assoc-in [:communities/channel-permissions-check community-id] false))})) -(rf/reg-event-fx :communities/check-all-community-channels-permissions-failed - (fn [{:keys [db]} [community-id]] - {:db (assoc-in db [:communities/channel-permissions-check community-id] false)})) - -(rf/reg-event-fx :communities/check-all-community-channels-permissions - (fn [{:keys [db]} [community-id]] - (when (get-in db [:communities community-id]) - {:db (assoc-in db [:communities/channel-permissions-check community-id] true) - :fx [[:json-rpc/call - [{:method "wakuext_checkAllCommunityChannelsPermissions" - :params [{:CommunityID community-id}] - :on-success [:communities/check-all-community-channels-permissions-success community-id] - :on-error (fn [error] - (rf/dispatch [:communities/check-all-community-channels-permissions-failed - community-id]) - (log/error "failed to check channels permissions" - {:error error - :community-id community-id - :event - :communities/check-all-community-channels-permissions}))}]]]}))) (rf/reg-event-fx :communities/check-permissions-to-join-community-success (fn [{:keys [db]} [community-id based-on-client-selection? result]] diff --git a/src/status_im/contexts/communities/overview/view.cljs b/src/status_im/contexts/communities/overview/view.cljs index 8dbf13b27f..829ab50949 100644 --- a/src/status_im/contexts/communities/overview/view.cljs +++ b/src/status_im/contexts/communities/overview/view.cljs @@ -48,15 +48,16 @@ :emoji emoji :customization-color community-color :mentions-count mentions-count + ;; NOTE: this is a troolean, nil/true/false have different meaning :locked? locked? :notification notification} channel-sheet-data {:selected-item (fn [] [quo/channel channel-options]) :content (fn [] sheet-content)}] [rn/view {:key id} [quo/channel - (merge channel-options - {:on-press on-press - :on-long-press #(rf/dispatch [:show-bottom-sheet channel-sheet-data])})]])) + (assoc channel-options + :on-press on-press + :on-long-press #(rf/dispatch [:show-bottom-sheet channel-sheet-data]))]])) (defn- channel-list-component [{:keys [on-category-layout community-id community-color on-first-channel-height-changed]} @@ -253,8 +254,7 @@ :description-accessibility-label :community-description}]) (defn- community-content - [id] - (rf/dispatch [:communities/check-all-community-channels-permissions id]) + [_] (fn [id {:keys [on-category-layout collapsed? diff --git a/src/status_im/subs/chats.cljs b/src/status_im/subs/chats.cljs index c864c3f02e..adaa33737d 100644 --- a/src/status_im/subs/chats.cljs +++ b/src/status_im/subs/chats.cljs @@ -181,15 +181,16 @@ (chat.events/public-chat? current-chat) (assoc :able-to-send-message? true) + (and (chat.events/community-chat? current-chat) + (get-in community [:chats (subs (:chat-id current-chat) 68) :can-post?])) + (assoc :able-to-send-message? true) + + (and (chat.events/group-chat? current-chat) (group-chats.db/member? my-public-key current-chat)) (assoc :able-to-send-message? true :member? true) - (and (chat.events/community-chat? current-chat) - (get-in community [:chats (subs (:chat-id current-chat) 68) :can-post?])) - (assoc :able-to-send-message? true) - (not group-chat) (assoc :contact-request-state (get-in contacts [chat-id :contact-request-state]) diff --git a/src/status_im/subs/communities.cljs b/src/status_im/subs/communities.cljs index ecc0000468..55db3871b4 100644 --- a/src/status_im/subs/communities.cljs +++ b/src/status_im/subs/communities.cljs @@ -229,42 +229,13 @@ (sort-by :position) (into [])))) -(defn- get-chat-lock-state - "Returns the chat lock state. - - - Nil: no lock (there are no permissions for the chat) - - True: locked (there are permissions and can-post? is false) - - False: unlocked (there are permissions and can-post? is true)" - [community-id channels-permissions {chat-id :id}] - (let [composite-key (keyword (str community-id chat-id)) - permissions (get channels-permissions composite-key) - {view-only-satisfied? :satisfied? - view-only-permissions :permissions} (:view-only permissions) - {view-and-post-satisfied? :satisfied? - view-and-post-permissions :permissions} (:view-and-post permissions) - can-access? (or (and (seq view-only-permissions) - view-only-satisfied?) - (and (seq view-and-post-permissions) - view-and-post-satisfied?))] - (if (and (empty? view-only-permissions) - (empty? view-and-post-permissions)) - nil - (not can-access?)))) - -(re-frame/reg-sub - :communities/community-channels-permissions - :<- [:communities/channels-permissions] - (fn [channel-permissions [_ community-id]] - (get channel-permissions community-id))) - (defn- reduce-over-categories [community-id categories collapsed-categories - full-chats-data - channels-permissions] + full-chats-data] (fn [acc - [_ {:keys [name categoryID position id emoji] :as chat}]] + [_ {:keys [name categoryID position id emoji can-post? token-gated?]}]] (let [category-id (if (seq categoryID) categoryID constants/empty-category-id) {:keys [unviewed-messages-count unviewed-mentions-count @@ -286,9 +257,11 @@ :unread-messages? (pos? unviewed-messages-count) :position position :mentions-count (or unviewed-mentions-count 0) - :locked? (get-chat-lock-state community-id - channels-permissions - chat) + :can-post? can-post? + ;; NOTE: this is a troolean nil->no permissions, true->no access, false -> + ;; has access + :locked? (when token-gated? + (not can-post?)) :id id}] (update-in acc-with-category [category-id :chats] conj categorized-chat)))) @@ -297,17 +270,14 @@ (fn [[_ community-id]] [(re-frame/subscribe [:communities/community community-id]) (re-frame/subscribe [:chats/chats]) - (re-frame/subscribe [:communities/collapsed-categories-for-community community-id]) - (re-frame/subscribe [:communities/community-channels-permissions community-id])]) - (fn [[{:keys [categories chats]} full-chats-data collapsed-categories - channels-permissions] + (re-frame/subscribe [:communities/collapsed-categories-for-community community-id])]) + (fn [[{:keys [categories chats]} full-chats-data collapsed-categories] [_ community-id]] (let [reduce-fn (reduce-over-categories community-id categories collapsed-categories - full-chats-data - channels-permissions) + full-chats-data) categories-and-chats (->> chats (reduce reduce-fn {}) diff --git a/src/status_im/subs/communities_test.cljs b/src/status_im/subs/communities_test.cljs index c1bf477b51..f4b97213cd 100644 --- a/src/status_im/subs/communities_test.cljs +++ b/src/status_im/subs/communities_test.cljs @@ -84,26 +84,29 @@ :communities {"0x1" {:id "0x1" :chats {"0x1" - {:id "0x1" - :position 1 - :name "chat1" - :muted? nil - :categoryID "1" - :can-post? true} + {:id "0x1" + :position 1 + :name "chat1" + :muted? nil + :categoryID "1" + :token-gated? false + :can-post? true} "0x2" - {:id "0x2" - :position 2 - :name "chat2" - :muted? nil - :categoryID "1" - :can-post? false} + {:id "0x2" + :position 2 + :name "chat2" + :muted? nil + :categoryID "1" + :token-gated? true + :can-post? false} "0x3" - {:id "0x3" - :position 3 - :name "chat3" - :muted? nil - :categoryID "2" - :can-post? true}} + {:id "0x3" + :position 3 + :name "chat3" + :muted? nil + :categoryID "2" + :token-gated? true + :can-post? true}} :categories {"1" {:id "1" :position 2 :name "category1"} @@ -112,141 +115,41 @@ :name "category2"}} :joined true}}) (is - (= [["2" - {:id "2" - :name "category2" - :collapsed? nil - :position 1 - :chats [{:name "chat3" - :position 3 - :emoji nil - :muted? nil - :locked? nil - :id "0x3" - :unread-messages? false - :mentions-count 0}]}] - ["1" - {:id "1" - :name "category1" - :collapsed? nil - :position 2 - :chats [{:name "chat1" - :emoji nil - :position 1 - :muted? nil - :locked? nil - :id "0x1" - :unread-messages? false - :mentions-count 0} - {:name "chat2" - :emoji nil - :position 2 - :muted? nil - :locked? nil - :id "0x2" - :unread-messages? false - :mentions-count 0}]}]] - (rf/sub [sub-name "0x1"])))) - - (testing "Channels with categories and token permissions" - (swap! rf-db/app-db assoc - :community-channels-permissions - {community-id - {(keyword (str community-id "0x100")) - {:view-only {:satisfied? false - :permissions {:token-permission-id-01 {:criteria [false]}}} - :view-and-post {:satisfied? true :permissions {}}} - (keyword (str community-id "0x200")) - {:view-only {:satisfied? true :permissions {}} - :view-and-post {:satisfied? true :permissions {}}} - (keyword (str community-id "0x300")) - {:view-only {:satisfied? false :permissions {}} - :view-and-post {:satisfied? true - :permissions {:token-permission-id-03 {:criteria [true]}}}} - (keyword (str community-id "0x400")) - {:view-only {:satisfied? true - :permissions {}} - :view-and-post {:satisfied? false - :permissions {:token-permission-id-04 {:criteria [false]}}}}}} - - :communities - {community-id {:id community-id - :chats {"0x100" {:id "0x100" - :position 1 - :name "chat1" - :muted? nil - :categoryID "1" - :can-post? false} - "0x200" {:id "0x200" - :position 2 - :name "chat2" - :muted? nil - :categoryID "1" - :can-post? false} - "0x300" {:id "0x300" - :position 3 - :name "chat3" - :muted? nil - :categoryID "2" - :can-post? true} - "0x400" {:id "0x400" - :position 4 - :name "chat4" - :muted? nil - :categoryID "2" - :can-post? true}} - :categories {"1" {:id "1" - :position 2 - :name "category1"} - "2" {:id "2" - :position 1 - :name "category2"}} - :joined true}}) - (is - (= [["2" - {:id "2" - :name "category2" - :collapsed? nil - :position 1 - :chats [{:name "chat3" - :position 3 - :emoji nil - :muted? nil - :locked? false - :id "0x300" - :unread-messages? false - :mentions-count 0} - {:name "chat4" - :position 4 - :emoji nil - :muted? nil - :locked? true - :id "0x400" - :unread-messages? false - :mentions-count 0}]}] - ["1" - {:id "1" - :name "category1" - :collapsed? nil - :position 2 - :chats [{:name "chat1" - :emoji nil - :position 1 - :muted? nil - :locked? true - :id "0x100" - :unread-messages? false - :mentions-count 0} - {:name "chat2" - :emoji nil - :position 2 - :muted? nil - :locked? nil - :id "0x200" - :unread-messages? false - :mentions-count 0}]}]] - (rf/sub [sub-name "0x1"])))) - + (match? [["2" + {:id "2" + :name "category2" + :collapsed? nil + :position 1 + :chats [{:name "chat3" + :position 3 + :emoji nil + :muted? nil + :locked? false + :id "0x3" + :unread-messages? false + :mentions-count 0}]}] + ["1" + {:id "1" + :name "category1" + :collapsed? nil + :position 2 + :chats [{:name "chat1" + :emoji nil + :position 1 + :muted? nil + :locked? nil + :id "0x1" + :unread-messages? false + :mentions-count 0} + {:name "chat2" + :emoji nil + :position 2 + :muted? nil + :locked? true + :id "0x2" + :unread-messages? false + :mentions-count 0}]}]] + (rf/sub [sub-name "0x1"])))) (testing "Channels without categories" (swap! rf-db/app-db assoc :communities @@ -274,7 +177,7 @@ :name "category2"}} :joined true}}) (is - (= + (match? [[constants/empty-category-id {:name (i18n/label :t/none) :collapsed? nil @@ -323,27 +226,27 @@ {"0x10x1" {:unviewed-messages-count 1 :unviewed-mentions-count 2} "0x10x2" {:unviewed-messages-count 0 :unviewed-mentions-count 0}}) (is - (= [["1" - {:name "category1" - :id "1" - :collapsed? nil - :chats [{:name "chat1" - :emoji nil - :position 1 - :locked? nil - :id "0x1" - :muted? nil - :unread-messages? true - :mentions-count 2} - {:name "chat2" - :emoji nil - :position 2 - :locked? nil - :muted? nil - :id "0x2" - :unread-messages? false - :mentions-count 0}]}]] - (rf/sub [sub-name "0x1"]))))) + (match? [["1" + {:name "category1" + :id "1" + :collapsed? nil + :chats [{:name "chat1" + :emoji nil + :position 1 + :locked? nil + :id "0x1" + :muted? nil + :unread-messages? true + :mentions-count 2} + {:name "chat2" + :emoji nil + :position 2 + :locked? nil + :muted? nil + :id "0x2" + :unread-messages? false + :mentions-count 0}]}]] + (rf/sub [sub-name "0x1"]))))) (h/deftest-sub :communities/my-pending-requests-to-join [sub-name] diff --git a/src/status_im/subs/root.cljs b/src/status_im/subs/root.cljs index 7f240f40c3..b2b789b3f6 100644 --- a/src/status_im/subs/root.cljs +++ b/src/status_im/subs/root.cljs @@ -141,7 +141,6 @@ (reg-root-key-sub :communities :communities) (reg-root-key-sub :communities/create :communities/create) (reg-root-key-sub :communities/create-channel :communities/create-channel) -(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/fetching-community :communities/fetching-community) diff --git a/status-go-version.json b/status-go-version.json index c619dd4bc8..386af1be0f 100644 --- a/status-go-version.json +++ b/status-go-version.json @@ -3,7 +3,7 @@ "_comment": "Instead use: scripts/update-status-go.sh ", "owner": "status-im", "repo": "status-go", - "version": "v0.176.8", - "commit-sha1": "8c0e24dc260031a822604f7a2771f22daea79aa4", - "src-sha256": "1c6r5gizcvixawl1c0lxd022wg54m5qxw8rr1q1i9221pzbfmmsl" + "version": "v0.176.9", + "commit-sha1": "a3ad05db58b8af20bf6b7ab852f8603212abac4e", + "src-sha256": "10c05kgihczbams3i8fkfm1mh5m61bnsjh255c22m4awc073rg12" }