Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(share): keep notes hidden from the tree out of note maps and name completion
Elian Doran6d7719cc8570
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Sharing.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Sharing.htmlindex ab3688138b..03b6ee793e 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Sharing.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Sharing.html@@ -392,8 +392,9 @@ class="admonition note"><tr><td><code>#shareHiddenFromTree</code></td>- <td>this note is hidden from the left navigation tree and from its parent's- list of subpages, but still accessible with its URL</td>+ <td>this note is hidden from the left navigation tree, from its parent's list+ of subpages, from shared note maps and from the share's search, but still+ accessible with its URL</td></tr><tr><td><code>#shareExternalLink</code>diff --git a/docs/User Guide/User Guide/Advanced Usage/Sharing.md b/docs/User Guide/User Guide/Advanced Usage/Sharing.mdindex 693bd95a3b..e03a7aa09d 100644--- a/docs/User Guide/User Guide/Advanced Usage/Sharing.md+++ b/docs/User Guide/User Guide/Advanced Usage/Sharing.md@@ -247,7 +247,7 @@ The URL must be absolute and include the scheme (e.g. `https://`).<tbody><tr><td><code>#shareHiddenFromTree</code></td>- <td>this note is hidden from the left navigation tree and from its parent's list of subpages, but still accessible with its URL</td>+ <td>this note is hidden from the left navigation tree, from its parent's list of subpages, from shared note maps and from the share's search, but still accessible with its URL</td></tr><tr><td><code>#shareExternalLink</code></td>diff --git a/packages/trilium-core/src/share/handlers.spec.ts b/packages/trilium-core/src/share/handlers.spec.tsindex bceec4f77b..379e92bf52 100644--- a/packages/trilium-core/src/share/handlers.spec.ts+++ b/packages/trilium-core/src/share/handlers.spec.ts@@ -546,12 +546,14 @@ describe("share handlers", () => {expect(filter({ searchString: "seen", ancestorNoteId: "missing" }).status).toBe(404);});- it("completes attribute names only from the notes the caller can read", () => {+ it("completes attribute names only from the notes the caller can read in the tree", () => {buildShareTree([{ "id": "open", "title": "Open", "content": "", "#projectPhase": "", "~projectOwner": "open" },{ "id": "guarded", "title": "Guarded", "content": "", "#projectBudget": "","#shareCredentials": "root:hunter2" },- { "id": "secret", "title": "Secret", "content": "", "#projectSecret": "", "isProtected": true }+ { "id": "secret", "title": "Secret", "content": "", "#projectSecret": "", "isProtected": true },+ { "id": "hidden", "title": "Hidden", "content": "", "#shareHiddenFromTree": "", "#projectHidden": "",+ "children": [ { "id": "belowHidden", "title": "Below", "content": "", "#projectDraft": "" } ] }]);const names = (query: ShareRequest["query"], authorization?: string) => {const reply = request("/share/api/attribute-names",@@ -624,7 +626,11 @@ describe("share handlers", () => {content: "",children: [{ id: "mapReadable", content: "" },- { "id": "mapLocked", "content": "", "#shareCredentials": "root:hunter2" }+ { "id": "mapLocked", "content": "", "#shareCredentials": "root:hunter2" },+ {+ "id": "mapHidden", "content": "", "#shareHiddenFromTree": "",+ "children": [ { id: "mapBelowHidden", content: "" } ]+ }]},{@@ -637,7 +643,10 @@ describe("share handlers", () => {buildNote({id: "mapRoot",type: "noteMap",- children: [ { id: "mapReadable" }, { id: "mapLocked" }, { id: "mapUnshared" } ]+ children: [+ { id: "mapReadable" }, { id: "mapLocked" }, { id: "mapUnshared" },+ { id: "mapHidden", children: [ { id: "mapBelowHidden" } ] }+ ]});buildNote({ id: "relationMap", type: "relationMap" });buildNote({ id: "scriptNote", type: "code", mime: frontendMime });@@ -649,7 +658,8 @@ describe("share handlers", () => {expect(tree.notes.map(([ noteId ]: [ string ]) => noteId).sort()).toEqual([ "mapReadable", "mapRoot" ]);expect(tree.links).toHaveLength(1);- // Counted from the notes the caller can read, so a count reveals no locked or unshared note.+ // Counted from the notes the caller can read and the share tree lists, so a count reveals+ // no locked, unshared or hidden note.expect(tree.noteIdToDescendantCountMap).toEqual({ mapRoot: 1, mapReadable: 0 });const linkMap = noteMap("link",{ excludeRelation: "imageLink", includeRelation: [ "author" ] });diff --git a/packages/trilium-core/src/share/handlers.ts b/packages/trilium-core/src/share/handlers.tsindex 103e0407e6..797d7c21f5 100644--- a/packages/trilium-core/src/share/handlers.ts+++ b/packages/trilium-core/src/share/handlers.ts@@ -18,6 +18,7 @@ import {} from "./froca_payload.js";import { SHARE_ROUTE_PATHS, type ShareRoutePath } from "./route_paths.js";import { isShareReady } from "./share_provider.js";+import shareRoot from "./share_root.js";import type SAttachment from "./shaca/entities/sattachment.js";import type SNote from "./shaca/entities/snote.js";import shaca from "./shaca/shaca.js";@@ -212,7 +213,8 @@ function loadTree(req: ShareRequest): ShareReply {/*** Answers the note map the app draws of a shared note, as a tree or as its relations, with only the- * notes a visitor of the share can read and the links between them. The relations to leave out or+ * notes a visitor of the share can read and the links between them. The tree leaves out the notes+ * the share tree does not list. The relations to leave out or* to keep come as repeated `excludeRelation` and `includeRelation` parameters.*/function getNoteMap(req: ShareRequest): ShareReply {@@ -226,7 +228,7 @@ function getNoteMap(req: ShareRequest): ShareReply {return jsonReply(200, buildVisibleNoteMap(mapRoot, mapType, {excludeRelations: toNames(req.query.excludeRelation),includeRelations: toNames(req.query.includeRelation)- }, (noteId) => isReadable(noteId, req)));+ }, (noteId) => isReadable(noteId, req) && (mapType === "link" || isListedInShareTree(noteId))));}/**@@ -259,6 +261,26 @@ function isReadable(noteId: string, req: ShareRequest) {return !!note && !note.isProtected && hasCredentialAccess(note, req);}+/**+ * Whether the share's navigation tree lists the note `noteId`: a path from the share root reaches+ * it through branches that are not hidden and notes without `#shareHiddenFromTree`, as+ * `SNote.getVisibleChildBranches()` walks it.+ */+function isListedInShareTree(noteId: string, visited = new Set<string>()): boolean {+ if (noteId === shareRoot.SHARE_ROOT_NOTE_ID) {+ return true;+ }++ const note = shaca.getNote(noteId);+ if (!note || visited.has(noteId) || note.isLabelTruthy("shareHiddenFromTree")) {+ return false;+ }++ visited.add(noteId);+ return note.getParentBranches().some((branch) => !branch.isHidden+ && isListedInShareTree(branch.parentNoteId, visited));+}+function getNoteAttachments(req: ShareRequest): ShareReply {const note = checkNoteContentAccess(req.params.noteId ?? "", req);return jsonReply(200, note.getAttachments().map(getAttachmentRow));@@ -389,7 +411,7 @@ function searchInSubtree(req: ShareRequest): ShareReply {/*** Completes an attribute name for the search editor from the attributes of the notes a visitor can- * read, and the built-in names. The names in the rest of the database are none of a visitor's+ * read and the share tree lists, and the built-in names. The names in the rest of the database are none of a visitor's* business.*/function getAttributeNames(req: ShareRequest): ShareReply {@@ -403,7 +425,8 @@ function getAttributeNames(req: ShareRequest): ShareReply {for (const attribute of Object.values(shaca.attributes)) {const { note } = attribute;if (attribute.type === type && attribute.name.toLowerCase().includes(nameLike)- && !note.isProtected && hasCredentialAccess(note, req)) {+ && !note.isProtected && hasCredentialAccess(note, req)+ && isListedInShareTree(note.noteId)) {names.add(attribute.name);}}