trilium/src/services/note_revisions.js

56 lines
1.5 KiB
JavaScript
Raw Normal View History

2019-11-09 18:58:52 +08:00
"use strict";
const NoteRevision = require('../entities/note_revision');
const dateUtils = require('../services/date_utils');
/**
* @param {Note} note
*/
async function protectNoteRevisions(note) {
if (await note.hasLabel('disableVersioning')) {
return;
}
2019-11-09 18:58:52 +08:00
for (const revision of await note.getRevisions()) {
if (note.isProtected !== revision.isProtected) {
2019-11-09 20:01:05 +08:00
const content = await revision.getContent();
2019-11-09 18:58:52 +08:00
revision.isProtected = note.isProtected;
2019-11-09 20:01:05 +08:00
// this will force de/encryption
await revision.setContent(content);
2019-11-09 18:58:52 +08:00
await revision.save();
}
}
}
/**
* @param {Note} note
* @return {NoteRevision}
*/
async function createNoteRevision(note) {
const noteRevision = await new NoteRevision({
noteId: note.noteId,
// title and text should be decrypted now
title: note.title,
contentLength: -1, // will be updated in .setContent()
type: note.type,
mime: note.mime,
isProtected: false, // will be fixed in the protectNoteRevisions() call
utcDateLastEdited: note.utcDateModified,
utcDateCreated: dateUtils.utcNowDateTime(),
utcDateModified: dateUtils.utcNowDateTime(),
dateLastEdited: note.dateModified,
dateCreated: dateUtils.localNowDateTime()
}).save();
await noteRevision.setContent(await note.getContent());
return noteRevision;
}
module.exports = {
protectNoteRevisions,
createNoteRevision
};