cache processed description

This commit is contained in:
Andrea Maria Piana 2024-03-08 15:42:39 +00:00
parent 4fa9a27cd9
commit e002fab376
No known key found for this signature in database
12 changed files with 125 additions and 334 deletions

View File

@ -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))

View File

@ -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))))

View File

@ -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?

View File

@ -21,16 +21,11 @@
(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))))
previous-last-opened-at (get-in db [:communities id :last-opened-at])]
(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)))
@ -39,10 +34,8 @@
[: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]])]})))
[:dispatch [:communities/get-revealed-accounts id]])]}))))
(rf/reg-event-fx :communities/handle-community handle-community)

View File

@ -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))))))

View File

@ -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]]

View File

@ -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?

View File

@ -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])

View File

@ -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 {})

View File

@ -89,6 +89,7 @@
:name "chat1"
:muted? nil
:categoryID "1"
:token-gated? false
:can-post? true}
"0x2"
{:id "0x2"
@ -96,6 +97,7 @@
:name "chat2"
:muted? nil
:categoryID "1"
:token-gated? true
:can-post? false}
"0x3"
{:id "0x3"
@ -103,6 +105,7 @@
:name "chat3"
:muted? nil
:categoryID "2"
:token-gated? true
:can-post? true}}
:categories {"1" {:id "1"
:position 2
@ -112,7 +115,7 @@
:name "category2"}}
:joined true}})
(is
(= [["2"
(match? [["2"
{:id "2"
:name "category2"
:collapsed? nil
@ -121,7 +124,7 @@
:position 3
:emoji nil
:muted? nil
:locked? nil
:locked? false
:id "0x3"
:unread-messages? false
:mentions-count 0}]}]
@ -142,111 +145,11 @@
:emoji nil
:position 2
:muted? nil
:locked? nil
:locked? true
: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"]))))
(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,7 +226,7 @@
{"0x10x1" {:unviewed-messages-count 1 :unviewed-mentions-count 2}
"0x10x2" {:unviewed-messages-count 0 :unviewed-mentions-count 0}})
(is
(= [["1"
(match? [["1"
{:name "category1"
:id "1"
:collapsed? nil

View File

@ -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)

View File

@ -3,7 +3,7 @@
"_comment": "Instead use: scripts/update-status-go.sh <rev>",
"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"
}