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
fix manual erasing note revisions
zadamd0578971f7eb
diff --git a/package.json b/package.jsonindex 82959c0d75..6157b23a50 100644--- a/package.json+++ b/package.json@@ -86,7 +86,7 @@"jsdoc": "3.6.6","lorem-ipsum": "2.0.3","rcedit": "3.0.0",- "webpack": "5.10.1",+ "webpack": "5.10.3","webpack-cli": "4.2.0"},"optionalDependencies": {diff --git a/src/entities/note.js b/src/entities/note.jsindex 40dec01378..5100c1e5a2 100644--- a/src/entities/note.js+++ b/src/entities/note.js@@ -21,7 +21,6 @@ const RELATION = 'relation';* @property {boolean} isProtected - true if note is protected* @property {boolean} isDeleted - true if note is deleted* @property {string|null} deleteId - ID identifying delete transaction- * @property {boolean} isErased - true if note's content is erased after it has been deleted* @property {string} dateCreated - local date time (with offset)* @property {string} dateModified - local date time (with offset)* @property {string} utcDateCreated@@ -70,9 +69,9 @@ class Note extends Entity {/** @returns {*} */getContent(silentNotFoundError = false) {if (this.content === undefined) {- const content = sql.getValue(`SELECT content FROM note_contents WHERE noteId = ?`, [this.noteId]);+ const row = sql.getRow(`SELECT content FROM note_contents WHERE noteId = ?`, [this.noteId]);- if (!content) {+ if (!row) {if (silentNotFoundError) {return undefined;}@@ -81,7 +80,7 @@ class Note extends Entity {}}- this.content = content;+ this.content = row.content;if (this.isProtected) {if (this.isContentAvailable) {@@ -783,7 +782,7 @@ class Note extends Entity {* @returns {NoteRevision[]}*/getRevisions() {- return this.repository.getEntities("SELECT * FROM note_revisions WHERE isErased = 0 AND noteId = ?", [this.noteId]);+ return this.repository.getEntities("SELECT * FROM note_revisions WHERE noteId = ?", [this.noteId]);}/**diff --git a/src/entities/note_revision.js b/src/entities/note_revision.jsindex eee980182c..53afce996b 100644--- a/src/entities/note_revision.js+++ b/src/entities/note_revision.js@@ -28,12 +28,11 @@ const entityChangesService = require('../services/entity_changes.js');class NoteRevision extends Entity {static get entityName() { return "note_revisions"; }static get primaryKeyName() { return "noteRevisionId"; }- static get hashedProperties() { return ["noteRevisionId", "noteId", "title", "isErased", "isProtected", "dateLastEdited", "dateCreated", "utcDateLastEdited", "utcDateCreated", "utcDateModified"]; }+ static get hashedProperties() { return ["noteRevisionId", "noteId", "title", "isProtected", "dateLastEdited", "dateCreated", "utcDateLastEdited", "utcDateCreated", "utcDateModified"]; }constructor(row) {super(row);- this.isErased = !!this.isErased;this.isProtected = !!this.isProtected;if (this.isProtected) {diff --git a/src/routes/api/note_revisions.js b/src/routes/api/note_revisions.jsindex c016768c7f..4fab89c79a 100644--- a/src/routes/api/note_revisions.js+++ b/src/routes/api/note_revisions.js@@ -13,8 +13,7 @@ function getNoteRevisions(req) {LENGTH(note_revision_contents.content) AS contentLengthFROM note_revisionsJOIN note_revision_contents ON note_revisions.noteRevisionId = note_revision_contents.noteRevisionId- WHERE noteId = ?- AND isErased = 0+ WHERE noteId = ?ORDER BY utcDateCreated DESC`, [req.params.noteId]);}@@ -80,33 +79,16 @@ function downloadNoteRevision(req, res) {res.send(noteRevision.getContent());}-/**- * @param {NoteRevision} noteRevision- */-function eraseOneNoteRevision(noteRevision) {- noteRevision.isErased = true;- noteRevision.title = null;- noteRevision.save();-- noteRevision.setContent(null);-}-function eraseAllNoteRevisions(req) {- const noteRevisionsToErase = repository.getEntities(- 'SELECT * FROM note_revisions WHERE isErased = 0 AND noteId = ?',+ const noteRevisionIdsToErase = sql.getColumn(+ 'SELECT noteRevisionId FROM note_revisions WHERE isErased = 0 AND noteId = ?',[req.params.noteId]);- for (const noteRevision of noteRevisionsToErase) {- eraseOneNoteRevision(noteRevision);- }+ noteRevisionService.eraseNoteRevisions(noteRevisionIdsToErase);}function eraseNoteRevision(req) {- const noteRevision = repository.getNoteRevision(req.params.noteRevisionId);-- if (noteRevision && !noteRevision.isErased) {- eraseOneNoteRevision(noteRevision);- }+ noteRevisionService.eraseNoteRevisions([req.params.noteRevisionId]);}function restoreNoteRevision(req) {diff --git a/src/routes/api/tree.js b/src/routes/api/tree.jsindex f371de4e50..b9cb69b35c 100644--- a/src/routes/api/tree.js+++ b/src/routes/api/tree.js@@ -114,6 +114,10 @@ function getTree(req) {const collectedNoteIds = new Set([subTreeNoteId]);function collect(parentNote) {+ if (!parentNote) {+ console.trace(parentNote);+ }+for (const childNote of parentNote.children) {collectedNoteIds.add(childNote.noteId);diff --git a/src/services/note_revisions.js b/src/services/note_revisions.jsindex d1e7f37b4a..60dfa00b58 100644--- a/src/services/note_revisions.js+++ b/src/services/note_revisions.js@@ -1,8 +1,9 @@"use strict";const NoteRevision = require('../entities/note_revision');-const dateUtils = require('../services/date_utils');-const log = require('../services/log');+const dateUtils = require('./date_utils');+const log = require('./log');+const sql = require('./sql');/*** @param {Note} note@@ -69,7 +70,20 @@ function createNoteRevision(note) {return noteRevision;}+function eraseNoteRevisions(noteRevisionIdsToErase) {+ if (noteRevisionIdsToErase.length === 0) {+ return;+ }++ sql.executeMany(`DELETE FROM note_revisions WHERE noteRevisionId IN (???)`, noteRevisionIdsToErase);+ sql.executeMany(`UPDATE entity_changes SET isErased = 1 WHERE entityName = 'note_revisions' AND entityId IN (???)`, noteRevisionIdsToErase);++ sql.executeMany(`DELETE FROM note_revision_contents WHERE noteRevisionId IN (???)`, noteRevisionIdsToErase);+ sql.executeMany(`UPDATE entity_changes SET isErased = 1 WHERE entityName = 'note_revision_contents' AND entityId IN (???)`, noteRevisionIdsToErase);+}+module.exports = {protectNoteRevisions,- createNoteRevision+ createNoteRevision,+ eraseNoteRevisions};diff --git a/src/services/notes.js b/src/services/notes.jsindex fbc07de8df..c1034726fb 100644--- a/src/services/notes.js+++ b/src/services/notes.js@@ -698,7 +698,7 @@ function eraseNotes(noteIdsToErase) {const noteRevisionIdsToErase = sql.getManyRows(`SELECT noteRevisionId FROM note_revisions WHERE noteId IN (???)`, noteIdsToErase).map(row => row.noteRevisionId);- eraseNoteRevisions(noteRevisionIdsToErase);+ noteRevisionService.eraseNoteRevisions(noteRevisionIdsToErase);log.info(`Erased notes: ${JSON.stringify(noteIdsToErase)}`);}@@ -723,18 +723,6 @@ function eraseAttributes(attributeIdsToErase) {sql.executeMany(`UPDATE entity_changes SET isErased = 1 WHERE entityName = 'attributes' AND entityId IN (???)`, attributeIdsToErase);}-function eraseNoteRevisions(noteRevisionIdsToErase) {- if (noteRevisionIdsToErase.length === 0) {- return;- }-- sql.executeMany(`DELETE FROM note_revisions WHERE noteRevisionId IN (???)`, noteRevisionIdsToErase);- sql.executeMany(`UPDATE entity_changes SET isErased = 1 WHERE entityName = 'note_revisions' AND entityId IN (???)`, noteRevisionIdsToErase);-- sql.executeMany(`DELETE FROM note_revision_contents WHERE noteRevisionId IN (???)`, noteRevisionIdsToErase);- sql.executeMany(`UPDATE entity_changes SET isErased = 1 WHERE entityName = 'note_revision_contents' AND entityId IN (???)`, noteRevisionIdsToErase);-}-function eraseDeletedEntities(eraseEntitiesAfterTimeInSeconds = null) {if (eraseEntitiesAfterTimeInSeconds === null) {eraseEntitiesAfterTimeInSeconds = optionService.getOptionInt('eraseEntitiesAfterTimeInSeconds');