Notes/src/services/note_revisions.js

68 lines
1.8 KiB
JavaScript
Raw Normal View History

2019-11-09 11:58:52 +01:00
"use strict";
const NoteRevision = require('../entities/note_revision');
const dateUtils = require('../services/date_utils');
/**
* @param {Note} note
*/
2020-06-20 12:31:38 +02:00
function protectNoteRevisions(note) {
for (const revision of note.getRevisions()) {
2019-11-09 11:58:52 +01:00
if (note.isProtected !== revision.isProtected) {
2020-06-20 12:31:38 +02:00
const content = revision.getContent();
2019-11-09 13:01:05 +01:00
2019-11-09 11:58:52 +01:00
revision.isProtected = note.isProtected;
2019-11-09 13:01:05 +01:00
// this will force de/encryption
2020-06-20 12:31:38 +02:00
revision.setContent(content);
2019-11-09 13:01:05 +01:00
2020-06-20 12:31:38 +02:00
revision.save();
2019-11-09 11:58:52 +01:00
}
}
}
/**
* @param {Note} note
* @return {NoteRevision|null}
2019-11-09 11:58:52 +01:00
*/
2020-06-20 12:31:38 +02:00
function createNoteRevision(note) {
if (note.hasLabel("disableVersioning")) {
return null;
}
const content = note.getContent();
if (!content || (Buffer.isBuffer(content) && content.byteLength === 0)) {
return null;
}
2020-09-18 21:47:59 +02:00
const contentMetadata = note.getContentMetadata();
2020-06-20 12:31:38 +02:00
const noteRevision = new NoteRevision({
2019-11-09 11:58:52 +01:00
noteId: note.noteId,
// title and text should be decrypted now
title: note.title,
type: note.type,
mime: note.mime,
isProtected: false, // will be fixed in the protectNoteRevisions() call
utcDateLastEdited: note.utcDateModified > contentMetadata.utcDateModified
? note.utcDateModified
: contentMetadata.utcDateModified,
2019-11-09 11:58:52 +01:00
utcDateCreated: dateUtils.utcNowDateTime(),
utcDateModified: dateUtils.utcNowDateTime(),
dateLastEdited: note.dateModified > contentMetadata.dateModified
? note.dateModified
: contentMetadata.dateModified,
2019-11-09 11:58:52 +01:00
dateCreated: dateUtils.localNowDateTime()
}).save();
noteRevision.setContent(content);
2019-11-09 11:58:52 +01:00
return noteRevision;
}
module.exports = {
protectNoteRevisions,
createNoteRevision
2020-06-20 12:31:38 +02:00
};