From 2451596e8cd4766b9d63a9e412f95aba6e161e2a Mon Sep 17 00:00:00 2001 From: zadam Date: Mon, 17 May 2021 22:35:36 +0200 Subject: [PATCH] converting note properties to methods --- spec/search/search.spec.js | 4 +-- src/becca/becca_loader.js | 4 +-- src/becca/becca_service.js | 4 +-- src/becca/entities/note.js | 36 +++++++++---------- src/becca/similarity.js | 4 +-- src/public/app/entities/note_short.js | 4 +-- src/public/app/widgets/note_paths.js | 2 +- src/routes/api/notes.js | 2 +- src/routes/api/search.js | 2 +- src/routes/api/stats.js | 2 +- src/services/handlers.js | 2 +- src/services/notes.js | 2 +- src/services/script.js | 4 +-- src/services/search/expressions/ancestor.js | 2 +- .../search/expressions/attribute_exists.js | 6 ++-- .../search/expressions/descendant_of.js | 2 +- .../search/expressions/label_comparison.js | 6 ++-- .../expressions/note_cache_flat_text.js | 2 +- .../search/expressions/relation_where.js | 6 ++-- 19 files changed, 48 insertions(+), 48 deletions(-) diff --git a/spec/search/search.spec.js b/spec/search/search.spec.js index 611072c5d..9c4ec61b9 100644 --- a/spec/search/search.spec.js +++ b/spec/search/search.spec.js @@ -337,11 +337,11 @@ describe("Search", () => { const searchContext = new SearchContext(); - let searchResults = searchService.findResultsWithQuery('#city AND note.ancestors.title = Europe', searchContext); + let searchResults = searchService.findResultsWithQuery('#city AND note.getAncestors().title = Europe', searchContext); expect(searchResults.length).toEqual(1); expect(findNoteByTitle(searchResults, "Prague")).toBeTruthy(); - searchResults = searchService.findResultsWithQuery('#city AND note.ancestors.title = Asia', searchContext); + searchResults = searchService.findResultsWithQuery('#city AND note.getAncestors().title = Asia', searchContext); expect(searchResults.length).toEqual(1); expect(findNoteByTitle(searchResults, "Taipei")).toBeTruthy(); }); diff --git a/src/becca/becca_loader.js b/src/becca/becca_loader.js index 675206f2b..ed45b9484 100644 --- a/src/becca/becca_loader.js +++ b/src/becca/becca_loader.js @@ -115,7 +115,7 @@ function attributeDeleted(attribute) { if (note) { // first invalidate and only then remove the attribute (otherwise invalidation wouldn't be complete) - if (attribute.isAffectingSubtree || note.isTemplate) { + if (attribute.isAffectingSubtree || note.isTemplate()) { note.invalidateSubTree(); } else { note.invalidateThisCache(); @@ -143,7 +143,7 @@ function attributeUpdated(attribute) { const note = becca.notes[attribute.noteId]; if (note) { - if (attribute.isAffectingSubtree || note.isTemplate) { + if (attribute.isAffectingSubtree || note.isTemplate()) { note.invalidateSubTree(); } else { note.invalidateThisCache(); diff --git a/src/becca/becca_service.js b/src/becca/becca_service.js index 6c51e9271..5b559c095 100644 --- a/src/becca/becca_service.js +++ b/src/becca/becca_service.js @@ -9,7 +9,7 @@ function isNotePathArchived(notePath) { const noteId = notePath[notePath.length - 1]; const note = becca.notes[noteId]; - if (note.isArchived) { + if (note.isArchived()) { return true; } @@ -17,7 +17,7 @@ function isNotePathArchived(notePath) { const note = becca.notes[notePath[i]]; // this is going through parents so archived must be inheritable - if (note.hasInheritableOwnedArchivedLabel) { + if (note.hasInheritableOwnedArchivedLabel()) { return true; } } diff --git a/src/becca/entities/note.js b/src/becca/entities/note.js index 776bf8675..29bf4bdbb 100644 --- a/src/becca/entities/note.js +++ b/src/becca/entities/note.js @@ -79,7 +79,7 @@ class Note extends AbstractEntity { // ------ Derived attributes ------ /** @param {boolean} */ - this.isDecrypted = !row.isProtected || row.isContentAvailable; + this.isDecrypted = !this.isProtected; this.decrypt(); @@ -87,7 +87,7 @@ class Note extends AbstractEntity { this.flatTextCache = null; } - get isContentAvailable() { + isContentAvailable() { return !this.noteId // new note which was not encrypted yet || !this.isProtected || protectedSessionService.isProtectedSessionAvailable() @@ -568,11 +568,11 @@ class Note extends AbstractEntity { return attrs.length > 0 ? attrs[0] : null; } - get isArchived() { + isArchived() { return this.hasAttribute('label', 'archived'); } - get hasInheritableOwnedArchivedLabel() { + hasInheritableOwnedArchivedLabel() { return !!this.ownedAttributes.find(attr => attr.type === 'label' && attr.name === 'archived' && attr.isInheritable); } @@ -581,7 +581,7 @@ class Note extends AbstractEntity { resortParents() { this.parentBranches.sort((a, b) => a.branchId.startsWith('virt-') - || a.parentNote.hasInheritableOwnedArchivedLabel ? 1 : -1); + || a.parentNote.hasInheritableOwnedArchivedLabel() ? 1 : -1); this.parents = this.parentBranches.map(branch => branch.parentNote); } @@ -593,7 +593,7 @@ class Note extends AbstractEntity { * * @return {string} - returns flattened textual representation of note, prefixes and attributes */ - get flatText() { + getFlatText() { if (!this.flatTextCache) { this.flatTextCache = this.noteId + ' ' + this.type + ' ' + this.mime + ' '; @@ -674,16 +674,16 @@ class Note extends AbstractEntity { } } - get isTemplate() { + isTemplate() { return !!this.targetRelations.find(rel => rel.name === 'template'); } /** @return {Note[]} */ - get subtreeNotesIncludingTemplated() { + getSubtreeNotesIncludingTemplated() { const arr = [[this]]; for (const childNote of this.children) { - arr.push(childNote.subtreeNotesIncludingTemplated); + arr.push(childNote.getSubtreeNotesIncludingTemplated()); } for (const targetRelation of this.targetRelations) { @@ -691,7 +691,7 @@ class Note extends AbstractEntity { const note = targetRelation.note; if (note) { - arr.push(note.subtreeNotesIncludingTemplated); + arr.push(note.getSubtreeNotesIncludingTemplated()); } } } @@ -700,23 +700,23 @@ class Note extends AbstractEntity { } /** @return {Note[]} */ - get subtreeNotes() { + getSubtreeNotes() { const arr = [[this]]; for (const childNote of this.children) { - arr.push(childNote.subtreeNotes); + arr.push(childNote.getSubtreeNotes()); } return arr.flat(); } /** @return {String[]} */ - get subtreeNoteIds() { - return this.subtreeNotes.map(note => note.noteId); + getSubtreeNoteIds() { + return this.getSubtreeNotes().map(note => note.noteId); } getDescendantNoteIds() { - return this.subtreeNoteIds; + return this.getSubtreeNoteIds(); } get parentCount() { @@ -767,7 +767,7 @@ class Note extends AbstractEntity { return this.getAttributes().length; } - get ancestors() { + getAncestors() { if (!this.ancestorCache) { const noteIds = new Set(); this.ancestorCache = []; @@ -778,7 +778,7 @@ class Note extends AbstractEntity { noteIds.add(parent.noteId); } - for (const ancestorNote of parent.ancestors) { + for (const ancestorNote of parent.getAncestors()) { if (!noteIds.has(ancestorNote.noteId)) { this.ancestorCache.push(ancestorNote); noteIds.add(ancestorNote.noteId); @@ -796,7 +796,7 @@ class Note extends AbstractEntity { /** @return {Note[]} - returns only notes which are templated, does not include their subtrees * in effect returns notes which are influenced by note's non-inheritable attributes */ - get templatedNotes() { + getTemplatedNotes() { const arr = [this]; for (const targetRelation of this.targetRelations) { diff --git a/src/becca/similarity.js b/src/becca/similarity.js index b4b1cb7de..5835ca629 100644 --- a/src/becca/similarity.js +++ b/src/becca/similarity.js @@ -64,7 +64,7 @@ function buildRewardMap(note) { } } - for (const ancestorNote of note.ancestors) { + for (const ancestorNote of note.getAncestors()) { if (ancestorNote.noteId === 'root') { continue; } @@ -249,7 +249,7 @@ async function findSimilarNotes(noteId) { const rewardMap = buildRewardMap(baseNote); let ancestorRewardCache = {}; - const ancestorNoteIds = new Set(baseNote.ancestors.map(note => note.noteId)); + const ancestorNoteIds = new Set(baseNote.getAncestors().map(note => note.noteId)); ancestorNoteIds.add(baseNote.noteId); let displayRewards = false; diff --git a/src/public/app/entities/note_short.js b/src/public/app/entities/note_short.js index 1fa815344..3a0e6d682 100644 --- a/src/public/app/entities/note_short.js +++ b/src/public/app/entities/note_short.js @@ -308,8 +308,8 @@ class NoteShort { return a.isInHoistedSubTree ? -1 : 1; } else if (a.isSearch !== b.isSearch) { return a.isSearch ? 1 : -1; - } else if (a.isArchived !== b.isArchived) { - return a.isArchived ? 1 : -1; + } else if (a.isArchived() !== b.isArchived()) { + return a.isArchived() ? 1 : -1; } else { return a.notePath.length - b.notePath.length; } diff --git a/src/public/app/widgets/note_paths.js b/src/public/app/widgets/note_paths.js index d18a664af..7bd33f279 100644 --- a/src/public/app/widgets/note_paths.js +++ b/src/public/app/widgets/note_paths.js @@ -100,7 +100,7 @@ export default class NotePathsWidget extends TabAwareWidget { icons.push(``); } - if (notePathRecord.isArchived) { + if (notePathRecord.isArchived()) { $noteLink.addClass("path-archived"); icons.push(``); diff --git a/src/routes/api/notes.js b/src/routes/api/notes.js index cb20e38c9..0ca8f5a2b 100644 --- a/src/routes/api/notes.js +++ b/src/routes/api/notes.js @@ -195,7 +195,7 @@ function changeTitle(req) { return [404, `Note ${noteId} has not been found`]; } - if (!note.isContentAvailable) { + if (!note.isContentAvailable()) { return [400, `Note ${noteId} is not available for change`]; } diff --git a/src/routes/api/search.js b/src/routes/api/search.js index ed618ff22..bbdd32a22 100644 --- a/src/routes/api/search.js +++ b/src/routes/api/search.js @@ -182,7 +182,7 @@ function searchFromRelation(note, relationName) { return []; } - if (!note.isContentAvailable) { + if (!note.isContentAvailable()) { log.info(`Note ${scriptNote.noteId} is not available outside of protected session.`); return []; diff --git a/src/routes/api/stats.js b/src/routes/api/stats.js index bbb16fd04..e6946ff3b 100644 --- a/src/routes/api/stats.js +++ b/src/routes/api/stats.js @@ -29,7 +29,7 @@ function getSubtreeSize(req) { return [404, `Note ${noteId} was not found.`]; } - const subTreeNoteIds = note.subtreeNotes.map(note => note.noteId); + const subTreeNoteIds = note.getSubtreeNotes().map(note => note.noteId); sql.fillParamList(subTreeNoteIds); diff --git a/src/services/handlers.js b/src/services/handlers.js index a427625b8..144287091 100644 --- a/src/services/handlers.js +++ b/src/services/handlers.js @@ -89,7 +89,7 @@ eventService.subscribe(eventService.ENTITY_CREATED, ({ entityName, entity }) => const note = becca.notes[entity.noteId]; if (note) { - for (const noteId of note.subtreeNoteIds) { + for (const noteId of note.getSubtreeNoteIds()) { treeService.sortNotesByTitle(noteId); } } diff --git a/src/services/notes.js b/src/services/notes.js index d823b318f..fe7f547f3 100644 --- a/src/services/notes.js +++ b/src/services/notes.js @@ -475,7 +475,7 @@ function saveNoteRevision(note) { function updateNote(noteId, noteUpdates) { const note = becca.getNote(noteId); - if (!note.isContentAvailable) { + if (!note.isContentAvailable()) { throw new Error(`Note ${noteId} is not available for change!`); } diff --git a/src/services/script.js b/src/services/script.js index 8bc1fc0f1..882b8154a 100644 --- a/src/services/script.js +++ b/src/services/script.js @@ -4,7 +4,7 @@ const log = require('./log'); const becca = require("../becca/becca.js"); function executeNote(note, apiParams) { - if (!note.isJavaScript() || note.getScriptEnv() !== 'backend' || !note.isContentAvailable) { + if (!note.isJavaScript() || note.getScriptEnv() !== 'backend' || !note.isContentAvailable()) { log.info(`Cannot execute note ${note.noteId} "${note.title}", note must be of type "Code: JS frontend"`); return; @@ -105,7 +105,7 @@ function getScriptBundleForFrontend(note) { } function getScriptBundle(note, root = true, scriptEnv = null, includedNoteIds = [], backendOverrideContent = null) { - if (!note.isContentAvailable) { + if (!note.isContentAvailable()) { return; } diff --git a/src/services/search/expressions/ancestor.js b/src/services/search/expressions/ancestor.js index 63384789c..fccebbd61 100644 --- a/src/services/search/expressions/ancestor.js +++ b/src/services/search/expressions/ancestor.js @@ -23,7 +23,7 @@ class AncestorExp extends Expression { return new NoteSet([]); } - const subTreeNoteSet = new NoteSet(ancestorNote.subtreeNotes).intersection(inputNoteSet); + const subTreeNoteSet = new NoteSet(ancestorNote.getSubtreeNotes()).intersection(inputNoteSet); if (!this.ancestorDepthComparator) { return subTreeNoteSet; diff --git a/src/services/search/expressions/attribute_exists.js b/src/services/search/expressions/attribute_exists.js index 8aba8fb32..e21fe4135 100644 --- a/src/services/search/expressions/attribute_exists.js +++ b/src/services/search/expressions/attribute_exists.js @@ -25,10 +25,10 @@ class AttributeExistsExp extends Expression { if (inputNoteSet.hasNoteId(note.noteId)) { if (attr.isInheritable) { - resultNoteSet.addAll(note.subtreeNotesIncludingTemplated); + resultNoteSet.addAll(note.getSubtreeNotesIncludingTemplated()); } - else if (note.isTemplate) { - resultNoteSet.addAll(note.templatedNotes); + else if (note.isTemplate()) { + resultNoteSet.addAll(note.getTemplatedNotes()); } else { resultNoteSet.add(note); diff --git a/src/services/search/expressions/descendant_of.js b/src/services/search/expressions/descendant_of.js index 8e12f40e2..473062107 100644 --- a/src/services/search/expressions/descendant_of.js +++ b/src/services/search/expressions/descendant_of.js @@ -18,7 +18,7 @@ class DescendantOfExp extends Expression { const subTreeNoteSet = new NoteSet(); for (const note of subResNoteSet.notes) { - subTreeNoteSet.addAll(note.subtreeNotes); + subTreeNoteSet.addAll(note.getSubtreeNotes()); } return inputNoteSet.intersection(subTreeNoteSet); diff --git a/src/services/search/expressions/label_comparison.js b/src/services/search/expressions/label_comparison.js index cb0b178ed..f808b4458 100644 --- a/src/services/search/expressions/label_comparison.js +++ b/src/services/search/expressions/label_comparison.js @@ -23,10 +23,10 @@ class LabelComparisonExp extends Expression { if (inputNoteSet.hasNoteId(note.noteId) && this.comparator(value)) { if (attr.isInheritable) { - resultNoteSet.addAll(note.subtreeNotesIncludingTemplated); + resultNoteSet.addAll(note.getSubtreeNotesIncludingTemplated()); } - else if (note.isTemplate) { - resultNoteSet.addAll(note.templatedNotes); + else if (note.isTemplate()) { + resultNoteSet.addAll(note.getTemplatedNotes()); } else { resultNoteSet.add(note); diff --git a/src/services/search/expressions/note_cache_flat_text.js b/src/services/search/expressions/note_cache_flat_text.js index 6324b6dd7..e355deeb5 100644 --- a/src/services/search/expressions/note_cache_flat_text.js +++ b/src/services/search/expressions/note_cache_flat_text.js @@ -129,7 +129,7 @@ class BeccaFlatTextExp extends Expression { for (const note of noteSet.notes) { for (const token of this.tokens) { - if (note.flatText.includes(token)) { + if (note.getFlatText().includes(token)) { candidateNotes.push(note); break; } diff --git a/src/services/search/expressions/relation_where.js b/src/services/search/expressions/relation_where.js index 8ea60c5fb..951326c3e 100644 --- a/src/services/search/expressions/relation_where.js +++ b/src/services/search/expressions/relation_where.js @@ -24,9 +24,9 @@ class RelationWhereExp extends Expression { if (subResNoteSet.hasNote(attr.targetNote)) { if (attr.isInheritable) { - candidateNoteSet.addAll(note.subtreeNotesIncludingTemplated); - } else if (note.isTemplate) { - candidateNoteSet.addAll(note.templatedNotes); + candidateNoteSet.addAll(note.getSubtreeNotesIncludingTemplated()); + } else if (note.isTemplate()) { + candidateNoteSet.addAll(note.getTemplatedNotes()); } else { candidateNoteSet.add(note); }