From 2cddc4946311d7e80ad667287d80e3f77f9bb768 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Fri, 3 Mar 2023 14:07:43 +0100 Subject: [PATCH] :zap: Remove several reflection calls --- backend/src/app/email.clj | 2 ++ backend/src/app/rpc/commands/comments.clj | 10 +++++----- backend/src/app/rpc/commands/files.clj | 24 +++++++++++------------ backend/src/app/rpc/commands/projects.clj | 7 ++++--- backend/src/app/rpc/commands/viewer.clj | 3 ++- backend/src/app/rpc/commands/webhooks.clj | 11 +++++------ backend/src/app/util/time.clj | 23 ++++++++++++++++++++-- common/src/app/common/file_builder.cljc | 14 ++++++------- common/src/app/common/media.cljc | 2 +- common/src/app/common/text.cljc | 2 +- common/test/common_tests/types_test.cljc | 16 +++++++-------- common/test/common_tests/uuid_test.cljc | 4 +--- 12 files changed, 69 insertions(+), 49 deletions(-) diff --git a/backend/src/app/email.clj b/backend/src/app/email.clj index 40958cb81..94e0010d2 100644 --- a/backend/src/app/email.clj +++ b/backend/src/app/email.clj @@ -37,6 +37,7 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defn- parse-address + ^"[Ljakarta.mail.internet.InternetAddress;" [v] (InternetAddress/parse ^String v)) @@ -149,6 +150,7 @@ "mail.smtp.connectiontimeout" timeout})) (defn- create-smtp-session + ^Session [cfg] (let [props (opts->props cfg)] (Session/getInstance props))) diff --git a/backend/src/app/rpc/commands/comments.clj b/backend/src/app/rpc/commands/comments.clj index 44366b894..c7c9b1606 100644 --- a/backend/src/app/rpc/commands/comments.clj +++ b/backend/src/app/rpc/commands/comments.clj @@ -101,7 +101,7 @@ (sv/defmethod ::get-comment-threads {::doc/added "1.15"} [{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id file-id share-id] :as params}] - (with-open [conn (db/open pool)] + (dm/with-open [conn (db/open pool)] (files/check-comment-permissions! conn profile-id file-id share-id) (get-comment-threads conn profile-id file-id))) @@ -144,7 +144,7 @@ (sv/defmethod ::get-unread-comment-threads {::doc/added "1.15"} [{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id team-id] :as params}] - (with-open [conn (db/open pool)] + (dm/with-open [conn (db/open pool)] (teams/check-read-permissions! conn profile-id team-id) (get-unread-comment-threads conn profile-id team-id))) @@ -191,7 +191,7 @@ (sv/defmethod ::get-comment-thread {::doc/added "1.15"} [{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id file-id id share-id] :as params}] - (with-open [conn (db/open pool)] + (dm/with-open [conn (db/open pool)] (files/check-comment-permissions! conn profile-id file-id share-id) (let [sql (str "with threads as (" sql:comment-threads ")" "select * from threads where id = ?")] @@ -211,7 +211,7 @@ (sv/defmethod ::get-comments {::doc/added "1.15"} [{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id thread-id share-id] :as params}] - (with-open [conn (db/open pool)] + (dm/with-open [conn (db/open pool)] (let [{:keys [file-id] :as thread} (get-comment-thread conn thread-id)] (files/check-comment-permissions! conn profile-id file-id share-id) (get-comments conn thread-id)))) @@ -263,7 +263,7 @@ {::doc/added "1.15" ::doc/changes ["1.15" "Imported from queries and renamed."]} [{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id file-id share-id]}] - (with-open [conn (db/open pool)] + (dm/with-open [conn (db/open pool)] (files/check-comment-permissions! conn profile-id file-id share-id) (get-file-comments-users conn file-id profile-id))) diff --git a/backend/src/app/rpc/commands/files.clj b/backend/src/app/rpc/commands/files.clj index 079df2321..719f63598 100644 --- a/backend/src/app/rpc/commands/files.clj +++ b/backend/src/app/rpc/commands/files.clj @@ -277,7 +277,7 @@ ::cond/get-object #(get-minimal-file %1 (:id %2)) ::cond/key-fn get-file-etag} [{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id id features]}] - (with-open [conn (db/open pool)] + (dm/with-open [conn (db/open pool)] (let [perms (get-permissions conn profile-id id)] (check-read-permissions! perms) (let [file (-> (get-file conn id features) @@ -305,7 +305,7 @@ {::doc/added "1.17" ::rpc/:auth false} [{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id file-id fragment-id share-id] }] - (with-open [conn (db/open pool)] + (dm/with-open [conn (db/open pool)] (let [perms (get-permissions conn profile-id file-id share-id)] (check-read-permissions! perms) (-> (get-file-fragment conn file-id fragment-id) @@ -341,7 +341,7 @@ ::cond/reuse-key? true ::cond/key-fn get-file-etag} [{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id file-id] :as params}] - (with-open [conn (db/open pool)] + (dm/with-open [conn (db/open pool)] (check-read-permissions! conn profile-id file-id) (get-object-thumbnails conn file-id))) @@ -372,7 +372,7 @@ "Get all files for the specified project." {::doc/added "1.17"} [{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id project-id]}] - (with-open [conn (db/open pool)] + (dm/with-open [conn (db/open pool)] (projects/check-read-permissions! conn profile-id project-id) (get-project-files conn project-id))) @@ -391,7 +391,7 @@ "Checks if the file has libraries. Returns a boolean" {::doc/added "1.15.1"} [{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id file-id]}] - (with-open [conn (db/open pool)] + (dm/with-open [conn (db/open pool)] (check-read-permissions! pool profile-id file-id) (get-has-file-libraries conn file-id))) @@ -458,7 +458,7 @@ Mainly used for rendering purposes." {::doc/added "1.17"} [{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id file-id] :as params}] - (with-open [conn (db/open pool)] + (dm/with-open [conn (db/open pool)] (check-read-permissions! conn profile-id file-id) (get-page conn params))) @@ -511,7 +511,7 @@ "Get all file (libraries) for the specified team." {::doc/added "1.17"} [{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id team-id]}] - (with-open [conn (db/open pool)] + (dm/with-open [conn (db/open pool)] (teams/check-read-permissions! conn profile-id team-id) (get-team-shared-files conn team-id))) @@ -565,7 +565,7 @@ "Get libraries used by the specified file." {::doc/added "1.17"} [{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id file-id features]}] - (with-open [conn (db/open pool)] + (dm/with-open [conn (db/open pool)] (check-read-permissions! conn profile-id file-id) (get-file-libraries conn file-id features))) @@ -591,7 +591,7 @@ "Returns all the file references that use specified file (library) id." {::doc/added "1.17"} [{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id file-id] :as params}] - (with-open [conn (db/open pool)] + (dm/with-open [conn (db/open pool)] (check-read-permissions! conn profile-id file-id) (get-library-file-references conn file-id))) @@ -628,7 +628,7 @@ (sv/defmethod ::get-team-recent-files {::doc/added "1.17"} [{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id team-id]}] - (with-open [conn (db/open pool)] + (dm/with-open [conn (db/open pool)] (teams/check-read-permissions! conn profile-id team-id) (get-team-recent-files conn team-id))) @@ -662,7 +662,7 @@ (sv/defmethod ::get-file-thumbnail {::doc/added "1.17"} [{:keys [::db/pool]} {:keys [::rpc/profile-id file-id revn]}] - (with-open [conn (db/open pool)] + (dm/with-open [conn (db/open pool)] (check-read-permissions! conn profile-id file-id) (-> (get-file-thumbnail conn file-id revn) (rph/with-http-cache long-cache-duration)))) @@ -758,7 +758,7 @@ mainly for render thumbnails on dashboard." {::doc/added "1.17"} [{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id file-id features] :as props}] - (with-open [conn (db/open pool)] + (dm/with-open [conn (db/open pool)] (check-read-permissions! conn profile-id file-id) ;; NOTE: we force here the "storage/pointer-map" feature, because ;; it used internally only and is independent if user supports it diff --git a/backend/src/app/rpc/commands/projects.clj b/backend/src/app/rpc/commands/projects.clj index f9555479d..ac1ce660e 100644 --- a/backend/src/app/rpc/commands/projects.clj +++ b/backend/src/app/rpc/commands/projects.clj @@ -6,6 +6,7 @@ (ns app.rpc.commands.projects (:require + [app.common.data.macros :as dm] [app.common.spec :as us] [app.db :as db] [app.loggers.audit :as-alias audit] @@ -79,7 +80,7 @@ (sv/defmethod ::get-projects {::doc/added "1.18"} [{:keys [::db/pool]} {:keys [::rpc/profile-id team-id]}] - (with-open [conn (db/open pool)] + (dm/with-open [conn (db/open pool)] (teams/check-read-permissions! conn profile-id team-id) (get-projects conn profile-id team-id))) @@ -114,7 +115,7 @@ (sv/defmethod ::get-all-projects {::doc/added "1.18"} [{:keys [::db/pool]} {:keys [::rpc/profile-id]}] - (with-open [conn (db/open pool)] + (dm/with-open [conn (db/open pool)] (get-all-projects conn profile-id))) (def sql:all-projects @@ -157,7 +158,7 @@ (sv/defmethod ::get-project {::doc/added "1.18"} [{:keys [::db/pool]} {:keys [::rpc/profile-id id]}] - (with-open [conn (db/open pool)] + (dm/with-open [conn (db/open pool)] (let [project (db/get-by-id conn :project id)] (check-read-permissions! conn profile-id id) project))) diff --git a/backend/src/app/rpc/commands/viewer.clj b/backend/src/app/rpc/commands/viewer.clj index 136bc309b..33cfdc72e 100644 --- a/backend/src/app/rpc/commands/viewer.clj +++ b/backend/src/app/rpc/commands/viewer.clj @@ -6,6 +6,7 @@ (ns app.rpc.commands.viewer (:require + [app.common.data.macros :as dm] [app.common.exceptions :as ex] [app.db :as db] [app.rpc :as-alias rpc] @@ -85,5 +86,5 @@ ::cond/reuse-key? true ::doc/added "1.17"} [{:keys [::db/pool]} {:keys [::rpc/profile-id] :as params}] - (with-open [conn (db/open pool)] + (dm/with-open [conn (db/open pool)] (get-view-only-bundle conn (assoc params :profile-id profile-id)))) diff --git a/backend/src/app/rpc/commands/webhooks.clj b/backend/src/app/rpc/commands/webhooks.clj index 14d452540..d07d9ca33 100644 --- a/backend/src/app/rpc/commands/webhooks.clj +++ b/backend/src/app/rpc/commands/webhooks.clj @@ -6,6 +6,7 @@ (ns app.rpc.commands.webhooks (:require + [app.common.data.macros :as dm] [app.common.exceptions :as ex] [app.common.spec :as us] [app.common.uri :as u] @@ -18,10 +19,8 @@ [app.rpc.doc :as-alias doc] [app.util.services :as sv] [app.util.time :as dt] - [app.worker :as-alias wrk] [clojure.spec.alpha :as s] - [cuerdas.core :as str] - [promesa.core :as p])) + [cuerdas.core :as str])) (defn decode-row [{:keys [uri] :as row}] @@ -102,7 +101,7 @@ (sv/defmethod ::create-webhook {::doc/added "1.17"} - [{:keys [::db/pool ::wrk/executor] :as cfg} {:keys [::rpc/profile-id team-id] :as params}] + [{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id team-id] :as params}] (check-edition-permissions! pool profile-id team-id) (validate-quotes! cfg params) (validate-webhook! cfg nil params) @@ -113,7 +112,7 @@ (sv/defmethod ::update-webhook {::doc/added "1.17"} - [{:keys [::db/pool ::wrk/executor] :as cfg} {:keys [::rpc/profile-id id] :as params}] + [{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id id] :as params}] (let [whook (-> (db/get pool :webhook {:id id}) (decode-row))] (check-edition-permissions! pool profile-id (:team-id whook)) (validate-webhook! cfg whook params) @@ -145,7 +144,7 @@ (sv/defmethod ::get-webhooks [{:keys [::db/pool] :as cfg} {:keys [::rpc/profile-id team-id]}] - (with-open [conn (db/open pool)] + (dm/with-open [conn (db/open pool)] (check-read-permissions! conn profile-id team-id) (->> (db/exec! conn [sql:get-webhooks team-id]) (mapv decode-row)))) diff --git a/backend/src/app/util/time.clj b/backend/src/app/util/time.clj index 8c9e4f099..a2cd237ad 100644 --- a/backend/src/app/util/time.clj +++ b/backend/src/app/util/time.clj @@ -19,6 +19,7 @@ java.time.ZonedDateTime java.time.format.DateTimeFormatter java.time.temporal.ChronoUnit + java.time.temporal.Temporal java.time.temporal.TemporalAmount java.time.temporal.TemporalUnit java.util.Date @@ -160,11 +161,29 @@ (defn plus [d ta] - (.plus d ^TemporalAmount (duration ta))) + (let [^TemporalAmount ta (duration ta)] + (cond + (instance? Duration d) + (.plus ^Duration d ta) + + (instance? Temporal d) + (.plus ^Temporal d ta) + + :else + (throw (UnsupportedOperationException. "unsupported type"))))) (defn minus [d ta] - (.minus d ^TemporalAmount (duration ta))) + (let [^TemporalAmount ta (duration ta)] + (cond + (instance? Duration d) + (.minus ^Duration d ta) + + (instance? Temporal d) + (.minus ^Temporal d ta) + + :else + (throw (UnsupportedOperationException. "unsupported type"))))) (defn now [] diff --git a/common/src/app/common/file_builder.cljc b/common/src/app/common/file_builder.cljc index e2d61fa43..2b7e2fc3f 100644 --- a/common/src/app/common/file_builder.cljc +++ b/common/src/app/common/file_builder.cljc @@ -8,6 +8,7 @@ "A version parsing helper." (:require [app.common.data :as d] + [app.common.exceptions :as ex] [app.common.geom.matrix :as gmt] [app.common.geom.point :as gpt] [app.common.geom.shapes :as gsh] @@ -28,11 +29,6 @@ (def conjv (fnil conj [])) (def conjs (fnil conj #{})) -(defn- raise - [err-str] - #?(:clj (throw (Exception. err-str)) - :cljs (throw (js/Error. err-str)))) - (defn- commit-change ([file change] (commit-change file change nil)) @@ -104,7 +100,9 @@ (defn setup-rect-selrect [{:keys [x y width height transform] :as obj}] (when-not (d/num? x y width height) - (raise "Coords not valid for object")) + (ex/raise :type :assertion + :code :invalid-condition + :hint "Coords not valid for object")) (let [rect (gsh/make-rect x y width height) center (gsh/center-rect rect) @@ -121,7 +119,9 @@ [{:keys [content center transform transform-inverse] :as obj}] (when (or (empty? content) (nil? center)) - (raise "Path not valid")) + (ex/raise :type :assertion + :code :invalid-condition + :hint "Path not valid")) (let [transform (gmt/transform-in center transform) transform-inverse (gmt/transform-in center transform-inverse) diff --git a/common/src/app/common/media.cljc b/common/src/app/common/media.cljc index 26574bd6a..064f11fb2 100644 --- a/common/src/app/common/media.cljc +++ b/common/src/app/common/media.cljc @@ -103,7 +103,7 @@ (defn font-weight->name [weight] - (case weight + (case (long weight) 100 "Hairline" 200 "Extra Light" 300 "Light" diff --git a/common/src/app/common/text.cljc b/common/src/app/common/text.cljc index aee191e1e..396f7ab46 100644 --- a/common/src/app/common/text.cljc +++ b/common/src/app/common/text.cljc @@ -157,7 +157,7 @@ (defn- code-points->text [cpoints start end] #?(:cljs (apply str (subvec cpoints start end)) - :clj (let [sb (StringBuilder. (- end start))] + :clj (let [sb (StringBuilder. (- ^long end ^long start))] (run! #(.appendCodePoint sb (int %)) (subvec cpoints start end)) (.toString sb)))) diff --git a/common/test/common_tests/types_test.cljc b/common/test/common_tests/types_test.cljc index 99dec9343..5ed39ab1b 100644 --- a/common/test/common_tests/types_test.cljc +++ b/common/test/common_tests/types_test.cljc @@ -17,43 +17,43 @@ [app.common.types.page :as ctp] [app.common.types.file :as ctf])) -(defspec transit-encode-decode-with-shape 30 +(defspec transit-encode-decode-with-shape 10 (props/for-all [fdata (s/gen ::cts/shape)] (let [res (-> fdata transit/encode-str transit/decode-str)] (t/is (= res fdata))))) -(defspec types-shape-spec 10 +(defspec types-shape-spec 5 (props/for-all [fdata (s/gen ::cts/shape)] (t/is (us/valid? ::cts/shape fdata)))) -(defspec types-page-spec 10 +(defspec types-page-spec 5 (props/for-all [fdata (s/gen ::ctp/page)] (t/is (us/valid? ::ctp/page fdata)))) -(defspec types-file-colors-spec 30 +(defspec types-file-colors-spec 10 (props/for-all [fdata (s/gen ::ctf/colors)] (t/is (us/valid? ::ctf/colors fdata)))) -(defspec types-file-recent-colors-spec 30 +(defspec types-file-recent-colors-spec 10 (props/for-all [fdata (s/gen ::ctf/recent-colors)] (t/is (us/valid? ::ctf/recent-colors fdata)))) -(defspec types-file-typographies-spec 30 +(defspec types-file-typographies-spec 10 (props/for-all [fdata (s/gen ::ctf/typographies)] (t/is (us/valid? ::ctf/typographies fdata)))) -(defspec types-file-media-spec 30 +(defspec types-file-media-spec 10 (props/for-all [fdata (s/gen ::ctf/media)] (t/is (us/valid? ::ctf/media fdata)))) -(defspec types-file-components-spec 10 +(defspec types-file-components-spec 1 (props/for-all [fdata (s/gen ::ctf/components)] (t/is (us/valid? ::ctf/components fdata)))) diff --git a/common/test/common_tests/uuid_test.cljc b/common/test/common_tests/uuid_test.cljc index e52b1f2e8..d189ec717 100644 --- a/common/test/common_tests/uuid_test.cljc +++ b/common/test/common_tests/uuid_test.cljc @@ -14,10 +14,8 @@ [clojure.test.check.generators :as gen] [clojure.test.check.properties :as props])) -(defspec non-repeating-uuid-next-1 5000 +(defspec non-repeating-uuid-next-1 100 (props/for-all [uuid1 (s/gen ::us/uuid) uuid2 (s/gen ::us/uuid)] (t/is (not= uuid1 uuid2)))) - -