0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-23 23:18:48 -05:00

Load workspace thumbnails in a separated request

This commit is contained in:
Andrey Antukh 2022-11-07 12:00:33 +01:00 committed by Andrés Moya
parent dfb73192b8
commit b20d2badfe
11 changed files with 83 additions and 66 deletions

View file

@ -90,7 +90,7 @@
where f.id = ?
and ppr.profile_id = ?")
(defn retrieve-file-permissions
(defn get-file-permissions
[conn profile-id file-id]
(when (and profile-id file-id)
(db/exec! conn [sql:file-permissions
@ -100,7 +100,7 @@
(defn get-permissions
([conn profile-id file-id]
(let [rows (retrieve-file-permissions conn profile-id file-id)
(let [rows (get-file-permissions conn profile-id file-id)
is-owner (boolean (some :is-owner rows))
is-admin (boolean (some :is-admin rows))
can-edit (boolean (some :can-edit rows))]
@ -154,7 +154,7 @@
;; --- HELPERS
(defn retrieve-team-id
(defn get-team-id
[conn project-id]
(:team-id (db/get-by-id conn :project project-id {:columns [:team-id]})))
@ -209,25 +209,7 @@
;; --- COMMAND QUERY: get-file (by id)
(defn retrieve-object-thumbnails
([conn file-id]
(let [sql (str/concat
"select object_id, data "
" from file_object_thumbnail"
" where file_id=?")]
(->> (db/exec! conn [sql file-id])
(d/index-by :object-id :data))))
([conn file-id object-ids]
(let [sql (str/concat
"select object_id, data "
" from file_object_thumbnail"
" where file_id=? and object_id = ANY(?)")
ids (db/create-array conn "text" (seq object-ids))]
(->> (db/exec! conn [sql file-id ids])
(d/index-by :object-id :data)))))
(defn retrieve-file
(defn get-file
[conn id client-features]
;; here we check if client requested features are supported
(check-features-compatibility! client-features)
@ -255,20 +237,10 @@
file)))
(defn get-file
[conn id features]
(let [file (retrieve-file conn id features)
thumbs (retrieve-object-thumbnails conn id)]
(assoc file :thumbnails thumbs)
#_file))
(s/def ::get-file
(s/keys :req-un [::profile-id ::id]
:opt-un [::features]))
;; TODO: this should be changed probably because thumbnails will not be included
(sv/defmethod ::get-file
"Retrieve a file by its ID. Only authenticated users."
{::doc/added "1.17"}
@ -280,6 +252,38 @@
(assoc :permissions perms)))))
;; --- COMMAND QUERY: get-file-object-thumbnails
(defn get-object-thumbnails
([conn file-id]
(let [sql (str/concat
"select object_id, data "
" from file_object_thumbnail"
" where file_id=?")]
(->> (db/exec! conn [sql file-id])
(d/index-by :object-id :data))))
([conn file-id object-ids]
(let [sql (str/concat
"select object_id, data "
" from file_object_thumbnail"
" where file_id=? and object_id = ANY(?)")
ids (db/create-array conn "text" (seq object-ids))]
(->> (db/exec! conn [sql file-id ids])
(d/index-by :object-id :data)))))
(s/def ::get-file-object-thumbnails
(s/keys :req-un [::profile-id ::file-id]))
(sv/defmethod ::get-file-object-thumbnails
"Retrieve a file object thumbnails."
{::doc/added "1.17"}
[{:keys [pool] :as cfg} {:keys [profile-id file-id] :as params}]
(with-open [conn (db/open pool)]
(check-read-permissions! conn profile-id file-id)
(get-object-thumbnails conn file-id)))
;; --- COMMAND QUERY: get-project-files
(def ^:private sql:project-files
@ -313,7 +317,7 @@
;; --- COMMAND QUERY: has-file-libraries
(declare retrieve-has-file-libraries)
(declare get-has-file-libraries)
(s/def ::file-id ::us/uuid)
(s/def ::profile-id ::us/uuid)
@ -327,7 +331,7 @@
[{:keys [pool] :as cfg} {:keys [profile-id file-id] :as params}]
(with-open [conn (db/open pool)]
(check-read-permissions! pool profile-id file-id)
(retrieve-has-file-libraries conn params)))
(get-has-file-libraries conn params)))
(def ^:private sql:has-file-libraries
"SELECT COUNT(*) > 0 AS has_libraries
@ -337,7 +341,7 @@
AND (fl.deleted_at IS NULL OR
fl.deleted_at > now())")
(defn- retrieve-has-file-libraries
(defn- get-has-file-libraries
[conn {:keys [file-id]}]
(let [row (db/exec-one! conn [sql:has-file-libraries file-id])]
(:has-libraries row)))
@ -361,7 +365,7 @@
(defn get-page
[conn {:keys [file-id page-id object-id features]}]
(let [file (retrieve-file conn file-id features)
(let [file (get-file conn file-id features)
page-id (or page-id (-> file :data :pages first))
page (dm/get-in file [:data :pages-index page-id])]
(cond-> (prune-thumbnails page)
@ -656,7 +660,7 @@
frame-ids (if (some? frame) (list frame-id) (map :id (ctt/get-frames (:objects page))))
obj-ids (map #(str page-id %) frame-ids)
thumbs (retrieve-object-thumbnails conn id obj-ids)]
thumbs (get-object-thumbnails conn id obj-ids)]
(cond-> page
;; If we have frame, we need to specify it on the page level
@ -681,7 +685,7 @@
[{:keys [pool] :as cfg} {:keys [profile-id file-id features] :as props}]
(with-open [conn (db/open pool)]
(check-read-permissions! conn profile-id file-id)
(let [file (retrieve-file conn file-id features)]
(let [file (get-file conn file-id features)]
{:file-id file-id
:revn (:revn file)
:page (get-file-data-for-thumbnail conn file)})))

View file

@ -77,7 +77,7 @@
[{:keys [pool] :as cfg} {:keys [profile-id project-id] :as params}]
(db/with-atomic [conn pool]
(proj/check-edition-permissions! conn profile-id project-id)
(let [team-id (files/retrieve-team-id conn project-id)]
(let [team-id (files/get-team-id conn project-id)]
(-> (create-file conn params)
(vary-meta assoc ::audit/props {:team-id team-id})))))

View file

@ -282,7 +282,7 @@
(when (and (:is-shared file) (seq lchanges))
(let [team-id (or (:team-id file)
(files/retrieve-team-id conn (:project-id file)))]
(files/get-team-id conn (:project-id file)))]
;; Asynchronously publish message to the msgbus
(mbus/pub! msgbus
:topic team-id

View file

@ -23,18 +23,19 @@
(defn- get-bundle
[conn file-id profile-id features]
(let [file (files/get-file conn file-id features)
project (get-project conn (:project-id file))
libs (files/get-file-libraries conn false file-id)
users (comments/get-file-comments-users conn file-id profile-id)
(let [file (files/get-file conn file-id features)
thumbnails (files/get-object-thumbnails conn file-id)
project (get-project conn (:project-id file))
libs (files/get-file-libraries conn false file-id)
users (comments/get-file-comments-users conn file-id profile-id)
links (->> (db/query conn :share-link {:file-id file-id})
(mapv slnk/decode-share-link-row))
links (->> (db/query conn :share-link {:file-id file-id})
(mapv slnk/decode-share-link-row))
fonts (db/query conn :team-font-variant
{:team-id (:team-id project)
:deleted-at nil})]
{:file file
fonts (db/query conn :team-font-variant
{:team-id (:team-id project)
:deleted-at nil})]
{:file (assoc file :thumbnails thumbnails)
:users users
:fonts fonts
:project project
@ -45,7 +46,7 @@
[conn {:keys [profile-id file-id share-id features] :as params}]
(let [slink (slnk/retrieve-share-link conn file-id share-id)
perms (files/get-permissions conn profile-id file-id share-id)
thumbs (files/retrieve-object-thumbnails conn file-id)
thumbs (files/get-object-thumbnails conn file-id)
bundle (-> (get-bundle conn file-id profile-id features)
(assoc :permissions perms)
(assoc-in [:file :thumbnails] thumbs))]

View file

@ -33,7 +33,7 @@
[{:keys [pool] :as cfg} {:keys [profile-id project-id features components-v2] :as params}]
(db/with-atomic [conn pool]
(proj/check-edition-permissions! conn profile-id project-id)
(let [team-id (cmd.files/retrieve-team-id conn project-id)
(let [team-id (cmd.files/get-team-id conn project-id)
features (cond-> (or features #{})
;; BACKWARD COMPATIBILITY with the components-v2 param
components-v2 (conj "components/v2"))

View file

@ -36,6 +36,12 @@
(s/and ::cmd.files/get-file
(s/keys :opt-un [::components-v2])))
(defn get-file
[conn id features]
(let [file (cmd.files/get-file conn id features)
thumbs (cmd.files/get-object-thumbnails conn id)]
(assoc file :thumbnails thumbs)))
(sv/defmethod ::file
"Retrieve a file by its ID. Only authenticated users."
{::doc/added "1.0"
@ -48,7 +54,7 @@
components-v2 (conj "components/v2"))]
(cmd.files/check-read-permissions! perms)
(-> (cmd.files/get-file conn id features)
(-> (get-file conn id features)
(assoc :permissions perms)))))
;; --- QUERY: page
@ -96,7 +102,7 @@
(let [;; BACKWARD COMPATIBILTY with the components-v2 parameter
features (cond-> (or features #{})
components-v2 (conj "components/v2"))
file (cmd.files/retrieve-file conn file-id features)]
file (cmd.files/get-file conn file-id features)]
{:file-id file-id
:revn (:revn file)
:page (cmd.files/get-file-data-for-thumbnail conn file)})))

View file

@ -132,7 +132,8 @@
(->> (rx/of bundle)
(rx/mapcat
(fn [bundle]
(let [bundle (assoc bundle :file (t/decode-str (:file-raw bundle)))
(let [file (-> bundle :file-raw t/decode-str)
bundle (assoc bundle :file file)
team-id (dm/get-in bundle [:project :team-id])]
(rx/merge
(rx/of (dwn/initialize team-id file-id)
@ -148,7 +149,7 @@
(unchecked-set ug/global "name" name)))))
(defn- file-initialized
[{:keys [file users project libraries file-comments-users] :as bundle}]
[{:keys [file thumbnails users project libraries file-comments-users] :as bundle}]
(ptk/reify ::file-initialized
ptk/UpdateEvent
(update [_ state]
@ -158,6 +159,7 @@
:workspace-undo {}
:workspace-project project
:workspace-file (assoc file :initialized true)
:workspace-thumbnails thumbnails
:workspace-data (-> (:data file)
(ctst/start-object-indices)
;; DEBUG: Uncomment this to try out migrations in local without changing

View file

@ -292,13 +292,15 @@
(features/active-feature? state :components-v2)
(conj "components/v2"))]
(->> (rx/zip (rp/cmd! :get-raw-file {:id file-id :features features})
(rp/cmd! :get-file-object-thumbnails {:file-id file-id})
(rp/query! :team-users {:file-id file-id})
(rp/query! :project {:id project-id})
(rp/cmd! :get-file-libraries {:file-id file-id})
(rp/cmd! :get-profiles-for-file-comments {:file-id file-id :share-id share-id}))
(rx/take 1)
(rx/map (fn [[file-raw users project libraries file-comments-users]]
(rx/map (fn [[file-raw thumbnails users project libraries file-comments-users]]
{:file-raw file-raw
:thumbnails thumbnails
:users users
:project project
:libraries libraries

View file

@ -52,7 +52,7 @@
ptk/UpdateEvent
(update [_ state]
(let [object-id (dm/str page-id frame-id)]
(assoc-in state [:workspace-file :thumbnails object-id] nil)))))
(update state :workspace-thumbnails dissoc object-id)))))
(defn update-thumbnail
"Updates the thumbnail information for the given frame `id`"
@ -63,8 +63,8 @@
(ptk/reify ::update-thumbnail
ptk/WatchEvent
(watch [_ state _]
(let [object-id (dm/str page-id frame-id)
file-id (or file-id (:current-file-id state))
(let [object-id (dm/str page-id frame-id)
file-id (or file-id (:current-file-id state))
blob-result (thumbnail-stream object-id)]
(->> blob-result
@ -80,7 +80,7 @@
(let [params {:file-id file-id :object-id object-id :data data}]
(rx/merge
;; Update the local copy of the thumbnails so we don't need to request it again
(rx/of #(assoc-in % [:workspace-file :thumbnails object-id] data))
(rx/of #(update % :workspace-thumbnails assoc object-id data))
(->> (rp/cmd! :upsert-file-object-thumbnail params)
(rx/ignore))))
@ -169,6 +169,6 @@
(ptk/reify ::duplicate-thumbnail
ptk/UpdateEvent
(update [_ state]
(let [page-id (get state :current-page-id)
old-shape-thumbnail (get-in state [:workspace-file :thumbnails (dm/str page-id old-id)])]
(-> state (assoc-in [:workspace-file :thumbnails (dm/str page-id new-id)] old-shape-thumbnail))))))
(let [page-id (:current-page-id state)
thumbnail (dm/get-in state [:workspace-thumbnails (dm/str page-id old-id)])]
(update state :workspace-thumbnails assoc (dm/str page-id new-id) thumbnail)))))

View file

@ -408,7 +408,7 @@
st/state))
(def thumbnail-data
(l/derived #(dm/get-in % [:workspace-file :thumbnails] {}) st/state))
(l/derived #(get % :workspace-thumbnails {}) st/state))
(defn thumbnail-frame-data
[page-id frame-id]
@ -434,7 +434,7 @@
(defn get-flex-child-viewer?
[ids page-id]
(l/derived
(fn [state]
(fn [state]
(let [objects (wsh/lookup-viewer-objects state page-id)]
(filterv #(= :flex (:layout (cph/get-parent objects %))) ids)))
st/state =))

View file

@ -13,6 +13,8 @@
[beicon.core :as rx]))
(derive :get-file ::query)
(derive :get-file-object-thumbnails ::query)
(derive :get-file-libraries ::query)
(defn handle-response
[{:keys [status body] :as response}]