mirror of
https://github.com/penpot/penpot.git
synced 2025-06-15 15:21:40 +02:00
✨ Add better reusability for comment related queries
This commit is contained in:
parent
aa583b0707
commit
45d5253915
1 changed files with 33 additions and 73 deletions
|
@ -243,30 +243,33 @@
|
||||||
(get-comment-threads conn profile-id file-id))))
|
(get-comment-threads conn profile-id file-id))))
|
||||||
|
|
||||||
(def ^:private sql:comment-threads
|
(def ^:private sql:comment-threads
|
||||||
"select distinct on (ct.id)
|
"SELECT DISTINCT ON (ct.id)
|
||||||
ct.*,
|
ct.*,
|
||||||
f.name as file_name,
|
p.team_id AS team_id,
|
||||||
f.project_id as project_id,
|
f.name AS file_name,
|
||||||
first_value(c.content) over w as content,
|
f.project_id AS project_id,
|
||||||
(select count(1)
|
first_value(c.content) OVER w AS content,
|
||||||
from comment as c
|
(SELECT count(1)
|
||||||
where c.thread_id = ct.id) as count_comments,
|
FROM comment AS c
|
||||||
(select count(1)
|
WHERE c.thread_id = ct.id) AS count_comments,
|
||||||
from comment as c
|
(SELECT count(1)
|
||||||
where c.thread_id = ct.id
|
FROM comment AS c
|
||||||
and c.created_at >= coalesce(cts.modified_at, ct.created_at)) as count_unread_comments
|
WHERE c.thread_id = ct.id
|
||||||
from comment_thread as ct
|
AND c.created_at >= coalesce(cts.modified_at, ct.created_at)) AS count_unread_comments
|
||||||
inner join comment as c on (c.thread_id = ct.id)
|
FROM comment_thread AS ct
|
||||||
inner join file as f on (f.id = ct.file_id)
|
INNER JOIN comment AS c ON (c.thread_id = ct.id)
|
||||||
left join comment_thread_status as cts
|
INNER JOIN file AS f ON (f.id = ct.file_id)
|
||||||
on (cts.thread_id = ct.id and
|
INNER JOIN project AS p ON (p.id = f.project_id)
|
||||||
cts.profile_id = ?)
|
LEFT JOIN comment_thread_status AS cts ON (cts.thread_id = ct.id AND cts.profile_id = ?)
|
||||||
where ct.file_id = ?
|
WINDOW w AS (PARTITION BY c.thread_id ORDER BY c.created_at ASC)")
|
||||||
window w as (partition by c.thread_id order by c.created_at asc)")
|
|
||||||
|
(def ^:private sql:comment-threads-by-file-id
|
||||||
|
(str "WITH threads AS (" sql:comment-threads ")"
|
||||||
|
"SELECT * FROM threads WHERE file_id = ?"))
|
||||||
|
|
||||||
(defn- get-comment-threads
|
(defn- get-comment-threads
|
||||||
[conn profile-id file-id]
|
[conn profile-id file-id]
|
||||||
(->> (db/exec! conn [sql:comment-threads profile-id file-id])
|
(->> (db/exec! conn [sql:comment-threads-by-file-id profile-id file-id])
|
||||||
(into [] xf-decode-row)))
|
(into [] xf-decode-row)))
|
||||||
|
|
||||||
;; --- COMMAND: Get Unread Comment Threads
|
;; --- COMMAND: Get Unread Comment Threads
|
||||||
|
@ -288,61 +291,18 @@
|
||||||
(teams/check-read-permissions! conn profile-id team-id)
|
(teams/check-read-permissions! conn profile-id team-id)
|
||||||
(get-unread-comment-threads conn profile-id team-id))))
|
(get-unread-comment-threads conn profile-id team-id))))
|
||||||
|
|
||||||
(def sql:all-comment-threads-by-team
|
|
||||||
"select distinct on (ct.id)
|
|
||||||
ct.*,
|
|
||||||
f.name as file_name,
|
|
||||||
f.project_id as project_id,
|
|
||||||
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 = ?)
|
|
||||||
where p.team_id = ?
|
|
||||||
window w as (partition by c.thread_id order by c.created_at asc)")
|
|
||||||
|
|
||||||
(def sql:unread-all-comment-threads-by-team
|
(def sql:unread-all-comment-threads-by-team
|
||||||
(str "with threads as (" sql:all-comment-threads-by-team ")"
|
(str "WITH threads AS (" sql:comment-threads ")"
|
||||||
"select * from threads where count_unread_comments > 0"))
|
"SELECT * FROM threads WHERE count_unread_comments > 0 AND team_id = ?"))
|
||||||
|
|
||||||
;; The partial configuration will retrieve only comments created by the user and
|
;; The partial configuration will retrieve only comments created by the user and
|
||||||
;; threads that have a mention to the user.
|
;; threads that have a mention to the user.
|
||||||
(def sql:partial-comment-threads-by-team
|
|
||||||
"SELECT DISTINCT ON (ct.id)
|
|
||||||
ct.*,
|
|
||||||
ct.owner_id,
|
|
||||||
f.name AS file_name,
|
|
||||||
f.project_id AS project_id,
|
|
||||||
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 = ?)
|
|
||||||
WHERE p.team_id = ?
|
|
||||||
AND (ct.owner_id = ? OR ? = any(ct.mentions))
|
|
||||||
WINDOW w AS (PARTITION BY c.thread_id ORDER BY c.created_at ASC)")
|
|
||||||
|
|
||||||
(def sql:unread-partial-comment-threads-by-team
|
(def sql:unread-partial-comment-threads-by-team
|
||||||
(str "WITH threads AS (" sql:partial-comment-threads-by-team ")"
|
(str "WITH threads AS (" sql:comment-threads ")"
|
||||||
"SELECT * FROM threads WHERE count_unread_comments > 0"))
|
"SELECT * FROM threads
|
||||||
|
WHERE count_unread_comments > 0
|
||||||
|
AND team_id = ?
|
||||||
|
AND (owner_id = ? OR ? = ANY(mentions))"))
|
||||||
|
|
||||||
(defn- get-unread-comment-threads
|
(defn- get-unread-comment-threads
|
||||||
[conn profile-id team-id]
|
[conn profile-id team-id]
|
||||||
|
@ -376,9 +336,9 @@
|
||||||
[cfg {:keys [::rpc/profile-id file-id id share-id] :as params}]
|
[cfg {:keys [::rpc/profile-id file-id 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)
|
||||||
(let [sql (str "with threads as (" sql:comment-threads ")"
|
(let [sql (str "WITH threads AS (" sql:comment-threads ")"
|
||||||
"select * from threads where id = ?")]
|
"SELECT * FROM threads WHERE id = ? AND file_id = ?")]
|
||||||
(-> (db/exec-one! conn [sql profile-id file-id id])
|
(-> (db/exec-one! conn [sql profile-id id file-id])
|
||||||
(decode-row))))))
|
(decode-row))))))
|
||||||
|
|
||||||
;; --- COMMAND: Retrieve Comments
|
;; --- COMMAND: Retrieve Comments
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue