fix manual erasing note revisions

This commit is contained in:
zadam 2020-12-16 14:36:24 +01:00
parent 1b0bb22273
commit d0578971f7
7 changed files with 33 additions and 47 deletions

View File

@ -86,7 +86,7 @@
"jsdoc": "3.6.6", "jsdoc": "3.6.6",
"lorem-ipsum": "2.0.3", "lorem-ipsum": "2.0.3",
"rcedit": "3.0.0", "rcedit": "3.0.0",
"webpack": "5.10.1", "webpack": "5.10.3",
"webpack-cli": "4.2.0" "webpack-cli": "4.2.0"
}, },
"optionalDependencies": { "optionalDependencies": {

View File

@ -21,7 +21,6 @@ const RELATION = 'relation';
* @property {boolean} isProtected - true if note is protected * @property {boolean} isProtected - true if note is protected
* @property {boolean} isDeleted - true if note is deleted * @property {boolean} isDeleted - true if note is deleted
* @property {string|null} deleteId - ID identifying delete transaction * @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} dateCreated - local date time (with offset)
* @property {string} dateModified - local date time (with offset) * @property {string} dateModified - local date time (with offset)
* @property {string} utcDateCreated * @property {string} utcDateCreated
@ -70,9 +69,9 @@ class Note extends Entity {
/** @returns {*} */ /** @returns {*} */
getContent(silentNotFoundError = false) { getContent(silentNotFoundError = false) {
if (this.content === undefined) { 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) { if (silentNotFoundError) {
return undefined; return undefined;
} }
@ -81,7 +80,7 @@ class Note extends Entity {
} }
} }
this.content = content; this.content = row.content;
if (this.isProtected) { if (this.isProtected) {
if (this.isContentAvailable) { if (this.isContentAvailable) {
@ -783,7 +782,7 @@ class Note extends Entity {
* @returns {NoteRevision[]} * @returns {NoteRevision[]}
*/ */
getRevisions() { 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]);
} }
/** /**

View File

@ -28,12 +28,11 @@ const entityChangesService = require('../services/entity_changes.js');
class NoteRevision extends Entity { class NoteRevision extends Entity {
static get entityName() { return "note_revisions"; } static get entityName() { return "note_revisions"; }
static get primaryKeyName() { return "noteRevisionId"; } 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) { constructor(row) {
super(row); super(row);
this.isErased = !!this.isErased;
this.isProtected = !!this.isProtected; this.isProtected = !!this.isProtected;
if (this.isProtected) { if (this.isProtected) {

View File

@ -14,7 +14,6 @@ function getNoteRevisions(req) {
FROM note_revisions FROM note_revisions
JOIN note_revision_contents ON note_revisions.noteRevisionId = note_revision_contents.noteRevisionId JOIN note_revision_contents ON note_revisions.noteRevisionId = note_revision_contents.noteRevisionId
WHERE noteId = ? WHERE noteId = ?
AND isErased = 0
ORDER BY utcDateCreated DESC`, [req.params.noteId]); ORDER BY utcDateCreated DESC`, [req.params.noteId]);
} }
@ -80,33 +79,16 @@ function downloadNoteRevision(req, res) {
res.send(noteRevision.getContent()); res.send(noteRevision.getContent());
} }
/**
* @param {NoteRevision} noteRevision
*/
function eraseOneNoteRevision(noteRevision) {
noteRevision.isErased = true;
noteRevision.title = null;
noteRevision.save();
noteRevision.setContent(null);
}
function eraseAllNoteRevisions(req) { function eraseAllNoteRevisions(req) {
const noteRevisionsToErase = repository.getEntities( const noteRevisionIdsToErase = sql.getColumn(
'SELECT * FROM note_revisions WHERE isErased = 0 AND noteId = ?', 'SELECT noteRevisionId FROM note_revisions WHERE isErased = 0 AND noteId = ?',
[req.params.noteId]); [req.params.noteId]);
for (const noteRevision of noteRevisionsToErase) { noteRevisionService.eraseNoteRevisions(noteRevisionIdsToErase);
eraseOneNoteRevision(noteRevision);
}
} }
function eraseNoteRevision(req) { function eraseNoteRevision(req) {
const noteRevision = repository.getNoteRevision(req.params.noteRevisionId); noteRevisionService.eraseNoteRevisions([req.params.noteRevisionId]);
if (noteRevision && !noteRevision.isErased) {
eraseOneNoteRevision(noteRevision);
}
} }
function restoreNoteRevision(req) { function restoreNoteRevision(req) {

View File

@ -114,6 +114,10 @@ function getTree(req) {
const collectedNoteIds = new Set([subTreeNoteId]); const collectedNoteIds = new Set([subTreeNoteId]);
function collect(parentNote) { function collect(parentNote) {
if (!parentNote) {
console.trace(parentNote);
}
for (const childNote of parentNote.children) { for (const childNote of parentNote.children) {
collectedNoteIds.add(childNote.noteId); collectedNoteIds.add(childNote.noteId);

View File

@ -1,8 +1,9 @@
"use strict"; "use strict";
const NoteRevision = require('../entities/note_revision'); const NoteRevision = require('../entities/note_revision');
const dateUtils = require('../services/date_utils'); const dateUtils = require('./date_utils');
const log = require('../services/log'); const log = require('./log');
const sql = require('./sql');
/** /**
* @param {Note} note * @param {Note} note
@ -69,7 +70,20 @@ function createNoteRevision(note) {
return noteRevision; 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 = { module.exports = {
protectNoteRevisions, protectNoteRevisions,
createNoteRevision createNoteRevision,
eraseNoteRevisions
}; };

View File

@ -698,7 +698,7 @@ function eraseNotes(noteIdsToErase) {
const noteRevisionIdsToErase = sql.getManyRows(`SELECT noteRevisionId FROM note_revisions WHERE noteId IN (???)`, noteIdsToErase) const noteRevisionIdsToErase = sql.getManyRows(`SELECT noteRevisionId FROM note_revisions WHERE noteId IN (???)`, noteIdsToErase)
.map(row => row.noteRevisionId); .map(row => row.noteRevisionId);
eraseNoteRevisions(noteRevisionIdsToErase); noteRevisionService.eraseNoteRevisions(noteRevisionIdsToErase);
log.info(`Erased notes: ${JSON.stringify(noteIdsToErase)}`); 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); 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) { function eraseDeletedEntities(eraseEntitiesAfterTimeInSeconds = null) {
if (eraseEntitiesAfterTimeInSeconds === null) { if (eraseEntitiesAfterTimeInSeconds === null) {
eraseEntitiesAfterTimeInSeconds = optionService.getOptionInt('eraseEntitiesAfterTimeInSeconds'); eraseEntitiesAfterTimeInSeconds = optionService.getOptionInt('eraseEntitiesAfterTimeInSeconds');