From 93cae44ba0112e1725e3d16b2d4bf93c6baa6e2a Mon Sep 17 00:00:00 2001 From: zadam Date: Sun, 25 Apr 2021 22:02:32 +0200 Subject: [PATCH] subtree duplication with becca --- src/routes/api/notes.js | 3 +- .../becca/entities/abstract_entity.js | 3 +- src/services/becca/entities/attribute.js | 6 +-- src/services/becca/entities/branch.js | 4 ++ src/services/becca/entities/note.js | 38 ++++++++++++++++--- src/services/notes.js | 12 +++--- 6 files changed, 50 insertions(+), 16 deletions(-) diff --git a/src/routes/api/notes.js b/src/routes/api/notes.js index b8710ae7b..6a0178a30 100644 --- a/src/routes/api/notes.js +++ b/src/routes/api/notes.js @@ -9,6 +9,7 @@ const log = require('../../services/log'); const TaskContext = require('../../services/task_context'); const fs = require('fs'); const noteRevisionService = require("../../services/note_revisions.js"); +const becca = require("../../services/becca/becca.js"); function getNote(req) { const noteId = req.params.noteId; @@ -107,7 +108,7 @@ function sortChildNotes(req) { function protectNote(req) { const noteId = req.params.noteId; - const note = repository.getNote(noteId); + const note = becca.notes[noteId]; const protect = !!parseInt(req.params.isProtected); const includingSubTree = !!parseInt(req.query.subtree); diff --git a/src/services/becca/entities/abstract_entity.js b/src/services/becca/entities/abstract_entity.js index 66a6c0ae9..59f0d9298 100644 --- a/src/services/becca/entities/abstract_entity.js +++ b/src/services/becca/entities/abstract_entity.js @@ -26,7 +26,8 @@ class AbstractEntity { } getUtcDateChanged() { - return this.utcDateModified || this.utcDateCreated; + // FIXME + return this.utcDateModified || this.utcDateCreated || "FAKE"; } get repository() { diff --git a/src/services/becca/entities/attribute.js b/src/services/becca/entities/attribute.js index 91e610634..0f9e69b61 100644 --- a/src/services/becca/entities/attribute.js +++ b/src/services/becca/entities/attribute.js @@ -121,7 +121,9 @@ class Attribute extends AbstractEntity { name: this.name, position: this.position, value: this.value, - isInheritable: this.isInheritable + isInheritable: this.isInheritable, + utcDateModified: dateUtils.utcNowDateTime(), + isDeleted: false }; } @@ -148,8 +150,6 @@ class Attribute extends AbstractEntity { } super.beforeSaving(); - - this.utcDateModified = dateUtils.utcNowDateTime(); } createClone(type, name, value, isInheritable) { diff --git a/src/services/becca/entities/branch.js b/src/services/becca/entities/branch.js index c36de904a..e3893dcaa 100644 --- a/src/services/becca/entities/branch.js +++ b/src/services/becca/entities/branch.js @@ -55,6 +55,10 @@ class Branch extends AbstractEntity { return this.becca.notes[this.noteId]; } + getNote() { + return this.childNote; + } + /** @return {Note} */ get parentNote() { if (!(this.parentNoteId in this.becca.notes)) { diff --git a/src/services/becca/entities/note.js b/src/services/becca/entities/note.js index 727bc10ab..4c25ae878 100644 --- a/src/services/becca/entities/note.js +++ b/src/services/becca/entities/note.js @@ -93,14 +93,22 @@ class Note extends AbstractEntity { return this.parentBranches; } + getBranches() { + return this.parentBranches; + } + getParentNotes() { return this.parents; } - getChildrenNotes() { + getChildNotes() { return this.children; } + getChildBranches() { + return this.children.map(childNote => this.becca.getBranch(childNote.noteId, this.noteId)); + } + /* * Note content has quite special handling - it's not a separate entity, but a lazily loaded * part of Note entity with it's own sync. Reasons behind this hybrid design has been: @@ -525,6 +533,26 @@ class Note extends AbstractEntity { return this.getOwnedAttributes(RELATION, name); } + /** + * @param {string} [type] - (optional) attribute type to filter + * @param {string} [name] - (optional) attribute name to filter + * @returns {Attribute[]} note's "owned" attributes - excluding inherited ones + */ + getOwnedAttributes(type, name) { + if (type && name) { + return this.ownedAttributes.filter(attr => attr.type === type && attr.name === name); + } + else if (type) { + return this.ownedAttributes.filter(attr => attr.type === type); + } + else if (name) { + return this.ownedAttributes.filter(attr => attr.name === name); + } + else { + return this.ownedAttributes.slice(); + } + } + get isArchived() { return this.hasAttribute('label', 'archived'); } @@ -672,6 +700,10 @@ class Note extends AbstractEntity { return this.subtreeNotes.map(note => note.noteId); } + getDescendantNoteIds() { + return this.subtreeNoteIds; + } + get parentCount() { return this.parents.length; } @@ -775,10 +807,6 @@ class Note extends AbstractEntity { return minDistance; } - getChildBranches() { - return this.children.map(childNote => this.becca.getBranch(childNote.noteId, this.noteId)); - } - decrypt() { if (this.isProtected && !this.isDecrypted && protectedSessionService.isProtectedSessionAvailable()) { try { diff --git a/src/services/notes.js b/src/services/notes.js index fe99d2c17..04233f5d0 100644 --- a/src/services/notes.js +++ b/src/services/notes.js @@ -762,7 +762,7 @@ function duplicateSubtree(origNoteId, newParentNoteId) { log.info(`Duplicating ${origNoteId} subtree into ${newParentNoteId}`); - const origNote = repository.getNote(origNoteId); + const origNote = becca.notes[origNoteId]; // might be null if orig note is not in the target newParentNoteId const origBranch = origNote.getBranches().find(branch => branch.parentNoteId === newParentNoteId); @@ -799,16 +799,16 @@ function duplicateSubtreeInner(origNote, origBranch, newParentNoteId, noteIdMapp const newNoteId = noteIdMapping[origNote.noteId]; - const newBranch = new Branch({ + const newBranch = new BeccaBranch(becca,{ noteId: newNoteId, parentNoteId: newParentNoteId, // here increasing just by 1 to make sure it's directly after original notePosition: origBranch ? origBranch.notePosition + 1 : null }).save(); - const existingNote = repository.getNote(newNoteId); + const existingNote = becca.notes[newNoteId]; - if (existingNote) { + if (existingNote.title !== undefined) { // checking that it's not just note's skeleton created because of Branch above // note has multiple clones and was already created from another placement in the tree // so a branch is all we need for this clone return { @@ -817,7 +817,7 @@ function duplicateSubtreeInner(origNote, origBranch, newParentNoteId, noteIdMapp } } - const newNote = new Note(origNote); + const newNote = new BeccaNote(becca, origNote); newNote.noteId = newNoteId; newNote.dateCreated = dateUtils.localNowDateTime(); newNote.utcDateCreated = dateUtils.utcNowDateTime(); @@ -833,7 +833,7 @@ function duplicateSubtreeInner(origNote, origBranch, newParentNoteId, noteIdMapp newNote.setContent(content); for (const attribute of origNote.getOwnedAttributes()) { - const attr = new Attribute(attribute); + const attr = new BeccaAttribute(becca, attribute); attr.attributeId = undefined; // force creation of new attribute attr.noteId = newNote.noteId;