Notes/src/services/revisions.js

50 lines
1.4 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");
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-04-14 16:49:06 +02:00
if (note.isProtected === revision.isProtected) {
continue;
}
2019-11-09 13:01:05 +01:00
2023-04-14 16:49:06 +02:00
try {
const content = revision.getContent();
2019-11-09 11:58:52 +01:00
2023-04-14 16:49:06 +02:00
revision.isProtected = note.isProtected;
2019-11-09 13:01:05 +01:00
2023-04-14 16:49:06 +02:00
// this will force de/encryption
revision.setContent(content, {forceSave: true});
} catch (e) {
log.error(`Could not un/protect note revision '${revision.revisionId}'`);
2020-12-09 22:49:55 +01:00
2023-04-14 16:49:06 +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 note revisions: ${JSON.stringify(revisionIdsToErase)}`);
sql.executeMany(`DELETE FROM revisions WHERE revisionId IN (???)`, revisionIdsToErase);
sql.executeMany(`UPDATE entity_changes SET isErased = 1 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
};