From 55d8ef7e81be894d8aac97d48ba4ac1fab3cf0e7 Mon Sep 17 00:00:00 2001 From: azivner Date: Sun, 28 Jan 2018 12:08:57 -0500 Subject: [PATCH] script API changes --- routes/api/script.js | 2 -- services/attributes.js | 27 ++++++++++++++++++++------- services/script_context.js | 18 +++++++++++++----- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/routes/api/script.js b/routes/api/script.js index 40cb1dabc..95412e85a 100644 --- a/routes/api/script.js +++ b/routes/api/script.js @@ -68,8 +68,6 @@ async function getSubTreeScripts(parentId, includedNoteIds, dataKey) { script += await getSubTreeScripts(child.note_id, includedNoteIds, dataKey); - console.log('MIME: ', child.mime); - if (child.mime === 'application/javascript') { child.note_text = ''; } diff --git a/services/attributes.js b/services/attributes.js index 7ae6dd423..7fd773a3a 100644 --- a/services/attributes.js +++ b/services/attributes.js @@ -14,17 +14,29 @@ async function getNoteIdWithAttribute(name, value) { WHERE notes.is_deleted = 0 AND attributes.name = ? AND attributes.value = ?`, [name, value]); } -async function getNoteWithAttribute(dataKey, name, value) { - const note = await sql.getFirst(`SELECT notes.* FROM notes JOIN attributes USING(note_id) - WHERE notes.is_deleted = 0 AND attributes.name = ? AND attributes.value = ?`, [name, value]); +async function getNotesWithAttribute(dataKey, name, value) { + let notes; - if (!note) { - return note; + if (value !== undefined) { + notes = await sql.getAll(`SELECT notes.* FROM notes JOIN attributes USING(note_id) + WHERE notes.is_deleted = 0 AND attributes.name = ? AND attributes.value = ?`, [name, value]); + } + else { + notes = await sql.getAll(`SELECT notes.* FROM notes JOIN attributes USING(note_id) + WHERE notes.is_deleted = 0 AND attributes.name = ?`, [name]); } - protected_session.decryptNote(dataKey, note); + for (const note of notes) { + protected_session.decryptNote(dataKey, note); + } - return note; + return notes; +} + +async function getNoteWithAttribute(dataKey, name, value) { + const notes = getNotesWithAttribute(dataKey, name, value); + + return notes.length > 0 ? notes[0] : null; } async function getNoteIdsWithAttribute(name) { @@ -51,6 +63,7 @@ async function createAttribute(noteId, name, value = null, sourceId = null) { module.exports = { getNoteAttributeMap, getNoteIdWithAttribute, + getNotesWithAttribute, getNoteWithAttribute, getNoteIdsWithAttribute, createAttribute diff --git a/services/script_context.js b/services/script_context.js index 506ad14d6..bd16201e2 100644 --- a/services/script_context.js +++ b/services/script_context.js @@ -11,7 +11,7 @@ function ScriptContext(noteId, dataKey) { this.dataKey = protected_session.getDataKey(dataKey); function deserializePayload(note) { - if (note.type === "code" && note.mime === "application/json") { + if (note && note.type === "code" && note.mime === "application/json") { note.payload = JSON.parse(note.note_text); } } @@ -28,12 +28,20 @@ function ScriptContext(noteId, dataKey) { return note; }; + this.getNotesWithAttribute = async function (attrName, attrValue) { + const notes = await attributes.getNotesWithAttribute(this.dataKey, attrName, attrValue); + + for (const note of notes) { + deserializePayload(note); + } + + return notes; + }; + this.getNoteWithAttribute = async function (attrName, attrValue) { - const note = await attributes.getNoteWithAttribute(this.dataKey, attrName, attrValue); + const notes = this.getNotesWithAttribute(attrName, attrValue); - deserializePayload(note); - - return note; + return notes.length > 0 ? notes[0] : null; }; this.createNote = async function (parentNoteId, name, payload, extraOptions = {}) {