Notes/src/routes/api/tree.ts

167 lines
4.5 KiB
TypeScript
Raw Normal View History

2017-10-21 21:10:33 -04:00
"use strict";
import becca from "../../becca/becca.js";
import log from "../../services/log.js";
import NotFoundError from "../../errors/not_found_error.js";
import type { Request } from "express";
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) {
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,
title: note.getTitleOrProtected(),
2020-12-11 22:06:12 +01:00
isProtected: note.isProtected,
type: note.type,
mime: note.mime,
blobId: note.blobId
2020-12-11 22:06:12 +01:00
});
}
const branches = [];
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
});
}
return {
branches,
2020-01-25 13:46:55 +01:00
notes,
attributes
};
}
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);
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)) {
throw new NotFoundError(`Note '${subTreeNoteId}' not found in the cache`);
}
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);
}
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
}
export default {
getTree,
load
2018-03-30 12:57:22 -04:00
};