2017-10-21 21:10:33 -04:00
|
|
|
"use strict";
|
|
|
|
|
2024-07-18 21:35:17 +03:00
|
|
|
import becca from "../../becca/becca.js";
|
|
|
|
import log from "../../services/log.js";
|
|
|
|
import NotFoundError from "../../errors/not_found_error.js";
|
2025-01-09 18:36:24 +02:00
|
|
|
import type { Request } from "express";
|
2025-01-13 23:18:10 +02:00
|
|
|
import type BNote from "../../becca/entities/bnote.js";
|
2024-04-06 23:34:47 +03:00
|
|
|
|
|
|
|
function getNotesAndBranchesAndAttributes(_noteIds: string[] | Set<string>) {
|
|
|
|
const noteIds = new Set(_noteIds);
|
|
|
|
const collectedNoteIds = new Set<string>();
|
|
|
|
const collectedAttributeIds = new Set<string>();
|
|
|
|
const collectedBranchIds = new Set<string>();
|
|
|
|
|
|
|
|
function collectEntityIds(note?: BNote) {
|
2022-08-16 23:46:56 +02:00
|
|
|
if (!note || collectedNoteIds.has(note.noteId)) {
|
2020-12-11 22:06:12 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
collectedNoteIds.add(note.noteId);
|
|
|
|
|
2021-05-18 22:14:35 +02:00
|
|
|
for (const branch of note.getParentBranches()) {
|
2024-04-06 23:34:47 +03:00
|
|
|
if (branch.branchId) {
|
|
|
|
collectedBranchIds.add(branch.branchId);
|
|
|
|
}
|
2020-12-11 22:06:12 +01:00
|
|
|
|
|
|
|
collectEntityIds(branch.parentNote);
|
|
|
|
}
|
|
|
|
|
2020-12-13 23:27:42 +01:00
|
|
|
for (const childNote of note.children) {
|
2021-04-26 22:18:14 +02:00
|
|
|
const childBranch = becca.getBranchFromChildAndParent(childNote.noteId, note.noteId);
|
2024-04-06 23:34:47 +03:00
|
|
|
if (childBranch && childBranch.branchId) {
|
|
|
|
collectedBranchIds.add(childBranch.branchId);
|
|
|
|
}
|
2020-12-13 23:27:42 +01:00
|
|
|
}
|
|
|
|
|
2020-12-11 22:06:12 +01:00
|
|
|
for (const attr of note.ownedAttributes) {
|
|
|
|
collectedAttributeIds.add(attr.attributeId);
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
if (attr.type === "relation" && ["template", "inherit"].includes(attr.name) && attr.targetNote) {
|
2020-12-11 22:06:12 +01:00
|
|
|
collectEntityIds(attr.targetNote);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const noteId of noteIds) {
|
2021-04-16 23:00:08 +02:00
|
|
|
const note = becca.notes[noteId];
|
2020-12-11 22:06:12 +01:00
|
|
|
|
|
|
|
if (!note) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
collectEntityIds(note);
|
|
|
|
}
|
|
|
|
|
|
|
|
const notes = [];
|
|
|
|
|
|
|
|
for (const noteId of collectedNoteIds) {
|
2021-04-16 23:00:08 +02:00
|
|
|
const note = becca.notes[noteId];
|
2020-12-11 22:06:12 +01:00
|
|
|
|
|
|
|
notes.push({
|
|
|
|
noteId: note.noteId,
|
2022-01-08 21:50:16 +01:00
|
|
|
title: note.getTitleOrProtected(),
|
2020-12-11 22:06:12 +01:00
|
|
|
isProtected: note.isProtected,
|
|
|
|
type: note.type,
|
2023-11-13 23:53:14 +01:00
|
|
|
mime: note.mime,
|
|
|
|
blobId: note.blobId
|
2020-12-11 22:06:12 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const branches = [];
|
2018-08-31 17:29:54 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
if (noteIds.has("root")) {
|
2020-12-11 15:27:57 +01:00
|
|
|
branches.push({
|
2025-01-09 18:07:02 +02:00
|
|
|
branchId: "none_root",
|
|
|
|
noteId: "root",
|
|
|
|
parentNoteId: "none",
|
2020-12-11 15:27:57 +01:00
|
|
|
notePosition: 0,
|
2025-01-09 18:07:02 +02:00
|
|
|
prefix: "",
|
2020-12-11 15:27:57 +01:00
|
|
|
isExpanded: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-11 22:06:12 +01:00
|
|
|
for (const branchId of collectedBranchIds) {
|
2021-04-16 23:00:08 +02:00
|
|
|
const branch = becca.branches[branchId];
|
2020-12-11 22:06:12 +01:00
|
|
|
|
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) {
|
2021-04-16 23:00:08 +02:00
|
|
|
const attribute = becca.attributes[attributeId];
|
2020-12-11 22:06:12 +01:00
|
|
|
|
2022-08-26 22:08:05 +02:00
|
|
|
if (!attribute) {
|
|
|
|
log.error(`Could not find attribute for attributeId=${attributeId}`);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-11 22:06:12 +01:00
|
|
|
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
|
|
|
|
2024-04-06 23:34:47 +03:00
|
|
|
function getTree(req: Request) {
|
2025-01-09 18:07:02 +02:00
|
|
|
const subTreeNoteId = typeof req.query.subTreeNoteId === "string" ? req.query.subTreeNoteId : "root";
|
2024-04-06 23:34:47 +03:00
|
|
|
const collectedNoteIds = new Set<string>([subTreeNoteId]);
|
2020-12-11 22:06:12 +01:00
|
|
|
|
2024-04-06 23:34:47 +03:00
|
|
|
function collect(parentNote: BNote) {
|
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);
|
|
|
|
|
2021-04-26 22:18:14 +02:00
|
|
|
const childBranch = becca.getBranchFromChildAndParent(childNote.noteId, parentNote.noteId);
|
2020-12-11 22:06:12 +01:00
|
|
|
|
2024-04-06 23:34:47 +03:00
|
|
|
if (childBranch?.isExpanded) {
|
2020-12-13 23:27:42 +01:00
|
|
|
collect(childBranch.childNote);
|
2020-12-11 22:06:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-16 23:00:08 +02:00
|
|
|
if (!(subTreeNoteId in becca.notes)) {
|
2022-12-09 16:04:13 +01:00
|
|
|
throw new NotFoundError(`Note '${subTreeNoteId}' not found in the cache`);
|
2021-02-20 21:28:22 +01:00
|
|
|
}
|
|
|
|
|
2021-04-16 23:00:08 +02:00
|
|
|
collect(becca.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
|
|
|
|
2024-04-06 23:34:47 +03:00
|
|
|
function load(req: Request) {
|
2020-06-20 12:31:38 +02:00
|
|
|
return getNotesAndBranchesAndAttributes(req.body.noteIds);
|
2018-03-30 12:57:22 -04:00
|
|
|
}
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2024-07-18 21:47:30 +03:00
|
|
|
export default {
|
2018-04-16 20:40:18 -04:00
|
|
|
getTree,
|
|
|
|
load
|
2018-03-30 12:57:22 -04:00
|
|
|
};
|