Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
perf(notes): delete a selection in one request, and report its progress
Elian Doran097b9bf527df
diff --git a/apps/client/src/services/branches.spec.ts b/apps/client/src/services/branches.spec.tsindex aac86a7ec8..c98675c763 100644--- a/apps/client/src/services/branches.spec.ts+++ b/apps/client/src/services/branches.spec.ts@@ -69,6 +69,7 @@ beforeEach(() => {vi.clearAllMocks();// Reset commonly-overridden collaborators back to safe defaults.server.put = vi.fn(async () => ({ success: true, message: "" })) as typeof server.put;+ server.post = vi.fn(async () => ({})) as typeof server.post;server.remove = vi.fn(async () => ({})) as typeof server.remove;toastService.showError = vi.fn();toastService.showPersistent = vi.fn();@@ -227,24 +228,30 @@ describe("deleteNotes", () => {getActiveContext: () => ({ notePathArray: ["root", note.noteId], setNote })} as any;appContext.triggerCommand = vi.fn((_name: any, data: any) => {- data.callback({ proceed: true, deleteAllClones: false, eraseNotes: false });+ data.callback({+ proceed: true, deleteAllClones: false, eraseNotes: false, noteCountToDelete: 7+ });}) as any;- // Two branches -> first iteration has last=false, second has last=true.+ // Two branches, one request: the backend deletes the selection in a single transaction.const result = await branches.deleteNotes(["delBranch2", "delBranch2b"]);expect(result).toBe(true);- expect(server.remove).toHaveBeenCalledTimes(2);- const firstArg = (server.remove as any).mock.calls[0][0] as string;- const secondArg = (server.remove as any).mock.calls[1][0] as string;- expect(firstArg.startsWith(`branches/delBranch2?taskId=`)).toBe(true);- expect(firstArg).toContain("eraseNotes=false");- expect(firstArg).toContain("last=false");- expect(secondArg).toContain("last=true");+ expect(server.post).toHaveBeenCalledTimes(1);+ const [url, body] = (server.post as any).mock.calls[0];+ expect(url).toBe("delete-notes");+ expect(body).toMatchObject({+ branchIdsToDelete: ["delBranch2", "delBranch2b"],+ deleteAllClones: false,+ eraseNotes: false,+ // The dialog's own preview count, so the toast counts towards the number it showed.+ totalCount: 7,+ taskId: expect.any(String)+ });// root has no children registered here, so the parent path ("root") is the fallbackexpect(setNote).toHaveBeenCalledWith("root");});- it("deletes all clones (note endpoint), erases & reloads, and tolerates navigation errors", async () => {+ it("asks for all clones, erases & reloads, and tolerates navigation errors", async () => {const note = buildNote({ title: "Del3" });makeBranch("delBranch3", note.noteId, "root");@@ -261,10 +268,9 @@ describe("deleteNotes", () => {const result = await branches.deleteNotes(["delBranch3"], false, true, "comp-9");expect(result).toBe(true);- // deleteAllClones -> notes/<noteId> endpoint- const removeArg = (server.remove as any).mock.calls[0][0] as string;- expect(removeArg.startsWith(`notes/${note.noteId}?taskId=`)).toBe(true);- expect(removeArg).toContain("eraseNotes=true");+ const [, body, componentId] = (server.post as any).mock.calls[0];+ expect(body).toMatchObject({ deleteAllClones: true, eraseNotes: true });+ expect(componentId).toBe("comp-9");expect(reloadSpy).toHaveBeenCalledTimes(1);expect(errSpy).toHaveBeenCalled();reloadSpy.mockRestore();@@ -287,24 +293,6 @@ describe("deleteNotes", () => {expect(getActiveContext).not.toHaveBeenCalled();});- it("handles a deleteAllClones request where the branch is no longer in froca", async () => {- // filterRootNote keeps non-root ids even if the branch is gone? No: filterRootNote drops- // unknown branches. So to exercise the `deleteAllClones && !branch` fallback we keep a real- // branch through filtering, then remove it from froca before the delete loop runs.- const note = buildNote({ title: "Del5" });- makeBranch("delBranch5", note.noteId, "root");- appContext.triggerCommand = vi.fn((_name: any, data: any) => {- // remove the branch right when the dialog "resolves", before the delete loop reads it- delete froca.branches["delBranch5"];- data.callback({ proceed: true, deleteAllClones: true, eraseNotes: false });- }) as any;-- const result = await branches.deleteNotes(["delBranch5"], false, false);- expect(result).toBe(true);- // branch missing -> falls back to deleting the branch id endpoint- const removeArg = (server.remove as any).mock.calls[0][0] as string;- expect(removeArg.startsWith("branches/delBranch5?taskId=")).toBe(true);- });});describe("activateNeighbouringNotePath (via deleteNotes navigation)", () => {@@ -544,6 +532,42 @@ describe("ws task-message subscribers", () => {expect(succeededToast.id).toBe("s1");expect(succeededToast.timeout).toBe(5000);expect(succeededToast.icon).toBe("trash");+ // The finished toast replaces the in-progress one by id, and showPersistent merges fields.+ expect(succeededToast.progress).toBeUndefined();+ expect(succeededToast.dismissible).toBe(true);+ });++ it("shows a delete progress bar only with a total, and drops it while erasing", async () => {+ await dispatchWs({+ taskType: "deleteNotes", type: "taskProgressCount",+ taskId: "b1", progressCount: 3, totalCount: 12+ });+ const withTotal = (toastService.showPersistent as any).mock.calls[0][0];+ expect(withTotal.progress).toBeCloseTo(3 / 12);+ expect(withTotal.dismissible).toBe(false);++ // The count walks branches while the total counts notes, so a surviving clone can push the+ // count past the total. The bar must not overflow.+ await dispatchWs({+ taskType: "deleteNotes", type: "taskProgressCount",+ taskId: "b2", progressCount: 20, totalCount: 12+ });+ expect((toastService.showPersistent as any).mock.calls[1][0].progress).toBe(1);++ // No total (a caller that never opened the dialog) -> a running count, no bar.+ await dispatchWs({+ taskType: "deleteNotes", type: "taskProgressCount", taskId: "b3", progressCount: 4+ });+ expect((toastService.showPersistent as any).mock.calls[2][0].progress).toBeUndefined();++ // Erasing is bulk SQL with nothing to count, so the bar goes even though a total is known.+ await dispatchWs({+ taskType: "deleteNotes", type: "taskProgressCount",+ taskId: "b4", progressCount: 12, totalCount: 12, phase: "erasing"+ });+ const erasing = (toastService.showPersistent as any).mock.calls[3][0];+ expect(erasing.progress).toBeUndefined();+ expect(erasing.dismissible).toBe(false);});it("undeleteNotes subscriber reacts to error/progress/success and ignores other task types", async () => {diff --git a/apps/client/src/services/branches.ts b/apps/client/src/services/branches.tsindex 6c057b20df..3980fff22f 100644--- a/apps/client/src/services/branches.ts+++ b/apps/client/src/services/branches.ts@@ -1,3 +1,5 @@+import type { ProgressPhase } from "@triliumnext/commons";+import appContext from "../components/app_context.js";import type FBranch from "../entities/fbranch.js";import type { ResolveOptions } from "../widgets/dialogs/delete_notes.js";@@ -111,9 +113,11 @@ async function deleteNotes(branchIdsToDelete: string[], forceDeleteAllClones = freturn false;}- const { proceed, deleteAllClones, eraseNotes } = await new Promise<ResolveOptions>((res) =>- appContext.triggerCommand("showDeleteNotesDialog", { branchIdsToDelete, callback: res, forceDeleteAllClones })- );+ const { proceed, deleteAllClones, eraseNotes, noteCountToDelete } =+ await new Promise<ResolveOptions>((res) =>+ appContext.triggerCommand("showDeleteNotesDialog",+ { branchIdsToDelete, callback: res, forceDeleteAllClones })+ );if (!proceed) {return false;@@ -127,24 +131,15 @@ async function deleteNotes(branchIdsToDelete: string[], forceDeleteAllClones = f}}- const taskId = utils.randomString(10);-- let counter = 0;-- for (const branchIdToDelete of branchIdsToDelete) {- counter++;-- const last = counter === branchIdsToDelete.length;- const query = `?taskId=${taskId}&eraseNotes=${eraseNotes ? "true" : "false"}&last=${last ? "true" : "false"}`;-- const branch = froca.getBranch(branchIdToDelete);-- if (deleteAllClones && branch) {- await server.remove(`notes/${branch.noteId}${query}`, componentId);- } else {- await server.remove(`branches/${branchIdToDelete}${query}`, componentId);- }- }+ // One request for the whole selection, so the backend deletes it in a single transaction+ // and the tree refreshes once instead of once per note.+ await server.post("delete-notes", {+ branchIdsToDelete,+ deleteAllClones,+ eraseNotes,+ totalCount: noteCountToDelete,+ taskId: utils.randomString(10)+ }, componentId);if (eraseNotes) {utils.reloadFrontendApp("erasing notes requires reload");@@ -269,7 +264,53 @@ function makeToast(id: string, message: string): ToastOptionsWithRequiredId {return {id,message,- icon: "trash"+ icon: "trash",+ // This toast replaces the in-progress one (same id), and showPersistent merges fields+ // rather than swapping the object, so clear the bar and restore the × that+ // makeDeleteProgressToast took away.+ progress: undefined,+ dismissible: true+ };+}++/**+ * Builds the persistent "deleting notes" toast. The delete dialog knows how many notes the deletion+ * takes down and passes that along, so the bar is there from the first count; a deletion asked for+ * from elsewhere has no total and shows a bare running count.+ *+ * The progress ratio is clamped because the two numbers count different things: the backend counts+ * every branch it walks, while the total counts the notes that go — and a note whose clone survives+ * the deletion is walked without being counted.+ */+function makeDeleteProgressToast(+ taskId: string,+ progressCount: number,+ totalCount?: number,+ phase?: ProgressPhase+): ToastOptionsWithRequiredId {+ const hasTotal = typeof totalCount === "number" && totalCount > 0;++ if (phase === "erasing") {+ return {+ id: taskId,+ icon: "bx bx-loader-circle bx-spin",+ message: t("branches.erasing-notes"),+ dismissible: false,+ progress: undefined+ };+ }++ return {+ id: taskId,+ icon: "bx bx-loader-circle bx-spin",+ message: hasTotal+ ? t("branches.delete-notes-in-progress-with-total",+ { progress: progressCount, total: totalCount })+ : t("branches.delete-notes-in-progress", { count: progressCount }),+ // The deletion runs to completion regardless of the toast, so don't offer a × that looks+ // like "cancel".+ dismissible: false,+ ...(hasTotal ? { progress: Math.min(1, progressCount / totalCount) } : {})};}@@ -282,7 +323,9 @@ ws.subscribeToMessages(async (message) => {toastService.closePersistent(message.taskId);toastService.showError(message.message);} else if (message.type === "taskProgressCount") {- toastService.showPersistent(makeToast(message.taskId, t("branches.delete-notes-in-progress", { count: message.progressCount })));+ toastService.showPersistent(makeDeleteProgressToast(+ message.taskId, message.progressCount, message.totalCount, message.phase+ ));} else if (message.type === "taskSucceeded") {const toast = makeToast(message.taskId, t("branches.delete-finished-successfully"));toast.timeout = 5000;diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 2bb525dd1d..9ced3dafc4 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -2835,7 +2835,9 @@"delete-notes-in-progress": "Delete notes in progress: {{count}}","delete-finished-successfully": "Delete finished successfully.","undeleting-notes-in-progress": "Undeleting notes in progress: {{count}}",- "undeleting-notes-finished-successfully": "Undeleting notes finished successfully."+ "undeleting-notes-finished-successfully": "Undeleting notes finished successfully.",+ "delete-notes-in-progress-with-total": "Deleted {{progress}} notes out of {{total}}",+ "erasing-notes": "Erasing notes…"},"frontend_script_api": {"async_warning": "You're passing an async function to `api.runOnBackend()` which will likely not work as you intended.\\nEither make the function synchronous (by removing `async` keyword), or use `api.runAsyncOnBackendWithManualTransactionHandling()`.",diff --git a/apps/client/src/widgets/dialogs/delete_notes.tsx b/apps/client/src/widgets/dialogs/delete_notes.tsxindex 61270f5f4d..9c7fde511f 100644--- a/apps/client/src/widgets/dialogs/delete_notes.tsx+++ b/apps/client/src/widgets/dialogs/delete_notes.tsx@@ -25,6 +25,12 @@ export interface ResolveOptions {proceed: boolean;deleteAllClones?: boolean;eraseNotes?: boolean;+ /**+ * How many notes the preview says the deletion takes down, which the backend reports progress+ * against. Carried from here because the dialog has already asked for it, and because the toast+ * then counts towards the number the reader was shown before they confirmed.+ */+ noteCountToDelete?: number;}interface ShowDeleteNotesDialogOpts {@@ -116,7 +122,12 @@ export default function DeleteNotesDialog() {<Button text={t("delete_notes.delete")} kind="primary"buttonRef={okButtonRef}onClick={() => {- opts.callback?.({ proceed: true, deleteAllClones, eraseNotes });+ opts.callback?.({+ proceed: true,+ deleteAllClones,+ eraseNotes,+ noteCountToDelete: noteIdsToBeDeleted.length+ });setShown(false);}} /></>}diff --git a/packages/commons/src/lib/ws_api.ts b/packages/commons/src/lib/ws_api.tsindex cfc62e87ca..df12d5c2e1 100644--- a/packages/commons/src/lib/ws_api.ts+++ b/packages/commons/src/lib/ws_api.ts@@ -77,8 +77,11 @@ export type TaskResult<T extends TaskType> = TaskResultDefinitions[T];* "throttled" is a transient state rather than a pipeline stage: the task is alive but deliberately* waiting out an external service's rate limiting (e.g. the OneNote importer under Graph throttling),* so the count will not move for a while and the client should say why instead of looking hung.+ *+ * "erasing" is the same kind of state for a permanent delete: `eraseNotesWithDeleteIds` runs bulk+ * SQL with nothing to count per item, so the client drops the bar and says what is happening.*/-export type ProgressPhase = "extracting" | "processing" | "throttled";+export type ProgressPhase = "extracting" | "processing" | "throttled" | "erasing";type TaskDefinition<T extends TaskType> = {type: "taskProgressCount",diff --git a/packages/trilium-core/src/routes/api/notes.spec.ts b/packages/trilium-core/src/routes/api/notes.spec.tsindex 2414114912..253bab1840 100644--- a/packages/trilium-core/src/routes/api/notes.spec.ts+++ b/packages/trilium-core/src/routes/api/notes.spec.ts@@ -3,6 +3,7 @@ import { beforeAll, describe, expect, it, vi } from "vitest";import becca from "../../becca/becca";import noteService from "../../services/notes";import { getSql } from "../../services/sql/index";+import ws from "../../services/ws";import { createTextNote } from "../../test/api_fixtures";import { CoreApiTester } from "../../test/api_tester";@@ -317,6 +318,146 @@ describe("Notes API (core)", () => {});});+ describe("deleting a selection", () => {+ it("deletes every branch of the selection, and erases them when asked", async () => {+ const first = await createTextNote(api, { title: "Selection note 1" });+ const second = await createTextNote(api, { title: "Selection note 2" });+ const child = await createTextNote(api,+ { parentNoteId: first.noteId, title: "Selection child" });++ const res = await api.post("/api/delete-notes", {+ body: {+ branchIdsToDelete: [ first.branchId, second.branchId ],+ taskId: "test-batch-delete"+ }+ });++ expect(res.status).toBe(204);+ expect(noteIsDeleted(first.noteId)).toBe(1);+ expect(noteIsDeleted(second.noteId)).toBe(1);+ // Deleting a note takes its subtree with it, as the per-note route does.+ expect(noteIsDeleted(child.noteId)).toBe(1);++ const toErase = await createTextNote(api, { title: "Selection to erase" });+ const eraseRes = await api.post("/api/delete-notes", {+ body: {+ branchIdsToDelete: [ toErase.branchId ],+ eraseNotes: true,+ taskId: "test-batch-erase"+ }+ });++ expect(eraseRes.status).toBe(204);+ // One request covers the whole selection, so nothing holds the erase back.+ expect(noteIsDeleted(toErase.noteId)).toBeNull();+ });++ it("removes only the selected branch of a clone, unless all clones are asked", async () => {+ const otherParent = await createTextNote(api, { title: "Other clone parent" });+ const cloned = await createTextNote(api, { title: "Cloned note" });++ const clone = await api.put<{ branchId: string }>(+ `/api/notes/${cloned.noteId}/clone-to-note/${otherParent.noteId}`,+ { body: {} }+ );+ expect(clone.status).toBe(200);++ const res = await api.post("/api/delete-notes", {+ body: {+ branchIdsToDelete: [ cloned.branchId ],+ taskId: "test-batch-clone-delete"+ }+ });++ expect(res.status).toBe(204);+ // The note outlives the branch: it still hangs under the other parent.+ expect(noteIsDeleted(cloned.noteId)).toBe(0);+ expect(becca.getNote(cloned.noteId)?.getParentNotes().map((note) => note.noteId))+ .toEqual([ otherParent.noteId ]);++ const allClones = await createTextNote(api, { title: "Cloned note, all clones" });+ await api.put(+ `/api/notes/${allClones.noteId}/clone-to-note/${otherParent.noteId}`, { body: {} });++ const allRes = await api.post("/api/delete-notes", {+ body: {+ branchIdsToDelete: [ allClones.branchId ],+ deleteAllClones: true,+ taskId: "test-batch-all-clones-delete"+ }+ });++ expect(allRes.status).toBe(204);+ expect(noteIsDeleted(allClones.noteId)).toBe(1);+ });++ it("skips a branch an earlier one of the selection already took down", async () => {+ const parent = await createTextNote(api, { title: "Selection ancestor" });+ const child = await createTextNote(api,+ { parentNoteId: parent.noteId, title: "Selection descendant" });++ // The tree only ever selects top-most nodes, but other callers (a note title menu,+ // the table view) can name both.+ const res = await api.post("/api/delete-notes", {+ body: {+ branchIdsToDelete: [ parent.branchId, child.branchId ],+ taskId: "test-batch-nested-delete"+ }+ });++ expect(res.status).toBe(204);+ expect(noteIsDeleted(parent.noteId)).toBe(1);+ expect(noteIsDeleted(child.noteId)).toBe(1);+ });++ it("reports the caller's total, and announces the erasing phase", async () => {+ const progress = vi.spyOn(ws, "sendMessageToAllClients").mockImplementation(() => {});++ try {+ const { branchId } = await createTextNote(api, { title: "Progress-reported" });++ const res = await api.post("/api/delete-notes", {+ body: {+ branchIdsToDelete: [ branchId ],+ eraseNotes: true,+ totalCount: 4,+ taskId: "test-batch-progress"+ }+ });+ expect(res.status).toBe(204);++ const counts = progress.mock.calls+ .map(([ message ]) => message)+ .filter((message) => message.type === "taskProgressCount");++ // The first message leaves the constructor before the total is known; every later+ // one carries it, so the client can draw a bar.+ expect(counts.length).toBeGreaterThan(1);+ expect(counts[0].totalCount).toBeUndefined();+ expect(counts.at(-1)).toMatchObject({ totalCount: 4, phase: "erasing" });+ } finally {+ progress.mockRestore();+ }+ });++ it("400s without a taskId or a branch id array", async () => {+ const { noteId, branchId } = await createTextNote(api, { title: "Needs a task id" });++ const noTaskId = await api.post("/api/delete-notes", {+ body: { branchIdsToDelete: [ branchId ] }+ });+ expect(noTaskId.status).toBe(400);++ const noBranchIds = await api.post("/api/delete-notes", {+ body: { taskId: "test-batch-no-branches" }+ });+ expect(noBranchIds.status).toBe(400);++ // Neither one deleted anything.+ expect(noteIsDeleted(noteId)).toBe(0);+ });+ });+describe("creating with targets", () => {it("creates a sibling note after a target branch", async () => {const anchor = await createTextNote(api, { title: "Anchor" });diff --git a/packages/trilium-core/src/routes/api/notes.ts b/packages/trilium-core/src/routes/api/notes.tsindex 5c5f33119b..f7760287ba 100644--- a/packages/trilium-core/src/routes/api/notes.ts+++ b/packages/trilium-core/src/routes/api/notes.ts@@ -427,6 +427,71 @@ function getDeleteNotesPreview(req: Request) {} satisfies DeleteNotesPreview;}+/**+ * Deletes a whole selection of branches, which is what the delete-notes dialog carries out.+ *+ * Takes the selection in one request so it deletes in one transaction, and the commit hook+ * broadcasts one frontend update for the whole batch rather than one per note.+ *+ * Each branch gets a deleteId of its own, so Recent Changes can undelete one note of the selection+ * without bringing the rest back.+ */+function deleteNotes(req: Request) {+ const { branchIdsToDelete, deleteAllClones, eraseNotes, totalCount, taskId } = req.body ?? {};++ if (!Array.isArray(branchIdsToDelete)) {+ throw new ValidationError("'branchIdsToDelete' must be an array of branch IDs.");+ }++ if (typeof taskId !== "string") {+ throw new ValidationError("Missing or incorrect type for task ID.");+ }++ const taskContext = new TaskContext(taskId, "deleteNotes", null);++ // What getDeleteNotesPreview told the caller the deletion costs. A caller that did not ask gets+ // a bare running count instead of a bar.+ if (typeof totalCount === "number" && totalCount > 0) {+ taskContext.setTotalCount(totalCount);+ }++ for (const branchId of branchIdsToDelete) {+ const branch = becca.getBranch(branchId);++ if (!branch) {+ // Either a stale id, or a branch an earlier one of the selection took down with+ // its subtree.+ getLog().info(`Branch '${branchId}' was not found, skipping its deletion.`);+ continue;+ }++ const deleteId = randomString(10);++ // Erasing one branch would leave the note's other clones pointing at erased content, so+ // eraseNotes deletes the note everywhere it hangs.+ if (deleteAllClones || eraseNotes) {+ branch.getNote().deleteNote(deleteId, taskContext);+ } else {+ branch.deleteBranch(deleteId, taskContext);+ }++ if (eraseNotes) {+ taskContext.scheduleErase(deleteId);+ }+ }++ const deleteIdsToErase = taskContext.takeScheduledErases();++ if (deleteIdsToErase.length > 0) {+ // Erasing is bulk SQL over the whole batch with no per-note step to count, so it reports a+ // phase rather than letting the bar sit at its last value.+ taskContext.reportPhase("erasing");+ eraseService.eraseNotesWithDeleteIds(deleteIdsToErase);+ }++ taskContext.taskSucceeded(null);+}+function forceSaveRevision(req: Request<{ noteId: string }>) {const { noteId } = req.params;const note = becca.getNoteOrThrow(noteId);@@ -476,6 +541,7 @@ export default {eraseDeletedNotesNow,eraseUnusedAttachmentsNow,getDeleteNotesPreview,+ deleteNotes,forceSaveRevision,convertNoteToAttachment,convertNoteFormatdiff --git a/packages/trilium-core/src/routes/index.ts b/packages/trilium-core/src/routes/index.tsindex 57f625c4ee..2a7465e6d4 100644--- a/packages/trilium-core/src/routes/index.ts+++ b/packages/trilium-core/src/routes/index.ts@@ -115,6 +115,7 @@ export function buildSharedApiRoutes({ route, asyncRoute, asyncRouteWithoutTransapiRoute(PST, "/api/notes/erase-deleted-notes-now", notesApiRoute.eraseDeletedNotesNow);apiRoute(PST, "/api/notes/erase-unused-attachments-now", notesApiRoute.eraseUnusedAttachmentsNow);apiRoute(PST, "/api/delete-notes-preview", notesApiRoute.getDeleteNotesPreview);+ apiRoute(PST, "/api/delete-notes", notesApiRoute.deleteNotes);apiRoute(GET, "/api/notes/:noteId/attachments", attachmentsApiRoute.getAttachments);apiRoute(PST, "/api/notes/:noteId/attachments", attachmentsApiRoute.saveAttachment);