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
|
|
|
|
|
|
|
/**
|
2023-01-03 13:52:37 +01:00
|
|
|
* @param {BNote} note
|
2019-11-09 11:58:52 +01:00
|
|
|
*/
|
2023-06-04 23:01:40 +02: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`);
|
|
|
|
}
|
2022-06-13 22:54:08 +02:00
|
|
|
|
2023-06-04 23:01:40 +02:00
|
|
|
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) {
|
2023-06-04 23:01:40 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-04 23:01:40 +02:00
|
|
|
function eraseRevisions(revisionIdsToErase) {
|
|
|
|
if (revisionIdsToErase.length === 0) {
|
2020-12-16 14:36:24 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-04 23:01:40 +02:00
|
|
|
log.info(`Removing note revisions: ${JSON.stringify(revisionIdsToErase)}`);
|
2021-02-14 21:35:13 +01:00
|
|
|
|
2023-06-04 23:01:40 +02:00
|
|
|
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 = {
|
2023-06-04 23:01:40 +02:00
|
|
|
protectRevisions,
|
|
|
|
eraseRevisions
|
2020-06-20 12:31:38 +02:00
|
|
|
};
|