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