mirror of
https://github.com/zadam/trilium.git
synced 2025-11-15 09:45:52 +01:00
expose ability to create note revisions in backend API #2890
This commit is contained in:
@@ -9,6 +9,8 @@ const entityChangesService = require('../../services/entity_changes');
|
||||
const AbstractEntity = require("./abstract_entity");
|
||||
const NoteRevision = require("./note_revision");
|
||||
const TaskContext = require("../../services/task_context.js");
|
||||
const optionService = require("../../services/options.js");
|
||||
const noteRevisionService = require("../../services/note_revisions.js");
|
||||
|
||||
const LABEL = 'label';
|
||||
const RELATION = 'relation';
|
||||
@@ -1164,6 +1166,41 @@ class Note extends AbstractEntity {
|
||||
return !(this.noteId in this.becca.notes);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {NoteRevision|null}
|
||||
*/
|
||||
saveNoteRevision() {
|
||||
const content = this.getContent();
|
||||
|
||||
if (!content || (Buffer.isBuffer(content) && content.byteLength === 0)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const contentMetadata = this.getContentMetadata();
|
||||
|
||||
const noteRevision = new NoteRevision({
|
||||
noteId: this.noteId,
|
||||
// title and text should be decrypted now
|
||||
title: this.title,
|
||||
type: this.type,
|
||||
mime: this.mime,
|
||||
isProtected: false, // will be fixed in the protectNoteRevisions() call
|
||||
utcDateLastEdited: this.utcDateModified > contentMetadata.utcDateModified
|
||||
? this.utcDateModified
|
||||
: contentMetadata.utcDateModified,
|
||||
utcDateCreated: dateUtils.utcNowDateTime(),
|
||||
utcDateModified: dateUtils.utcNowDateTime(),
|
||||
dateLastEdited: this.dateModified > contentMetadata.dateModified
|
||||
? this.dateModified
|
||||
: contentMetadata.dateModified,
|
||||
dateCreated: dateUtils.localNowDateTime()
|
||||
}).save();
|
||||
|
||||
noteRevision.setContent(content);
|
||||
|
||||
return noteRevision;
|
||||
}
|
||||
|
||||
beforeSaving() {
|
||||
super.beforeSaving();
|
||||
|
||||
|
||||
@@ -206,7 +206,7 @@ function changeTitle(req) {
|
||||
const noteTitleChanged = note.title !== title;
|
||||
|
||||
if (noteTitleChanged) {
|
||||
noteService.saveNoteRevision(note);
|
||||
noteService.saveNoteRevisionIfNeeded(note);
|
||||
}
|
||||
|
||||
note.title = title;
|
||||
|
||||
@@ -30,46 +30,6 @@ function protectNoteRevisions(note) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Note} note
|
||||
* @return {NoteRevision|null}
|
||||
*/
|
||||
function createNoteRevision(note) {
|
||||
if (note.hasLabel("disableVersioning")) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const content = note.getContent();
|
||||
|
||||
if (!content || (Buffer.isBuffer(content) && content.byteLength === 0)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const contentMetadata = note.getContentMetadata();
|
||||
|
||||
const noteRevision = new NoteRevision({
|
||||
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,
|
||||
utcDateCreated: dateUtils.utcNowDateTime(),
|
||||
utcDateModified: dateUtils.utcNowDateTime(),
|
||||
dateLastEdited: note.dateModified > contentMetadata.dateModified
|
||||
? note.dateModified
|
||||
: contentMetadata.dateModified,
|
||||
dateCreated: dateUtils.localNowDateTime()
|
||||
}).save();
|
||||
|
||||
noteRevision.setContent(content);
|
||||
|
||||
return noteRevision;
|
||||
}
|
||||
|
||||
function eraseNoteRevisions(noteRevisionIdsToErase) {
|
||||
if (noteRevisionIdsToErase.length === 0) {
|
||||
return;
|
||||
@@ -86,6 +46,5 @@ function eraseNoteRevisions(noteRevisionIdsToErase) {
|
||||
|
||||
module.exports = {
|
||||
protectNoteRevisions,
|
||||
createNoteRevision,
|
||||
eraseNoteRevisions
|
||||
};
|
||||
|
||||
@@ -491,7 +491,7 @@ function saveLinks(note, content) {
|
||||
return content;
|
||||
}
|
||||
|
||||
function saveNoteRevision(note) {
|
||||
function saveNoteRevisionIfNeeded(note) {
|
||||
// files and images are versioned separately
|
||||
if (note.type === 'file' || note.type === 'image' || note.hasLabel('disableVersioning')) {
|
||||
return;
|
||||
@@ -508,7 +508,7 @@ function saveNoteRevision(note) {
|
||||
const msSinceDateCreated = now.getTime() - dateUtils.parseDateTime(note.utcDateCreated).getTime();
|
||||
|
||||
if (!existingNoteRevisionId && msSinceDateCreated >= noteRevisionSnapshotTimeInterval * 1000) {
|
||||
noteRevisionService.createNoteRevision(note);
|
||||
note.saveNoteRevision();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -519,7 +519,7 @@ function updateNote(noteId, noteUpdates) {
|
||||
throw new Error(`Note '${noteId}' is not available for change!`);
|
||||
}
|
||||
|
||||
saveNoteRevision(note);
|
||||
saveNoteRevisionIfNeeded(note);
|
||||
|
||||
// if protected status changed, then we need to encrypt/decrypt the content anyway
|
||||
if (['file', 'image'].includes(note.type) && note.isProtected !== noteUpdates.isProtected) {
|
||||
@@ -910,6 +910,6 @@ module.exports = {
|
||||
triggerNoteTitleChanged,
|
||||
eraseDeletedNotesNow,
|
||||
eraseNotesWithDeleteId,
|
||||
saveNoteRevision,
|
||||
saveNoteRevisionIfNeeded,
|
||||
downloadImages
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user