converting note properties to methods

This commit is contained in:
zadam 2021-05-17 22:35:36 +02:00
parent a5d702b143
commit 2451596e8c
19 changed files with 48 additions and 48 deletions

View File

@ -337,11 +337,11 @@ describe("Search", () => {
const searchContext = new SearchContext(); 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(searchResults.length).toEqual(1);
expect(findNoteByTitle(searchResults, "Prague")).toBeTruthy(); 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(searchResults.length).toEqual(1);
expect(findNoteByTitle(searchResults, "Taipei")).toBeTruthy(); expect(findNoteByTitle(searchResults, "Taipei")).toBeTruthy();
}); });

View File

@ -115,7 +115,7 @@ function attributeDeleted(attribute) {
if (note) { if (note) {
// first invalidate and only then remove the attribute (otherwise invalidation wouldn't be complete) // 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(); note.invalidateSubTree();
} else { } else {
note.invalidateThisCache(); note.invalidateThisCache();
@ -143,7 +143,7 @@ function attributeUpdated(attribute) {
const note = becca.notes[attribute.noteId]; const note = becca.notes[attribute.noteId];
if (note) { if (note) {
if (attribute.isAffectingSubtree || note.isTemplate) { if (attribute.isAffectingSubtree || note.isTemplate()) {
note.invalidateSubTree(); note.invalidateSubTree();
} else { } else {
note.invalidateThisCache(); note.invalidateThisCache();

View File

@ -9,7 +9,7 @@ function isNotePathArchived(notePath) {
const noteId = notePath[notePath.length - 1]; const noteId = notePath[notePath.length - 1];
const note = becca.notes[noteId]; const note = becca.notes[noteId];
if (note.isArchived) { if (note.isArchived()) {
return true; return true;
} }
@ -17,7 +17,7 @@ function isNotePathArchived(notePath) {
const note = becca.notes[notePath[i]]; const note = becca.notes[notePath[i]];
// this is going through parents so archived must be inheritable // this is going through parents so archived must be inheritable
if (note.hasInheritableOwnedArchivedLabel) { if (note.hasInheritableOwnedArchivedLabel()) {
return true; return true;
} }
} }

View File

@ -79,7 +79,7 @@ class Note extends AbstractEntity {
// ------ Derived attributes ------ // ------ Derived attributes ------
/** @param {boolean} */ /** @param {boolean} */
this.isDecrypted = !row.isProtected || row.isContentAvailable; this.isDecrypted = !this.isProtected;
this.decrypt(); this.decrypt();
@ -87,7 +87,7 @@ class Note extends AbstractEntity {
this.flatTextCache = null; this.flatTextCache = null;
} }
get isContentAvailable() { isContentAvailable() {
return !this.noteId // new note which was not encrypted yet return !this.noteId // new note which was not encrypted yet
|| !this.isProtected || !this.isProtected
|| protectedSessionService.isProtectedSessionAvailable() || protectedSessionService.isProtectedSessionAvailable()
@ -568,11 +568,11 @@ class Note extends AbstractEntity {
return attrs.length > 0 ? attrs[0] : null; return attrs.length > 0 ? attrs[0] : null;
} }
get isArchived() { isArchived() {
return this.hasAttribute('label', 'archived'); return this.hasAttribute('label', 'archived');
} }
get hasInheritableOwnedArchivedLabel() { hasInheritableOwnedArchivedLabel() {
return !!this.ownedAttributes.find(attr => attr.type === 'label' && attr.name === 'archived' && attr.isInheritable); return !!this.ownedAttributes.find(attr => attr.type === 'label' && attr.name === 'archived' && attr.isInheritable);
} }
@ -581,7 +581,7 @@ class Note extends AbstractEntity {
resortParents() { resortParents() {
this.parentBranches.sort((a, b) => this.parentBranches.sort((a, b) =>
a.branchId.startsWith('virt-') a.branchId.startsWith('virt-')
|| a.parentNote.hasInheritableOwnedArchivedLabel ? 1 : -1); || a.parentNote.hasInheritableOwnedArchivedLabel() ? 1 : -1);
this.parents = this.parentBranches.map(branch => branch.parentNote); 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 * @return {string} - returns flattened textual representation of note, prefixes and attributes
*/ */
get flatText() { getFlatText() {
if (!this.flatTextCache) { if (!this.flatTextCache) {
this.flatTextCache = this.noteId + ' ' + this.type + ' ' + this.mime + ' '; 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 !!this.targetRelations.find(rel => rel.name === 'template');
} }
/** @return {Note[]} */ /** @return {Note[]} */
get subtreeNotesIncludingTemplated() { getSubtreeNotesIncludingTemplated() {
const arr = [[this]]; const arr = [[this]];
for (const childNote of this.children) { for (const childNote of this.children) {
arr.push(childNote.subtreeNotesIncludingTemplated); arr.push(childNote.getSubtreeNotesIncludingTemplated());
} }
for (const targetRelation of this.targetRelations) { for (const targetRelation of this.targetRelations) {
@ -691,7 +691,7 @@ class Note extends AbstractEntity {
const note = targetRelation.note; const note = targetRelation.note;
if (note) { if (note) {
arr.push(note.subtreeNotesIncludingTemplated); arr.push(note.getSubtreeNotesIncludingTemplated());
} }
} }
} }
@ -700,23 +700,23 @@ class Note extends AbstractEntity {
} }
/** @return {Note[]} */ /** @return {Note[]} */
get subtreeNotes() { getSubtreeNotes() {
const arr = [[this]]; const arr = [[this]];
for (const childNote of this.children) { for (const childNote of this.children) {
arr.push(childNote.subtreeNotes); arr.push(childNote.getSubtreeNotes());
} }
return arr.flat(); return arr.flat();
} }
/** @return {String[]} */ /** @return {String[]} */
get subtreeNoteIds() { getSubtreeNoteIds() {
return this.subtreeNotes.map(note => note.noteId); return this.getSubtreeNotes().map(note => note.noteId);
} }
getDescendantNoteIds() { getDescendantNoteIds() {
return this.subtreeNoteIds; return this.getSubtreeNoteIds();
} }
get parentCount() { get parentCount() {
@ -767,7 +767,7 @@ class Note extends AbstractEntity {
return this.getAttributes().length; return this.getAttributes().length;
} }
get ancestors() { getAncestors() {
if (!this.ancestorCache) { if (!this.ancestorCache) {
const noteIds = new Set(); const noteIds = new Set();
this.ancestorCache = []; this.ancestorCache = [];
@ -778,7 +778,7 @@ class Note extends AbstractEntity {
noteIds.add(parent.noteId); noteIds.add(parent.noteId);
} }
for (const ancestorNote of parent.ancestors) { for (const ancestorNote of parent.getAncestors()) {
if (!noteIds.has(ancestorNote.noteId)) { if (!noteIds.has(ancestorNote.noteId)) {
this.ancestorCache.push(ancestorNote); this.ancestorCache.push(ancestorNote);
noteIds.add(ancestorNote.noteId); 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 /** @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 */ * in effect returns notes which are influenced by note's non-inheritable attributes */
get templatedNotes() { getTemplatedNotes() {
const arr = [this]; const arr = [this];
for (const targetRelation of this.targetRelations) { for (const targetRelation of this.targetRelations) {

View File

@ -64,7 +64,7 @@ function buildRewardMap(note) {
} }
} }
for (const ancestorNote of note.ancestors) { for (const ancestorNote of note.getAncestors()) {
if (ancestorNote.noteId === 'root') { if (ancestorNote.noteId === 'root') {
continue; continue;
} }
@ -249,7 +249,7 @@ async function findSimilarNotes(noteId) {
const rewardMap = buildRewardMap(baseNote); const rewardMap = buildRewardMap(baseNote);
let ancestorRewardCache = {}; 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); ancestorNoteIds.add(baseNote.noteId);
let displayRewards = false; let displayRewards = false;

View File

@ -308,8 +308,8 @@ class NoteShort {
return a.isInHoistedSubTree ? -1 : 1; return a.isInHoistedSubTree ? -1 : 1;
} else if (a.isSearch !== b.isSearch) { } else if (a.isSearch !== b.isSearch) {
return a.isSearch ? 1 : -1; return a.isSearch ? 1 : -1;
} else if (a.isArchived !== b.isArchived) { } else if (a.isArchived() !== b.isArchived()) {
return a.isArchived ? 1 : -1; return a.isArchived() ? 1 : -1;
} else { } else {
return a.notePath.length - b.notePath.length; return a.notePath.length - b.notePath.length;
} }

View File

@ -100,7 +100,7 @@ export default class NotePathsWidget extends TabAwareWidget {
icons.push(`<span class="bx bx-trending-up" title="This path is outside of hoisted note and you would have to unhoist."></span>`); icons.push(`<span class="bx bx-trending-up" title="This path is outside of hoisted note and you would have to unhoist."></span>`);
} }
if (notePathRecord.isArchived) { if (notePathRecord.isArchived()) {
$noteLink.addClass("path-archived"); $noteLink.addClass("path-archived");
icons.push(`<span class="bx bx-archive" title="Archived"></span>`); icons.push(`<span class="bx bx-archive" title="Archived"></span>`);

View File

@ -195,7 +195,7 @@ function changeTitle(req) {
return [404, `Note ${noteId} has not been found`]; return [404, `Note ${noteId} has not been found`];
} }
if (!note.isContentAvailable) { if (!note.isContentAvailable()) {
return [400, `Note ${noteId} is not available for change`]; return [400, `Note ${noteId} is not available for change`];
} }

View File

@ -182,7 +182,7 @@ function searchFromRelation(note, relationName) {
return []; return [];
} }
if (!note.isContentAvailable) { if (!note.isContentAvailable()) {
log.info(`Note ${scriptNote.noteId} is not available outside of protected session.`); log.info(`Note ${scriptNote.noteId} is not available outside of protected session.`);
return []; return [];

View File

@ -29,7 +29,7 @@ function getSubtreeSize(req) {
return [404, `Note ${noteId} was not found.`]; 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); sql.fillParamList(subTreeNoteIds);

View File

@ -89,7 +89,7 @@ eventService.subscribe(eventService.ENTITY_CREATED, ({ entityName, entity }) =>
const note = becca.notes[entity.noteId]; const note = becca.notes[entity.noteId];
if (note) { if (note) {
for (const noteId of note.subtreeNoteIds) { for (const noteId of note.getSubtreeNoteIds()) {
treeService.sortNotesByTitle(noteId); treeService.sortNotesByTitle(noteId);
} }
} }

View File

@ -475,7 +475,7 @@ function saveNoteRevision(note) {
function updateNote(noteId, noteUpdates) { function updateNote(noteId, noteUpdates) {
const note = becca.getNote(noteId); const note = becca.getNote(noteId);
if (!note.isContentAvailable) { if (!note.isContentAvailable()) {
throw new Error(`Note ${noteId} is not available for change!`); throw new Error(`Note ${noteId} is not available for change!`);
} }

View File

@ -4,7 +4,7 @@ const log = require('./log');
const becca = require("../becca/becca.js"); const becca = require("../becca/becca.js");
function executeNote(note, apiParams) { 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"`); log.info(`Cannot execute note ${note.noteId} "${note.title}", note must be of type "Code: JS frontend"`);
return; return;
@ -105,7 +105,7 @@ function getScriptBundleForFrontend(note) {
} }
function getScriptBundle(note, root = true, scriptEnv = null, includedNoteIds = [], backendOverrideContent = null) { function getScriptBundle(note, root = true, scriptEnv = null, includedNoteIds = [], backendOverrideContent = null) {
if (!note.isContentAvailable) { if (!note.isContentAvailable()) {
return; return;
} }

View File

@ -23,7 +23,7 @@ class AncestorExp extends Expression {
return new NoteSet([]); return new NoteSet([]);
} }
const subTreeNoteSet = new NoteSet(ancestorNote.subtreeNotes).intersection(inputNoteSet); const subTreeNoteSet = new NoteSet(ancestorNote.getSubtreeNotes()).intersection(inputNoteSet);
if (!this.ancestorDepthComparator) { if (!this.ancestorDepthComparator) {
return subTreeNoteSet; return subTreeNoteSet;

View File

@ -25,10 +25,10 @@ class AttributeExistsExp extends Expression {
if (inputNoteSet.hasNoteId(note.noteId)) { if (inputNoteSet.hasNoteId(note.noteId)) {
if (attr.isInheritable) { if (attr.isInheritable) {
resultNoteSet.addAll(note.subtreeNotesIncludingTemplated); resultNoteSet.addAll(note.getSubtreeNotesIncludingTemplated());
} }
else if (note.isTemplate) { else if (note.isTemplate()) {
resultNoteSet.addAll(note.templatedNotes); resultNoteSet.addAll(note.getTemplatedNotes());
} }
else { else {
resultNoteSet.add(note); resultNoteSet.add(note);

View File

@ -18,7 +18,7 @@ class DescendantOfExp extends Expression {
const subTreeNoteSet = new NoteSet(); const subTreeNoteSet = new NoteSet();
for (const note of subResNoteSet.notes) { for (const note of subResNoteSet.notes) {
subTreeNoteSet.addAll(note.subtreeNotes); subTreeNoteSet.addAll(note.getSubtreeNotes());
} }
return inputNoteSet.intersection(subTreeNoteSet); return inputNoteSet.intersection(subTreeNoteSet);

View File

@ -23,10 +23,10 @@ class LabelComparisonExp extends Expression {
if (inputNoteSet.hasNoteId(note.noteId) && this.comparator(value)) { if (inputNoteSet.hasNoteId(note.noteId) && this.comparator(value)) {
if (attr.isInheritable) { if (attr.isInheritable) {
resultNoteSet.addAll(note.subtreeNotesIncludingTemplated); resultNoteSet.addAll(note.getSubtreeNotesIncludingTemplated());
} }
else if (note.isTemplate) { else if (note.isTemplate()) {
resultNoteSet.addAll(note.templatedNotes); resultNoteSet.addAll(note.getTemplatedNotes());
} }
else { else {
resultNoteSet.add(note); resultNoteSet.add(note);

View File

@ -129,7 +129,7 @@ class BeccaFlatTextExp extends Expression {
for (const note of noteSet.notes) { for (const note of noteSet.notes) {
for (const token of this.tokens) { for (const token of this.tokens) {
if (note.flatText.includes(token)) { if (note.getFlatText().includes(token)) {
candidateNotes.push(note); candidateNotes.push(note);
break; break;
} }

View File

@ -24,9 +24,9 @@ class RelationWhereExp extends Expression {
if (subResNoteSet.hasNote(attr.targetNote)) { if (subResNoteSet.hasNote(attr.targetNote)) {
if (attr.isInheritable) { if (attr.isInheritable) {
candidateNoteSet.addAll(note.subtreeNotesIncludingTemplated); candidateNoteSet.addAll(note.getSubtreeNotesIncludingTemplated());
} else if (note.isTemplate) { } else if (note.isTemplate()) {
candidateNoteSet.addAll(note.templatedNotes); candidateNoteSet.addAll(note.getTemplatedNotes());
} else { } else {
candidateNoteSet.add(note); candidateNoteSet.add(note);
} }