diff --git a/src/entities/entity_constructor.js b/src/entities/entity_constructor.js index d5ebe2edf..892c0a71d 100644 --- a/src/entities/entity_constructor.js +++ b/src/entities/entity_constructor.js @@ -1,6 +1,6 @@ const repository = require('../services/repository'); const Note = require('../entities/note'); -const NoteRevision = require('../entities/note_revision'); +const NoteRevision = require('../services/becca/entities/note_revision.js'); const Branch = require('../entities/branch'); const Attribute = require('../entities/attribute'); const RecentNote = require('../entities/recent_note'); diff --git a/src/entities/note.js b/src/entities/note.js index 70b5c6140..fda454c05 100644 --- a/src/entities/note.js +++ b/src/entities/note.js @@ -801,7 +801,7 @@ class Note extends Entity { * * @returns {NoteRevision[]} */ - getRevisions() { + getNoteRevisions() { return this.repository.getEntities("SELECT * FROM note_revisions WHERE noteId = ?", [this.noteId]); } diff --git a/src/routes/api/search.js b/src/routes/api/search.js index 7b6cd333c..a3b90b2e2 100644 --- a/src/routes/api/search.js +++ b/src/routes/api/search.js @@ -62,7 +62,7 @@ const ACTION_HANDLERS = { note.save(); }, deleteNoteRevisions: (action, note) => { - noteRevisionService.eraseNoteRevisions(note.getRevisions().map(rev => rev.noteRevisionId)); + noteRevisionService.eraseNoteRevisions(note.getNoteRevisions().map(rev => rev.noteRevisionId)); }, deleteLabel: (action, note) => { for (const label of note.getOwnedLabels(action.labelName)) { diff --git a/src/services/becca/entities/note.js b/src/services/becca/entities/note.js index 4c25ae878..dc251dd72 100644 --- a/src/services/becca/entities/note.js +++ b/src/services/becca/entities/note.js @@ -7,6 +7,7 @@ const utils = require('../../utils'); const dateUtils = require('../../date_utils'); const entityChangesService = require('../../entity_changes.js'); const AbstractEntity = require("./abstract_entity.js"); +const NoteRevision = require("./note_revision.js"); const LABEL = 'label'; const RELATION = 'relation'; @@ -807,6 +808,11 @@ class Note extends AbstractEntity { return minDistance; } + getNoteRevisions() { + return sql.getRows("SELECT * FROM note_revisions WHERE noteId = ?", [this.noteId]) + .map(row => new NoteRevision(row)); + } + decrypt() { if (this.isProtected && !this.isDecrypted && protectedSessionService.isProtectedSessionAvailable()) { try { diff --git a/src/entities/note_revision.js b/src/services/becca/entities/note_revision.js similarity index 61% rename from src/entities/note_revision.js rename to src/services/becca/entities/note_revision.js index 7b25bb5a5..681ac2e75 100644 --- a/src/entities/note_revision.js +++ b/src/services/becca/entities/note_revision.js @@ -1,38 +1,48 @@ "use strict"; -const Entity = require('./entity'); -const protectedSessionService = require('../services/protected_session'); -const utils = require('../services/utils'); -const sql = require('../services/sql'); -const dateUtils = require('../services/date_utils'); -const entityChangesService = require('../services/entity_changes.js'); +const protectedSessionService = require('../../protected_session'); +const utils = require('../../utils'); +const sql = require('../../sql'); +const dateUtils = require('../../date_utils'); +const becca = require('../../becca/becca'); +const entityChangesService = require('../../entity_changes'); +const AbstractEntity = require("./abstract_entity"); /** * NoteRevision represents snapshot of note's title and content at some point in the past. It's used for seamless note versioning. * - * @property {string} noteRevisionId - * @property {string} noteId - * @property {string} type - * @property {string} mime - * @property {string} title - * @property {boolean} isProtected - * @property {string} dateLastEdited - * @property {string} dateCreated - * @property {string} utcDateLastEdited - * @property {string} utcDateCreated - * @property {string} utcDateModified - * * @extends Entity */ -class NoteRevision extends Entity { +class NoteRevision extends AbstractEntity { static get entityName() { return "note_revisions"; } static get primaryKeyName() { return "noteRevisionId"; } static get hashedProperties() { return ["noteRevisionId", "noteId", "title", "isProtected", "dateLastEdited", "dateCreated", "utcDateLastEdited", "utcDateCreated", "utcDateModified"]; } constructor(row) { - super(row); + super(); - this.isProtected = !!this.isProtected; + /** @param {string} */ + this.noteRevisionId = row.noteRevisionId; + /** @param {string} */ + this.noteId = row.noteId; + /** @param {string} */ + this.type = row.type; + /** @param {string} */ + this.mime = row.mime; + /** @param {boolean} */ + this.isProtected = !!row.isProtected; + /** @param {string} */ + this.title = row.title; + /** @param {string} */ + this.dateLastEdited = row.dateLastEdited; + /** @param {string} */ + this.dateCreated = row.dateCreated; + /** @param {string} */ + this.utcDateLastEdited = row.utcDateLastEdited; + /** @param {string} */ + this.utcDateCreated = row.utcDateCreated; + /** @param {string} */ + this.utcDateModified = row.utcDateModified; if (this.isProtected) { if (protectedSessionService.isProtectedSessionAvailable()) { @@ -45,7 +55,7 @@ class NoteRevision extends Entity { } getNote() { - return this.repository.getNote(this.noteId); + return becca.notes[this.noteId]; } /** @returns {boolean} true if the note has string content (not binary) */ @@ -64,42 +74,39 @@ class NoteRevision extends Entity { /** @returns {*} */ getContent(silentNotFoundError = false) { - if (this.content === undefined) { - const res = sql.getRow(`SELECT content FROM note_revision_contents WHERE noteRevisionId = ?`, [this.noteRevisionId]); + const res = sql.getRow(`SELECT content FROM note_revision_contents WHERE noteRevisionId = ?`, [this.noteRevisionId]); - if (!res) { - if (silentNotFoundError) { - return undefined; - } - else { - throw new Error("Cannot find note revision content for noteRevisionId=" + this.noteRevisionId); - } + if (!res) { + if (silentNotFoundError) { + return undefined; } + else { + throw new Error("Cannot find note revision content for noteRevisionId=" + this.noteRevisionId); + } + } - this.content = res.content; - if (this.isProtected) { - if (protectedSessionService.isProtectedSessionAvailable()) { - this.content = protectedSessionService.decrypt(this.content); - } - else { - this.content = ""; - } + let content = res.content; + + if (this.isProtected) { + if (protectedSessionService.isProtectedSessionAvailable()) { + content = protectedSessionService.decrypt(content); + } + else { + content = ""; } } if (this.isStringNote()) { - return this.content === null + return content === null ? "" - : this.content.toString("UTF-8"); + : content.toString("UTF-8"); } else { - return this.content; + return content; } } setContent(content) { - this.content = content; - const pojo = { noteRevisionId: this.noteRevisionId, content: content, diff --git a/src/services/content_hash.js b/src/services/content_hash.js index 6a0c7c138..331318ac0 100644 --- a/src/services/content_hash.js +++ b/src/services/content_hash.js @@ -7,7 +7,7 @@ const ApiToken = require('../entities/api_token'); const Branch = require('../entities/branch'); const Note = require('../entities/note'); const Attribute = require('../entities/attribute'); -const NoteRevision = require('../entities/note_revision'); +const NoteRevision = require('./becca/entities/note_revision.js'); const RecentNote = require('../entities/recent_note'); const Option = require('../entities/option'); diff --git a/src/services/note_revisions.js b/src/services/note_revisions.js index 0d0fa88aa..8d0c77996 100644 --- a/src/services/note_revisions.js +++ b/src/services/note_revisions.js @@ -1,6 +1,6 @@ "use strict"; -const NoteRevision = require('../entities/note_revision'); +const NoteRevision = require('./becca/entities/note_revision.js'); const dateUtils = require('./date_utils'); const log = require('./log'); const sql = require('./sql'); @@ -9,7 +9,7 @@ const sql = require('./sql'); * @param {Note} note */ function protectNoteRevisions(note) { - for (const revision of note.getRevisions()) { + for (const revision of note.getNoteRevisions()) { if (note.isProtected !== revision.isProtected) { try { const content = revision.getContent();