From 72bd2507fe767a99fea5dfc75c47ead6fb578a59 Mon Sep 17 00:00:00 2001 From: azivner Date: Tue, 30 Jan 2018 21:25:47 -0500 Subject: [PATCH] script bugfixes --- src/entities/note.js | 2 +- src/services/repository.js | 2 +- src/services/script.js | 2 -- src/services/script_context.js | 30 +++++------------------------- 4 files changed, 7 insertions(+), 29 deletions(-) diff --git a/src/entities/note.js b/src/entities/note.js index fe04defaa..f9c504a56 100644 --- a/src/entities/note.js +++ b/src/entities/note.js @@ -40,7 +40,7 @@ class Note extends Entity { } beforeSaving() { - this.content = JSON.stringify(this.jsonContent, null, 4); + this.content = JSON.stringify(this.jsonContent, null, '\t'); if (this.isProtected) { protected_session.encryptNote(this.dataKey, this); diff --git a/src/services/repository.js b/src/services/repository.js index 9392a4a1b..194b8a66f 100644 --- a/src/services/repository.js +++ b/src/services/repository.js @@ -70,7 +70,7 @@ class Repository { delete clone.jsonContent; delete clone.repository; - await sql.replace(entity.constructor.tableName, entity); + await sql.replace(entity.constructor.tableName, clone); const primaryKey = entity[entity.constructor.primaryKeyName]; diff --git a/src/services/script.js b/src/services/script.js index 82cc05778..826aa0ec0 100644 --- a/src/services/script.js +++ b/src/services/script.js @@ -15,8 +15,6 @@ async function executeScript(noteId, dataKey, script, params) { ret = await (function() { return eval(`(${script})(${paramsStr})`); }.call(ctx)); }); - log.info('Execution result: ' + ret); - return ret; } diff --git a/src/services/script_context.js b/src/services/script_context.js index 98edb6a77..a450c99d4 100644 --- a/src/services/script_context.js +++ b/src/services/script_context.js @@ -3,8 +3,6 @@ const protected_session = require('./protected_session'); const notes = require('./notes'); const attributes = require('./attributes'); const date_notes = require('./date_notes'); -const sql = require('./sql'); -const sync_table = require('./sync_table'); const Repository = require('./repository'); function ScriptContext(noteId, dataKey) { @@ -12,10 +10,6 @@ function ScriptContext(noteId, dataKey) { this.dataKey = protected_session.getDataKey(dataKey); this.repository = new Repository(dataKey); - function serializePayload(payload) { - return JSON.stringify(payload, null, '\t'); - } - this.getNoteById = async function(noteId) { return this.repository.getNote(noteId); }; @@ -25,15 +19,15 @@ function ScriptContext(noteId, dataKey) { }; this.getNoteWithAttribute = async function (attrName, attrValue) { - const notes = this.getNotesWithAttribute(attrName, attrValue); + const notes = await this.getNotesWithAttribute(attrName, attrValue); return notes.length > 0 ? notes[0] : null; }; - this.createNote = async function (parentNoteId, name, payload, extraOptions = {}) { + this.createNote = async function (parentNoteId, name, jsonContent, extraOptions = {}) { const note = { title: name, - content: extraOptions.json ? serializePayload(payload) : payload, + content: extraOptions.json ? JSON.stringify(jsonContent, null, '\t') : jsonContent, target: 'into', isProtected: extraOptions.isProtected !== undefined ? extraOptions.isProtected : false, type: extraOptions.type, @@ -50,7 +44,7 @@ function ScriptContext(noteId, dataKey) { note.mime = "text/html"; } - const noteId = (await notes.createNewNote(parentNoteId, note)).noteId; + const noteId = (await notes.createNewNote(parentNoteId, note, this.dataKey)).noteId; if (extraOptions.attributes) { for (const attrName in extraOptions.attributes) { @@ -61,21 +55,7 @@ function ScriptContext(noteId, dataKey) { return noteId; }; - this.updateNote = async function (note) { - if (note.isJson()) { - note.content = serializePayload(note.jsonContent); - } - - delete note.jsonContent; - - if (note.isProtected) { - protected_session.encryptNote(this.dataKey, note); - } - - await sql.replace("notes", note); - - await sync_table.addNoteSync(note.noteId); - }; + this.updateEntity = this.repository.updateEntity; this.log = function(message) { log.info(`Script ${this.scriptNoteId}: ${message}`);