Skip to content

Build your personal knowledge base with Trilium Notes

core/deleted notes: show only unique items

Adorian Doranda513109ebf7
diff --git a/packages/trilium-core/src/routes/api/recent_changes.spec.ts b/packages/trilium-core/src/routes/api/recent_changes.spec.ts
index c51e6596fc..631fa7c232 100644
--- a/packages/trilium-core/src/routes/api/recent_changes.spec.ts
+++ b/packages/trilium-core/src/routes/api/recent_changes.spec.ts
@@ -107,6 +107,8 @@ describe("Recent changes API (core)", () => {
// ...the deleted note is present, and the live one is filtered out.
expect(res.body.some((change) => change.noteId === deletedId)).toBe(true);
expect(res.body.some((change) => change.noteId === live.noteId)).toBe(false);
+ // Each deleted note appears exactly once (only its deletion point, not creation/revisions).
+ expect(res.body.filter((change) => change.noteId === deletedId)).toHaveLength(1);
});
it("includes live notes when deletedOnly is not set", async () => {
diff --git a/packages/trilium-core/src/routes/api/recent_changes.ts b/packages/trilium-core/src/routes/api/recent_changes.ts
index f30d0a4b5c..46210c437d 100644
--- a/packages/trilium-core/src/routes/api/recent_changes.ts
+++ b/packages/trilium-core/src/routes/api/recent_changes.ts
@@ -12,33 +12,50 @@ function getRecentChanges(req: Request<{ ancestorNoteId: string }, unknown, unkn
let recentChanges: RecentChangeRow[] = [];
const sql = getSql();
- const revisionRows = sql.getRows<RecentChangeRow>(`
- SELECT
- notes.noteId,
- notes.isDeleted AS current_isDeleted,
- notes.deleteId AS current_deleteId,
- notes.title AS current_title,
- notes.isProtected AS current_isProtected,
- revisions.title,
- revisions.utcDateCreated AS utcDate,
- revisions.dateCreated AS date
- FROM
- revisions
- JOIN notes USING(noteId)`);
-
- for (const revisionRow of revisionRows) {
- const note = becca.getNote(revisionRow.noteId);
- // for deleted notes, the becca note is null, and it's not possible to (easily) determine if it belongs to a subtree
- if (ancestorNoteId === "root" || note?.hasAncestor(ancestorNoteId)) {
- recentChanges.push(revisionRow);
+ // Revisions are individual change events. The deleted-notes view lists each note once (at its
+ // deletion), so revisions (and the per-note creation point below) are only collected for the
+ // full recent-changes view.
+ if (!deletedOnly) {
+ const revisionRows = sql.getRows<RecentChangeRow>(`
+ SELECT
+ notes.noteId,
+ notes.isDeleted AS current_isDeleted,
+ notes.deleteId AS current_deleteId,
+ notes.title AS current_title,
+ notes.isProtected AS current_isProtected,
+ revisions.title,
+ revisions.utcDateCreated AS utcDate,
+ revisions.dateCreated AS date
+ FROM
+ revisions
+ JOIN notes USING(noteId)`);
+
+ for (const revisionRow of revisionRows) {
+ const note = becca.getNote(revisionRow.noteId);
+
+ // for deleted notes, the becca note is null, and it's not possible to (easily) determine if it belongs to a subtree
+ if (ancestorNoteId === "root" || note?.hasAncestor(ancestorNoteId)) {
+ recentChanges.push(revisionRow);
+ }
}
}
- // now we need to also collect date points not represented in note revisions:
- // 1. creation for all notes (dateCreated)
- // 2. deletion for deleted notes (dateModified)
- const noteRows = sql.getRows<RecentChangeRow>(`
+ // Date points from the notes table:
+ // - deleted-only view: just the deletion point (dateModified), so each deleted note appears exactly once
+ // - full view: creation (dateCreated) for every note, plus deletion (dateModified) for deleted notes
+ const noteRows = sql.getRows<RecentChangeRow>(deletedOnly ? `
+ SELECT
+ notes.noteId,
+ notes.isDeleted AS current_isDeleted,
+ notes.deleteId AS current_deleteId,
+ notes.title AS current_title,
+ notes.isProtected AS current_isProtected,
+ notes.title,
+ notes.utcDateModified AS utcDate,
+ notes.dateModified AS date
+ FROM notes
+ WHERE notes.isDeleted = 1` : `
SELECT
notes.noteId,
notes.isDeleted AS current_isDeleted,
@@ -71,10 +88,6 @@ function getRecentChanges(req: Request<{ ancestorNoteId: string }, unknown, unkn
}
}
- if (deletedOnly) {
- recentChanges = recentChanges.filter((change) => change.current_isDeleted);
- }
-
recentChanges.sort((a, b) => (a.utcDate > b.utcDate ? -1 : 1));
recentChanges = recentChanges.slice(0, Math.min(500, recentChanges.length));