mirror of
https://github.com/penpot/penpot.git
synced 2026-11-19 08:30:42 +00:00
* ✨ Add review mode, review comments and tasks to design reviews A design review now opens a read-only review mode. The left panel shows the pages and layers with the View only badge, the toolbar offers the review tools (move, comment, comment an area, create a task, show the changes) and the right panel holds the inspect of the selection, the comments of the review and its tasks. A bar at the bottom names the review, shows the progress of its tasks and opens its details. Comments can point at a spot, a layer or an area, and a comment written in a review says so with its own mark, wherever the file is open. The layers panel counts the open comments of each layer. Tasks are new: work on a file, assigned to someone and tied to a layer or to a spot of the canvas. A comment can become a task and stays attached to it. A task moves through open, done, verified, reopened and won't do; anybody may move it, and each move is an event that records who made it. Deleting a layer with open tasks moves them to its board and offers to undo the deletion or to mark them done; a task can then be linked to another layer or get its layer back. Assignees are told by email. Merging a branch moves its tasks and their threads into main: the open ones keep going, the closed ones stay as its history, and the merge dialog lists them and can tell their assignees. The details of a review show its overview, its tasks and its activity: what the saves of the branch did to each layer since the review opened, by person and day, with the values before and after. The changes can be shown on the canvas one after another. The backend adds the review fields of a comment thread, the task and task event tables, the activity of a review (recorded by each save of a branch under review) and the commands of the tasks. * ♻️ Bring the design review styles and components up to the DS Move what the review UI repeated into the design system: layer-chip*, progress-bar*, comment-card*, link-button* and textarea*, each with its story and docs. The screens keep the look they had: the same review states compare equal, pixel by pixel, before and after. The stylesheets follow the rules of the UI guide: local variables for the colors that change by state, the right tokens, no unused imports and fewer element and nested selectors. What has no DS token or typography says so in a TODO. The aliases left in comments.scss resolve to their tokens. The components get complete schemas and a single owner for the copyright. Translations take literal keys and templates instead of dynamic keys and glued strings, and comments.cljs no longer depends on the review tasks: the workspace passes it what a thread shows about its task. Fix three bugs of the review: - The inspect took the width of a narrower panel and its height from the window, so it looked squeezed and cut its last rows. It now fills the panel that holds it. - Enter on the menu of a task opened the task. The row now reacts only to keys on itself, and the menu keeps Enter from the workspace shortcuts. - The resolved and lost marks ignored the pointer: hover now wins over the state of a mark. * ✨ Turn each review message into a task and restore the filter dropdown Each message of a thread can now become a task, so one thread can hold several. The new file_task.comment_id column records the message, the migration fills it with the first message of the thread for existing tasks, and the backend rejects a message from another thread or one that already has a task. Comments and tasks of a review behave like normal ones. The canvas marks show a short preview on hover and there is one mark per task. A blue label marks what belongs to a review, in the thread and in the sidebar, and a selected card takes a blue border there and the primary color elsewhere. The new review-label* component holds the label. The sidebar and the review panel go back to a filter dropdown in place of the chips. Comments offer the views all, yours and mentions, with an option to hide the resolved ones. Tasks offer all, yours, open and to verify, with an option to hide the closed ones. Resolved comments now show by default. "New task" outside a review now arms the task tool: it shows the task cursor and the next click opens the task form, not a comment. Then the comment tool returns, and Esc leaves the tool. The details modal shows changed properties in the format of the compare dialogs, and showing a task on the canvas syncs the WASM viewport. The sidebar cards put the layer chip on the line of the replies, and task states lose the dot and take a color each. Fix these bugs: - The hover card of the activity tab crashed on a change with no token. - The assignee and reviewer dropdowns came out cut. The select now portals into the closest dialog, because the modal makes the rest of the page inert. - The review label cut the header of the thread. It now sits below it. * ✨ Group overlapping review marks and let tasks change assignee Comments and tasks of a review that fall on the same spot now show as one mark with a counter in a white bubble. A click fans them out, as the comment bubbles do, and the user picks one. They group on the same layer and, by proximity, at a low zoom. The card of the mark lists up to five items. Hovering a mark outlines its layer, or its board when the layer is gone. The WASM viewport draws no outlines under the comments tool, so it now draws the highlighted ones there. Opening a comment from a mark closes the open task. A task can change assignee from the selector in its state and from "Assign to" in its menu, which holds the team in the same popover. The change shows right away and goes to the history of the task. A closed task keeps its assignee. Deleting a task or a comment now asks first, with the usual confirmation. A task deleted from the details of a review brings the dialog back on its tab. The state of a task takes two rows, in its panel and in a thread: state and layer, then assignee and the review it comes from, which a review omits. The text of tasks and comments sits in a box. The history no longer says that the task was created. * ✨ Keep the branch bar and the palettes at one level on a branch The open branch bar covered the horizontal scroll of the canvas. It now sits as far from the bottom as the review bar. Folded, it stays flush. On a branch the palettes no longer lift over the branch bar: both sit at the same level and work as a toggle. Opening one folds the other, also through the palette shortcuts, and both can stay folded. The new `:bottom-bar` state says which one is open. It exists only while the branch bar does, so a file that is not a branch keeps the palettes on their layout flags and at their usual place, and what happens on a branch does not reach them. The branch bar comes open on a branch and centers on the canvas, not on the window, so that it clears the folded palettes at usual widths. The room it published for the palettes goes away with the lifting. * ✨ Give every task a thread and tidy the design review screens Every task now lives in a message of a comment thread, so anyone can go on commenting on it. A task made from scratch gets a thread whose first message is its title, on its layer and in its review, created in the same transaction. A comment turned into a task keeps its thread, as before. The panel of a task with no thread is gone: a task always opens as its thread. Migration 0814 gives a thread to the tasks that had none. Deleting a thread deletes its tasks, after a confirmation that says so. The anchor of a task was a plain map, not a point, so a thread on a layer refused replies. It is now a point. The details of a review filter the tasks by state with the chips of the activity, in the colour of each state, and show how many are closed on the right. The "New task" button leaves the dialog. Descriptions look the same everywhere: the box of the branch info dialog, an "Edit" button and a dashed box when empty. Read, they grow up to 400px and scroll inside; edited, the textarea starts at 400px. The text of a review in the sidebar does the same once unfolded. In the review bar, the author gets a "Publish latest changes" button when the branch moved past the review, with a hint that approvals must be renewed. The others keep a badge, now "Unpublished changes", with a hint. The bar refreshes once the publish lands. Smaller changes: the resolve checkbox lines up with the task button and has a tooltip, messages sit in a filled box, task events take an outline, and the change navigator sits right above the review bar. * 💄 Sort the test runner requires that cljfmt rejects The Frontend Tests lint step fails on runner.cljs: the requires for workspace-tasks-test, review-details-test and review-markers-test sat out of alphabetical order. This is the output of cljfmt fix. AI-assisted-by: claude-opus-5-5 Signed-off-by: Álvaro Tejero Cantero <alvorithm@teje.ro> * 🐛 Test the design review mode in the pull request spec The review bar replaced the branch banner, so four tests still drove the branch bar: a visible Compare button, the rectangle tool and the Review button. They now drive the design review instead. * Opening a file with a pr-id enters the review: the bar names the review, the header trades the file name for the review title, and comparing, merging and cancelling sit in the bar's options menu. * The compare dialog opens from that menu. * The review is read-only rather than an editable file: the review toolbar offers its five tools, the drawing tools are gone, the sidebar says View only, and the right panel is the review's own Inspect, Comments and Tasks tabs. * The verdict comes from the bar's Submit review button and the dialog it opens, scoped by role so the bar action and the dialog action do not collide. The tests also reach the review bar by its region label instead of walking up three parents from the Reviewing the design review text, which broke as soon as the bar's markup changed. AI-assisted-by: claude-opus-5-5 Signed-off-by: Álvaro Tejero Cantero <alvorithm@teje.ro> * 🐛 Count only the test thread's acquisitions in the climit test acquires-the-command-own-limits-before-the-root-limits failed on CI with an extra "root/global" ahead of the expected order. The test records metrics through with-redefs, which is global, so a call of another thread of the test JVM, such as an RPC an earlier test left running, can take a root limiter inside the window and land in the same atom. Record the thread with each metrics call and keep, in acquired-in-order, only the acquisitions of the calling thread. Both tests that read it acquire their limiters on the test thread. The same class of flake was fixed on develop for the waiting-count test (12d73d0f63); this one is still open there. AI-assisted-by: claude-opus-5-5 Signed-off-by: Álvaro Tejero Cantero <alvorithm@teje.ro> * 📚 Describe the task moves the server allows in the table comment The comment on file_task said a task is reopened only from done and that editors move tasks. files_task.clj::transitions also reopens a verified or won't-do task, and set-task-status lets anybody who can read the file move one. Say the same in the schema. AI-assisted-by: claude-opus-5-5 Signed-off-by: Álvaro Tejero Cantero <alvorithm@teje.ro> --------- Signed-off-by: Álvaro Tejero Cantero <alvorithm@teje.ro> Co-authored-by: Álvaro Tejero Cantero <alvorithm@teje.ro>
1009 lines
36 KiB
Clojure
1009 lines
36 KiB
Clojure
;; This Source Code Form is subject to the terms of the Mozilla Public
|
|
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
;;
|
|
;; Copyright (c) KALEIDOS SUBSIDIARY SL
|
|
|
|
(ns app.rpc.commands.comments
|
|
(:require
|
|
[app.binfile.common :as bfc]
|
|
[app.common.data :as d]
|
|
[app.common.data.macros :as dm]
|
|
[app.common.exceptions :as ex]
|
|
[app.common.geom.point :as gpt]
|
|
[app.common.schema :as sm]
|
|
[app.common.time :as ct]
|
|
[app.common.uri :as uri]
|
|
[app.common.uuid :as uuid]
|
|
[app.config :as cf]
|
|
[app.db :as db]
|
|
[app.db.sql :as sql]
|
|
[app.email :as eml]
|
|
[app.features.fdata :as feat.fdata]
|
|
[app.loggers.audit :as-alias audit]
|
|
[app.loggers.webhooks :as-alias webhooks]
|
|
[app.msgbus :as mbus]
|
|
[app.rpc :as-alias rpc]
|
|
[app.rpc.commands.files :as files]
|
|
[app.rpc.commands.profile :as profile]
|
|
[app.rpc.commands.teams :as teams]
|
|
[app.rpc.doc :as-alias doc]
|
|
[app.rpc.quotes :as quotes]
|
|
[app.rpc.retry :as rtry]
|
|
[app.util.services :as sv]
|
|
[clojure.set :as set]
|
|
[cuerdas.core :as str]))
|
|
|
|
;; --- GENERAL PURPOSE INTERNAL HELPERS
|
|
|
|
(def r-mentions-split #"@\[[^\]]*\]\([^\)]*\)")
|
|
(def r-mentions #"@\[([^\]]*)\]\(([^\)]*)\)")
|
|
|
|
(def comment-max-length 750)
|
|
|
|
(defn- share-link-pages
|
|
"The set of pages granted by a share-link, or nil when the access to the
|
|
file is not restricted to a page scope."
|
|
[perms]
|
|
(when (= :share-link (:type perms))
|
|
(or (:pages perms) #{})))
|
|
|
|
(defn- check-page-scope
|
|
"Raises a :not-found error when `page-id` is outside the page scope granted
|
|
by a share-link."
|
|
[perms page-id]
|
|
(when-let [pages (share-link-pages perms)]
|
|
(when-not (contains? pages page-id)
|
|
(ex/raise :type :not-found
|
|
:code :object-not-found
|
|
:hint "not found"))))
|
|
|
|
(defn- format-comment
|
|
[{:keys [content]}]
|
|
(->> (d/interleave-all
|
|
(str/split content r-mentions-split)
|
|
(->> (re-seq r-mentions content)
|
|
(map (fn [[_ user _]] user))))
|
|
(str/join "")))
|
|
|
|
(defn- format-comment-url
|
|
[thread file]
|
|
(str/ffmt "%/#/workspace?%"
|
|
(cf/get :public-uri)
|
|
(uri/map->query-string
|
|
{:file-id (:id file)
|
|
:page-id (:page-id file)
|
|
:team-id (:team-id file)
|
|
:comment-id (:id thread)})))
|
|
|
|
(defn- format-comment-ref
|
|
[thread file]
|
|
(str/ffmt "#%, %, %"
|
|
(:seqn thread)
|
|
(:name file)
|
|
(:page-name file)))
|
|
|
|
(defn- get-team-users
|
|
[conn team-id]
|
|
(->> (teams/get-users+props conn team-id)
|
|
(map profile/decode-row)
|
|
(d/index-by :id)))
|
|
|
|
(defn- notification-email?
|
|
[profile-id owner-id props]
|
|
(if (= profile-id owner-id)
|
|
(not= :none (-> props :notifications :email-comments))
|
|
(= :all (-> props :notifications :email-comments))))
|
|
|
|
(defn- mention-email?
|
|
[props]
|
|
(not= :none (-> props :notifications :email-comments)))
|
|
|
|
(defn send-comment-emails!
|
|
[{:keys [::db/conn] :as cfg} profile comment thread file]
|
|
(let [team-users (get-team-users conn (:team-id file))
|
|
comment-reference (format-comment-ref thread file)
|
|
comment-content (format-comment comment)
|
|
comment-url (format-comment-url thread file)
|
|
profile-id (get profile :id)
|
|
|
|
;; Users mentioned in this comment
|
|
comment-mentions
|
|
(-> (:mentions comment)
|
|
(disj profile-id))
|
|
|
|
;; Users mentioned in this thread
|
|
thread-mentions
|
|
(-> (:mentions thread)
|
|
;; Remove the mentions in the comment because we're already sending a
|
|
;; notification
|
|
(set/difference comment-mentions)
|
|
(disj profile-id))
|
|
|
|
;; All users
|
|
notificate-users-ids
|
|
(-> (set (keys team-users))
|
|
(set/difference comment-mentions)
|
|
(set/difference thread-mentions)
|
|
(disj profile-id))]
|
|
|
|
(doseq [mention comment-mentions]
|
|
(let [{:keys [fullname email props]} (get team-users mention)]
|
|
(when (mention-email? props)
|
|
(eml/send-template
|
|
cfg
|
|
{::eml/reuse-conn true
|
|
::eml/template "comment-mention"
|
|
:public-uri (cf/get :public-uri)
|
|
:to email
|
|
:name fullname
|
|
:source-user (:fullname profile)
|
|
:comment-reference comment-reference
|
|
:comment-content comment-content
|
|
:comment-url comment-url}))))
|
|
|
|
;; Send to the thread users
|
|
(doseq [mention thread-mentions]
|
|
(let [{:keys [fullname email props]} (get team-users mention)]
|
|
(when (mention-email? props)
|
|
(eml/send-template
|
|
cfg
|
|
{::eml/reuse-conn true
|
|
::eml/template "comment-thread"
|
|
:public-uri (cf/get :public-uri)
|
|
:to email
|
|
:name fullname
|
|
:source-user (:fullname profile)
|
|
:comment-reference comment-reference
|
|
:comment-content comment-content
|
|
:comment-url comment-url}))))
|
|
|
|
;; Send to users with the "all" flag activated
|
|
(doseq [user-id notificate-users-ids]
|
|
(let [{:keys [id fullname email props]} (get team-users user-id)]
|
|
(when (notification-email? id (:owner-id thread) props)
|
|
(eml/send-template
|
|
cfg
|
|
{::eml/reuse-conn true
|
|
::eml/template "comment-notification"
|
|
:public-uri (cf/get :public-uri)
|
|
:to email
|
|
:name fullname
|
|
:source-user (:fullname profile)
|
|
:comment-reference comment-reference
|
|
:comment-content comment-content
|
|
:comment-url comment-url}))))))
|
|
|
|
(defn- decode-row
|
|
[{:keys [participants position mentions area anchor-type] :as row}]
|
|
(cond-> row
|
|
(db/pgpoint? position) (assoc :position (db/decode-pgpoint position))
|
|
(db/pgobject? participants) (assoc :participants (db/decode-transit-pgobject participants))
|
|
(db/pgobject? area) (assoc :area (db/decode-transit-pgobject area))
|
|
(string? anchor-type) (assoc :anchor-type (keyword anchor-type))
|
|
(db/pgarray? mentions) (assoc :mentions (db/decode-pgarray mentions #{}))))
|
|
|
|
(def xf-decode-row
|
|
(map decode-row))
|
|
|
|
(defn- get-file
|
|
"A specialized version of get-file for comments module."
|
|
[cfg file-id page-id]
|
|
(feat.fdata/with-pointers cfg file-id
|
|
(let [file (bfc/get-file cfg file-id)
|
|
data (get file :data)]
|
|
(-> file
|
|
(assoc :page-name (dm/get-in data [:pages-index page-id :name]))
|
|
(assoc :page-id page-id)
|
|
(dissoc :data)))))
|
|
|
|
;; FIXME: rename
|
|
(defn- get-comment-thread
|
|
[conn thread-id & {:as opts}]
|
|
(-> (db/get-by-id conn :comment-thread thread-id opts)
|
|
(decode-row)))
|
|
|
|
(defn- get-comment
|
|
[conn comment-id & {:as opts}]
|
|
(db/get-by-id conn :comment comment-id opts))
|
|
|
|
(def ^:private sql:get-next-seqn
|
|
"SELECT (f.comment_thread_seqn + 1) AS next_seqn
|
|
FROM file AS f
|
|
WHERE f.id = ?
|
|
FOR UPDATE")
|
|
|
|
(defn- get-next-seqn
|
|
[conn file-id]
|
|
(let [res (db/exec-one! conn [sql:get-next-seqn file-id])]
|
|
(:next-seqn res)))
|
|
|
|
(def sql:upsert-comment-thread-status
|
|
"insert into comment_thread_status (thread_id, profile_id, modified_at)
|
|
values (?, ?, ?)
|
|
on conflict (thread_id, profile_id)
|
|
do update set modified_at = ?
|
|
returning modified_at;")
|
|
|
|
(defn upsert-comment-thread-status!
|
|
([conn profile-id thread-id]
|
|
(upsert-comment-thread-status! conn profile-id thread-id (ct/in-future "1s")))
|
|
([conn profile-id thread-id mod-at]
|
|
(db/exec-one! conn [sql:upsert-comment-thread-status thread-id profile-id mod-at mod-at])))
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
;; QUERY COMMANDS
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; --- COMMAND: Get Comment Threads
|
|
|
|
(declare ^:private get-comment-threads)
|
|
|
|
(def ^:private
|
|
schema:get-comment-threads
|
|
[:and
|
|
[:map {:title "get-comment-threads"}
|
|
[:file-id {:optional true} ::sm/uuid]
|
|
[:team-id {:optional true} ::sm/uuid]
|
|
[:share-id {:optional true} [:maybe ::sm/uuid]]]
|
|
[::sm/contains-any #{:file-id :team-id}]])
|
|
|
|
(sv/defmethod ::get-comment-threads
|
|
{::doc/added "1.15"
|
|
::sm/params schema:get-comment-threads}
|
|
[cfg {:keys [::rpc/profile-id file-id share-id] :as params}]
|
|
(db/run! cfg (fn [{:keys [::db/conn] :as cfg}]
|
|
(let [perms (files/check-comment-permissions! cfg profile-id file-id share-id)
|
|
threads (get-comment-threads conn profile-id file-id)]
|
|
(if-let [pages (share-link-pages perms)]
|
|
(filterv #(contains? pages (:page-id %)) threads)
|
|
threads)))))
|
|
|
|
(defn- get-comment-threads-sql
|
|
[where]
|
|
(str/ffmt
|
|
"SELECT DISTINCT ON (ct.id)
|
|
ct.*,
|
|
pf.fullname AS owner_fullname,
|
|
pf.email AS owner_email,
|
|
pf.photo_id AS owner_photo_id,
|
|
p.team_id AS team_id,
|
|
f.name AS file_name,
|
|
f.project_id AS project_id,
|
|
fpr.title AS pull_request_title,
|
|
fpr.status AS pull_request_status,
|
|
first_value(c.content) OVER w AS content,
|
|
(SELECT count(1)
|
|
FROM comment AS c
|
|
WHERE c.thread_id = ct.id) AS count_comments,
|
|
(SELECT count(1)
|
|
FROM comment AS c
|
|
WHERE c.thread_id = ct.id
|
|
AND c.created_at >= coalesce(cts.modified_at, ct.created_at)) AS count_unread_comments
|
|
FROM comment_thread AS ct
|
|
INNER JOIN comment AS c ON (c.thread_id = ct.id)
|
|
INNER JOIN file AS f ON (f.id = ct.file_id)
|
|
INNER JOIN project AS p ON (p.id = f.project_id)
|
|
LEFT JOIN comment_thread_status AS cts ON (cts.thread_id = ct.id AND cts.profile_id = ?)
|
|
LEFT JOIN profile AS pf ON (ct.owner_id = pf.id)
|
|
LEFT JOIN file_pull_request AS fpr ON (fpr.id = ct.pull_request_id)
|
|
WHERE f.deleted_at IS NULL
|
|
AND p.deleted_at IS NULL
|
|
%1
|
|
WINDOW w AS (PARTITION BY c.thread_id ORDER BY c.created_at ASC)"
|
|
where))
|
|
|
|
(def ^:private sql:comment-threads-by-file-id
|
|
(get-comment-threads-sql "AND ct.file_id = ?"))
|
|
|
|
(defn- get-comment-threads
|
|
[conn profile-id file-id]
|
|
(->> (db/exec! conn [sql:comment-threads-by-file-id profile-id file-id])
|
|
(into [] xf-decode-row)))
|
|
|
|
;; --- COMMAND: Get Unread Comment Threads
|
|
|
|
(def ^:private sql:unread-all-comment-threads-by-team
|
|
(str "WITH threads AS ("
|
|
(get-comment-threads-sql "AND p.team_id = ?")
|
|
")"
|
|
"SELECT t.* FROM threads AS t
|
|
WHERE t.count_unread_comments > 0"))
|
|
|
|
(def ^:private sql:unread-partial-comment-threads-by-team
|
|
(str "WITH threads AS ("
|
|
(get-comment-threads-sql "AND p.team_id = ? AND (ct.owner_id = ? OR ? = ANY(ct.mentions))")
|
|
")"
|
|
"SELECT t.* FROM threads AS t
|
|
WHERE t.count_unread_comments > 0"))
|
|
|
|
(defn- get-unread-comment-threads
|
|
[cfg profile-id team-id]
|
|
(let [profile (-> (db/get cfg :profile {:id profile-id} ::db/remove-deleted false)
|
|
(profile/decode-row))
|
|
notify (or (-> profile :props :notifications :dashboard-comments) :all)
|
|
result (case notify
|
|
:all (db/exec! cfg [sql:unread-all-comment-threads-by-team profile-id team-id])
|
|
:partial (db/exec! cfg [sql:unread-partial-comment-threads-by-team profile-id team-id profile-id profile-id])
|
|
[])]
|
|
(into [] xf-decode-row result)))
|
|
|
|
(def ^:private
|
|
schema:get-unread-comment-threads
|
|
[:map {:title "get-unread-comment-threads"}
|
|
[:team-id ::sm/uuid]])
|
|
|
|
(sv/defmethod ::get-unread-comment-threads
|
|
{::doc/added "1.15"
|
|
::sm/params schema:get-unread-comment-threads}
|
|
[cfg {:keys [::rpc/profile-id team-id] :as params}]
|
|
(teams/check-read-permissions! cfg profile-id team-id)
|
|
(get-unread-comment-threads cfg profile-id team-id))
|
|
|
|
;; --- COMMAND: Get Single Comment Thread
|
|
|
|
(def ^:private
|
|
schema:get-comment-thread
|
|
[:map {:title "get-comment-thread"}
|
|
[:file-id ::sm/uuid]
|
|
[:id ::sm/uuid]
|
|
[:share-id {:optional true} [:maybe ::sm/uuid]]])
|
|
|
|
(def ^:private sql:get-comment-thread
|
|
(get-comment-threads-sql "AND ct.file_id = ? AND ct.id = ?"))
|
|
|
|
(sv/defmethod ::get-comment-thread
|
|
{::doc/added "1.15"
|
|
::sm/params schema:get-comment-thread}
|
|
[cfg {:keys [::rpc/profile-id file-id id share-id] :as params}]
|
|
(db/run! cfg (fn [{:keys [::db/conn] :as cfg}]
|
|
(let [perms (files/check-comment-permissions! cfg profile-id file-id share-id)
|
|
thread (some-> (db/exec-one! conn [sql:get-comment-thread profile-id file-id id])
|
|
(decode-row))]
|
|
(when thread
|
|
(check-page-scope perms (:page-id thread)))
|
|
thread))))
|
|
|
|
;; --- COMMAND: Retrieve Comments
|
|
|
|
(declare ^:private get-comments)
|
|
|
|
(def ^:private
|
|
schema:get-comments
|
|
[:map {:title "get-comments"}
|
|
[:thread-id ::sm/uuid]
|
|
[:share-id {:optional true} [:maybe ::sm/uuid]]])
|
|
|
|
(sv/defmethod ::get-comments
|
|
{::doc/added "1.15"
|
|
::sm/params schema:get-comments}
|
|
[cfg {:keys [::rpc/profile-id thread-id share-id]}]
|
|
(db/run! cfg (fn [{:keys [::db/conn] :as cfg}]
|
|
(let [{:keys [file-id page-id]} (get-comment-thread conn thread-id)
|
|
perms (files/check-comment-permissions! cfg profile-id file-id share-id)]
|
|
(check-page-scope perms page-id)
|
|
(get-comments conn thread-id)))))
|
|
|
|
(def sql:get-comments
|
|
"SELECT c.*,
|
|
ct.file_id AS file_id,
|
|
pf.fullname AS owner_fullname,
|
|
pf.email AS owner_email,
|
|
pf.photo_id AS owner_photo_id
|
|
FROM comment AS c
|
|
INNER JOIN comment_thread AS ct ON (ct.id = c.thread_id)
|
|
LEFT JOIN profile AS pf ON (c.owner_id = pf.id)
|
|
WHERE c.thread_id = ?
|
|
ORDER BY c.created_at ASC")
|
|
|
|
(defn- get-comments
|
|
[conn thread-id]
|
|
(->> (db/exec! conn [sql:get-comments thread-id])
|
|
(into [] xf-decode-row)))
|
|
|
|
;; --- COMMAND: Get file comments users
|
|
|
|
;; All the profiles that had comment the file, plus the current
|
|
;; profile.
|
|
|
|
(defn- file-comment-users-sql
|
|
[where]
|
|
(str/ffmt
|
|
"WITH available_profiles AS (
|
|
SELECT DISTINCT c.owner_id AS id
|
|
FROM comment c
|
|
JOIN comment_thread ct
|
|
ON ct.id = c.thread_id
|
|
WHERE ct.file_id = ?::uuid
|
|
%1
|
|
),
|
|
profile_ids AS (
|
|
SELECT id FROM available_profiles
|
|
UNION
|
|
SELECT ?::uuid
|
|
)
|
|
SELECT p.id,
|
|
p.email,
|
|
p.fullname AS name,
|
|
p.fullname,
|
|
p.photo_id,
|
|
p.is_active
|
|
FROM profile p
|
|
JOIN profile_ids AS x
|
|
ON x.id = p.id;"
|
|
where))
|
|
|
|
(def ^:private sql:file-comment-users
|
|
(file-comment-users-sql ""))
|
|
|
|
(def ^:private sql:file-comment-users-by-page
|
|
(file-comment-users-sql "AND ct.page_id = ANY(?::uuid[])"))
|
|
|
|
(defn get-file-comments-users
|
|
"Returns the profiles of the users that commented the file. When `pages` is
|
|
not nil, only the commenters of the threads on those pages are returned."
|
|
[conn file-id profile-id pages]
|
|
(if (some? pages)
|
|
(db/exec! conn [sql:file-comment-users-by-page
|
|
file-id
|
|
(db/create-array conn "uuid" pages)
|
|
profile-id])
|
|
(db/exec! conn [sql:file-comment-users file-id profile-id])))
|
|
|
|
(def ^:private
|
|
schema:get-profiles-for-file-comments
|
|
[:map {:title "get-profiles-for-file-comments"}
|
|
[:file-id ::sm/uuid]
|
|
[:share-id {:optional true} [:maybe ::sm/uuid]]])
|
|
|
|
(sv/defmethod ::get-profiles-for-file-comments
|
|
"Retrieves a list of profiles with limited set of properties of all
|
|
participants on comment threads of the file. When the request is done with
|
|
a share link, only the participants on the pages granted by the link are
|
|
returned."
|
|
{::doc/added "1.15"
|
|
::doc/changes ["1.15" "Imported from queries and renamed."
|
|
"2.19" "With a share link, only the participants on the pages granted by the link are returned."]
|
|
::sm/params schema:get-profiles-for-file-comments}
|
|
[cfg {:keys [::rpc/profile-id file-id share-id]}]
|
|
(db/run! cfg (fn [{:keys [::db/conn] :as cfg}]
|
|
(let [perms (files/check-comment-permissions! cfg profile-id file-id share-id)]
|
|
(get-file-comments-users conn file-id profile-id (share-link-pages perms))))))
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
;; MUTATION COMMANDS
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
(declare ^:private insert-comment-thread)
|
|
|
|
;; --- COMMAND: Create Comment Thread
|
|
|
|
(def schema:area
|
|
"A rectangle of the canvas a thread points at, in canvas coordinates."
|
|
[:map {:title "CommentThreadArea"}
|
|
[:x ::sm/safe-number]
|
|
[:y ::sm/safe-number]
|
|
[:width ::sm/safe-number]
|
|
[:height ::sm/safe-number]])
|
|
|
|
(def ^:private schema:anchor
|
|
"What a thread points at: a spot of a board (`:canvas`, the default),
|
|
a layer or a rectangle."
|
|
[:multi {:dispatch :type :title "CommentThreadAnchor"}
|
|
[:canvas [:map [:type [:= :canvas]]]]
|
|
[:layer [:map
|
|
[:type [:= :layer]]
|
|
[:shape-id ::sm/uuid]]]
|
|
[:area [:map
|
|
[:type [:= :area]]
|
|
[:area schema:area]]]])
|
|
|
|
(def ^:private
|
|
schema:create-comment-thread
|
|
[:map {:title "create-comment-thread"}
|
|
[:file-id ::sm/uuid]
|
|
[:position ::gpt/point]
|
|
[:content [:string {:max comment-max-length}]]
|
|
[:page-id ::sm/uuid]
|
|
[:frame-id ::sm/uuid]
|
|
[:share-id {:optional true} [:maybe ::sm/uuid]]
|
|
[:mentions {:optional true} [::sm/set ::sm/uuid]]
|
|
;; the design review the thread is written in: an open pull request
|
|
;; of this file (the branch file it reviews)
|
|
[:pull-request-id {:optional true} ::sm/uuid]
|
|
[:anchor {:optional true} schema:anchor]])
|
|
|
|
(def ^:private sql:get-open-pull-request-of-file
|
|
"SELECT id FROM file_pull_request
|
|
WHERE id = ?
|
|
AND source_file_id = ?
|
|
AND status = 'open'
|
|
AND deleted_at IS NULL")
|
|
|
|
(defn- check-review-of-file
|
|
"Refuse a thread for a design review that is not an open review of the
|
|
file the thread is written on."
|
|
[conn pull-request-id file-id]
|
|
(when-not (db/exec-one! conn [sql:get-open-pull-request-of-file pull-request-id file-id])
|
|
(ex/raise :type :validation
|
|
:code :pull-request-not-open
|
|
:hint "the thread names a design review that is not open on this file"
|
|
:pull-request-id pull-request-id)))
|
|
|
|
(defn- update-thread-seqn
|
|
[conn file-id seqn]
|
|
(db/update! conn :file
|
|
{:comment-thread-seqn seqn}
|
|
{:id file-id}
|
|
{::db/return-keys false}))
|
|
|
|
(defn add-owner
|
|
[thread-or-comment profile]
|
|
(-> thread-or-comment
|
|
(assoc :owner-fullname (:fullname profile))
|
|
(assoc :owner-email (:email profile))
|
|
(assoc :owner-photo-id (:photo-id profile))))
|
|
|
|
(defn create-comment-thread
|
|
"Create a thread with its first message on the page `page-id` of
|
|
`file-id`, once the quotes of threads and comments allow it. It joins
|
|
the transaction of `cfg` when there is one: a task created on the canvas
|
|
gets its thread this way (`app.rpc.commands.files-task`). The caller
|
|
checks the permissions. Returns the thread with the id of its first
|
|
message (`:comment-id`)."
|
|
[cfg {:keys [::rpc/profile-id file-id page-id] :as params}]
|
|
(let [{:keys [team-id project-id] :as file} (get-file cfg file-id page-id)]
|
|
(-> cfg
|
|
(assoc ::quotes/profile-id profile-id)
|
|
(assoc ::quotes/team-id team-id)
|
|
(assoc ::quotes/project-id project-id)
|
|
(assoc ::quotes/file-id file-id)
|
|
(quotes/check! {::quotes/id ::quotes/comment-threads-per-file}
|
|
{::quotes/id ::quotes/comments-per-file}))
|
|
|
|
(let [thread (db/tx-run! cfg insert-comment-thread (assoc params ::file file))]
|
|
(vary-meta thread assoc ::audit/props (assoc thread :team-id team-id)))))
|
|
|
|
(sv/defmethod ::create-comment-thread
|
|
{::doc/added "1.15"
|
|
::webhooks/event? true
|
|
::rtry/enabled true
|
|
::rtry/when rtry/conflict-exception?
|
|
::sm/params schema:create-comment-thread}
|
|
[cfg {:keys [::rpc/profile-id file-id page-id share-id] :as params}]
|
|
(let [perms (files/check-comment-permissions! cfg profile-id file-id share-id)]
|
|
(check-page-scope perms page-id)
|
|
(create-comment-thread cfg params)))
|
|
|
|
(defn- insert-comment-thread
|
|
[{:keys [::db/conn] :as cfg}
|
|
{:keys [::rpc/profile-id ::rpc/request-at ::file position content mentions frame-id
|
|
pull-request-id anchor]}]
|
|
|
|
(let [;; NOTE: we take the next seq number from a separate query
|
|
;; because we need to lock the file for avoid race conditions
|
|
|
|
;; FIXME: this method touches and locks the file table,which
|
|
;; is already heavy-update tablel; we need to think on move
|
|
;; the sequence state management to a different table or
|
|
;; different storage (example: redis) for alivate the update
|
|
;; pression on the file table
|
|
|
|
_ (when pull-request-id
|
|
(check-review-of-file conn pull-request-id (:id file)))
|
|
|
|
profile (profile/get-profile conn profile-id)
|
|
seqn (get-next-seqn conn (:id file))
|
|
|
|
file-id (get file :id)
|
|
thread-id (uuid/next)
|
|
|
|
thread (-> (db/insert! conn :comment-thread
|
|
{:id thread-id
|
|
:file-id file-id
|
|
:page-name (:page-name file)
|
|
:page-id (:page-id file)
|
|
:owner-id profile-id
|
|
:participants (db/tjson #{profile-id})
|
|
:created-at request-at
|
|
:modified-at request-at
|
|
:seqn seqn
|
|
:frame-id frame-id
|
|
:position (db/pgpoint position)
|
|
:mentions (db/encode-pgarray mentions conn "uuid")
|
|
:pull-request-id pull-request-id
|
|
:anchor-type (d/name (get anchor :type :canvas))
|
|
:shape-id (:shape-id anchor)
|
|
:area (some-> (:area anchor) db/tjson)})
|
|
(decode-row))
|
|
comment (-> (db/insert! conn :comment
|
|
{:id (uuid/next)
|
|
:thread-id thread-id
|
|
:owner-id profile-id
|
|
:created-at request-at
|
|
:modified-at request-at
|
|
:mentions (db/encode-pgarray mentions conn "uuid")
|
|
:content content})
|
|
(decode-row))]
|
|
|
|
;; Make the current thread as read.
|
|
(upsert-comment-thread-status! conn profile-id thread-id request-at)
|
|
|
|
;; Optimistic update of current seq number on file.
|
|
(update-thread-seqn conn file-id seqn)
|
|
|
|
;; Send mentions emails
|
|
(send-comment-emails! cfg profile comment thread file)
|
|
|
|
(-> thread
|
|
(add-owner profile)
|
|
(assoc :comment-id (:id comment)))))
|
|
|
|
;; --- COMMAND: Update Comment Thread Status
|
|
|
|
(def ^:private
|
|
schema:update-comment-thread-status
|
|
[:map {:title "update-comment-thread-status"}
|
|
[:id ::sm/uuid]
|
|
[:share-id {:optional true} [:maybe ::sm/uuid]]])
|
|
|
|
(sv/defmethod ::update-comment-thread-status
|
|
{::doc/added "1.15"
|
|
::sm/params schema:update-comment-thread-status
|
|
::db/transaction true}
|
|
[{:keys [::db/conn] :as cfg} {:keys [::rpc/profile-id id share-id]}]
|
|
(let [thread (get-comment-thread conn id ::sql/for-update true)
|
|
perms (files/check-comment-permissions! cfg profile-id (:file-id thread) share-id)]
|
|
(check-page-scope perms (:page-id thread))
|
|
(upsert-comment-thread-status! conn profile-id id)))
|
|
|
|
;; --- COMMAND: Update Comment Thread
|
|
|
|
(def ^:private
|
|
schema:update-comment-thread
|
|
[:map {:title "update-comment-thread"}
|
|
[:id ::sm/uuid]
|
|
[:is-resolved :boolean]
|
|
[:share-id {:optional true} [:maybe ::sm/uuid]]])
|
|
|
|
(sv/defmethod ::update-comment-thread
|
|
{::doc/added "1.15"
|
|
::sm/params schema:update-comment-thread
|
|
::db/transaction true}
|
|
[{:keys [::db/conn] :as cfg} {:keys [::rpc/profile-id id is-resolved share-id]}]
|
|
(let [thread (get-comment-thread conn id ::sql/for-update true)
|
|
perms (files/check-comment-permissions! cfg profile-id (:file-id thread) share-id)]
|
|
(check-page-scope perms (:page-id thread))
|
|
(db/update! conn :comment-thread
|
|
{:is-resolved is-resolved}
|
|
{:id id})
|
|
nil))
|
|
|
|
;; --- COMMAND: Add Comment
|
|
|
|
(def ^:private
|
|
schema:create-comment
|
|
[:map {:title "create-comment"}
|
|
[:thread-id ::sm/uuid]
|
|
[:content [:string {:max comment-max-length}]]
|
|
[:share-id {:optional true} [:maybe ::sm/uuid]]
|
|
[:mentions {:optional true} [::sm/set ::sm/uuid]]])
|
|
|
|
(sv/defmethod ::create-comment
|
|
{::doc/added "1.15"
|
|
::webhooks/event? true
|
|
::sm/params schema:create-comment
|
|
::db/transaction true}
|
|
[{:keys [::db/conn] :as cfg} {:keys [::rpc/profile-id ::rpc/request-at thread-id share-id content mentions]}]
|
|
(let [{:keys [file-id page-id] :as thread}
|
|
(get-comment-thread conn thread-id ::sql/for-update true)
|
|
|
|
perms (files/check-comment-permissions! cfg profile-id file-id share-id)]
|
|
|
|
(check-page-scope perms page-id)
|
|
|
|
(let [{:keys [team-id project-id] :as file}
|
|
(get-file cfg file-id page-id)]
|
|
|
|
(quotes/check! cfg {::quotes/id ::quotes/comments-per-file
|
|
::quotes/profile-id profile-id
|
|
::quotes/team-id team-id
|
|
::quotes/project-id project-id
|
|
::quotes/file-id file-id})
|
|
|
|
(let [profile (profile/get-profile conn profile-id)
|
|
mentions (into #{} mentions)
|
|
params {:id (uuid/next)
|
|
:created-at request-at
|
|
:modified-at request-at
|
|
:thread-id thread-id
|
|
:owner-id profile-id
|
|
:content content
|
|
:mentions (db/encode-pgarray mentions conn "uuid")}
|
|
|
|
comment (-> (db/insert! conn :comment params)
|
|
(decode-row)
|
|
(assoc :file-id file-id)
|
|
(add-owner profile))]
|
|
|
|
;; Update thread modified-at attribute and assoc the current
|
|
;; profile to the participant set.
|
|
(let [mentions (into (:mentions thread) mentions)
|
|
participants (-> (:participants thread #{})
|
|
(conj profile-id))
|
|
params {:modified-at request-at
|
|
:participants (db/tjson participants)
|
|
:mentions (db/encode-pgarray mentions conn "uuid")}
|
|
|
|
;; Update the page-name cached attribute on comment thread table.
|
|
params (cond-> params
|
|
(not= (:page-name file) (:page-name thread))
|
|
(assoc :page-name (:page-name file)))]
|
|
|
|
(db/update! conn :comment-thread params
|
|
{:id thread-id}
|
|
{::db/return-keys false}))
|
|
|
|
;; Update the current profile status in relation to the current thread
|
|
(upsert-comment-thread-status! conn profile-id thread-id)
|
|
|
|
(send-comment-emails! cfg profile comment thread file)
|
|
|
|
(vary-meta comment assoc ::audit/props (assoc comment :team-id team-id))))))
|
|
|
|
;; --- COMMAND: Update Comment
|
|
|
|
(def ^:private
|
|
schema:update-comment
|
|
[:map {:title "update-comment"}
|
|
[:id ::sm/uuid]
|
|
[:content [:string {:max comment-max-length}]]
|
|
[:share-id {:optional true} [:maybe ::sm/uuid]]
|
|
[:mentions {:optional true} [::sm/set ::sm/uuid]]])
|
|
|
|
;; TODO: Check if there are new mentions, if there are send the new emails.
|
|
|
|
(sv/defmethod ::update-comment
|
|
{::doc/added "1.15"
|
|
::sm/params schema:update-comment
|
|
::db/transaction true}
|
|
[{:keys [::db/conn] :as cfg} {:keys [::rpc/profile-id ::rpc/request-at id share-id content mentions]}]
|
|
(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)
|
|
|
|
perms (files/check-comment-permissions! cfg profile-id file-id share-id)]
|
|
|
|
(check-page-scope perms page-id)
|
|
|
|
;; Don't allow edit comments to not owners
|
|
(when-not (= owner-id profile-id)
|
|
(ex/raise :type :validation
|
|
:code :not-allowed))
|
|
|
|
(let [{:keys [page-name]} (get-file cfg file-id page-id)]
|
|
(db/update! conn :comment
|
|
{:content content
|
|
:modified-at request-at
|
|
:mentions (db/encode-pgarray mentions conn "uuid")}
|
|
{:id id})
|
|
|
|
(db/update! conn :comment-thread
|
|
{:modified-at request-at
|
|
:page-name page-name
|
|
:mentions
|
|
(-> (:mentions thread)
|
|
(into mentions)
|
|
(db/encode-pgarray conn "uuid"))}
|
|
{:id thread-id}
|
|
{::db/return-keys false})
|
|
nil)))
|
|
|
|
;; --- COMMAND: Delete Comment Thread
|
|
|
|
(def ^:private
|
|
schema:delete-comment-thread
|
|
[:map {:title "delete-comment-thread"}
|
|
[:id ::sm/uuid]
|
|
[:share-id {:optional true} [:maybe ::sm/uuid]]])
|
|
|
|
;; The thread delete that follows clears `thread_id` and `comment_id` of
|
|
;; the task rows through two ON DELETE SET NULL actions, whose order
|
|
;; Postgres picks by trigger name. A row this statement already updated
|
|
;; gets its FK rechecked by the first of them, and when that is the
|
|
;; `thread_id` one, `comment_id` still names a message the cascade has
|
|
;; deleted. Clearing both here leaves the delete no referencing row.
|
|
(def ^:private sql:delete-thread-tasks
|
|
"UPDATE file_task
|
|
SET deleted_at = ?,
|
|
updated_at = ?,
|
|
thread_id = NULL,
|
|
comment_id = NULL
|
|
WHERE thread_id = ?
|
|
AND deleted_at IS NULL")
|
|
|
|
(defn- delete-thread-tasks
|
|
"Delete the tasks of the thread `thread-id`, which live in its messages,
|
|
and tell the open clients of `file-id` once the transaction commits."
|
|
[{:keys [::db/conn ::mbus/msgbus]} profile-id file-id thread-id]
|
|
(let [ts (ct/now)
|
|
result (db/exec-one! conn [sql:delete-thread-tasks ts ts thread-id])]
|
|
(when (pos? (db/get-update-count result))
|
|
(db/after-commit
|
|
#(mbus/pub! msgbus
|
|
:topic file-id
|
|
:message {:type :file-tasks-change
|
|
:file-id file-id
|
|
:profile-id profile-id})))))
|
|
|
|
(sv/defmethod ::delete-comment-thread
|
|
"Delete a thread with its messages and the tasks they hold."
|
|
{::doc/added "1.15"
|
|
::sm/params schema:delete-comment-thread
|
|
::db/transaction true}
|
|
[{:keys [::db/conn] :as cfg} {:keys [::rpc/profile-id id share-id]}]
|
|
(let [{:keys [owner-id file-id page-id] :as thread}
|
|
(get-comment-thread conn id ::sql/for-update true)
|
|
|
|
perms (files/check-comment-permissions! cfg profile-id file-id share-id)]
|
|
|
|
(check-page-scope perms page-id)
|
|
|
|
(when-not (= owner-id profile-id)
|
|
(ex/raise :type :validation
|
|
:code :not-allowed))
|
|
|
|
(delete-thread-tasks cfg profile-id file-id id)
|
|
(db/delete! conn :comment-thread {:id id}
|
|
{::db/return-keys false})
|
|
nil))
|
|
|
|
;; --- COMMAND: Delete comment
|
|
|
|
(def ^:private
|
|
schema:delete-comment
|
|
[:map {:title "delete-comment"}
|
|
[:id ::sm/uuid]
|
|
[:share-id {:optional true} [:maybe ::sm/uuid]]])
|
|
|
|
(sv/defmethod ::delete-comment
|
|
{::doc/added "1.15"
|
|
::sm/params schema:delete-comment
|
|
::db/transaction true}
|
|
[{:keys [::db/conn] :as cfg} {:keys [::rpc/profile-id id share-id]}]
|
|
(let [{:keys [owner-id thread-id] :as comment}
|
|
(get-comment conn id ::sql/for-update true)
|
|
|
|
{:keys [file-id page-id]}
|
|
(get-comment-thread conn thread-id)
|
|
|
|
perms (files/check-comment-permissions! cfg profile-id file-id share-id)]
|
|
|
|
(check-page-scope perms page-id)
|
|
(when-not (= owner-id profile-id)
|
|
(ex/raise :type :validation
|
|
:code :not-allowed))
|
|
|
|
(db/delete! conn :comment {:id id}
|
|
{::db/return-keys false})
|
|
nil))
|
|
|
|
;; --- COMMAND: Update comment thread position
|
|
|
|
(def ^:private
|
|
schema:update-comment-thread-position
|
|
[:map {:title "update-comment-thread-position"}
|
|
[:id ::sm/uuid]
|
|
[:position ::gpt/point]
|
|
[:frame-id ::sm/uuid]
|
|
[:share-id {:optional true} [:maybe ::sm/uuid]]])
|
|
|
|
(sv/defmethod ::update-comment-thread-position
|
|
{::doc/added "1.15"
|
|
::sm/params schema:update-comment-thread-position
|
|
::db/transaction true}
|
|
[{:keys [::db/conn] :as cfg} {:keys [::rpc/profile-id ::rpc/request-at id position frame-id share-id]}]
|
|
(let [thread (get-comment-thread conn id ::sql/for-update true)
|
|
perms (files/check-comment-permissions! cfg profile-id (:file-id thread) share-id)]
|
|
(check-page-scope perms (:page-id thread))
|
|
(db/update! conn :comment-thread
|
|
{:modified-at request-at
|
|
:position (db/pgpoint position)
|
|
:frame-id frame-id}
|
|
{:id id}
|
|
{::db/return-keys false})
|
|
nil))
|
|
|
|
;; --- COMMAND: Update comment frame
|
|
|
|
(def ^:private
|
|
schema:update-comment-thread-frame
|
|
[:map {:title "update-comment-thread-frame"}
|
|
[:id ::sm/uuid]
|
|
[:frame-id ::sm/uuid]
|
|
[:share-id {:optional true} [:maybe ::sm/uuid]]])
|
|
|
|
(sv/defmethod ::update-comment-thread-frame
|
|
{::doc/added "1.15"
|
|
::sm/params schema:update-comment-thread-frame
|
|
::db/transaction true}
|
|
[{:keys [::db/conn] :as cfg} {:keys [::rpc/profile-id ::rpc/request-at id frame-id share-id]}]
|
|
(let [thread (get-comment-thread conn id ::sql/for-update true)
|
|
perms (files/check-comment-permissions! cfg profile-id (:file-id thread) share-id)]
|
|
(check-page-scope perms (:page-id thread))
|
|
(db/update! conn :comment-thread
|
|
{:modified-at request-at
|
|
:frame-id frame-id}
|
|
{:id id}
|
|
{::db/return-keys false})
|
|
nil))
|
|
|
|
;; --- COMMAND: Mark all comment threads as read
|
|
|
|
;; The dashboard sends the thread ids in batches of this same size
|
|
;; (frontend/src/app/main/data/comments.cljs), so the batch has to fit in
|
|
;; this limit.
|
|
(def ^:private max-mark-all-threads 500)
|
|
|
|
(def ^:private
|
|
schema:mark-all-threads-as-read
|
|
[:map {:title "mark-all-threads-as-read"}
|
|
[:threads [:vector {:max max-mark-all-threads} ::sm/uuid]]])
|
|
|
|
(def ^:private sql:comment-threads-files
|
|
"SELECT ct.id, ct.file_id
|
|
FROM comment_thread ct
|
|
WHERE ct.id = ANY(?::uuid[])")
|
|
|
|
(def ^:private sql:upsert-comment-thread-status-batch
|
|
"insert into comment_thread_status (thread_id, profile_id, modified_at)
|
|
select t.id, ?::uuid, ?::timestamptz
|
|
from unnest(?::uuid[]) as t(id)
|
|
on conflict (thread_id, profile_id)
|
|
do update set modified_at = excluded.modified_at;")
|
|
|
|
(defn- get-comment-threads-files
|
|
[conn thread-ids]
|
|
(db/exec! conn [sql:comment-threads-files
|
|
(db/create-array conn "uuid" thread-ids)]))
|
|
|
|
(defn- check-threads-access
|
|
"Raises a :not-found error when any of the threads does not exist or the
|
|
caller has no access to the file that owns it."
|
|
[cfg profile-id thread-ids]
|
|
(let [rows (get-comment-threads-files (::db/conn cfg) thread-ids)
|
|
found (into #{} (map :id) rows)]
|
|
(when-not (= (count thread-ids) (count found))
|
|
(ex/raise :type :not-found
|
|
:code :object-not-found
|
|
:hint "not found"))
|
|
(doseq [file-id (distinct (map :file-id rows))]
|
|
(files/check-comment-permissions! cfg profile-id file-id nil))))
|
|
|
|
(defn- upsert-comment-thread-status-batch
|
|
[conn profile-id thread-ids]
|
|
(db/exec! conn [sql:upsert-comment-thread-status-batch
|
|
profile-id
|
|
(ct/in-future "1s")
|
|
(db/create-array conn "uuid" thread-ids)]))
|
|
|
|
(sv/defmethod ::mark-all-threads-as-read
|
|
"Marks the given comment threads as read for the current profile. The
|
|
caller needs to have access to the file that owns every thread."
|
|
{::doc/added "1.15"
|
|
::doc/changes [["2.19" "The caller needs access to the file that owns every thread and the number of threads is limited to 500"]]
|
|
::sm/params schema:mark-all-threads-as-read}
|
|
[cfg {:keys [::rpc/profile-id threads]}]
|
|
(let [thread-ids (distinct threads)]
|
|
;; Nothing to check and nothing to write: an empty list must not open a
|
|
;; transaction nor run the queries with an empty array.
|
|
(when (seq thread-ids)
|
|
(db/tx-run!
|
|
cfg
|
|
(fn [{:keys [::db/conn] :as cfg}]
|
|
(check-threads-access cfg profile-id thread-ids)
|
|
(upsert-comment-thread-status-batch conn profile-id thread-ids)
|
|
nil)))))
|