0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-23 07:16:07 -05:00

💄 Remove neesting with ::db/transaction

This commit is contained in:
Andrey Antukh 2025-01-08 12:00:38 +01:00 committed by alonso.torres
parent 27c2db6cde
commit 1f4fafe781

View file

@ -238,7 +238,6 @@
{::doc/added "1.15" {::doc/added "1.15"
::sm/params schema:get-comment-threads} ::sm/params schema:get-comment-threads}
[cfg {:keys [::rpc/profile-id file-id share-id] :as params}] [cfg {:keys [::rpc/profile-id file-id share-id] :as params}]
(db/run! cfg (fn [{:keys [::db/conn]}] (db/run! cfg (fn [{:keys [::db/conn]}]
(files/check-comment-permissions! conn profile-id file-id share-id) (files/check-comment-permissions! conn profile-id file-id share-id)
(get-comment-threads conn profile-id file-id)))) (get-comment-threads conn profile-id file-id))))
@ -474,8 +473,7 @@
::rtry/enabled true ::rtry/enabled true
::rtry/when rtry/conflict-exception? ::rtry/when rtry/conflict-exception?
::sm/params schema:create-comment-thread} ::sm/params schema:create-comment-thread}
[cfg [cfg {:keys [::rpc/profile-id ::rpc/request-at file-id page-id share-id mentions position content frame-id]}]
{:keys [::rpc/profile-id ::rpc/request-at file-id page-id share-id mentions position content frame-id]}]
(files/check-comment-permissions! cfg profile-id file-id share-id) (files/check-comment-permissions! cfg profile-id file-id share-id)
(let [{:keys [team-id project-id page-name name]} (get-file cfg file-id page-id)] (let [{:keys [team-id project-id page-name name]} (get-file cfg file-id page-id)]
@ -569,12 +567,12 @@
(sv/defmethod ::update-comment-thread-status (sv/defmethod ::update-comment-thread-status
{::doc/added "1.15" {::doc/added "1.15"
::sm/params schema:update-comment-thread-status} ::sm/params schema:update-comment-thread-status
[cfg {:keys [::rpc/profile-id id share-id]}] ::db/transaction true}
(db/tx-run! cfg (fn [{:keys [::db/conn]}] [{:keys [::db/conn]} {:keys [::rpc/profile-id id share-id]}]
(let [{:keys [file-id] :as thread} (get-comment-thread conn id ::sql/for-update true)] (let [{:keys [file-id] :as thread} (get-comment-thread conn id ::sql/for-update true)]
(files/check-comment-permissions! conn profile-id file-id share-id) (files/check-comment-permissions! conn profile-id file-id share-id)
(upsert-comment-thread-status! conn profile-id id))))) (upsert-comment-thread-status! conn profile-id id)))
;; --- COMMAND: Update Comment Thread ;; --- COMMAND: Update Comment Thread
@ -587,16 +585,15 @@
(sv/defmethod ::update-comment-thread (sv/defmethod ::update-comment-thread
{::doc/added "1.15" {::doc/added "1.15"
::sm/params schema:update-comment-thread} ::sm/params schema:update-comment-thread
[cfg {:keys [::rpc/profile-id id is-resolved share-id]}] ::db/transaction true}
(db/tx-run! cfg (fn [{:keys [::db/conn]}] [{:keys [::db/conn]} {:keys [::rpc/profile-id id is-resolved share-id]}]
(let [{:keys [file-id] :as thread} (get-comment-thread conn id ::sql/for-update true)] (let [{:keys [file-id] :as thread} (get-comment-thread conn id ::sql/for-update true)]
(files/check-comment-permissions! conn profile-id file-id share-id) (files/check-comment-permissions! conn profile-id file-id share-id)
(db/update! conn :comment-thread (db/update! conn :comment-thread
{:is-resolved is-resolved} {:is-resolved is-resolved}
{:id id}) {:id id})
nil)))) nil))
;; --- COMMAND: Add Comment ;; --- COMMAND: Add Comment
@ -613,70 +610,68 @@
(sv/defmethod ::create-comment (sv/defmethod ::create-comment
{::doc/added "1.15" {::doc/added "1.15"
::webhooks/event? true ::webhooks/event? true
::sm/params schema:create-comment} ::sm/params schema:create-comment
[cfg {:keys [::rpc/profile-id ::rpc/request-at thread-id share-id content mentions]}] ::db/transaction true}
(db/tx-run! [{:keys [::db/conn] :as cfg} {:keys [::rpc/profile-id ::rpc/request-at thread-id share-id content mentions]}]
cfg (let [{:keys [file-id page-id] :as thread} (get-comment-thread conn thread-id ::sql/for-update true)
(fn [{:keys [::db/conn] :as cfg}] {file-name :name :keys [team-id project-id page-name] :as file} (get-file cfg file-id page-id)]
(let [{:keys [file-id page-id] :as thread} (get-comment-thread conn thread-id ::sql/for-update true)
{file-name :name :keys [team-id project-id page-name] :as file} (get-file cfg file-id page-id)]
(files/check-comment-permissions! conn profile-id file-id share-id) (files/check-comment-permissions! conn profile-id file-id share-id)
(quotes/check! cfg {::quotes/id ::quotes/comments-per-file (quotes/check! cfg {::quotes/id ::quotes/comments-per-file
::quotes/profile-id profile-id ::quotes/profile-id profile-id
::quotes/team-id team-id ::quotes/team-id team-id
::quotes/project-id project-id ::quotes/project-id project-id
::quotes/file-id file-id}) ::quotes/file-id file-id})
;; Update the page-name cached attribute on comment thread table. ;; Update the page-name cached attribute on comment thread table.
(when (not= page-name (:page-name thread)) (when (not= page-name (:page-name thread))
(db/update! conn :comment-thread (db/update! conn :comment-thread
{:page-name page-name} {:page-name page-name}
{:id thread-id})) {:id thread-id}))
(let [comment (-> (db/insert! (let [comment (-> (db/insert!
conn :comment conn :comment
{:id (uuid/next) {:id (uuid/next)
:created-at request-at :created-at request-at
:modified-at request-at :modified-at request-at
:thread-id thread-id :thread-id thread-id
:owner-id profile-id :owner-id profile-id
:content content :content content
:mentions :mentions
(-> mentions (-> mentions
(set) (set)
(db/encode-pgarray conn "uuid"))}) (db/encode-pgarray conn "uuid"))})
(decode-row)) (decode-row))
props {:file-id file-id props {:file-id file-id
:share-id nil}] :share-id nil}]
;; Update thread modified-at attribute and assoc the current ;; Update thread modified-at attribute and assoc the current
;; profile to the participant set. ;; profile to the participant set.
(db/update! conn :comment-thread (db/update! conn :comment-thread
{:modified-at request-at {:modified-at request-at
:participants (-> (:participants thread #{}) :participants (-> (:participants thread #{})
(conj profile-id) (conj profile-id)
(db/tjson)) (db/tjson))
:mentions (-> (:mentions thread) :mentions (-> (:mentions thread)
(set) (set)
(into mentions) (into mentions)
(db/encode-pgarray conn "uuid"))} (db/encode-pgarray conn "uuid"))}
{:id thread-id}) {:id thread-id})
;; Update the current profile status in relation to the ;; Update the current profile status in relation to the
;; current thread. ;; current thread.
(upsert-comment-thread-status! conn profile-id thread-id) (upsert-comment-thread-status! conn profile-id thread-id)
(let [params {:project-id project-id (let [params {:project-id project-id
:profile-id profile-id :profile-id profile-id
:team-id team-id :team-id team-id
:file-id (:file-id thread) :file-id (:file-id thread)
:page-id (:page-id thread) :page-id (:page-id thread)
:file-name file-name :file-name file-name
:page-name page-name}] :page-name page-name}]
(send-comment-emails! conn params comment thread)) (send-comment-emails! conn params comment thread))
(vary-meta comment assoc ::audit/props props)))))) (vary-meta comment assoc ::audit/props props))))
;; --- COMMAND: Update Comment ;; --- COMMAND: Update Comment
@ -691,37 +686,36 @@
;; TODO Check if there are new mentions, if there are send the new emails. ;; TODO Check if there are new mentions, if there are send the new emails.
(sv/defmethod ::update-comment (sv/defmethod ::update-comment
{::doc/added "1.15" {::doc/added "1.15"
::sm/params schema:update-comment} ::sm/params schema:update-comment
[cfg {:keys [::rpc/profile-id ::rpc/request-at id share-id content mentions]}] ::db/transaction true}
(db/tx-run! cfg [{:keys [::db/conn] :as cfg} {:keys [::rpc/profile-id ::rpc/request-at id share-id content mentions]}]
(fn [{:keys [::db/conn] :as cfg}] (let [{:keys [thread-id owner-id] :as comment} (get-comment conn id ::sql/for-update true)
(let [{:keys [thread-id owner-id] :as comment} (get-comment conn id ::sql/for-update true) {:keys [file-id page-id] :as thread} (get-comment-thread conn thread-id ::sql/for-update true)]
{:keys [file-id page-id] :as thread} (get-comment-thread conn thread-id ::sql/for-update true)]
(files/check-comment-permissions! conn profile-id file-id share-id) (files/check-comment-permissions! conn profile-id file-id share-id)
;; Don't allow edit comments to not owners ;; Don't allow edit comments to not owners
(when-not (= owner-id profile-id) (when-not (= owner-id profile-id)
(ex/raise :type :validation (ex/raise :type :validation
:code :not-allowed)) :code :not-allowed))
(let [{:keys [page-name]} (get-file cfg file-id page-id)] (let [{:keys [page-name]} (get-file cfg file-id page-id)]
(db/update! conn :comment (db/update! conn :comment
{:content content {:content content
:modified-at request-at :modified-at request-at
:mentions (db/encode-pgarray mentions conn "uuid")} :mentions (db/encode-pgarray mentions conn "uuid")}
{:id id}) {:id id})
(db/update! conn :comment-thread (db/update! conn :comment-thread
{:modified-at request-at {:modified-at request-at
:page-name page-name :page-name page-name
:mentions :mentions
(-> (:mentions thread) (-> (:mentions thread)
(set) (set)
(into mentions) (into mentions)
(db/encode-pgarray conn "uuid"))} (db/encode-pgarray conn "uuid"))}
{:id thread-id}) {:id thread-id})
nil))))) nil)))
;; --- COMMAND: Delete Comment Thread ;; --- COMMAND: Delete Comment Thread
@ -733,17 +727,17 @@
(sv/defmethod ::delete-comment-thread (sv/defmethod ::delete-comment-thread
{::doc/added "1.15" {::doc/added "1.15"
::sm/params schema:delete-comment-thread} ::sm/params schema:delete-comment-thread
[cfg {:keys [::rpc/profile-id id share-id]}] ::db/transaction true}
(db/tx-run! cfg (fn [{:keys [::db/conn]}] [{:keys [::db/conn]} {:keys [::rpc/profile-id id share-id]}]
(let [{:keys [owner-id file-id] :as thread} (get-comment-thread conn id ::sql/for-update true)] (let [{:keys [owner-id file-id] :as thread} (get-comment-thread conn id ::sql/for-update true)]
(files/check-comment-permissions! conn profile-id file-id share-id) (files/check-comment-permissions! conn profile-id file-id share-id)
(when-not (= owner-id profile-id) (when-not (= owner-id profile-id)
(ex/raise :type :validation (ex/raise :type :validation
:code :not-allowed)) :code :not-allowed))
(db/delete! conn :comment-thread {:id id}) (db/delete! conn :comment-thread {:id id})
nil)))) nil))
;; --- COMMAND: Delete comment ;; --- COMMAND: Delete comment
@ -755,17 +749,17 @@
(sv/defmethod ::delete-comment (sv/defmethod ::delete-comment
{::doc/added "1.15" {::doc/added "1.15"
::sm/params schema:delete-comment} ::sm/params schema:delete-comment
[cfg {:keys [::rpc/profile-id id share-id]}] ::db/transaction true}
(db/tx-run! cfg (fn [{:keys [::db/conn]}] [{:keys [::db/conn]} {:keys [::rpc/profile-id id share-id]}]
(let [{:keys [owner-id thread-id] :as comment} (get-comment conn id ::sql/for-update true) (let [{:keys [owner-id thread-id] :as comment} (get-comment conn id ::sql/for-update true)
{:keys [file-id] :as thread} (get-comment-thread conn thread-id)] {:keys [file-id] :as thread} (get-comment-thread conn thread-id)]
(files/check-comment-permissions! conn profile-id file-id share-id) (files/check-comment-permissions! conn profile-id file-id share-id)
(when-not (= owner-id profile-id) (when-not (= owner-id profile-id)
(ex/raise :type :validation (ex/raise :type :validation
:code :not-allowed)) :code :not-allowed))
(db/delete! conn :comment {:id id}) (db/delete! conn :comment {:id id})
nil)))) nil))
;; --- COMMAND: Update comment thread position ;; --- COMMAND: Update comment thread position
@ -779,17 +773,17 @@
(sv/defmethod ::update-comment-thread-position (sv/defmethod ::update-comment-thread-position
{::doc/added "1.15" {::doc/added "1.15"
::sm/params schema:update-comment-thread-position} ::sm/params schema:update-comment-thread-position
[cfg {:keys [::rpc/profile-id ::rpc/request-at id position frame-id share-id]}] ::db/transaction true}
(db/tx-run! cfg (fn [{:keys [::db/conn]}] [{:keys [::db/conn]} {:keys [::rpc/profile-id ::rpc/request-at id position frame-id share-id]}]
(let [{:keys [file-id] :as thread} (get-comment-thread conn id ::sql/for-update true)] (let [{:keys [file-id] :as thread} (get-comment-thread conn id ::sql/for-update true)]
(files/check-comment-permissions! conn profile-id file-id share-id) (files/check-comment-permissions! conn profile-id file-id share-id)
(db/update! conn :comment-thread (db/update! conn :comment-thread
{:modified-at request-at {:modified-at request-at
:position (db/pgpoint position) :position (db/pgpoint position)
:frame-id frame-id} :frame-id frame-id}
{:id (:id thread)}) {:id (:id thread)})
nil)))) nil))
;; --- COMMAND: Update comment frame ;; --- COMMAND: Update comment frame
@ -802,13 +796,13 @@
(sv/defmethod ::update-comment-thread-frame (sv/defmethod ::update-comment-thread-frame
{::doc/added "1.15" {::doc/added "1.15"
::sm/params schema:update-comment-thread-frame} ::sm/params schema:update-comment-thread-frame
[cfg {:keys [::rpc/profile-id ::rpc/request-at id frame-id share-id]}] ::db/transaction true}
(db/tx-run! cfg (fn [{:keys [::db/conn]}] [{:keys [::db/conn]} {:keys [::rpc/profile-id ::rpc/request-at id frame-id share-id]}]
(let [{:keys [file-id] :as thread} (get-comment-thread conn id ::sql/for-update true)] (let [{:keys [file-id] :as thread} (get-comment-thread conn id ::sql/for-update true)]
(files/check-comment-permissions! conn profile-id file-id share-id) (files/check-comment-permissions! conn profile-id file-id share-id)
(db/update! conn :comment-thread (db/update! conn :comment-thread
{:modified-at request-at {:modified-at request-at
:frame-id frame-id} :frame-id frame-id}
{:id id}) {:id id})
nil)))) nil))