converted NoteRevision entity to the becca

This commit is contained in:
zadam 2021-04-26 22:00:55 +02:00
parent cb3a5bba61
commit d8f1c39282
7 changed files with 63 additions and 50 deletions

View File

@ -1,6 +1,6 @@
const repository = require('../services/repository'); const repository = require('../services/repository');
const Note = require('../entities/note'); 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 Branch = require('../entities/branch');
const Attribute = require('../entities/attribute'); const Attribute = require('../entities/attribute');
const RecentNote = require('../entities/recent_note'); const RecentNote = require('../entities/recent_note');

View File

@ -801,7 +801,7 @@ class Note extends Entity {
* *
* @returns {NoteRevision[]} * @returns {NoteRevision[]}
*/ */
getRevisions() { getNoteRevisions() {
return this.repository.getEntities("SELECT * FROM note_revisions WHERE noteId = ?", [this.noteId]); return this.repository.getEntities("SELECT * FROM note_revisions WHERE noteId = ?", [this.noteId]);
} }

View File

@ -62,7 +62,7 @@ const ACTION_HANDLERS = {
note.save(); note.save();
}, },
deleteNoteRevisions: (action, note) => { deleteNoteRevisions: (action, note) => {
noteRevisionService.eraseNoteRevisions(note.getRevisions().map(rev => rev.noteRevisionId)); noteRevisionService.eraseNoteRevisions(note.getNoteRevisions().map(rev => rev.noteRevisionId));
}, },
deleteLabel: (action, note) => { deleteLabel: (action, note) => {
for (const label of note.getOwnedLabels(action.labelName)) { for (const label of note.getOwnedLabels(action.labelName)) {

View File

@ -7,6 +7,7 @@ const utils = require('../../utils');
const dateUtils = require('../../date_utils'); const dateUtils = require('../../date_utils');
const entityChangesService = require('../../entity_changes.js'); const entityChangesService = require('../../entity_changes.js');
const AbstractEntity = require("./abstract_entity.js"); const AbstractEntity = require("./abstract_entity.js");
const NoteRevision = require("./note_revision.js");
const LABEL = 'label'; const LABEL = 'label';
const RELATION = 'relation'; const RELATION = 'relation';
@ -807,6 +808,11 @@ class Note extends AbstractEntity {
return minDistance; return minDistance;
} }
getNoteRevisions() {
return sql.getRows("SELECT * FROM note_revisions WHERE noteId = ?", [this.noteId])
.map(row => new NoteRevision(row));
}
decrypt() { decrypt() {
if (this.isProtected && !this.isDecrypted && protectedSessionService.isProtectedSessionAvailable()) { if (this.isProtected && !this.isDecrypted && protectedSessionService.isProtectedSessionAvailable()) {
try { try {

View File

@ -1,38 +1,48 @@
"use strict"; "use strict";
const Entity = require('./entity'); const protectedSessionService = require('../../protected_session');
const protectedSessionService = require('../services/protected_session'); const utils = require('../../utils');
const utils = require('../services/utils'); const sql = require('../../sql');
const sql = require('../services/sql'); const dateUtils = require('../../date_utils');
const dateUtils = require('../services/date_utils'); const becca = require('../../becca/becca');
const entityChangesService = require('../services/entity_changes.js'); 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. * 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 * @extends Entity
*/ */
class NoteRevision extends Entity { class NoteRevision extends AbstractEntity {
static get entityName() { return "note_revisions"; } static get entityName() { return "note_revisions"; }
static get primaryKeyName() { return "noteRevisionId"; } static get primaryKeyName() { return "noteRevisionId"; }
static get hashedProperties() { return ["noteRevisionId", "noteId", "title", "isProtected", "dateLastEdited", "dateCreated", "utcDateLastEdited", "utcDateCreated", "utcDateModified"]; } static get hashedProperties() { return ["noteRevisionId", "noteId", "title", "isProtected", "dateLastEdited", "dateCreated", "utcDateLastEdited", "utcDateCreated", "utcDateModified"]; }
constructor(row) { 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 (this.isProtected) {
if (protectedSessionService.isProtectedSessionAvailable()) { if (protectedSessionService.isProtectedSessionAvailable()) {
@ -45,7 +55,7 @@ class NoteRevision extends Entity {
} }
getNote() { getNote() {
return this.repository.getNote(this.noteId); return becca.notes[this.noteId];
} }
/** @returns {boolean} true if the note has string content (not binary) */ /** @returns {boolean} true if the note has string content (not binary) */
@ -64,7 +74,6 @@ class NoteRevision extends Entity {
/** @returns {*} */ /** @returns {*} */
getContent(silentNotFoundError = false) { 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 (!res) {
@ -76,30 +85,28 @@ class NoteRevision extends Entity {
} }
} }
this.content = res.content; let content = res.content;
if (this.isProtected) { if (this.isProtected) {
if (protectedSessionService.isProtectedSessionAvailable()) { if (protectedSessionService.isProtectedSessionAvailable()) {
this.content = protectedSessionService.decrypt(this.content); content = protectedSessionService.decrypt(content);
} }
else { else {
this.content = ""; content = "";
}
} }
} }
if (this.isStringNote()) { if (this.isStringNote()) {
return this.content === null return content === null
? "" ? ""
: this.content.toString("UTF-8"); : content.toString("UTF-8");
} }
else { else {
return this.content; return content;
} }
} }
setContent(content) { setContent(content) {
this.content = content;
const pojo = { const pojo = {
noteRevisionId: this.noteRevisionId, noteRevisionId: this.noteRevisionId,
content: content, content: content,

View File

@ -7,7 +7,7 @@ const ApiToken = require('../entities/api_token');
const Branch = require('../entities/branch'); const Branch = require('../entities/branch');
const Note = require('../entities/note'); const Note = require('../entities/note');
const Attribute = require('../entities/attribute'); 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 RecentNote = require('../entities/recent_note');
const Option = require('../entities/option'); const Option = require('../entities/option');

View File

@ -1,6 +1,6 @@
"use strict"; "use strict";
const NoteRevision = require('../entities/note_revision'); const NoteRevision = require('./becca/entities/note_revision.js');
const dateUtils = require('./date_utils'); const dateUtils = require('./date_utils');
const log = require('./log'); const log = require('./log');
const sql = require('./sql'); const sql = require('./sql');
@ -9,7 +9,7 @@ const sql = require('./sql');
* @param {Note} note * @param {Note} note
*/ */
function protectNoteRevisions(note) { function protectNoteRevisions(note) {
for (const revision of note.getRevisions()) { for (const revision of note.getNoteRevisions()) {
if (note.isProtected !== revision.isProtected) { if (note.isProtected !== revision.isProtected) {
try { try {
const content = revision.getContent(); const content = revision.getContent();