Notes/src/services/note_revisions.js

92 lines
2.8 KiB
JavaScript
Raw Normal View History

2019-11-09 11:58:52 +01:00
"use strict";
const NoteRevision = require('../entities/note_revision');
2020-12-16 14:36:24 +01:00
const dateUtils = require('./date_utils');
const log = require('./log');
const sql = require('./sql');
2019-11-09 11:58:52 +01:00
/**
* @param {Note} note
*/
2020-06-20 12:31:38 +02:00
function protectNoteRevisions(note) {
for (const revision of note.getRevisions()) {
2019-11-09 11:58:52 +01:00
if (note.isProtected !== revision.isProtected) {
2020-12-09 22:49:55 +01:00
try {
const content = revision.getContent();
2019-11-09 13:01:05 +01:00
2020-12-09 22:49:55 +01:00
revision.isProtected = note.isProtected;
2019-11-09 11:58:52 +01:00
2020-12-09 22:49:55 +01:00
// this will force de/encryption
revision.setContent(content);
2019-11-09 13:01:05 +01:00
2020-12-09 22:49:55 +01:00
revision.save();
}
catch (e) {
log.error("Could not un/protect note revision ID = " + revision.noteRevisionId);
throw e;
}
2019-11-09 11:58:52 +01:00
}
}
}
/**
* @param {Note} note
* @return {NoteRevision|null}
2019-11-09 11:58:52 +01:00
*/
2020-06-20 12:31:38 +02:00
function createNoteRevision(note) {
if (note.hasLabel("disableVersioning")) {
return null;
}
const content = note.getContent();
if (!content || (Buffer.isBuffer(content) && content.byteLength === 0)) {
return null;
}
2020-09-18 21:47:59 +02:00
const contentMetadata = note.getContentMetadata();
2020-06-20 12:31:38 +02:00
const noteRevision = new NoteRevision({
2019-11-09 11:58:52 +01:00
noteId: note.noteId,
// title and text should be decrypted now
title: note.title,
type: note.type,
mime: note.mime,
isProtected: false, // will be fixed in the protectNoteRevisions() call
utcDateLastEdited: note.utcDateModified > contentMetadata.utcDateModified
? note.utcDateModified
: contentMetadata.utcDateModified,
2019-11-09 11:58:52 +01:00
utcDateCreated: dateUtils.utcNowDateTime(),
utcDateModified: dateUtils.utcNowDateTime(),
dateLastEdited: note.dateModified > contentMetadata.dateModified
? note.dateModified
: contentMetadata.dateModified,
2019-11-09 11:58:52 +01:00
dateCreated: dateUtils.localNowDateTime()
}).save();
noteRevision.setContent(content);
2019-11-09 11:58:52 +01:00
return noteRevision;
}
2020-12-16 14:36:24 +01:00
function eraseNoteRevisions(noteRevisionIdsToErase) {
if (noteRevisionIdsToErase.length === 0) {
return;
}
log.info(`Removing note revisions: ${JSON.stringify(noteRevisionIdsToErase)}`);
2020-12-16 14:36:24 +01:00
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);
}
2019-11-09 11:58:52 +01:00
module.exports = {
protectNoteRevisions,
2020-12-16 14:36:24 +01:00
createNoteRevision,
eraseNoteRevisions
2020-06-20 12:31:38 +02:00
};