Improve input validation in plugins

This commit is contained in:
alonso.torres 2024-06-14 15:19:57 +02:00
parent e13d543dcd
commit c5c8be4b4a
10 changed files with 120 additions and 40 deletions

View file

@ -26,7 +26,7 @@
[app.plugins.page :as page] [app.plugins.page :as page]
[app.plugins.shape :as shape] [app.plugins.shape :as shape]
[app.plugins.user :as user] [app.plugins.user :as user]
[app.plugins.utils :as utils] [app.plugins.utils :as u]
[app.plugins.viewport :as viewport] [app.plugins.viewport :as viewport]
[app.util.object :as obj] [app.util.object :as obj]
[beicon.v2.core :as rx] [beicon.v2.core :as rx]
@ -61,7 +61,7 @@
(getViewport (getViewport
[_] [_]
(viewport/create-proxy $plugin)) (viewport/viewport-proxy $plugin))
(getFile (getFile
[_] [_]
@ -108,28 +108,50 @@
(uploadMediaUrl (uploadMediaUrl
[_ name url] [_ name url]
(let [file-id (:current-file-id @st/state)] (cond
(p/create (not (string? name))
(fn [resolve reject] (u/display-not-valid :uploadMedia-name name)
(->> (dwm/upload-media-url name file-id url)
(rx/map utils/to-js) (not (string? url))
(rx/take 1) (u/display-not-valid :uploadMedia-url url)
(rx/subs! resolve reject))))))
:else
(let [file-id (:current-file-id @st/state)]
(p/create
(fn [resolve reject]
(->> (dwm/upload-media-url name file-id url)
(rx/map u/to-js)
(rx/take 1)
(rx/subs! resolve reject)))))))
(group (group
[_ shapes] [_ shapes]
(let [file-id (:current-file-id @st/state) (cond
page-id (:current-page-id @st/state) (or (not (array? shapes)) (not (every? shape/shape-proxy? shapes)))
id (uuid/next) (u/display-not-valid :group-shapes shapes)
ids (into #{} (map #(obj/get % "$id")) shapes)]
(st/emit! (dwg/group-shapes id ids)) :else
(shape/shape-proxy $plugin file-id page-id id))) (let [file-id (:current-file-id @st/state)
page-id (:current-page-id @st/state)
id (uuid/next)
ids (into #{} (map #(obj/get % "$id")) shapes)]
(st/emit! (dwg/group-shapes id ids))
(shape/shape-proxy $plugin file-id page-id id))))
(ungroup (ungroup
[_ group & rest] [_ group & rest]
(let [shapes (concat [group] rest)
ids (into #{} (map #(obj/get % "$id")) shapes)] (cond
(st/emit! (dwg/ungroup-shapes ids)))) (not (shape/shape-proxy? group))
(u/display-not-valid :ungroup group)
(and (some? rest) (not (every? shape/shape-proxy? rest)))
(u/display-not-valid :ungroup rest)
:else
(let [shapes (concat [group] rest)
ids (into #{} (map #(obj/get % "$id")) shapes)]
(st/emit! (dwg/ungroup-shapes ids)))))
(createFrame (createFrame
[_] [_]
@ -161,23 +183,32 @@
(createText (createText
[_ text] [_ text]
(let [file-id (:current-file-id @st/state) (cond
page-id (:current-page-id @st/state) (or (not (string? text)) (empty? text))
page (dm/get-in @st/state [:workspace-data :pages-index page-id]) (u/display-not-valid :createText text)
shape (-> (cts/setup-shape {:type :text :x 0 :y 0 :grow-type :auto-width})
(txt/change-text text) :else
(assoc :position-data nil)) (let [file-id (:current-file-id @st/state)
changes page-id (:current-page-id @st/state)
(-> (cb/empty-changes) page (dm/get-in @st/state [:workspace-data :pages-index page-id])
(cb/with-page page) shape (-> (cts/setup-shape {:type :text :x 0 :y 0 :grow-type :auto-width})
(cb/with-objects (:objects page)) (txt/change-text text)
(cb/add-object shape))] (assoc :position-data nil))
(st/emit! (ch/commit-changes changes)) changes
(shape/shape-proxy $plugin file-id page-id (:id shape)))) (-> (cb/empty-changes)
(cb/with-page page)
(cb/with-objects (:objects page))
(cb/add-object shape))]
(st/emit! (ch/commit-changes changes))
(shape/shape-proxy $plugin file-id page-id (:id shape)))))
(createShapeFromSvg (createShapeFromSvg
[_ svg-string] [_ svg-string]
(when (some? svg-string) (cond
(not (string? svg-string))
(u/display-not-valid :createShapeFromSvg svg-string)
:else
(let [id (uuid/next) (let [id (uuid/next)
file-id (:current-file-id @st/state) file-id (:current-file-id @st/state)
page-id (:current-page-id @st/state)] page-id (:current-page-id @st/state)]
@ -185,15 +216,19 @@
(shape/shape-proxy $plugin file-id page-id id)))) (shape/shape-proxy $plugin file-id page-id id))))
(createBoolean [_ bool-type shapes] (createBoolean [_ bool-type shapes]
(let [ids (into #{} (map #(obj/get % "$id")) shapes) (let [bool-type (keyword bool-type)]
bool-type (keyword bool-type)] (cond
(not (contains? cts/bool-types bool-type))
(u/display-not-valid :createBoolean-boolType bool-type)
(if (contains? cts/bool-types bool-type) (or (not (array? shapes)) (empty? shapes) (not (every? shape/shape-proxy? shapes)))
(let [id-ret (atom nil)] (u/display-not-valid :createBoolean-shapes shapes)
:else
(let [ids (into #{} (map #(obj/get % "$id")) shapes)
id-ret (atom nil)]
(st/emit! (dwb/create-bool bool-type ids {:id-ret id-ret})) (st/emit! (dwb/create-bool bool-type ids {:id-ret id-ret}))
(shape/shape-proxy $plugin @id-ret)) (shape/shape-proxy $plugin @id-ret))))))
(utils/display-not-valid :bool-shape bool-type)))))
(defn create-context (defn create-context
[plugin-id] [plugin-id]

View file

@ -93,6 +93,9 @@
{:name js/Symbol.toStringTag {:name js/Symbol.toStringTag
:get (fn [] (str "FileProxy"))}) :get (fn [] (str "FileProxy"))})
(defn file-proxy? [p]
(instance? FileProxy p))
(defn file-proxy (defn file-proxy
[plugin-id id] [plugin-id id]
(crc/add-properties! (crc/add-properties!

View file

@ -29,6 +29,9 @@
(st/emit! (dwt/move-shapes-to-frame #{child-id} $id nil nil) (st/emit! (dwt/move-shapes-to-frame #{child-id} $id nil nil)
(ptk/data-event :layout/update {:ids [$id]}))))) (ptk/data-event :layout/update {:ids [$id]})))))
(defn flex-layout-proxy? [p]
(instance? FlexLayout p))
(defn flex-layout-proxy (defn flex-layout-proxy
[plugin-id file-id page-id id] [plugin-id file-id page-id id]
(-> (FlexLayout. plugin-id file-id page-id id) (-> (FlexLayout. plugin-id file-id page-id id)
@ -151,6 +154,9 @@
(deftype LayoutChildProxy [$plugin $file $page $id]) (deftype LayoutChildProxy [$plugin $file $page $id])
(defn layout-child-proxy? [p]
(instance? LayoutChildProxy p))
(defn layout-child-proxy (defn layout-child-proxy
[plugin-id file-id page-id id] [plugin-id file-id page-id id]
(-> (LayoutChildProxy. plugin-id file-id page-id id) (-> (LayoutChildProxy. plugin-id file-id page-id id)

View file

@ -39,6 +39,9 @@
:font-weight (d/nilv (obj/get variant "fontWeight") fontWeight)}] :font-weight (d/nilv (obj/get variant "fontWeight") fontWeight)}]
(st/emit! (dwt/update-text-range id start end values))))) (st/emit! (dwt/update-text-range id start end values)))))
(defn font-proxy? [p]
(instance? PenpotFont p))
(defn font-proxy (defn font-proxy
[{:keys [id name variants] :as font}] [{:keys [id name variants] :as font}]
(when (some? font) (when (some? font)

View file

@ -74,6 +74,9 @@
(st/emit! (dwt/move-shapes-to-frame #{child-id} $id nil [row column]) (st/emit! (dwt/move-shapes-to-frame #{child-id} $id nil [row column])
(ptk/data-event :layout/update {:ids [$id]}))))) (ptk/data-event :layout/update {:ids [$id]})))))
(defn grid-layout-proxy? [p]
(instance? GridLayout p))
(defn grid-layout-proxy (defn grid-layout-proxy
[plugin-id file-id page-id id] [plugin-id file-id page-id id]
(-> (GridLayout. plugin-id file-id page-id id) (-> (GridLayout. plugin-id file-id page-id id)
@ -199,6 +202,9 @@
(deftype GridCellProxy [$plugin $file $page $id]) (deftype GridCellProxy [$plugin $file $page $id])
(defn layout-cell-proxy? [p]
(instance? GridCellProxy p))
(defn layout-cell-proxy (defn layout-cell-proxy
[plugin-id file-id page-id id] [plugin-id file-id page-id id]
(letfn [(locate-cell [_] (letfn [(locate-cell [_]

View file

@ -137,6 +137,9 @@
(let [color (u/proxy->library-color self)] (let [color (u/proxy->library-color self)]
(apply array (keys (dm/get-in color [:plugin-data (keyword "shared" namespace)]))))))) (apply array (keys (dm/get-in color [:plugin-data (keyword "shared" namespace)])))))))
(defn lib-color-proxy? [p]
(instance? LibraryColorProxy p))
(defn lib-color-proxy (defn lib-color-proxy
[plugin-id file-id id] [plugin-id file-id id]
(assert (uuid? file-id)) (assert (uuid? file-id))
@ -316,6 +319,9 @@
(let [typography (u/proxy->library-typography self)] (let [typography (u/proxy->library-typography self)]
(apply array (keys (dm/get-in typography [:plugin-data (keyword "shared" namespace)]))))))) (apply array (keys (dm/get-in typography [:plugin-data (keyword "shared" namespace)])))))))
(defn lib-typography-proxy? [p]
(instance? LibraryTypographyProxy p))
(defn lib-typography-proxy (defn lib-typography-proxy
[plugin-id file-id id] [plugin-id file-id id]
(assert (uuid? file-id)) (assert (uuid? file-id))
@ -522,6 +528,9 @@
(let [component (u/proxy->library-component self)] (let [component (u/proxy->library-component self)]
(apply array (keys (dm/get-in component [:plugin-data (keyword "shared" namespace)]))))))) (apply array (keys (dm/get-in component [:plugin-data (keyword "shared" namespace)])))))))
(defn lib-component-proxy? [p]
(instance? LibraryComponentProxy p))
(defn lib-component-proxy (defn lib-component-proxy
[plugin-id file-id id] [plugin-id file-id id]
(assert (uuid? file-id)) (assert (uuid? file-id))
@ -643,6 +652,9 @@
(let [file (u/proxy->file self)] (let [file (u/proxy->file self)]
(apply array (keys (dm/get-in file [:data :plugin-data (keyword "shared" namespace)]))))))) (apply array (keys (dm/get-in file [:data :plugin-data (keyword "shared" namespace)])))))))
(defn library-proxy? [p]
(instance? Library p))
(defn library-proxy (defn library-proxy
[plugin-id file-id] [plugin-id file-id]
(assert (uuid? file-id) "File id not valid") (assert (uuid? file-id) "File id not valid")

View file

@ -107,6 +107,9 @@
{:name js/Symbol.toStringTag {:name js/Symbol.toStringTag
:get (fn [] (str "PageProxy"))}) :get (fn [] (str "PageProxy"))})
(defn page-proxy? [p]
(instance? PageProxy p))
(defn page-proxy (defn page-proxy
[plugin-id file-id id] [plugin-id file-id id]
(crc/add-properties! (crc/add-properties!

View file

@ -380,6 +380,9 @@
{:name js/Symbol.toStringTag {:name js/Symbol.toStringTag
:get (fn [] (str "ShapeProxy"))}) :get (fn [] (str "ShapeProxy"))})
(defn shape-proxy? [p]
(instance? ShapeProxy p))
(defn shape-proxy (defn shape-proxy
([plugin-id id] ([plugin-id id]
(shape-proxy plugin-id (:current-file-id @st/state) (:current-page-id @st/state) id)) (shape-proxy plugin-id (:current-file-id @st/state) (:current-page-id @st/state) id))

View file

@ -38,11 +38,17 @@
{:name "sessionId" {:name "sessionId"
:get (fn [_] (str session-id))}))) :get (fn [_] (str session-id))})))
(defn current-user-proxy? [p]
(instance? CurrentUserProxy p))
(defn current-user-proxy (defn current-user-proxy
[plugin-id session-id] [plugin-id session-id]
(-> (CurrentUserProxy. plugin-id session-id) (-> (CurrentUserProxy. plugin-id session-id)
(add-user-properties))) (add-user-properties)))
(defn active-user-proxy? [p]
(instance? ActiveUserProxy p))
(defn active-user-proxy (defn active-user-proxy
[plugin-id session-id] [plugin-id session-id]
(-> (ActiveUserProxy. plugin-id session-id) (-> (ActiveUserProxy. plugin-id session-id)

View file

@ -32,7 +32,10 @@
{:name js/Symbol.toStringTag {:name js/Symbol.toStringTag
:get (fn [] (str "ViewportProxy"))}) :get (fn [] (str "ViewportProxy"))})
(defn create-proxy (defn viewport-proxy? [p]
(instance? ViewportProxy p))
(defn viewport-proxy
[plugin-id] [plugin-id]
(crc/add-properties! (crc/add-properties!
(ViewportProxy. plugin-id) (ViewportProxy. plugin-id)