From ff3e9e1609d93fcc8ab5bcd799295ed3d9830032 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Tejero=20Cantero?= Date: Mon, 23 Nov 2026 21:58:31 +0100 Subject: [PATCH] :sparkles: Land a merged thread on its nearest ancestor and keep the lost name Apply the designer's rules for a thread or task whose layer did not reach the target of a merge: - It lands 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. A task lands with its thread whatever its status. - It keeps the lost layer's name, so the interface shows the lost layer and offers relink. Restore is not offered after a merge. - Migration 0818 gives threads the lost name and stores, at the merge, what a later move needs once the branch document is gone. - relink-comment-thread anchors a thread to a layer again; the tasks created with the thread follow it. AI-assisted-by: claude-opus-5-5 --- backend/src/app/migrations.clj | 5 +- ...0818-mod-comment-thread-add-lost-layer.sql | 22 ++ backend/src/app/rpc/commands/comments.clj | 5 +- backend/src/app/rpc/commands/files_task.clj | 339 +++++++++++++----- .../backend_tests/branch_family_gc_test.clj | 117 ++++++ .../test/backend_tests/rpc_file_task_test.clj | 204 ++++++++++- .../src/app/main/data/workspace/branches.cljs | 8 +- .../src/app/main/data/workspace/tasks.cljs | 18 +- .../data/workspace_tasks_test.cljs | 13 + 9 files changed, 635 insertions(+), 96 deletions(-) create mode 100644 backend/src/app/migrations/sql/0818-mod-comment-thread-add-lost-layer.sql 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_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/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_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 c83c71c317..c139863420 100644 --- a/frontend/src/app/main/data/workspace/branches.cljs +++ b/frontend/src/app/main/data/workspace/branches.cljs @@ -239,9 +239,11 @@ (defn load-file-branches "Load the open and archived branches of `file-id` into - `:dashboard-branches` (used by the dashboard branches popover), the set - its badge counts. The previous list is cleared first so a popover opened - on another file never shows stale entries." + `: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 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/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}