Skip to content

Build your personal knowledge base with Trilium Notes

core: fix some issues

Adorian Doran98b6269797a4
diff --git a/packages/trilium-core/src/routes/api/notes.spec.ts b/packages/trilium-core/src/routes/api/notes.spec.ts
index 4a53b31d47..3ee712a1bb 100644
--- a/packages/trilium-core/src/routes/api/notes.spec.ts
+++ b/packages/trilium-core/src/routes/api/notes.spec.ts
@@ -117,11 +117,43 @@ describe("Notes API (core)", () => {
expect(del.status).toBe(204);
expect(noteIsDeleted(noteId)).toBe(1);
- const undel = await api.put(`/api/notes/${noteId}/undelete`);
- expect(undel.status).toBe(204);
+ const undel = await api.put<{ undeleted: boolean }>(`/api/notes/${noteId}/undelete`);
+ expect(undel.status).toBe(200);
+ expect(undel.body.undeleted).toBe(true);
expect(noteIsDeleted(noteId)).toBe(0);
});
+ it("reports failure (and restores nothing) when the note's parent is still deleted", async () => {
+ const parent = await createTextNote(api, { title: "Deleted parent" });
+ const child = await createTextNote(api, { parentNoteId: parent.noteId, title: "Orphaned child" });
+
+ // Deleting the parent soft-deletes the whole subtree, including the child.
+ const del = await api.delete(`/api/notes/${parent.noteId}`, {
+ query: { taskId: "test-parent-delete", last: "true" }
+ });
+ expect(del.status).toBe(204);
+ expect(noteIsDeleted(child.noteId)).toBe(1);
+
+ // Undeleting the child directly can't succeed — its only parent is itself still deleted.
+ const undel = await api.put<{ undeleted: boolean }>(`/api/notes/${child.noteId}/undelete`);
+ expect(undel.status).toBe(200);
+ expect(undel.body.undeleted).toBe(false);
+ expect(noteIsDeleted(child.noteId)).toBe(1);
+ });
+
+ it("reports failure (instead of crashing) when undeleting an already-erased note", async () => {
+ const { noteId } = await createTextNote(api, { title: "Erase then undelete" });
+ const del = await api.delete(`/api/notes/${noteId}`, {
+ query: { taskId: "test-erase-undelete", eraseNotes: "true", last: "true" }
+ });
+ expect(del.status).toBe(204);
+ expect(noteIsDeleted(noteId)).toBeNull(); // row is gone entirely
+
+ const undel = await api.put<{ undeleted: boolean }>(`/api/notes/${noteId}/undelete`);
+ expect(undel.status).toBe(200);
+ expect(undel.body.undeleted).toBe(false);
+ });
+
it("400s when deleting without a taskId", async () => {
const { noteId } = await createTextNote(api, { title: "Needs taskId" });
const res = await api.delete(`/api/notes/${noteId}`);
diff --git a/packages/trilium-core/src/routes/api/notes.ts b/packages/trilium-core/src/routes/api/notes.ts
index 68a731a44c..b881c1eed8 100644
--- a/packages/trilium-core/src/routes/api/notes.ts
+++ b/packages/trilium-core/src/routes/api/notes.ts
@@ -201,9 +201,14 @@ function deleteNote(req: Request<{ noteId: string }>) {
function undeleteNote(req: Request<{ noteId: string }>) {
const taskContext = TaskContext.getInstance(randomString(10), "undeleteNotes", null);
- noteService.undeleteNote(req.params.noteId, taskContext);
+ // Report whether the note was actually restored: it may fail silently if the note was already
+ // erased or its parent is no longer available, so the client can give feedback instead of
+ // navigating to a note that was never brought back.
+ const undeleted = noteService.undeleteNote(req.params.noteId, taskContext);
taskContext.taskSucceeded(null);
+
+ return { undeleted };
}
function sortChildNotes(req: Request<{ noteId: string }>) {
diff --git a/packages/trilium-core/src/services/notes.ts b/packages/trilium-core/src/services/notes.ts
index 4915bea589..3efbf5d21a 100644
--- a/packages/trilium-core/src/services/notes.ts
+++ b/packages/trilium-core/src/services/notes.ts
@@ -1093,24 +1093,39 @@ function updateNoteData(noteId: string, content: string, attachments: Attachment
}
}
-function undeleteNote(noteId: string, taskContext: TaskContext<"undeleteNotes">) {
- const noteRow = getSql().getRow<NoteRow>("SELECT * FROM notes WHERE noteId = ?", [noteId]);
+/**
+ * Attempts to restore a soft-deleted note (and the deleted subtree below it, sharing its deleteId).
+ *
+ * @returns `true` if the note was restored, `false` if it could not be — because it has already been
+ * erased, is not actually deleted, or has no surviving (undeleted) parent to reattach to.
+ */
+function undeleteNote(noteId: string, taskContext: TaskContext<"undeleteNotes">): boolean {
+ // Use getRowOrNull: the note may have been erased (e.g. by the erasure scheduler or another client)
+ // between the caller observing it and this call, in which case its row no longer exists.
+ const noteRow = getSql().getRowOrNull<NoteRow>("SELECT * FROM notes WHERE noteId = ?", [noteId]);
+
+ if (!noteRow) {
+ getLog().info(`Note '${noteId}' no longer exists (has been erased) and thus cannot be undeleted.`);
+ return false;
+ }
if (!noteRow.isDeleted || !noteRow.deleteId) {
getLog().error(`Note '${noteId}' is not deleted and thus cannot be undeleted.`);
- return;
+ return false;
}
const undeletedParentBranchIds = getUndeletedParentBranchIds(noteId, noteRow.deleteId);
if (undeletedParentBranchIds.length === 0) {
- // cannot undelete if there's no undeleted parent
- return;
+ // cannot undelete if there's no undeleted parent (its parent is itself still deleted or erased)
+ return false;
}
for (const parentBranchId of undeletedParentBranchIds) {
undeleteBranch(parentBranchId, noteRow.deleteId, taskContext);
}
+
+ return true;
}
function undeleteBranch(branchId: string, deleteId: string, taskContext: TaskContext<"undeleteNotes">) {