Skip to content

Build your personal knowledge base with Trilium Notes

fix(share): count only readable notes in a shared note map

Elian Doran4765e7e03301
diff --git a/packages/trilium-core/src/routes/api/note_map.ts b/packages/trilium-core/src/routes/api/note_map.ts
index cdb6362ed9..8a282fc419 100644
--- a/packages/trilium-core/src/routes/api/note_map.ts
+++ b/packages/trilium-core/src/routes/api/note_map.ts
@@ -11,7 +11,11 @@ export interface TreeLink {
targetNoteId: string;
}
-function buildDescendantCountMap(noteIdsToCount: string[]) {
+/**
+ * Counts the descendants of each note, of which only those `isCounted` accepts: a note it refuses
+ * is left out together with everything under it.
+ */
+function buildDescendantCountMap(noteIdsToCount: string[], isCounted: (noteId: string) => boolean) {
/* v8 ignore next 3 -- defensive guard: callers always pass a real array (noteIdsArray / Array.from(noteIds)) */
if (!Array.isArray(noteIdsToCount)) {
throw new Error("noteIdsToCount: type error");
@@ -28,12 +32,14 @@ function buildDescendantCountMap(noteIdsToCount: string[]) {
}
const hiddenImageNoteIds = note.getRelations("imageLink").map((rel) => rel.value);
- const childNoteIds = note.children.map((child) => child.noteId);
- const nonHiddenNoteIds = childNoteIds.filter((childNoteId) => !hiddenImageNoteIds.includes(childNoteId));
+ const children = note.children.filter((child) => isCounted(child.noteId));
+ const nonHiddenNoteIds = children
+ .map((child) => child.noteId)
+ .filter((childNoteId) => !hiddenImageNoteIds.includes(childNoteId));
noteIdToCountMap[noteId] = nonHiddenNoteIds.length;
- for (const child of note.children) {
+ for (const child of children) {
noteIdToCountMap[noteId] += getCount(child.noteId);
}
}
@@ -110,7 +116,11 @@ function getLinkMap(req: Request<{ noteId: string }>) {
* Returns the notes of the link map of `mapRootNote` (its subtree, or a search note's results, and
* the notes within three relations of it) and the relations between them, which `filters` narrow.
*/
-export function buildLinkMap(mapRootNote: BNote, filters: { excludeRelations: string[]; includeRelations: string[] }) {
+export function buildLinkMap(
+ mapRootNote: BNote,
+ filters: { excludeRelations: string[]; includeRelations: string[] },
+ isCounted: (noteId: string) => boolean = () => true
+) {
// if the map root itself has "excludeFromNoteMap" attribute (journal typically) then there wouldn't be anything
// to display, so we'll just ignore it
@@ -185,7 +195,7 @@ export function buildLinkMap(mapRootNote: BNote, filters: { excludeRelations: st
return {
notes,
- noteIdToDescendantCountMap: buildDescendantCountMap(noteIdsArray),
+ noteIdToDescendantCountMap: buildDescendantCountMap(noteIdsArray, isCounted),
links
};
}
@@ -195,7 +205,7 @@ function getTreeMap(req: Request<{ noteId: string }>) {
}
/** Returns the notes of the subtree of `mapRootNote` and the branches between them. */
-export function buildTreeMap(mapRootNote: BNote) {
+export function buildTreeMap(mapRootNote: BNote, isCounted: (noteId: string) => boolean = () => true) {
// if the map root itself has "excludeFromNoteMap" (journal typically) then there wouldn't be anything to display,
// so we'll just ignore it
const ignoreExcludeFromNoteMap = mapRootNote.isLabelTruthy("excludeFromNoteMap");
@@ -239,7 +249,7 @@ export function buildTreeMap(mapRootNote: BNote) {
});
}
- const noteIdToDescendantCountMap = buildDescendantCountMap(Array.from(noteIds));
+ const noteIdToDescendantCountMap = buildDescendantCountMap(Array.from(noteIds), isCounted);
updateDescendantCountMapForSearch(noteIdToDescendantCountMap, subtree.relationships);
diff --git a/packages/trilium-core/src/share/froca_payload.ts b/packages/trilium-core/src/share/froca_payload.ts
index 8f3cca1abf..2dad1c10cc 100644
--- a/packages/trilium-core/src/share/froca_payload.ts
+++ b/packages/trilium-core/src/share/froca_payload.ts
@@ -151,7 +151,7 @@ export type IsVisibleNote = (noteId: string) => boolean;
/**
* The note map of `mapRoot` the app draws, as a tree or as its relations, with only the notes
- * `isVisible` accepts and the links between them.
+ * `isVisible` accepts, the links between them, and descendant counts made of those notes alone.
*/
export function buildVisibleNoteMap(
mapRoot: BNote,
@@ -159,7 +159,9 @@ export function buildVisibleNoteMap(
filters: { excludeRelations: string[]; includeRelations: string[] },
isVisible: IsVisibleNote
) {
- const map = mapType === "tree" ? buildTreeMap(mapRoot) : buildLinkMap(mapRoot, filters);
+ const map = mapType === "tree"
+ ? buildTreeMap(mapRoot, isVisible)
+ : buildLinkMap(mapRoot, filters, isVisible);
const notes = map.notes.filter(([ noteId ]) => isVisible(noteId));
const noteIds = new Set(notes.map(([ noteId ]) => noteId));
return {
diff --git a/packages/trilium-core/src/share/handlers.spec.ts b/packages/trilium-core/src/share/handlers.spec.ts
index 6b1318691b..bceec4f77b 100644
--- a/packages/trilium-core/src/share/handlers.spec.ts
+++ b/packages/trilium-core/src/share/handlers.spec.ts
@@ -649,7 +649,8 @@ describe("share handlers", () => {
expect(tree.notes.map(([ noteId ]: [ string ]) => noteId).sort())
.toEqual([ "mapReadable", "mapRoot" ]);
expect(tree.links).toHaveLength(1);
- expect(Object.keys(tree.noteIdToDescendantCountMap)).not.toContain("mapLocked");
+ // Counted from the notes the caller can read, so a count reveals no locked or unshared note.
+ expect(tree.noteIdToDescendantCountMap).toEqual({ mapRoot: 1, mapReadable: 0 });
const linkMap = noteMap("link",
{ excludeRelation: "imageLink", includeRelation: [ "author" ] });
expect(JSON.parse(String(linkMap.body)).links).toEqual([]);