From d9b9d730bb9b7b81befe71c29b7dab9dcd7664a5 Mon Sep 17 00:00:00 2001 From: zadam Date: Sun, 1 Dec 2019 11:10:04 +0100 Subject: [PATCH] moving from inherited attribute queries to owned one where it makes sense --- src/entities/note.js | 16 ++++++++++++++++ src/routes/api/clipper.js | 2 +- src/routes/api/notes.js | 26 -------------------------- src/routes/api/options.js | 2 +- src/routes/routes.js | 1 - src/services/date_notes.js | 4 ++-- src/services/export/opml.js | 2 +- src/services/export/tar.js | 2 +- src/services/handlers.js | 4 ++-- src/services/script.js | 4 ++-- 10 files changed, 26 insertions(+), 37 deletions(-) diff --git a/src/entities/note.js b/src/entities/note.js index 567300804..5b7067e7d 100644 --- a/src/entities/note.js +++ b/src/entities/note.js @@ -278,6 +278,14 @@ class Note extends Entity { return await this.getAttributes(LABEL, name); } + /** + * @param {string} [name] - label name to filter + * @returns {Promise} all note's labels (attributes with type label), excluding inherited ones + */ + async getOwnedLabels(name) { + return await this.getOwnedAttributes(LABEL, name); + } + /** * @param {string} [name] - label name to filter * @returns {Promise} all note's label definitions, including inherited ones @@ -294,6 +302,14 @@ class Note extends Entity { return await this.getAttributes(RELATION, name); } + /** + * @param {string} [name] - relation name to filter + * @returns {Promise} all note's relations (attributes with type relation), excluding inherited ones + */ + async getOwnedRelations(name) { + return await this.getOwnedAttributes(RELATION, name); + } + /** * @param {string} [name] - relation name to filter * @returns {Promise} diff --git a/src/routes/api/clipper.js b/src/routes/api/clipper.js index ffdb4b363..1554941c3 100644 --- a/src/routes/api/clipper.js +++ b/src/routes/api/clipper.js @@ -15,7 +15,7 @@ async function findClippingNote(todayNote, pageUrl) { const notes = await todayNote.getDescendantNotesWithLabel('pageUrl', pageUrl); for (const note of notes) { - if (await note.getLabelValue('clipType') === 'clippings') { + if (await note.getOwnedLabelValue('clipType') === 'clippings') { return note; } } diff --git a/src/routes/api/notes.js b/src/routes/api/notes.js index d01004388..c9de2783f 100644 --- a/src/routes/api/notes.js +++ b/src/routes/api/notes.js @@ -27,31 +27,6 @@ async function getNote(req) { return note; } -async function getChildren(req) { - const parentNoteId = req.params.parentNoteId; - const parentNote = await repository.getNote(parentNoteId); - - if (!parentNote) { - return [404, `Note ${parentNoteId} has not been found.`]; - } - - const ret = []; - - for (const childNote of await parentNote.getChildNotes()) { - ret.push({ - noteId: childNote.noteId, - title: childNote.title, - relations: (await childNote.getRelations()).map(relation => { return { - attributeId: relation.attributeId, - name: relation.name, - targetNoteId: relation.value - }; }) - }); - } - - return ret; -} - async function createNote(req) { const params = Object.assign({}, req.body); // clone params.parentNoteId = req.params.parentNoteId; @@ -198,7 +173,6 @@ module.exports = { sortNotes, protectSubtree, setNoteTypeMime, - getChildren, getRelationMap, changeTitle, duplicateNote diff --git a/src/routes/api/options.js b/src/routes/api/options.js index 09ee01532..384b5f376 100644 --- a/src/routes/api/options.js +++ b/src/routes/api/options.js @@ -90,7 +90,7 @@ async function getUserThemes() { const ret = []; for (const note of notes) { - let value = await note.getLabelValue('appTheme'); + let value = await note.getOwnedLabelValue('appTheme'); if (!value) { value = note.title.toLowerCase().replace(/[^a-z0-9]/gi, '-'); diff --git a/src/routes/routes.js b/src/routes/routes.js index 725d07ae5..fb8f1ebdf 100644 --- a/src/routes/routes.js +++ b/src/routes/routes.js @@ -128,7 +128,6 @@ function register(app) { apiRoute(PUT, '/api/notes/:noteId', notesApiRoute.updateNote); apiRoute(DELETE, '/api/notes/:noteId', notesApiRoute.deleteNote); apiRoute(POST, '/api/notes/:parentNoteId/children', notesApiRoute.createNote); - apiRoute(GET, '/api/notes/:parentNoteId/children', notesApiRoute.getChildren); apiRoute(PUT, '/api/notes/:noteId/sort', notesApiRoute.sortNotes); apiRoute(PUT, '/api/notes/:noteId/protect/:isProtected', notesApiRoute.protectSubtree); apiRoute(PUT, /\/api\/notes\/(.*)\/type\/(.*)\/mime\/(.*)/, notesApiRoute.setNoteTypeMime); diff --git a/src/services/date_notes.js b/src/services/date_notes.js index 265048705..d6b1a396b 100644 --- a/src/services/date_notes.js +++ b/src/services/date_notes.js @@ -81,7 +81,7 @@ async function getYearNote(dateStr, rootNote) { } async function getMonthNoteTitle(rootNote, monthNumber, dateObj) { - const pattern = await rootNote.getLabelValue("monthPattern") || "{monthNumberPadded} - {month}"; + const pattern = await rootNote.getOwnedLabelValue("monthPattern") || "{monthNumberPadded} - {month}"; const monthName = MONTHS[dateObj.getMonth()]; return pattern @@ -127,7 +127,7 @@ async function getMonthNote(dateStr, rootNote) { } async function getDateNoteTitle(rootNote, dayNumber, dateObj) { - const pattern = await rootNote.getLabelValue("datePattern") || "{dayInMonthPadded} - {weekDay}"; + const pattern = await rootNote.getOwnedLabelValue("datePattern") || "{dayInMonthPadded} - {weekDay}"; const weekDay = DAYS[dateObj.getDay()]; return pattern diff --git a/src/services/export/opml.js b/src/services/export/opml.js index 16399da5b..e78408117 100644 --- a/src/services/export/opml.js +++ b/src/services/export/opml.js @@ -16,7 +16,7 @@ async function exportToOpml(taskContext, branch, version, res) { const branch = await repository.getBranch(branchId); const note = await branch.getNote(); - if (!note.isStringNote() || await note.hasLabel('excludeFromExport')) { + if (!note.isStringNote() || await note.hasOwnedLabel('excludeFromExport')) { return; } diff --git a/src/services/export/tar.js b/src/services/export/tar.js index db8ffbc02..441f62d9d 100644 --- a/src/services/export/tar.js +++ b/src/services/export/tar.js @@ -79,7 +79,7 @@ async function exportToTar(taskContext, branch, format, res) { async function getNoteMeta(branch, parentMeta, existingFileNames) { const note = await branch.getNote(); - if (await note.hasLabel('excludeFromExport')) { + if (await note.hasOwnedLabel('excludeFromExport')) { return; } diff --git a/src/services/handlers.js b/src/services/handlers.js index 634844e08..12ac55afb 100644 --- a/src/services/handlers.js +++ b/src/services/handlers.js @@ -27,7 +27,7 @@ eventService.subscribe(eventService.NOTE_TITLE_CHANGED, async note => { const parents = await note.getParentNotes(); for (const parent of parents) { - if (await parent.hasLabel("sorted")) { + if (await parent.hasOwnedLabel("sorted")) { await treeService.sortNotesAlphabetically(parent.noteId); } } @@ -119,7 +119,7 @@ eventService.subscribe(eventService.ENTITY_CHANGED, async ({ entityName, entity eventService.subscribe(eventService.ENTITY_DELETED, async ({ entityName, entity }) => { await processInverseRelations(entityName, entity, async (definition, note, targetNote) => { // if one inverse attribute is deleted then the other should be deleted as well - const relations = await targetNote.getRelations(definition.inverseRelation); + const relations = await targetNote.getOwnedRelations(definition.inverseRelation); let deletedSomething = false; for (const relation of relations) { diff --git a/src/services/script.js b/src/services/script.js index 5a3949d0b..153d535aa 100644 --- a/src/services/script.js +++ b/src/services/script.js @@ -40,7 +40,7 @@ async function executeBundle(bundle, apiParams = {}) { const ctx = new ScriptContext(bundle.allNotes, apiParams); try { - if (await bundle.note.hasLabel('manualTransactionHandling')) { + if (await bundle.note.hasOwnedLabel('manualTransactionHandling')) { return await execute(ctx, script); } else { @@ -117,7 +117,7 @@ async function getScriptBundle(note, root = true, scriptEnv = null, includedNote return; } - if (!root && await note.hasLabel('disableInclusion')) { + if (!root && await note.hasOwnedLabel('disableInclusion')) { return; }