2017-10-21 21:10:33 -04:00
|
|
|
"use strict";
|
|
|
|
|
2020-12-11 22:06:12 +01:00
|
|
|
const noteCache = require('../../services/note_cache/note_cache');
|
2021-02-19 22:58:53 +01:00
|
|
|
const log = require('../../services/log');
|
2018-04-16 20:40:18 -04:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function getNotesAndBranchesAndAttributes(noteIds) {
|
2020-12-11 22:06:12 +01:00
|
|
|
noteIds = new Set(noteIds);
|
|
|
|
const collectedNoteIds = new Set();
|
|
|
|
const collectedAttributeIds = new Set();
|
|
|
|
const collectedBranchIds = new Set();
|
|
|
|
|
|
|
|
function collectEntityIds(note) {
|
|
|
|
if (collectedNoteIds.has(note.noteId)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
collectedNoteIds.add(note.noteId);
|
|
|
|
|
|
|
|
for (const branch of note.parentBranches) {
|
|
|
|
collectedBranchIds.add(branch.branchId);
|
|
|
|
|
|
|
|
collectEntityIds(branch.parentNote);
|
|
|
|
}
|
|
|
|
|
2020-12-13 23:27:42 +01:00
|
|
|
for (const childNote of note.children) {
|
|
|
|
const childBranch = noteCache.getBranch(childNote.noteId, note.noteId);
|
|
|
|
|
|
|
|
collectedBranchIds.add(childBranch.branchId);
|
|
|
|
}
|
|
|
|
|
2020-12-11 22:06:12 +01:00
|
|
|
for (const attr of note.ownedAttributes) {
|
|
|
|
collectedAttributeIds.add(attr.attributeId);
|
|
|
|
|
|
|
|
if (attr.type === 'relation' && attr.name === 'template') {
|
|
|
|
collectEntityIds(attr.targetNote);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const noteId of noteIds) {
|
|
|
|
const note = noteCache.notes[noteId];
|
|
|
|
|
|
|
|
if (!note) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
collectEntityIds(note);
|
|
|
|
}
|
|
|
|
|
|
|
|
const notes = [];
|
|
|
|
|
|
|
|
for (const noteId of collectedNoteIds) {
|
|
|
|
const note = noteCache.notes[noteId];
|
|
|
|
|
|
|
|
notes.push({
|
|
|
|
noteId: note.noteId,
|
2020-12-21 23:10:08 +01:00
|
|
|
title: note.isDecrypted ? note.title : '[protected]',
|
2020-12-11 22:06:12 +01:00
|
|
|
isProtected: note.isProtected,
|
|
|
|
type: note.type,
|
2020-12-14 23:04:56 +01:00
|
|
|
mime: note.mime
|
2020-12-11 22:06:12 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const branches = [];
|
2018-08-31 17:29:54 +02:00
|
|
|
|
2020-12-11 15:27:57 +01:00
|
|
|
if (noteIds.has('root')) {
|
|
|
|
branches.push({
|
|
|
|
branchId: 'root',
|
|
|
|
noteId: 'root',
|
|
|
|
parentNoteId: 'none',
|
|
|
|
notePosition: 0,
|
|
|
|
prefix: '',
|
|
|
|
isExpanded: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-11 22:06:12 +01:00
|
|
|
for (const branchId of collectedBranchIds) {
|
|
|
|
const branch = noteCache.branches[branchId];
|
|
|
|
|
2021-02-19 22:58:53 +01:00
|
|
|
if (!branch) {
|
|
|
|
log.error(`Could not find branch for branchId=${branchId}`);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-11 22:06:12 +01:00
|
|
|
branches.push({
|
|
|
|
branchId: branch.branchId,
|
|
|
|
noteId: branch.noteId,
|
|
|
|
parentNoteId: branch.parentNoteId,
|
|
|
|
notePosition: branch.notePosition,
|
|
|
|
prefix: branch.prefix,
|
|
|
|
isExpanded: branch.isExpanded
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const attributes = [];
|
|
|
|
|
|
|
|
for (const attributeId of collectedAttributeIds) {
|
|
|
|
const attribute = noteCache.attributes[attributeId];
|
|
|
|
|
|
|
|
attributes.push({
|
|
|
|
attributeId: attribute.attributeId,
|
|
|
|
noteId: attribute.noteId,
|
|
|
|
type: attribute.type,
|
|
|
|
name: attribute.name,
|
|
|
|
value: attribute.value,
|
|
|
|
position: attribute.position,
|
|
|
|
isInheritable: attribute.isInheritable
|
|
|
|
});
|
2020-12-10 21:27:21 +01:00
|
|
|
}
|
2020-01-25 11:52:45 +01:00
|
|
|
|
2019-10-25 21:47:14 +02:00
|
|
|
return {
|
|
|
|
branches,
|
2020-01-25 13:46:55 +01:00
|
|
|
notes,
|
|
|
|
attributes
|
2019-10-25 21:47:14 +02:00
|
|
|
};
|
2018-04-16 20:40:18 -04:00
|
|
|
}
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2020-08-26 16:50:16 +02:00
|
|
|
function getTree(req) {
|
2020-11-22 23:05:02 +01:00
|
|
|
const subTreeNoteId = req.query.subTreeNoteId || 'root';
|
2020-12-13 23:27:42 +01:00
|
|
|
const collectedNoteIds = new Set([subTreeNoteId]);
|
2020-12-11 22:06:12 +01:00
|
|
|
|
|
|
|
function collect(parentNote) {
|
2020-12-16 14:36:24 +01:00
|
|
|
if (!parentNote) {
|
|
|
|
console.trace(parentNote);
|
|
|
|
}
|
|
|
|
|
2020-12-13 23:27:42 +01:00
|
|
|
for (const childNote of parentNote.children) {
|
2020-12-11 22:06:12 +01:00
|
|
|
collectedNoteIds.add(childNote.noteId);
|
|
|
|
|
|
|
|
const childBranch = noteCache.getBranch(childNote.noteId, parentNote.noteId);
|
|
|
|
|
|
|
|
if (childBranch.isExpanded) {
|
2020-12-13 23:27:42 +01:00
|
|
|
collect(childBranch.childNote);
|
2020-12-11 22:06:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
collect(noteCache.notes[subTreeNoteId]);
|
2018-12-11 21:53:56 +01:00
|
|
|
|
2020-12-11 22:06:12 +01:00
|
|
|
return getNotesAndBranchesAndAttributes(collectedNoteIds);
|
2018-04-16 20:40:18 -04:00
|
|
|
}
|
2018-04-16 16:26:47 -04:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function load(req) {
|
|
|
|
return getNotesAndBranchesAndAttributes(req.body.noteIds);
|
2018-03-30 12:57:22 -04:00
|
|
|
}
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2018-03-30 12:57:22 -04:00
|
|
|
module.exports = {
|
2018-04-16 20:40:18 -04:00
|
|
|
getTree,
|
|
|
|
load
|
2018-03-30 12:57:22 -04:00
|
|
|
};
|