diff --git a/src/public/app/services/render.js b/src/public/app/services/render.js index a0ddd7545..565d954be 100644 --- a/src/public/app/services/render.js +++ b/src/public/app/services/render.js @@ -26,4 +26,4 @@ async function render(note, $el) { export default { render -} \ No newline at end of file +} diff --git a/src/routes/api/image.js b/src/routes/api/image.js index d36788bf4..4c21386c5 100644 --- a/src/routes/api/image.js +++ b/src/routes/api/image.js @@ -1,7 +1,7 @@ "use strict"; const imageService = require('../../services/image'); -const repository = require('../../services/repository'); +const becca = require('../../services/becca/becca'); const RESOURCE_DIR = require('../../services/resource_dir').RESOURCE_DIR; const fs = require('fs'); diff --git a/src/routes/api/notes.js b/src/routes/api/notes.js index 63a26838d..ce2a2dfc5 100644 --- a/src/routes/api/notes.js +++ b/src/routes/api/notes.js @@ -81,11 +81,9 @@ function deleteNote(req) { } function undeleteNote(req) { - const note = becca.getNote(req.params.noteId); - const taskContext = TaskContext.getInstance(utils.randomString(10), 'undeleteNotes'); - noteService.undeleteNote(note, note.deleteId, taskContext); + noteService.undeleteNote(req.params.noteId, taskContext); taskContext.taskSucceeded(); } diff --git a/src/services/becca/becca_loader.js b/src/services/becca/becca_loader.js index d33ef233e..0eb3df1b4 100644 --- a/src/services/becca/becca_loader.js +++ b/src/services/becca/becca_loader.js @@ -9,12 +9,13 @@ const Note = require('./entities/note'); const Branch = require('./entities/branch'); const Attribute = require('./entities/attribute'); const Option = require('./entities/option'); +const cls = require("../cls.js"); const beccaLoaded = new Promise((res, rej) => { sqlInit.dbReady.then(() => { load(); - require('../options_init').initStartupOptions(); + cls.init(() => require('../options_init').initStartupOptions()); res(); }); diff --git a/src/services/becca/entities/abstract_entity.js b/src/services/becca/entities/abstract_entity.js index be429b969..465f1a69f 100644 --- a/src/services/becca/entities/abstract_entity.js +++ b/src/services/becca/entities/abstract_entity.js @@ -3,8 +3,9 @@ const utils = require('../../utils'); const sql = require('../../sql'); const entityChangesService = require('../../entity_changes'); -const eventService = require("../../events.js"); -const cls = require("../../cls.js"); +const eventService = require("../../events"); +const dateUtils = require("../../date_utils"); +const cls = require("../../cls"); let becca = null; @@ -99,8 +100,14 @@ class AbstractEntity { const entityId = this[this.constructor.primaryKeyName]; const entityName = this.constructor.entityName; - sql.execute(`UPDATE ${entityName} SET isDeleted = 1, deleteId = ? WHERE ${this.constructor.primaryKeyName} = ?`, - [deleteId, entityId]); + sql.execute(`UPDATE ${entityName} SET isDeleted = 1, deleteId = ?, utcDateModified = ? + WHERE ${this.constructor.primaryKeyName} = ?`, + [deleteId, dateUtils.utcNowDateTime(), entityId]); + + if (this.dateModified) { + sql.execute(`UPDATE ${entityName} SET dateModified = ? WHERE ${this.constructor.primaryKeyName} = ?`, + [dateUtils.localNowDateTime(), entityId]); + } this.addEntityChange(true); diff --git a/src/services/becca/entities/branch.js b/src/services/becca/entities/branch.js index d814d3c8d..0d6792571 100644 --- a/src/services/becca/entities/branch.js +++ b/src/services/becca/entities/branch.js @@ -91,6 +91,7 @@ class Branch extends AbstractEntity { prefix: this.prefix, notePosition: this.notePosition, isExpanded: this.isExpanded, + isDeleted: false, utcDateModified: this.utcDateModified, // not used for anything, will be later dropped utcDateCreated: dateUtils.utcNowDateTime() diff --git a/src/services/becca/entities/note.js b/src/services/becca/entities/note.js index d060d7b84..2eb311744 100644 --- a/src/services/becca/entities/note.js +++ b/src/services/becca/entities/note.js @@ -79,7 +79,7 @@ class Note extends AbstractEntity { // ------ Derived attributes ------ /** @param {boolean} */ - this.isDecrypted = !row.isProtected || !!row.isContentAvailable; + this.isDecrypted = !row.isProtected || row.isContentAvailable; this.decrypt(); @@ -87,6 +87,12 @@ class Note extends AbstractEntity { this.flatTextCache = null; } + get isContentAvailable() { + return !this.noteId // new note which was not encrypted yet + || !this.isProtected + || protectedSessionService.isProtectedSessionAvailable() + } + getParentBranches() { return this.parentBranches; } @@ -852,6 +858,7 @@ class Note extends AbstractEntity { isProtected: this.isProtected, type: this.type, mime: this.mime, + isDeleted: false, dateCreated: this.dateCreated, dateModified: this.dateModified, utcDateCreated: this.utcDateCreated, diff --git a/src/services/notes.js b/src/services/notes.js index 690c4a869..223afafef 100644 --- a/src/services/notes.js +++ b/src/services/notes.js @@ -5,9 +5,6 @@ const dateUtils = require('./date_utils'); const entityChangesService = require('./entity_changes.js'); const eventService = require('./events'); const cls = require('../services/cls'); -const BeccaNote = require('../services/becca/entities/note.js'); -const BeccaBranch = require('../services/becca/entities/branch.js'); -const BeccaAttribute = require('../services/becca/entities/attribute.js'); const protectedSessionService = require('../services/protected_session'); const log = require('../services/log'); const utils = require('../services/utils'); @@ -69,7 +66,7 @@ function deriveMime(type, mime) { function copyChildAttributes(parentNote, childNote) { for (const attr of parentNote.getAttributes()) { if (attr.name.startsWith("child:")) { - new BeccaAttribute({ + new Attribute({ noteId: childNote.noteId, type: attr.type, name: attr.name.substr(6), @@ -110,7 +107,7 @@ function createNewNote(params) { } return sql.transactional(() => { - const note = new BeccaNote({ + const note = new Note({ noteId: params.noteId, // optionally can force specific noteId title: params.title, isProtected: !!params.isProtected, @@ -120,7 +117,7 @@ function createNewNote(params) { note.setContent(params.content); - const branch = new BeccaBranch({ + const branch = new Branch({ noteId: note.noteId, parentNoteId: params.parentNoteId, notePosition: params.notePosition !== undefined ? params.notePosition : getNewNotePosition(params.parentNoteId), @@ -430,7 +427,7 @@ function saveLinks(note, content) { && existingLink.name === foundLink.name); if (!existingLink) { - const newLink = new BeccaAttribute({ + const newLink = new Attribute({ noteId: note.noteId, type: 'relation', name: foundLink.name, @@ -566,12 +563,18 @@ function deleteBranch(branch, deleteId, taskContext) { } /** - * @param {Note} note - * @param {string} deleteId + * @param {string} noteId * @param {TaskContext} taskContext */ -function undeleteNote(note, deleteId, taskContext) { - const undeletedParentBranchIds = getUndeletedParentBranchIds(note.noteId, deleteId); +function undeleteNote(noteId, taskContext) { + const note = sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]); + + if (!note.isDeleted) { + log.error(`Note ${noteId} is not deleted and thus cannot be undeleted.`); + return; + } + + const undeletedParentBranchIds = getUndeletedParentBranchIds(noteId, note.deleteId); if (undeletedParentBranchIds.length === 0) { // cannot undelete if there's no undeleted parent @@ -579,7 +582,7 @@ function undeleteNote(note, deleteId, taskContext) { } for (const parentBranchId of undeletedParentBranchIds) { - undeleteBranch(parentBranchId, deleteId, taskContext); + undeleteBranch(parentBranchId, note.deleteId, taskContext); } } @@ -620,7 +623,7 @@ function undeleteBranch(branchId, deleteId, taskContext) { } const childBranchIds = sql.getColumn(` - SELECT branches.id + SELECT branches.branchId FROM branches WHERE branches.isDeleted = 1 AND branches.deleteId = ? @@ -789,7 +792,7 @@ function duplicateSubtreeInner(origNote, origBranch, newParentNoteId, noteIdMapp const newNoteId = noteIdMapping[origNote.noteId]; - const newBranch = new BeccaBranch({ + const newBranch = new Branch({ noteId: newNoteId, parentNoteId: newParentNoteId, // here increasing just by 1 to make sure it's directly after original @@ -807,7 +810,7 @@ function duplicateSubtreeInner(origNote, origBranch, newParentNoteId, noteIdMapp } } - const newNote = new BeccaNote(origNote); + const newNote = new Note(origNote); newNote.noteId = newNoteId; newNote.dateCreated = dateUtils.localNowDateTime(); newNote.utcDateCreated = dateUtils.utcNowDateTime(); @@ -823,7 +826,7 @@ function duplicateSubtreeInner(origNote, origBranch, newParentNoteId, noteIdMapp newNote.setContent(content); for (const attribute of origNote.getOwnedAttributes()) { - const attr = new BeccaAttribute(attribute); + const attr = new Attribute(attribute); attr.attributeId = undefined; // force creation of new attribute attr.noteId = newNote.noteId;