Notes/src/services/revisions.js

52 lines
1.5 KiB
JavaScript
Raw Normal View History

2019-11-09 11:58:52 +01:00
"use strict";
const log = require('./log');
2024-02-16 22:44:12 +02:00
const sql = require('./sql');
2024-02-16 23:09:59 +02:00
const protectedSessionService = require('./protected_session');
2024-02-16 21:38:09 +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
}
}
}
module.exports = {
2023-12-04 00:11:24 +01:00
protectRevisions
2020-06-20 12:31:38 +02:00
};