diff --git a/backend/src/app/migrations.clj b/backend/src/app/migrations.clj index 4bff5299e6..c3c53b5399 100644 --- a/backend/src/app/migrations.clj +++ b/backend/src/app/migrations.clj @@ -599,7 +599,10 @@ :fn (mg/resource "app/migrations/sql/0816-mod-file-task-add-created-thread.sql")} {:name "0817-file-family-and-branch-review" - :fn (mg/resource "app/migrations/sql/0817-file-family-and-branch-review.sql")}]) + :fn (mg/resource "app/migrations/sql/0817-file-family-and-branch-review.sql")} + + {:name "0818-mod-comment-thread-add-lost-layer" + :fn (mg/resource "app/migrations/sql/0818-mod-comment-thread-add-lost-layer.sql")}]) (defn apply-migrations! [pool name migrations] diff --git a/backend/src/app/migrations/sql/0818-mod-comment-thread-add-lost-layer.sql b/backend/src/app/migrations/sql/0818-mod-comment-thread-add-lost-layer.sql new file mode 100644 index 0000000000..91dd4f9182 --- /dev/null +++ b/backend/src/app/migrations/sql/0818-mod-comment-thread-add-lost-layer.sql @@ -0,0 +1,22 @@ +-- A comment thread whose layer did not reach the target of a merge keeps +-- the name of that layer in `lost_shape_name`, as a task does +-- (`file_task.lost_shape_name`), so the thread can say which layer it +-- lost and offer to link it to another one. Linking the thread to a +-- layer again clears it. +-- +-- A review thread that the merge leaves behind on the branch can be +-- moved to the target later, once the branch document is gone. For such +-- a thread whose layer the target lacks, the merge stores what the later +-- move needs to land it: the ancestors of the layer in the branch +-- document, nearest first and without the root frame +-- (`landing_ancestors`), and the rectangle of the canvas the layer +-- covered (`landing_area`, the `x`, `y`, `width` and `height` of the +-- layer). The later move lands the thread on the first of those +-- ancestors that the target has, else on the area, and clears both +-- columns. `landing_area` is not the `area` column: `area` is what an +-- `area` thread points at, and the thread still points at its layer. + +ALTER TABLE comment_thread + ADD COLUMN lost_shape_name text NULL, + ADD COLUMN landing_ancestors uuid[] NULL, + ADD COLUMN landing_area jsonb NULL; diff --git a/backend/src/app/rpc/commands/comments.clj b/backend/src/app/rpc/commands/comments.clj index a1f4e98342..f3e955ef21 100644 --- a/backend/src/app/rpc/commands/comments.clj +++ b/backend/src/app/rpc/commands/comments.clj @@ -180,8 +180,11 @@ :comment-url comment-url}))))))) (defn- decode-row + "Decode a thread or comment row for the client. The landing columns of + a thread are what a later move of a thread left behind by a merge + reads (`files-task/move-branch-review-thread`), not for the client." [{:keys [participants position mentions area anchor-type] :as row}] - (cond-> row + (cond-> (dissoc row :landing-ancestors :landing-area) (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)) diff --git a/backend/src/app/rpc/commands/files.clj b/backend/src/app/rpc/commands/files.clj index 241e17c62d..23bc1b46bc 100644 --- a/backend/src/app/rpc/commands/files.clj +++ b/backend/src/app/rpc/commands/files.clj @@ -296,10 +296,13 @@ f.is_shared, ft.media_id AS thumbnail_id, p.team_id, + -- the live branches of the family, archived ones included: + -- the set the branches-per-file quote counts (select count(*) from file_branch as fb - where fb.source_file_id = f.id + join file as sf on (sf.id = fb.source_file_id) + where sf.family_id = f.family_id and fb.deleted_at is null - and fb.status = 'open') as branches_count + and fb.status <> 'merged') as branches_count from file as f inner join project as p on (p.id = f.project_id) left join file_thumbnail as ft on (ft.file_id = f.id @@ -858,10 +861,12 @@ f.name, f.is_shared, ft.media_id AS thumbnail_id, + -- the same count as sql:project-files (select count(*) from file_branch as fb - where fb.source_file_id = f.id + join file as sf on (sf.id = fb.source_file_id) + where sf.family_id = f.family_id and fb.deleted_at is null - and fb.status = 'open') as branches_count, + and fb.status <> 'merged') as branches_count, row_number() over w as row_num, p.team_id from file as f diff --git a/backend/src/app/rpc/commands/files_branch.clj b/backend/src/app/rpc/commands/files_branch.clj index 79d84d677b..34b0820136 100644 --- a/backend/src/app/rpc/commands/files_branch.clj +++ b/backend/src/app/rpc/commands/files_branch.clj @@ -585,6 +585,32 @@ (bfc/update-file-row! cfg file) nil)) +;; --- Helpers: a branch as the source of another (`:branch-stacking`) + +(def ^:private sql:get-source-edge + "SELECT f.is_branch, fb.status, fb.deleted_at + FROM file AS f + LEFT JOIN file_branch AS fb ON (fb.branch_file_id = f.id) + WHERE f.id = ?") + +(defn- check-source-open + "Refuse to use the file `file-id` as the source of a branch edge (cut a + branch from it, merge into it, update from it) when the file is itself a + branch whose own edge is not open: an archived branch is set aside, and + a merged or deleted one is gone. A root file, and a file detached from + its branch, pass. A caller that writes into `file-id` or pins it runs + the check under the lock of `file-id`, the lock an archive takes, so + the archive cannot commit between the check and the write." + [conn file-id] + (let [{:keys [is-branch status deleted-at]} (db/exec-one! conn [sql:get-source-edge file-id])] + (when (and is-branch + (or (not= "open" status) (some? deleted-at))) + (ex/raise :type :validation + :code :source-branch-not-open + :hint "the source of this operation is a branch that is not open" + :file-id file-id + :status status)))) + ;; --- COMMAND: create-file-branch (defn- check-branch-name! @@ -612,7 +638,7 @@ reviews, threads, and tasks of every file in it. A branch of a branch is refused unless the `:branch-stacking` flag is - on." + on, and then only while the branch it is cut from is open." {::doc/added "2.16" ::webhooks/event? true ::sm/params schema:create-file-branch @@ -642,12 +668,24 @@ ;; (same advisory lock) so the base snapshot and the ;; branch's initial state (the empty op log over that ;; base) are taken from the SAME state of main. - (db/xact-lock! conn file-id) + ;; + ;; The family id is locked too: the quota counts the + ;; whole family, and two creators cutting from two files + ;; of it (main and a child of main) would otherwise take + ;; two different locks and both pass the quota for the + ;; last slot. Sorted, as `merge-file-branch` takes its + ;; two, to avoid deadlocks. The family id is the root + ;; file id, so branching the root takes one lock. + (run! (partial db/xact-lock! conn) + (sort (distinct [file-id (:family-id file-row)]))) + + ;; an archived branch is no source for a new one + (check-source-open conn file-id) ;; The quota check runs INSIDE the transaction and - ;; AFTER the lock: two creators racing for the last - ;; branch slot serialize on the lock, and the loser's - ;; count must see the winner's committed row. + ;; AFTER the locks: two creators racing for the last + ;; branch slot serialize on the family lock, and the + ;; loser's count must see the winner's committed row. ;; `quotes/check!` reads through `::db/conn`, so it ;; counts on this transaction's connection. (-> cfg @@ -1247,6 +1285,9 @@ (ex/raise :type :validation :code :branch-not-open :branch-id branch-id)) + ;; a merge into a branch needs that branch open: an + ;; archived target is set aside (`:branch-stacking`) + _ (check-source-open conn main-id) [main-stored main-file] (read-main-for-write cfg main-id :merge) _ (check-oplog-depth-limit! (bfc/get-branch-oplog-depth cfg branch-file-id) branch-file-id @@ -1600,6 +1641,9 @@ (ex/raise :type :validation :code :branch-not-open :branch-id branch-id)) + ;; an archived source is set aside, so nothing flows + ;; from it either (`:branch-stacking`) + _ (check-source-open conn main-id) [main-stored main-file] (read-main-for-write cfg main-id :update-from-source) _ (when (and (some? expected-source-revn) (not= expected-source-revn (:revn main-file))) @@ -2106,25 +2150,27 @@ [:file-id ::sm/uuid]]) (def ^:private sql:get-file-branch-count + ;; the same count as the `branches-per-file` quote + ;; (`quotes.clj::sql:get-branches-per-file`), so the number a user sees + ;; is the number the limit is checked against "SELECT count(*) AS count FROM file_branch AS fb - WHERE fb.deleted_at IS NULL - AND fb.status != 'merged' - AND fb.source_file_id = coalesce((SELECT b.source_file_id - FROM file_branch AS b - WHERE b.branch_file_id = ? - AND b.deleted_at IS NULL), - ?)") + JOIN file AS sf ON (sf.id = fb.source_file_id) + WHERE sf.family_id = (SELECT f.family_id FROM file AS f WHERE f.id = ?) + AND fb.deleted_at IS NULL + AND fb.status != 'merged'") (sv/defmethod ::get-file-branch-count - "How many branches the file has, archived ones included, as the - listing with `:include-archived` returns them. When the file is - itself a branch, the branches of its source (main) are counted. + "How many live branches the family of the file has, archived ones + included and merged ones left out: the count the `branches-per-file` + quote checks. A branch of a branch (`:branch-stacking`) counts in the + family of its root, so a file and every branch in its family answer the + same number. The workspace header asks this on every file open to decide whether to - show its branch row, so it compares nothing and takes no climit: it is - the dashboard's `branches_count` subquery, one indexed count. Returns a - zero count when branching is disabled, so it is safe on every open." + show its branch row, so it compares nothing and takes no climit: one + indexed count. Returns a zero count when branching is disabled, so it + is safe on every open." {::doc/added "2.16" ::sm/params schema:get-file-branch-count} [cfg {:keys [::rpc/profile-id file-id]}] @@ -2132,7 +2178,7 @@ (db/run! cfg (fn [{:keys [::db/conn] :as cfg}] (files/check-read-permissions! cfg profile-id file-id) - (-> (db/exec-one! conn [sql:get-file-branch-count file-id file-id]) + (-> (db/exec-one! conn [sql:get-file-branch-count file-id]) (select-keys [:count])))) {:count 0})) diff --git a/backend/src/app/rpc/commands/files_branch_review.clj b/backend/src/app/rpc/commands/files_branch_review.clj index 195cf86d5a..95374902ca 100644 --- a/backend/src/app/rpc/commands/files_branch_review.clj +++ b/backend/src/app/rpc/commands/files_branch_review.clj @@ -88,7 +88,8 @@ fb.source_file_id AS target_file_id, bf.revn AS branch_revn, sf.revn AS source_revn, - sf.name AS target_name + sf.name AS target_name, + (sf.id = sf.family_id) AS target_is_main FROM file_branch_review AS r JOIN file_branch AS fb ON (fb.id = r.file_branch_id) LEFT JOIN file AS bf ON (bf.id = fb.branch_file_id) @@ -463,7 +464,8 @@ fb.source_file_id AS target_file_id, bf.revn AS branch_revn, sf.revn AS source_revn, - sf.name AS target_name + sf.name AS target_name, + (sf.id = sf.family_id) AS target_is_main FROM file_branch_review AS r JOIN file_branch AS fb ON (fb.id = r.file_branch_id) LEFT JOIN file AS bf ON (bf.id = fb.branch_file_id) @@ -1071,7 +1073,8 @@ fb.branch_file_id AS source_file_id, fb.source_file_id AS target_file_id, fb.name AS branch_name, - sf.name AS target_name + sf.name AS target_name, + (sf.id = sf.family_id) AS target_is_main FROM file_branch_reviewer AS v JOIN file_branch_review AS r ON (r.id = v.review_id) JOIN file_branch AS fb ON (fb.id = r.file_branch_id) diff --git a/backend/src/app/rpc/commands/files_task.clj b/backend/src/app/rpc/commands/files_task.clj index 8f575c2acc..78c5f0a358 100644 --- a/backend/src/app/rpc/commands/files_task.clj +++ b/backend/src/app/rpc/commands/files_task.clj @@ -19,12 +19,16 @@ The merge of a branch moves into its target the threads that hold an open task, whole with their tasks, and the threads written outside any - review (`move-branch-tasks`, called from + review, resolved or not (`move-branch-tasks`, called from `app.rpc.commands.files-branch`). Every other review thread stays on the branch file as the record of its review, and outlives that file: `get-review-threads` reads it and `move-branch-review-thread` moves it - later. Every task, thread, and review belongs to the family of its file - (`file_family`), which the branch shares with its target." + later. A thread or task whose layer did not reach the target lands on + the nearest ancestor of the layer the target has, else on the area the + layer covered, and keeps the name of the lost layer + (`lost-layer-landing`). Every task, thread, and review belongs to the + family of its file (`file_family`), which the branch shares with its + target." (:require [app.binfile.common :as bfc] [app.common.data :as d] @@ -610,29 +614,35 @@ (defn- anchor-thread "Point the thread `thread-id` at the layer `shape-id`; with no layer, at the rectangle `area` of the canvas, or at the spot `position` when there - is no area either." - [conn thread-id {:keys [frame-id shape-id area position]}] + is no area either. The thread keeps `lost-shape-name`, the name of the + layer it lost (nil when it lost none), and drops what a merge stored + for a later move (`keep-lost-layers`)." + [conn thread-id {:keys [frame-id shape-id area position lost-shape-name]}] (db/update! conn :comment-thread - (cond - (some? shape-id) - {:frame-id frame-id - :shape-id shape-id - :anchor-type "layer" - :area nil} + (merge + {:lost-shape-name lost-shape-name + :landing-ancestors nil + :landing-area nil} + (cond + (some? shape-id) + {:frame-id frame-id + :shape-id shape-id + :anchor-type "layer" + :area nil} - (some? area) - {:frame-id frame-id - :shape-id nil - :anchor-type "area" - :area (db/tjson area) - :position (db/pgpoint position)} + (some? area) + {:frame-id frame-id + :shape-id nil + :anchor-type "area" + :area (db/tjson area) + :position (db/pgpoint position)} - :else - {:frame-id frame-id - :shape-id nil - :anchor-type "point" - :area nil - :position (db/pgpoint position)}) + :else + {:frame-id frame-id + :shape-id nil + :anchor-type "point" + :area nil + :position (db/pgpoint position)})) {:id thread-id} {::db/return-keys false})) @@ -679,6 +689,75 @@ (insert-event conn id profile-id "relinked" {:shape-id shape-id :shape-name shape-name}) (merge task changes)))))) +;; --- COMMAND: relink-comment-thread + +(def ^:private schema:relink-comment-thread + [:map {:title "relink-comment-thread" :closed true} + [:id ::sm/uuid] + [:page-id ::sm/uuid] + [:frame-id ::sm/uuid] + [:shape-id ::sm/uuid] + ;; the name the layer has now, kept in the events of the tasks that + ;; follow the thread + [:shape-name [:string {:max 250}]]]) + +(def ^:private sql:get-tasks-created-with-thread + "SELECT * FROM file_task + WHERE thread_id = ? + AND created_thread + AND deleted_at IS NULL + FOR UPDATE") + +(sv/defmethod ::relink-comment-thread + "Point a comment thread at a layer of its page. A thread whose layer + was lost stops being lost, and drops what a merge stored to land it + later. The tasks created with the thread, whose mark on the canvas the + thread is, follow it and stop being lost, each with a `relinked` + event, as the thread created with a task follows the task + (`relink-task`). A task made from a message of the thread keeps its + own layer. Returns the thread id and the ids of the tasks that + followed." + {::doc/added "2.16" + ::sm/params schema:relink-comment-thread} + [cfg {:keys [::rpc/profile-id ::rpc/session-id ::rpc/request-at id page-id frame-id shape-id shape-name]}] + (let [thread (db/run! cfg (fn [{:keys [::db/conn]}] (db/get* conn :comment-thread {:id id})))] + (when (or (nil? thread) (nil? (:file-id thread))) + (ex/raise :type :not-found + :code :thread-not-found + :hint "unable to find a comment thread with the provided id" + :thread-id id)) + (files/check-comment-permissions! cfg profile-id (:file-id thread) nil) + (when (not= page-id (:page-id thread)) + (ex/raise :type :validation + :code :layer-not-on-thread-page + :hint "the layer must be on the page of the thread" + :thread-id id)) + (run-and-notify + cfg {:file-id (:file-id thread) :profile-id profile-id :session-id session-id} + :task-ids + (fn [{:keys [::db/conn]}] + (db/get conn :comment-thread {:id id} {::sql/for-update true}) + (anchor-thread conn id {:frame-id frame-id :shape-id shape-id}) + (db/update! conn :comment-thread + {:modified-at (or request-at (ct/now))} + {:id id} + {::db/return-keys false}) + (let [ts (ct/now) + tasks (db/exec! conn [sql:get-tasks-created-with-thread id])] + (doseq [{task-id :id} tasks] + (db/update! conn :file-task + {:page-id page-id + :frame-id frame-id + :shape-id shape-id + :lost-shape-name nil + :updated-at ts} + {:id task-id} + {::db/return-keys false}) + (insert-event conn task-id profile-id "relinked" + {:shape-id shape-id :shape-name shape-name})) + {:thread-id id + :task-ids (mapv :id tasks)}))))) + ;; --- COMMAND: mark-tasks-layer-deleted (def ^:private schema:deleted-layer @@ -1011,44 +1090,82 @@ [data page-id shape-id] (get-in data [:pages-index page-id :objects shape-id])) +(defn layer-ancestor-ids + "The ids of the ancestors of the layer `shape-id` among `objects`, + nearest first, without the root frame." + [objects shape-id] + (into [] (remove #(= uuid/zero %)) (cfh/get-parent-ids objects shape-id))) + +(defn lost-layer + "What a mark needs to land on a target that lacks its layer `shape-id` + of the page `page-id`: the name of the layer, its ancestors + (`layer-ancestor-ids`), and the area of the canvas it covered, read + from the first document of `datas` that has the layer. Nil when none + has it." + [datas page-id shape-id] + (some (fn [data] + (let [objects (get-in data [:pages-index page-id :objects])] + (when-let [layer (get objects shape-id)] + {:shape-name (:name layer) + :ancestor-ids (layer-ancestor-ids objects shape-id) + :area (when-let [{:keys [x y width height]} (:selrect layer)] + {:x x :y y :width width :height height})}))) + datas)) + +(defn lost-layer-landing + "Where a mark lands among `objects`, the layers of its page on the + target, when the target lacks its layer `lost` (`lost-layer`, nil when + unknown): on the nearest ancestor of the layer the target has, so an + excluded group does not strand it, else on the area the layer covered, + else on the spot `position`. The root frame is no landing. Every + landing keeps the name of the lost layer as `:lost-shape-name`." + [objects {:keys [shape-name ancestor-ids area]} position] + (let [ancestor (some (fn [id] + (let [shape (get objects id)] + (when (and (some? shape) (not (cfh/root? shape))) + shape))) + ancestor-ids)] + (cond + (some? ancestor) + {:shape-id (:id ancestor) + :shape-name (:name ancestor) + :frame-id (if (cfh/frame-shape? ancestor) (:id ancestor) (:frame-id ancestor)) + :lost-shape-name shape-name} + + (some? area) + {:frame-id uuid/zero + :area area + ;; the top right corner, where the mark of a task goes + :position (gpt/point (+ (:x area) (:width area)) (:y area)) + :lost-shape-name shape-name} + + :else + {:frame-id uuid/zero + :position position + :lost-shape-name shape-name}))) + (defn- layer-landing "Where a mark on the layer `shape-id` lands on the target when the - target lacks the layer: on the immediate parent of the layer when the - target has it, otherwise on the area of the canvas the layer covered. - The layer is read from the branch document, or from the merge base when - the branch deleted it; when neither has it, the mark keeps its spot - `position` on the canvas. Nil when the mark stays as it is: it points - at the canvas, its layer is on the target, or its page is not." - [{:keys [merged-data branch-data base-data]} page-id shape-id position] + target lacks the layer (`lost-layer-landing`). The layer is the one + `lost-layers` stores for `shape-id`, else the one of the branch + document, else the one of the merge base. Nil when the mark stays as + it is: it points at the canvas, its layer is on the target, or its + page is not." + [{:keys [merged-data branch-data base-data lost-layers]} page-id shape-id position] (when (and (some? shape-id) (contains? (:pages-index merged-data) page-id) (nil? (find-shape merged-data page-id shape-id))) - (let [layer (or (find-shape branch-data page-id shape-id) - (find-shape base-data page-id shape-id)) - parent (some->> (:parent-id layer) (find-shape merged-data page-id))] - (cond - (and (some? parent) (not (cfh/root? parent))) - {:shape-id (:id parent) - :shape-name (:name parent) - :frame-id (if (cfh/frame-shape? parent) (:id parent) (:frame-id parent))} - - (some? layer) - (let [{:keys [x y width height]} (:selrect layer)] - {:frame-id uuid/zero - :area {:x x :y y :width width :height height} - ;; the top right corner, where the mark of a task goes - :position (gpt/point (+ x width) y)}) - - :else - {:frame-id uuid/zero - :position position})))) + (lost-layer-landing (get-in merged-data [:pages-index page-id :objects]) + (or (get lost-layers shape-id) + (lost-layer [branch-data base-data] page-id shape-id)) + position))) (defn- task-landing - "Where an open task of a merged branch lands on the target - (`layer-landing`). Nil for a closed task: it stays as it is." - [datas {:keys [page-id shape-id position] :as task}] - (when (open-task? task) - (layer-landing datas page-id shape-id position))) + "Where a task of a merged branch lands on the target (`layer-landing`). + Closed tasks land too: a task never leaves its thread, so it shares the + thread's place whatever its status." + [datas {:keys [page-id shape-id position]}] + (layer-landing datas page-id shape-id position)) (defn- thread-landing "Where a thread of a merged branch lands on the target @@ -1060,12 +1177,14 @@ (defn- land-task "Point `task` where `task-landing` puts it, its thread created with it - too, and record it as a `relinked` event." - [conn profile-id task {:keys [shape-id shape-name frame-id area position] :as landing}] + too, and record it as a `relinked` event. The task keeps the name of + its lost layer, so it shows as lost and may be linked to another one; + a task whose layer no document has keeps the name it had." + [conn profile-id task {:keys [shape-id shape-name frame-id area position lost-shape-name] :as landing}] (db/update! conn :file-task (cond-> {:shape-id shape-id :frame-id frame-id - :lost-shape-name nil} + :lost-shape-name (or lost-shape-name (:lost-shape-name task))} (some? position) (assoc :position (db/pgpoint position))) {:id (:id task)} {::db/return-keys false}) @@ -1078,19 +1197,29 @@ (defn- move-to-target "Move `threads` and `tasks` into the file `target-id` for the merge `merge-id`. A thread keeps its number, which its family gives it. Each - task records the merge and gets a `merged` event. A thread, or an open - task, whose layer the target lacks lands where `thread-landing` or + task records the merge and gets a `merged` event. A thread or task + whose layer the target lacks lands where `thread-landing` or `task-landing` says, reading the layer in `datas`; a landed task gets - a `relinked` event too." - [conn {:keys [target-id merge-id branch-name profile-id]} datas threads tasks] + a `relinked` event too. A moved thread drops what the merge stored for + a later move (`keep-lost-layers`), and a thread with such a store whose + layer the target has by now stops being lost." + [conn {:keys [target-id merge-id branch-name profile-id]} + {:keys [merged-data lost-layers] :as datas} threads tasks] (let [ts (ct/now)] - (doseq [{:keys [id] :as thread} threads] - (db/update! conn :comment-thread - {:file-id target-id} - {:id id} - {::db/return-keys false}) - (when-let [landing (thread-landing datas thread)] - (anchor-thread conn id landing))) + (doseq [{:keys [id page-id shape-id] :as thread} threads] + (let [landing (thread-landing datas thread)] + (db/update! conn :comment-thread + (cond-> {:file-id target-id + :landing-ancestors nil + :landing-area nil} + (and (nil? landing) + (contains? lost-layers shape-id) + (some? (find-shape merged-data page-id shape-id))) + (assoc :lost-shape-name nil)) + {:id id} + {::db/return-keys false}) + (when (some? landing) + (anchor-thread conn id landing)))) (doseq [{:keys [id] :as task} tasks] (db/update! conn :file-task {:file-id target-id @@ -1119,16 +1248,41 @@ {::sql/columns [:name]}))} open)))) +(def ^:private sql:get-layer-threads + "SELECT id, page_id, shape_id + FROM comment_thread + WHERE file_id = ? + AND anchor_type = 'layer'") + +(defn- keep-lost-layers + "Store on each thread of `threads`, those the merge leaves behind, what + a later move needs to land it when the target lacks its layer + (`move-branch-review-thread`), since the branch document is unreadable + by then: the name of the layer, its ancestors and its area + (`lost-layer`, read from `branch-data`, else `base-data`)." + [conn {:keys [merged-data branch-data base-data]} threads] + (doseq [{:keys [id page-id shape-id]} threads] + (when (nil? (find-shape merged-data page-id shape-id)) + (when-let [{:keys [shape-name ancestor-ids area]} + (lost-layer [branch-data base-data] page-id shape-id)] + (db/update! conn :comment-thread + {:lost-shape-name shape-name + :landing-ancestors (db/create-array conn "uuid" ancestor-ids) + :landing-area (some-> area db/tjson)} + {:id id} + {::db/return-keys false}))))) + (defn move-branch-tasks "Move into the merge target `target-id`, inside the merge transaction, the threads of the branch file that hold an open task, whole with all - their tasks, and the threads written outside any review, with theirs. - An open task with no thread moves too. Every other thread stays on the - branch file with its tasks, as the record of its review. The moved - things land as `move-to-target` says, reading the layers in - `merged-data`, `branch-data` and `base-data`. When `notify-assignees` - is set, the assignees of the moved open tasks are told by email. - Returns the moved tasks. + their tasks, and the threads written outside any review, resolved or + not, with theirs. An open task with no thread moves too. Every other + thread stays on the branch file with its tasks, as the record of its + review, and keeps what a later move needs to land it + (`keep-lost-layers`). The moved things land as `move-to-target` says, + reading the layers in `merged-data`, `branch-data` and `base-data`. + When `notify-assignees` is set, the assignees of the moved open tasks + are told by email. Returns the moved tasks. The branch file row is locked before the tasks are read, as task creation locks it (`lock-file`): a task created meanwhile is either @@ -1146,6 +1300,9 @@ (contains? thread-ids thread-id) (open-task? task))) branch-tasks)] + (keep-lost-layers conn params + (remove #(contains? thread-ids (:id %)) + (db/exec! conn [sql:get-layer-threads branch-file-id]))) (move-to-target conn params params threads tasks) (when notify-assignees (notify-merged-assignees cfg params tasks)) @@ -1164,6 +1321,9 @@ ct.shape_id, ct.anchor_type, ct.position, + ct.lost_shape_name, + ct.landing_ancestors, + ct.landing_area, fb.source_file_id, m.id AS merge_id, m.file_id AS target_id, @@ -1174,6 +1334,16 @@ LEFT JOIN file_branch_merge AS m ON (m.file_branch_id = fb.id) WHERE ct.id = ?") +(defn- stored-lost-layer + "The lost layer the merge stored on a thread it left behind + (`keep-lost-layers`), as `lost-layer` describes one, or nil when it + stored none." + [{:keys [lost-shape-name landing-ancestors landing-area]}] + (when (or (some? lost-shape-name) (some? landing-ancestors) (some? landing-area)) + {:shape-name lost-shape-name + :ancestor-ids (db/decode-pgarray landing-ancestors []) + :area (cond-> landing-area (db/pgobject? landing-area) (db/decode-transit-pgobject))})) + (def ^:private sql:get-thread-tasks "SELECT * FROM file_task WHERE thread_id = ? @@ -1201,9 +1371,12 @@ the merge, whole with its tasks: a review thread with no open task does not move with the merge. Anybody with edit rights on the target may move it. The thread lands on the target as the merge lands it, against the - current data of the target: the branch document of a merged branch is - not readable, so a thread or task whose layer the target lacks keeps - its stored spot on the canvas. Each task records the merge with a + current data of the target. The branch document of a merged branch is + not readable, so a thread whose layer the target lacks lands on what + the merge stored for it (`keep-lost-layers`): the nearest ancestor of + the layer the target has now, else the area the layer covered, else + its stored spot. Its tasks on the same layer land the same way, and + the other ones keep their stored spot. Each task records the merge with a `merged` event, and the assignees of its open tasks are told by a `tasks-merged` email. Refuses a thread of a branch that is not merged, and a thread already on the target." @@ -1236,9 +1409,13 @@ :hint "the thread is already on the target of the merge" :thread-id thread-id)) (let [tasks (into [] (map decode-row) (db/exec! conn [sql:get-thread-tasks thread-id])) - ;; no `branch-data` or `base-data`: a lost layer cannot be - ;; looked up, so `layer-landing` keeps the stored spot - datas {:merged-data (:data (bfc/get-file cfg target-id :realize? true))}] + ;; the branch document is gone: the layer the target + ;; lacks is read from what the merge stored on the thread + ;; (`keep-lost-layers`), which the tasks on the same layer + ;; share + lost (stored-lost-layer thread) + datas {:merged-data (:data (bfc/get-file cfg target-id :realize? true)) + :lost-layers (when (some? lost) {(:shape-id thread) lost})}] (move-to-target conn {:target-id target-id :merge-id merge-id :branch-name branch-name diff --git a/backend/src/app/rpc/quotes.clj b/backend/src/app/rpc/quotes.clj index e55ca6cc5c..a914b18bef 100644 --- a/backend/src/app/rpc/quotes.clj +++ b/backend/src/app/rpc/quotes.clj @@ -494,9 +494,17 @@ (sm/lazy-validator schema:branches-per-file)) (def ^:private sql:get-branches-per-file + ;; The limit holds for the family of the file the branch is cut from: a + ;; branch of a branch (`:branch-stacking`) counts against the same + ;; limit as its parent, so a family never holds more live branches than + ;; the quote allows. An edge belongs to the family of its source file. + ;; An archived branch counts, because its designer still has it and can + ;; restore it. A merged branch does not: its work is in its target and + ;; the merge deleted it. "SELECT count(*) AS total FROM file_branch AS fb - WHERE fb.source_file_id = ? + JOIN file AS sf ON (sf.id = fb.source_file_id) + WHERE sf.family_id = (SELECT f.family_id FROM file AS f WHERE f.id = ?) AND fb.deleted_at IS NULL AND fb.status <> 'merged'") diff --git a/backend/test/backend_tests/branch_family_gc_test.clj b/backend/test/backend_tests/branch_family_gc_test.clj index f92833ad45..06daf1c98d 100644 --- a/backend/test/backend_tests/branch_family_gc_test.clj +++ b/backend/test/backend_tests/branch_family_gc_test.clj @@ -15,6 +15,7 @@ file reaches every branch cut from it, however deep the chain." (:require [app.common.features :as cfeat] + [app.common.files.branch-merge :as bm] [app.common.geom.point :as gpt] [app.common.types.shape :as cts] [app.common.uuid :as uuid] @@ -303,6 +304,122 @@ (t/is (empty? (th/db-query :file-branch-change-mark {:file-branch-id (:id branch)}))))))) +(defn- add-shape* + [profile file-id page-id {:keys [id parent-id frame-id] :as attrs}] + (update-file* profile file-id + [{:type :add-obj + :page-id page-id + :id id + :parent-id parent-id + :frame-id frame-id + :obj (cts/setup-shape attrs)}])) + +(t/deftest a-thread-left-behind-lands-later-on-its-nearest-ancestor + ;; A review thread with no open task stays on the branch at the merge. + ;; Its layer sits in a group the merge left out. Once the GC took the + ;; branch file, moving the thread lands it on the board that held the + ;; group, the nearest ancestor main has, from what the merge stored on + ;; the thread, and the thread keeps the name of its lost layer. + (with-redefs [cf/flags (flags)] + (let [author (th/create-profile* 1 {:is-active true}) + reviewer (th/create-profile* 2 {:is-active true}) + _ (th/create-team-role* {:team-id (:default-team-id author) + :profile-id (:id reviewer) + :role :editor}) + file (th/create-file* 1 {:profile-id (:id author) + :project-id (:default-project-id author)}) + page-id (page-id-of author (:id file)) + board-id (uuid/random) + group-id (uuid/random) + badge-id (uuid/random) + _ (add-shape* author (:id file) page-id + {:id board-id :name "Checkout" :type :frame + :x 0 :y 0 :width 400 :height 800 + :parent-id uuid/zero :frame-id uuid/zero}) + branch (create-branch* author (:id file) "promo-badge") + bfile-id (:branch-file-id branch) + review (create-review* author branch reviewer)] + (add-shape* author bfile-id page-id + {:id group-id :name "Promo" :type :group + :x 20 :y 100 :width 300 :height 48 + :parent-id board-id :frame-id board-id}) + (add-shape* author bfile-id page-id + {:id badge-id :name "Badge" :type :rect + :x 20 :y 100 :width 300 :height 48 + :parent-id group-id :frame-id board-id}) + ;; something the merge carries, besides the group it leaves out + (update-file* author bfile-id + [{:type :mod-obj :page-id page-id :id board-id + :operations [{:type :set :attr :name :val "Checkout v2"}]}]) + (let [thread (create-thread* reviewer bfile-id page-id + {:frame-id board-id + :anchor {:type :layer :shape-id badge-id} + :review-id (:id review)}) + ;; a closed task on the same layer: its own thread stays behind + ;; too, and the task follows that thread when it moves + task (:result (th/command! {::th/type :create-task + ::rpc/profile-id (:id reviewer) + :file-id bfile-id + :page-id page-id + :frame-id board-id + :shape-id badge-id + :position (gpt/point 320 100) + :review-id (:id review) + :title "Shorten the badge"})) + _ (t/is (nil? (:error (th/command! {::th/type :set-task-status + ::rpc/profile-id (:id reviewer) + :id (:id task) + :status "wont-do"})))) + out (th/command! {::th/type :merge-file-branch + ::rpc/profile-id (:id author) + :branch-id (:id branch) + :excluded #{(bm/change-key {:kind :shape :page-id page-id :id group-id})}})] + (t/is (nil? (:error out))) + + (t/testing "the merge leaves the thread behind with what a later move needs" + (let [stored (row :comment-thread (:id thread))] + (t/is (= bfile-id (:file-id stored))) + (t/is (= "Badge" (:lost-shape-name stored))) + (t/is (= [group-id board-id] (db/decode-pgarray (:landing-ancestors stored) []))) + (t/is (some? (:landing-area stored))))) + + (run-gc!) + (t/is (nil? (row :file bfile-id))) + + (t/testing "the review lists the thread with the name of its lost layer only" + (let [listed (->> (th/command! {::th/type :get-branch-review-threads + ::rpc/profile-id (:id author) + :review-id (:id review)}) + :result + (some #(when (= (:id thread) (:id %)) %)))] + (t/is (= "Badge" (:lost-shape-name listed))) + (t/is (not (contains? listed :landing-ancestors))) + (t/is (not (contains? listed :landing-area))))) + + (t/testing "the later move lands it on the board, the group being absent" + (let [out (th/command! {::th/type :move-branch-review-thread + ::rpc/profile-id (:id author) + :thread-id (:id thread)}) + moved (row :comment-thread (:id thread))] + (t/is (nil? (:error out))) + (t/is (= (:id file) (:file-id moved))) + (t/is (= "layer" (:anchor-type moved))) + (t/is (= board-id (:shape-id moved))) + (t/is (= board-id (:frame-id moved))) + (t/is (= "Badge" (:lost-shape-name moved))) + (t/is (nil? (:landing-ancestors moved))) + (t/is (nil? (:landing-area moved))))) + + (t/testing "a closed task lands with its thread on the later move" + (let [out (th/command! {::th/type :move-branch-review-thread + ::rpc/profile-id (:id author) + :thread-id (:thread-id task)}) + moved (row :file-task (:id task))] + (t/is (nil? (:error out))) + (t/is (= (:id file) (:file-id moved))) + (t/is (= board-id (:shape-id moved))) + (t/is (= "Badge" (:lost-shape-name moved))))))))) + (t/deftest a-deleted-unmerged-branch-takes-its-thread-and-review-with-it ;; The mirror: nothing moved a thread or a task to the target, so the ;; branch was all they had. Deleting it deletes them, its review with diff --git a/backend/test/backend_tests/rpc_file_branch_test.clj b/backend/test/backend_tests/rpc_file_branch_test.clj index 8dca399310..8c15680e1b 100644 --- a/backend/test/backend_tests/rpc_file_branch_test.clj +++ b/backend/test/backend_tests/rpc_file_branch_test.clj @@ -73,7 +73,7 @@ src (first (filter #(= (:id %) (:id file)) rows))] (t/is (contains? ids (:id file))) (t/is (not (contains? ids @branch-file-id))) - ;; the source file card reports its open branch count + ;; the source file card reports the live branches of its family (t/is (= 1 (:branches-count src)))))) (t/testing "list branches" @@ -1280,7 +1280,112 @@ :name name}))) child (create (:id file) "child") grand (create (:branch-file-id child) "grandchild")] - {:proj-id proj-id :file file :branches [child grand]})) + {:profile profile :proj-id proj-id :file file :branches [child grand]})) + +(defn- archive-branch* + [profile branch archived] + (th/command! {::th/type :archive-file-branch + ::rpc/profile-id (:id profile) + :branch-id (:id branch) + :archived archived})) + +(t/deftest the-branch-quota-counts-the-whole-family + ;; Counted by direct source, the chain below holds one branch per + ;; source, so a third branch from main passed a quota of two. + (with-redefs [cf/flags (stacking-flags) + cf/get (th/config-get-mock {:quotes-branches-per-file 2})] + (let [{:keys [profile proj-id file branches]} (branch-chain*) + [child grand] branches + create (fn [source-id name] + (th/command! {::th/type :create-file-branch + ::rpc/profile-id (:id profile) + :file-id source-id + :name name})) + count-of (fn [file-id] + (-> (th/command! {::th/type :get-file-branch-count + ::rpc/profile-id (:id profile) + :file-id file-id}) + :result :count))] + + (t/testing "the chain fills the family's two slots" + (doseq [source-id [(:id file) (:branch-file-id child) (:branch-file-id grand)]] + (let [data (-> (create source-id "one-too-many") :error ex-data)] + (t/is (= :restriction (:type data))) + (t/is (= :max-quote-reached (:code data)))))) + + (t/testing "every file of the family shows the count the quota checks" + (doseq [file-id [(:id file) (:branch-file-id child) (:branch-file-id grand)]] + (t/is (= 2 (count-of file-id)))) + (let [rows (:result (th/command! {::th/type :get-project-files + ::rpc/profile-id (:id profile) + :project-id proj-id})) + src (first (filter #(= (:id %) (:id file)) rows))] + (t/is (= 2 (:branches-count src))))) + + (t/testing "an archived branch still counts, a deleted one frees its slot" + (t/is (nil? (:error (archive-branch* profile grand true)))) + (t/is (= 2 (count-of (:id file)))) + (t/is (nil? (:error (th/command! {::th/type :delete-file-branch + ::rpc/profile-id (:id profile) + :branch-id (:id grand)})))) + (t/is (= 1 (count-of (:id file)))) + (t/is (nil? (:error (create (:branch-file-id child) "fits")))))))) + +(t/deftest merge-into-an-archived-branch-is-refused + ;; The merge checked only the merged branch: an archived parent took + ;; its child's changes. + (with-redefs [cf/flags (stacking-flags)] + (let [{:keys [profile branches]} (branch-chain*) + [child grand] branches + _ (apply-change* profile (:branch-file-id grand) (color-change (uuid/random))) + merge-grand (fn [] + (th/command! {::th/type :merge-file-branch + ::rpc/profile-id (:id profile) + :branch-id (:id grand)}))] + (t/is (nil? (:error (archive-branch* profile child true)))) + + (let [data (-> (merge-grand) :error ex-data)] + (t/is (= :validation (:type data))) + (t/is (= :source-branch-not-open (:code data)))) + (t/is (= "open" (:status (th/db-get :file-branch {:id (:id grand)})))) + + (t/testing "the merge goes through once the parent is restored" + (t/is (nil? (:error (archive-branch* profile child false)))) + (let [out (merge-grand)] + (t/is (nil? (:error out))) + (t/is (= :merged (-> out :result :status)))))))) + +(t/deftest an-archived-branch-is-no-source + ;; Creation checked only the stacking flag and the update checked only + ;; the child, so both ran against an archived parent. + (with-redefs [cf/flags (stacking-flags)] + (let [{:keys [profile branches]} (branch-chain*) + [child grand] branches + _ (apply-change* profile (:branch-file-id child) (color-change (uuid/random))) + _ (t/is (nil? (:error (archive-branch* profile child true))))] + + (t/testing "no branch is cut from it" + (let [data (-> (th/command! {::th/type :create-file-branch + ::rpc/profile-id (:id profile) + :file-id (:branch-file-id child) + :name "from-archived"}) + :error ex-data)] + (t/is (= :validation (:type data))) + (t/is (= :source-branch-not-open (:code data))))) + + (t/testing "its child does not update from it" + (let [data (-> (th/command! {::th/type :update-file-branch-from-source + ::rpc/profile-id (:id profile) + :branch-id (:id grand)}) + :error ex-data)] + (t/is (= :validation (:type data))) + (t/is (= :source-branch-not-open (:code data))))) + + (t/testing "a restored branch is a source again" + (t/is (nil? (:error (archive-branch* profile child false)))) + (t/is (nil? (:error (th/command! {::th/type :update-file-branch-from-source + ::rpc/profile-id (:id profile) + :branch-id (:id grand)})))))))) (defn- branch-files-deleted? [branches] diff --git a/backend/test/backend_tests/rpc_file_task_test.clj b/backend/test/backend_tests/rpc_file_task_test.clj index 98f0a89c91..e7e87a54a7 100644 --- a/backend/test/backend_tests/rpc_file_task_test.clj +++ b/backend/test/backend_tests/rpc_file_task_test.clj @@ -16,6 +16,7 @@ [app.email :as eml] [app.msgbus :as mbus] [app.rpc :as-alias rpc] + [app.rpc.commands.files-task :as ftask] [backend-tests.helpers :as th] [clojure.test :as t])) @@ -818,10 +819,11 @@ (t/is (= (:id file) (:file-id row))) (t/is (= board-id (:shape-id row))) (t/is (= board-id (:frame-id row))) - (t/is (nil? (:lost-shape-name row))) + (t/is (= "Pay button" (:lost-shape-name row))) (t/testing "the thread created with the task follows it" (t/is (= "layer" (:anchor-type thread))) - (t/is (= board-id (:shape-id thread))))) + (t/is (= board-id (:shape-id thread))) + (t/is (= "Pay button" (:lost-shape-name thread))))) (t/testing "the landing is an event naming the layer" (let [event (last (task-events* author (:id task)))] (t/is (= "relinked" (:type event))) @@ -864,6 +866,8 @@ ;; the mark sits on the top right corner of the lost layer (t/is (= {:x 820.0 :y 600.0} (into {} (db/decode-pgpoint (:position row))))) (t/is (= "area" (:anchor-type thread))) + (t/is (= "Badge" (:lost-shape-name row))) + (t/is (= "Badge" (:lost-shape-name thread))) (t/is (nil? (:shape-id thread))) (t/is (= [520.0 600.0 300.0 48.0] (mapv #(double (get area %)) [:x :y :width :height])))) @@ -914,6 +918,202 @@ (t/is (= (:id file) (:file-id row))) (t/is (= board-id (:shape-id row))))))) +(t/deftest a-resolved-thread-outside-any-review-moves-with-the-merge + ;; A thread without a review belongs to the file, which the merge + ;; carries to main: resolved or not, it moves. + (with-redefs [cf/flags (task-flags)] + (let [{:keys [author file page-id branch branch-file-id]} (setup-review) + thread (thread* author branch-file-id page-id {:content "settled already"})] + (t/is (nil? (:error (th/command! {::th/type :update-comment-thread + ::rpc/profile-id (:id author) + :id (:id thread) + :is-resolved true})))) + (t/is (nil? (:error (merge* author branch)))) + (let [row (th/db-get :comment-thread {:id (:id thread)})] + (t/is (true? (:is-resolved row))) + (t/is (= (:id file) (:file-id row))))))) + +(t/deftest a-lost-layer-lands-on-its-nearest-ancestor-the-target-has + (let [page-id (uuid/random) + board-id (uuid/random) + group-id (uuid/random) + badge-id (uuid/random) + board (cts/setup-shape {:id board-id :name "Checkout" :type :frame + :x 0 :y 0 :width 400 :height 800 + :parent-id uuid/zero :frame-id uuid/zero}) + group (cts/setup-shape {:id group-id :name "Promo" :type :group + :x 20 :y 100 :width 300 :height 48 + :parent-id board-id :frame-id board-id}) + badge (cts/setup-shape {:id badge-id :name "Badge" :type :rect + :x 20 :y 100 :width 300 :height 48 + :parent-id group-id :frame-id board-id}) + branch {:pages-index {page-id {:objects {board-id board group-id group badge-id badge}}}} + lost (ftask/lost-layer [nil branch] page-id badge-id)] + (t/testing "the lost layer is read from the first document that has it" + (t/is (= "Badge" (:shape-name lost))) + (t/is (= [group-id board-id] (:ancestor-ids lost))) + (t/is (= [20.0 100.0 300.0 48.0] + (mapv #(double (get (:area lost) %)) [:x :y :width :height]))) + (t/is (nil? (ftask/lost-layer [branch] page-id (uuid/random))))) + + (t/testing "the nearest ancestor the target has, past the group it lacks" + (t/is (= {:shape-id board-id + :shape-name "Checkout" + :frame-id board-id + :lost-shape-name "Badge"} + (ftask/lost-layer-landing {board-id board} lost (gpt/point 0))))) + + (t/testing "no ancestor on the target: the area the layer covered" + (let [landing (ftask/lost-layer-landing {} lost (gpt/point 0))] + (t/is (= uuid/zero (:frame-id landing))) + (t/is (= (:area lost) (:area landing))) + (t/is (= [320.0 100.0] (mapv #(double (get (:position landing) %)) [:x :y]))) + (t/is (= "Badge" (:lost-shape-name landing))))) + + (t/testing "an unknown layer keeps its spot" + (t/is (= {:frame-id uuid/zero :position (gpt/point 5) :lost-shape-name nil} + (ftask/lost-layer-landing {board-id board} nil (gpt/point 5))))))) + +(defn- add-group* + "Add on the branch, inside the board `board-id` of main, a group with a + rect inside, which main lacks. Returns their ids." + [author branch-file-id page-id board-id] + (let [group-id (uuid/random) + badge-id (uuid/random)] + (add-shape* author branch-file-id page-id + {:id group-id :name "Promo" :type :group + :x 20 :y 100 :width 300 :height 48 + :parent-id board-id :frame-id board-id}) + (add-shape* author branch-file-id page-id + {:id badge-id :name "Badge" :type :rect + :x 20 :y 100 :width 300 :height 48 + :parent-id group-id :frame-id board-id}) + {:group-id group-id :badge-id badge-id})) + +(defn- rename-board* + "Rename the board `board-id` on the branch, so a merge that leaves the + other changes out still carries something." + [author branch-file-id page-id board-id] + (update-file* author branch-file-id + [{:type :mod-obj :page-id page-id :id board-id + :operations [{:type :set :attr :name :val "Checkout v2"}]}])) + +(t/deftest a-mark-in-an-excluded-group-lands-on-the-nearest-ancestor-on-main + ;; The merge leaves the new group out, and its rect with it. The thread + ;; and the task on the rect land on the board that holds the group, the + ;; nearest ancestor main has, not on the group, and keep the name of + ;; the rect. + (with-redefs [cf/flags (task-flags)] + (let [{:keys [author file page-id board-id branch branch-file-id]} (setup-board-branch) + {:keys [group-id badge-id]} (add-group* author branch-file-id page-id board-id) + thread (thread* author branch-file-id page-id + {:frame-id board-id + :anchor {:type :layer :shape-id badge-id} + :content "is the badge final?"}) + task (:result (create-task* author {:file-id branch-file-id + :page-id page-id + :frame-id board-id + :shape-id badge-id}))] + (rename-board* author branch-file-id page-id board-id) + (t/is (nil? (:error (merge-excluding* author branch page-id [group-id])))) + (doseq [id [(:id thread) (:thread-id task)]] + (let [row (th/db-get :comment-thread {:id id})] + (t/is (= (:id file) (:file-id row))) + (t/is (= "layer" (:anchor-type row))) + (t/is (= board-id (:shape-id row))) + (t/is (= board-id (:frame-id row))) + (t/is (= "Badge" (:lost-shape-name row))))) + (let [row (th/db-get :file-task {:id (:id task)})] + (t/is (= (:id file) (:file-id row))) + (t/is (= board-id (:shape-id row))) + (t/is (= "Badge" (:lost-shape-name row)))) + (t/testing "main lists the thread with the name of its lost layer" + (let [threads (:result (th/command! {::th/type :get-comment-threads + ::rpc/profile-id (:id author) + :file-id (:id file)})) + listed (some #(when (= (:id thread) (:id %)) %) threads)] + (t/is (= "Badge" (:lost-shape-name listed))) + (t/is (not (contains? listed :landing-ancestors))))) + (t/testing "the landing is an event naming the ancestor" + (let [event (last (task-events* author (:id task)))] + (t/is (= "relinked" (:type event))) + (t/is (= {:shape-id board-id :shape-name "Checkout v2"} (:payload event)))))))) + +(defn- setup-area-landing + "A branch whose new board, with a rect inside, the merge leaves out, + with a thread outside any review and a task on the rect. Returns the + ids the tests need, after the merge." + [] + (let [{:keys [author page-id board-id branch branch-file-id] :as setup} (setup-board-branch) + {:keys [promo-id badge-id]} (add-promo* author branch-file-id page-id) + thread (thread* author branch-file-id page-id + {:frame-id promo-id + :anchor {:type :layer :shape-id badge-id} + :content "is the badge final?"}) + task (:result (create-task* author {:file-id branch-file-id + :page-id page-id + :frame-id promo-id + :shape-id badge-id}))] + (rename-board* author branch-file-id page-id board-id) + (t/is (nil? (:error (merge-excluding* author branch page-id [promo-id])))) + (assoc setup :thread thread :task task :badge-id badge-id))) + +(t/deftest a-mark-with-no-ancestor-on-main-lands-on-the-area-and-keeps-the-name + (with-redefs [cf/flags (task-flags)] + (let [{:keys [thread task]} (setup-area-landing)] + (doseq [id [(:id thread) (:thread-id task)]] + (let [row (th/db-get :comment-thread {:id id})] + (t/is (= "area" (:anchor-type row))) + (t/is (nil? (:shape-id row))) + (t/is (= "Badge" (:lost-shape-name row))))) + (let [row (th/db-get :file-task {:id (:id task)})] + (t/is (nil? (:shape-id row))) + (t/is (= "Badge" (:lost-shape-name row))))))) + +(defn- relink-thread* + [profile thread-id page-id shape-id] + (th/command! {::th/type :relink-comment-thread + ::rpc/profile-id (:id profile) + :id thread-id + :page-id page-id + :frame-id shape-id + :shape-id shape-id + :shape-name "Checkout v2"})) + +(t/deftest relinking-a-lost-thread-points-it-at-the-layer-and-its-task-follows + ;; The thread created with a task is the mark of the task, so the task + ;; follows it, as the thread follows a relinked task; a thread with no + ;; such task moves alone. + (with-redefs [cf/flags (task-flags)] + (let [{:keys [author page-id board-id thread task]} (setup-area-landing)] + (t/testing "a layer on another page is refused" + (t/is (= :layer-not-on-thread-page + (-> (relink-thread* author (:id thread) (uuid/random) board-id) + :error ex-data :code)))) + + (t/testing "a thread with no task of its own moves alone" + (let [out (relink-thread* author (:id thread) page-id board-id) + row (th/db-get :comment-thread {:id (:id thread)})] + (t/is (nil? (:error out))) + (t/is (= [] (-> out :result :task-ids))) + (t/is (= "layer" (:anchor-type row))) + (t/is (= board-id (:shape-id row))) + (t/is (= board-id (:frame-id row))) + (t/is (nil? (:area row))) + (t/is (nil? (:lost-shape-name row))))) + + (t/testing "the thread created with a task takes the task along" + (let [out (relink-thread* author (:thread-id task) page-id board-id) + row (th/db-get :file-task {:id (:id task)}) + event (last (task-events* author (:id task)))] + (t/is (nil? (:error out))) + (t/is (= [(:id task)] (-> out :result :task-ids))) + (t/is (= board-id (:shape-id (th/db-get :comment-thread {:id (:thread-id task)})))) + (t/is (= board-id (:shape-id row))) + (t/is (nil? (:lost-shape-name row))) + (t/is (= "relinked" (:type event))) + (t/is (= {:shape-id board-id :shape-name "Checkout v2"} (:payload event)))))))) + ;; --- Deleted layers of any size (t/deftest a-task-on-a-board-larger-than-the-snapshot-cap-is-still-marked-lost diff --git a/frontend/src/app/main/data/workspace/branches.cljs b/frontend/src/app/main/data/workspace/branches.cljs index cc4c23dfc2..c139863420 100644 --- a/frontend/src/app/main/data/workspace/branches.cljs +++ b/frontend/src/app/main/data/workspace/branches.cljs @@ -85,8 +85,8 @@ (assoc state :workspace-branch-count (assoc count-state :file-id file-id))))) (defn fetch-branch-count - "Load how many branches the open file has, archived ones included and - counted from main when the file is itself a branch. The header reads it + "Load how many live branches the family of the open file has, archived + ones included: the count the branch quota checks. The header reads it to decide whether to show its branch row, so it loads with the file; it compares nothing, unlike the listing, which loads only when a user opens the switcher or the all-branches dialog. A count belonging to ANOTHER @@ -118,9 +118,10 @@ (defn fetch-branches "Load the branch listing of the open file. A failure leaves the listing in `:error`, never in an empty list, so it cannot read as a file with no - branches. A successful listing also refreshes the branch count, since it - covers the same branches, so the header follows a delete or a - materialize made from the listing." + branches. A successful listing also reloads the branch count, so the + header follows a delete or a materialize made from the listing. The + count is reloaded rather than taken from the listing, which holds only + the branches of one source while the count covers the family." [] (ptk/reify ::fetch-branches ptk/WatchEvent @@ -135,7 +136,7 @@ (rp/cmd! :get-file-branches {:file-id root :include-archived true})))) (rx/mapcat (fn [data] (rx/of (update-branches-state {:status :loaded :data data}) - (set-branch-count file-id {:count (count data)})))) + (fetch-branch-count)))) (rx/catch (fn [_] (rx/of (update-branches-state {:status :error}))))))))) @@ -207,6 +208,11 @@ (:branch-not-found :branch-merged :branch-not-open) (ntf/warn (tr "workspace.branches.limits.refuse-stale")) + ;; the branch to cut from, merge into, or update from is archived + ;; or gone (`:branch-stacking`) + :source-branch-not-open + (ntf/warn (tr "workspace.branches.limits.refuse-source-not-open")) + :invalid-branch-name (ntf/warn (tr "workspace.branches.limits.refuse-name")) @@ -232,9 +238,12 @@ (assoc state :dashboard-branches bs)))) (defn load-file-branches - "Load the open branches of `file-id` into `:dashboard-branches` (used by - the dashboard branches popover). The previous list is cleared first so a - popover opened on another file never shows stale entries." + "Load the open and archived branches of `file-id` into + `:dashboard-branches` (used by the dashboard branches popover). These + are the file's direct branches: the badge counts the whole family, so + the two agree only while branches of branches are off. The previous + list is cleared first so a popover opened on another file never shows + stale entries." [file-id] (ptk/reify ::load-file-branches ptk/UpdateEvent @@ -243,7 +252,7 @@ ptk/WatchEvent (watch [_ _ _] - (->> (rp/cmd! :get-file-branches {:file-id file-id}) + (->> (rp/cmd! :get-file-branches {:file-id file-id :include-archived true}) (rx/map set-dashboard-branches) (rx/catch (fn [_] (rx/of (set-dashboard-branches [])))))))) diff --git a/frontend/src/app/main/data/workspace/tasks.cljs b/frontend/src/app/main/data/workspace/tasks.cljs index af2de15747..7cc369c33e 100644 --- a/frontend/src/app/main/data/workspace/tasks.cljs +++ b/frontend/src/app/main/data/workspace/tasks.cljs @@ -73,15 +73,17 @@ (some? (:lost-shape-name task))) (defn restorable? - "The deleted layer of a task can be put back: the last deletion among - the `events` of the task kept the layer, which the server skips for a - very large one." + "The deleted layer of a task can be put back: the last move of the + layer among the `events` of the task is a deletion that kept the + layer, which the server skips for a very large one. A task the merge + of a branch landed elsewhere (a later `relinked` event) is lost + without a layer to restore." [events] - (-> (filter #(= "layer-deleted" (:type %)) events) - (last) - (get-in [:payload :objects]) - (seq) - (some?))) + (let [last-move (->> events + (filter #(contains? #{"layer-deleted" "layer-restored" "relinked"} (:type %))) + (last))] + (and (= "layer-deleted" (:type last-move)) + (some? (seq (get-in last-move [:payload :objects])))))) (defn history? "The task came with a merged branch and is already closed: it is part diff --git a/frontend/src/app/main/ui/dashboard/branches_popover.cljs b/frontend/src/app/main/ui/dashboard/branches_popover.cljs index 2fb210525d..2d184aa625 100644 --- a/frontend/src/app/main/ui/dashboard/branches_popover.cljs +++ b/frontend/src/app/main/ui/dashboard/branches_popover.cljs @@ -77,7 +77,10 @@ [:> branch-row* {:label (:name b) :data-branch (dm/str (:branch-file-id b)) :on-click on-open-branch} - [:> branch-status* {:ahead (:ahead b) - :behind (:behind b) - :conflicts (:conflicts b) - :has-error (boolean (or (:diff-error b) (nil? (:ahead b))))}]]])]]]])) + ;; an archived branch has no live comparison to show + (if (= "archived" (:status b)) + (tr "workspace.branches.archived") + [:> branch-status* {:ahead (:ahead b) + :behind (:behind b) + :conflicts (:conflicts b) + :has-error (boolean (or (:diff-error b) (nil? (:ahead b))))}])]])]]]])) diff --git a/frontend/src/app/main/ui/workspace/review/common.cljs b/frontend/src/app/main/ui/workspace/review/common.cljs index ab4076500f..7f571ce466 100644 --- a/frontend/src/app/main/ui/workspace/review/common.cljs +++ b/frontend/src/app/main/ui/workspace/review/common.cljs @@ -20,6 +20,15 @@ (or (get-in profiles [profile-id :fullname]) (tr "workspace.review.unknown-name"))) +(defn route-target + "The name the route of the review `pr` gives the file it merges into: + `main-label` when that file is the root of its family (main), and the + file's own name when it is a branch (`:branch-stacking`). A row that + does not say keeps `main-label`, the wording of every review before + branches could stack." + [{:keys [target-is-main target-name]} main-label] + (if (false? target-is-main) target-name main-label)) + (defn status-tone "The tone a task in `status` is drawn in, the colour of its state." [status] diff --git a/frontend/src/app/main/ui/workspace/review/details.cljs b/frontend/src/app/main/ui/workspace/review/details.cljs index 6ea35c3cef..a0b7c1b6dd 100644 --- a/frontend/src/app/main/ui/workspace/review/details.cljs +++ b/frontend/src/app/main/ui/workspace/review/details.cljs @@ -653,7 +653,8 @@ [:> pr-state-badge* {:pr pr}] [:> text* {:as "span" :typography t/body-small :class (stl/css :muted)} (tr "workspace.review.text-and-detail" - (tr "workspace.review.route" (:branch-name pr) (tr "workspace.branches.main")) + (tr "workspace.review.route" (:branch-name pr) + (rvc/route-target pr (tr "workspace.branches.main"))) (tr "workspace.review.details.opened-by" (ct/format-inst (:created-at pr) :localized-date) (or (:fullname author) (tr "workspace.review.unknown-name"))))]])}] diff --git a/frontend/src/app/main/ui/workspace/review/panel.cljs b/frontend/src/app/main/ui/workspace/review/panel.cljs index 34d756f313..b47fe68f01 100644 --- a/frontend/src/app/main/ui/workspace/review/panel.cljs +++ b/frontend/src/app/main/ui/workspace/review/panel.cljs @@ -228,7 +228,8 @@ {::mf/private true} [{:keys [pr]}] (let [[approvals total] (prui/current-approvals pr) - route (tr "workspace.review.route" (:branch-name pr) (tr "workspace.branches.main"))] + route (tr "workspace.review.route" (:branch-name pr) + (rvc/route-target pr (tr "workspace.branches.main")))] [:div {:class (stl/css :review-header)} [:div {:class (stl/css :review-title-row)} [:> heading* {:level 2 :typography t/body-large :class (stl/css :review-title)} diff --git a/frontend/src/app/main/ui/workspace/sidebar/pull_requests.cljs b/frontend/src/app/main/ui/workspace/sidebar/pull_requests.cljs index 402ba57e64..a818e395ff 100644 --- a/frontend/src/app/main/ui/workspace/sidebar/pull_requests.cljs +++ b/frontend/src/app/main/ui/workspace/sidebar/pull_requests.cljs @@ -38,6 +38,7 @@ [app.main.ui.ds.product.avatar :refer [avatar*]] [app.main.ui.ds.tooltip :refer [tooltip*]] [app.main.ui.workspace.compare.common :as cc] + [app.main.ui.workspace.review.common :as rvc] [app.util.dom :as dom] [app.util.i18n :refer [tr]] [cuerdas.core :as str] @@ -443,10 +444,11 @@ [:> text* {:as "span" :typography t/body-small :class (stl/css :dialog-subtitle)} - ;; the route reads branch → target BRANCH (reviews always merge - ;; into main); the target file name would be redundant here, - ;; inside the file itself - (tr "workspace.review.route" (:branch-name pr) (tr "workspace.branches.main"))] + ;; the route reads branch → target: "Main" for the root of the + ;; family, whose file name would be redundant inside the file + ;; itself, and the target's name for a branch of a branch + (tr "workspace.review.route" (:branch-name pr) + (rvc/route-target pr (tr "workspace.branches.main")))] (when (and open? (:outdated pr)) [:span {:class (stl/css :pr-badge :badge-outdated)} (tr "workspace.pull-requests.badge.outdated")]) @@ -566,8 +568,12 @@ (tr "workspace.compare.bar.comparing")] [:span {:class (stl/css :pr-banner-name)} (if (:incoming comparison) - (tr "workspace.review.route" (tr "workspace.branches.main") (:branch-name pr)) - (tr "workspace.review.route" (:branch-name pr) (tr "workspace.branches.main")))]])] + (tr "workspace.review.route" + (rvc/route-target pr (tr "workspace.branches.main")) + (:branch-name pr)) + (tr "workspace.review.route" + (:branch-name pr) + (rvc/route-target pr (tr "workspace.branches.main"))))]])] (when (some? progress) [:* diff --git a/frontend/test/frontend_tests/data/workspace_branches_test.cljs b/frontend/test/frontend_tests/data/workspace_branches_test.cljs index 074046e168..47f72da1e4 100644 --- a/frontend/test/frontend_tests/data/workspace_branches_test.cljs +++ b/frontend/test/frontend_tests/data/workspace_branches_test.cljs @@ -461,14 +461,15 @@ (await (mock/with-mocks* {rp/cmd! (mock/stub (fn [cmd _] (case cmd - :get-file-branch-info (rx/of nil) - :get-file-branches (rx/of [{:id (uuid/next) :status "open"}]) + :get-file-branch-info (rx/of nil) + :get-file-branches (rx/of [{:id (uuid/next) :status "open"}]) + :get-file-branch-count (rx/of {:count 1}) (rx/empty))))} (let [store (make-store-with (assoc main-without-branches :workspace-branch-count {:file-id file-id :status :error}))] (ptk/emit! store (dwb/init-branches-state)) - (await (async/wait-for #(= :loaded (get-in @store [:workspace-branches :status])) - "the listing lands")) + (await (async/wait-for #(some? (get-in @store [:workspace-branch-count :count])) + "the listing reloads the count")) (t/is (= {:file-id file-id :count 1} (:workspace-branch-count @store))) (t/is (false? (dwb/branch-count-failed? @store))) (rx/dispose! store))))) @@ -477,13 +478,14 @@ (await (mock/with-mocks* {rp/cmd! (mock/stub (fn [cmd _] (case cmd - :get-file-branch-info (rx/of nil) - :get-file-branches (rx/of []) + :get-file-branch-info (rx/of nil) + :get-file-branches (rx/of []) + :get-file-branch-count (rx/of {:count 0}) (rx/empty))))} (let [store (make-store-with main-with-branches)] (ptk/emit! store (dwb/fetch-branches)) - (await (async/wait-for #(= :loaded (get-in @store [:workspace-branches :status])) - "the listing lands")) + (await (async/wait-for #(= 0 (get-in @store [:workspace-branch-count :count])) + "the listing reloads the count")) (t/is (= 0 (get-in @store [:workspace-branch-count :count])) "deleting the last branch from the listing hides the switcher") (t/is (false? (dwb/has-branches? @store))) diff --git a/frontend/test/frontend_tests/data/workspace_tasks_test.cljs b/frontend/test/frontend_tests/data/workspace_tasks_test.cljs index 012e799ec9..49cc826925 100644 --- a/frontend/test/frontend_tests/data/workspace_tasks_test.cljs +++ b/frontend/test/frontend_tests/data/workspace_tasks_test.cljs @@ -57,6 +57,19 @@ (t/is (dwtk/history? {:status "verified" :merge-id (uuid/next)})) (t/is (not (dwtk/history? {:status "open" :merge-id (uuid/next)})))) +(t/deftest a-layer-the-merge-landed-elsewhere-is-not-restorable + ;; The merge of a branch lands a task whose layer did not reach the + ;; target on another layer with a `relinked` event, and the task stays + ;; lost: an older deletion that kept the layer offers no restore. + (let [deleted {:type "layer-deleted" + :payload {:shape-id button-id + :objects [{:id button-id :type :rect}]}}] + (t/is (dwtk/restorable? [{:type "created"} deleted])) + (t/is (not (dwtk/restorable? [deleted + {:type "merged" :payload {}} + {:type "relinked" :payload {:shape-id board-id}}]))) + (t/is (not (dwtk/restorable? [(assoc-in deleted [:payload :objects] nil)]))))) + (t/deftest tasks-for-counts-the-open-work-of-a-person (let [me (uuid/next)] (t/is (= 1 (count (dwtk/tasks-for [{:status "open" :assignee-id me} diff --git a/frontend/test/frontend_tests/ui/review_details_test.cljs b/frontend/test/frontend_tests/ui/review_details_test.cljs index 79870bc2f5..bc023e87f4 100644 --- a/frontend/test/frontend_tests/ui/review_details_test.cljs +++ b/frontend/test/frontend_tests/ui/review_details_test.cljs @@ -45,6 +45,11 @@ (t/is (= ["info" "success" "warning" "purple" "error"] (mapv rvc/status-tone dwrv/task-statuses)))) +(t/deftest the-route-names-a-branch-target-and-calls-the-root-main + (t/is (= "Parent" (rvc/route-target {:target-name "Parent" :target-is-main false} "Main"))) + (t/is (= "Main" (rvc/route-target {:target-name "Design file" :target-is-main true} "Main"))) + (t/is (= "Main" (rvc/route-target {:target-name "Design file"} "Main")))) + (t/deftest the-activity-comes-folded-from-the-server (let [entry {:id (uuid/random) :page-id (uuid/random) :kind :added} session {:profile-id (uuid/random) diff --git a/frontend/translations/en.po b/frontend/translations/en.po index ed93957bb9..c2c48c30a6 100644 --- a/frontend/translations/en.po +++ b/frontend/translations/en.po @@ -7251,6 +7251,12 @@ msgstr "" "This branch is no longer in the state the panel showed. Reopen the branches " "panel to see where it is now." +#: src/app/main/data/workspace/branches.cljs +msgid "workspace.branches.limits.refuse-source-not-open" +msgstr "" +"The branch to branch from, merge into, or update from is archived or gone. " +"Restore it first." + #: src/app/main/data/workspace/branches.cljs msgid "workspace.branches.limits.refuse-name" msgstr "A branch name can't be empty." diff --git a/frontend/translations/es.po b/frontend/translations/es.po index ebc595c31a..c4c8e28b81 100644 --- a/frontend/translations/es.po +++ b/frontend/translations/es.po @@ -7357,6 +7357,12 @@ msgstr "" "Esta rama ya no está como la mostraba el panel. Vuelve a abrir el panel de " "ramas para ver su estado actual." +#: src/app/main/data/workspace/branches.cljs +msgid "workspace.branches.limits.refuse-source-not-open" +msgstr "" +"La rama desde la que quieres crear, en la que quieres fusionar o desde la que " +"quieres actualizar está archivada o ya no existe. Restáurala primero." + #: src/app/main/data/workspace/branches.cljs msgid "workspace.branches.limits.refuse-name" msgstr "El nombre de una rama no puede estar vacío."