Notes/src/services/revisions.js

64 lines
2.0 KiB
JavaScript
Raw Normal View History

2019-11-09 11:58:52 +01:00
"use strict";
2020-12-16 14:36:24 +01:00
const log = require('./log');
const sql = require('./sql');
2023-04-14 16:49:06 +02:00
const protectedSessionService = require("./protected_session");
2023-07-29 21:59:20 +02:00
const dateUtils = require("./date_utils");
2019-11-09 11:58:52 +01:00
/**
* @param {BNote} note
2019-11-09 11:58:52 +01:00
*/
function protectRevisions(note) {
2023-04-14 16:49:06 +02:00
if (!protectedSessionService.isProtectedSessionAvailable()) {
throw new Error(`Cannot (un)protect revisions of note '${note.noteId}' without active protected session`);
}
for (const revision of note.getRevisions()) {
2023-09-08 00:19:30 +02:00
if (note.isProtected !== revision.isProtected) {
try {
const content = revision.getContent();
revision.isProtected = note.isProtected;
2019-11-09 13:01:05 +01:00
2023-09-08 00:19:30 +02:00
// this will force de/encryption
revision.setContent(content, {forceSave: true});
} catch (e) {
log.error(`Could not un/protect note revision '${revision.revisionId}'`);
throw e;
}
}
2019-11-09 11:58:52 +01:00
2023-09-08 00:19:30 +02:00
for (const attachment of revision.getAttachments()) {
if (note.isProtected !== attachment.isProtected) {
try {
const content = attachment.getContent();
2019-11-09 13:01:05 +01:00
2023-09-08 00:19:30 +02:00
attachment.isProtected = note.isProtected;
attachment.setContent(content, {forceSave: true});
} catch (e) {
log.error(`Could not un/protect attachment '${attachment.attachmentId}'`);
2020-12-09 22:49:55 +01:00
2023-09-08 00:19:30 +02:00
throw e;
}
}
2019-11-09 11:58:52 +01:00
}
}
}
function eraseRevisions(revisionIdsToErase) {
if (revisionIdsToErase.length === 0) {
2020-12-16 14:36:24 +01:00
return;
}
log.info(`Removing revisions: ${JSON.stringify(revisionIdsToErase)}`);
sql.executeMany(`DELETE FROM revisions WHERE revisionId IN (???)`, revisionIdsToErase);
2023-07-29 21:59:20 +02:00
sql.executeMany(`UPDATE entity_changes SET isErased = 1, utcDateChanged = '${dateUtils.utcNowDateTime()}' WHERE entityName = 'revisions' AND entityId IN (???)`, revisionIdsToErase);
2020-12-16 14:36:24 +01:00
}
2019-11-09 11:58:52 +01:00
module.exports = {
protectRevisions,
eraseRevisions
2020-06-20 12:31:38 +02:00
};