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
feat(share): let froca read what a shared page did not embed from the share
Elian Doran923097bd3e53
diff --git a/apps/client/src/services/froca-interface.ts b/apps/client/src/services/froca-interface.tsindex 80015cc343..3058c1bff3 100644--- a/apps/client/src/services/froca-interface.ts+++ b/apps/client/src/services/froca-interface.ts@@ -1,8 +1,33 @@import type FAttachment from "../entities/fattachment.js";+import type { FAttachmentRow } from "../entities/fattachment.js";import type FAttribute from "../entities/fattribute.js";+import type { FAttributeRow } from "../entities/fattribute.js";import type FBlob from "../entities/fblob.js";+import type { FBlobRow } from "../entities/fblob.js";import type FBranch from "../entities/fbranch.js";+import type { FBranchRow } from "../entities/fbranch.js";import type FNote from "../entities/fnote.js";+import type { FNoteRow } from "../entities/fnote.js";++/** The rows of notes with their branches and attributes, which `froca.addResp()` takes. */+export interface SubtreeResponse {+ notes: FNoteRow[];+ branches: FBranchRow[];+ attributes: FAttributeRow[];+}++/**+ * Where froca reads the notes, attachments and blobs it does not hold yet. The app reads them from+ * its API; a page hosting an app view outside the app, such as a shared page, sets its own with+ * `froca.setSource()`.+ */+export interface FrocaSource {+ loadNotes(noteIds: string[]): Promise<SubtreeResponse>;+ /** The attachments of the note owning `attachmentId`; rejects if there is no such attachment. */+ getSiblingAttachments(attachmentId: string): Promise<FAttachmentRow[]>;+ getAttachments(noteId: string): Promise<FAttachmentRow[]>;+ getBlob(entityType: string, entityId: string): Promise<FBlobRow>;+}export interface Froca {notes: Record<string, FNote>;diff --git a/apps/client/src/services/froca.ts b/apps/client/src/services/froca.tsindex 2a3fd998fc..acfd20a6be 100644--- a/apps/client/src/services/froca.ts+++ b/apps/client/src/services/froca.ts@@ -5,15 +5,10 @@ import FAttribute, { type FAttributeRow } from "../entities/fattribute.js";import FBlob, { type FBlobRow } from "../entities/fblob.js";import FBranch, { type FBranchRow } from "../entities/fbranch.js";import FNote, { type FNoteRow } from "../entities/fnote.js";-import type { Froca } from "./froca-interface.js";+import type { Froca, FrocaSource, SubtreeResponse } from "./froca-interface.js";import server from "./server.js";import { isPreAuthScreen } from "./utils.js";-interface SubtreeResponse {- notes: FNoteRow[];- branches: FBranchRow[];- attributes: FAttributeRow[];-}interface SearchNoteResponse {searchResultNoteIds: string[];@@ -42,12 +37,18 @@ class FrocaImpl implements Froca {attributes!: Record<string, FAttribute>;attachments!: Record<string, FAttachment>;blobPromises!: Record<string, Promise<FBlob | null> | null>;+ private source: FrocaSource = SERVER_SOURCE;constructor() {this.initializedPromise = this.loadInitialTree();this.#clear();}+ /** Reads what froca does not hold from `source` instead of the app's API. */+ setSource(source: FrocaSource) {+ this.source = source;+ }+async loadInitialTree() {// Skip on the setup screen (!dbInitialized) and on the login / set-password pre-auth// screens (isPreAuthScreen) — otherwise this fires an unauthenticated GET /api/tree (#10589).@@ -189,7 +190,7 @@ class FrocaImpl implements Froca {noteIds = Array.from(new Set(noteIds)); // make noteIds unique- const resp = await server.post<SubtreeResponse>("tree/load", { noteIds });+ const resp = await this.source.loadNotes(noteIds);this.addResp(resp);@@ -349,7 +350,7 @@ class FrocaImpl implements Froca {// load all attachments for the given note even if one is requested, don't load one by onelet attachmentRows;try {- attachmentRows = await server.getWithSilentNotFound<FAttachmentRow[]>(`attachments/${attachmentId}/all`);+ attachmentRows = await this.source.getSiblingAttachments(attachmentId);} catch (e: any) {if (silentNotFoundError) {logInfo(`Attachment '${attachmentId}' not found, but silentNotFoundError is enabled: ${e.message}`);@@ -378,7 +379,7 @@ class FrocaImpl implements Froca {}async getAttachmentsForNote(noteId: string) {- const attachmentRows = await server.get<FAttachmentRow[]>(`notes/${noteId}/attachments`);+ const attachmentRows = await this.source.getAttachments(noteId);return this.processAttachmentRows(attachmentRows);}@@ -405,8 +406,8 @@ class FrocaImpl implements Froca {const key = `${entityType}-${entityId}`;if (!this.blobPromises[key]) {- this.blobPromises[key] = server- .getWithSilentNotFound<FBlobRow>(`${entityType}/${entityId}/blob`)+ this.blobPromises[key] = this.source+ .getBlob(entityType, entityId).then((row) => new FBlob(row)).catch((e) => {console.error(`Cannot get blob for ${entityType} '${entityId}'`, e);@@ -423,6 +424,16 @@ class FrocaImpl implements Froca {}}+/** Reads from the app's API. */+const SERVER_SOURCE: FrocaSource = {+ loadNotes: (noteIds) => server.post<SubtreeResponse>("tree/load", { noteIds }),+ getSiblingAttachments: (attachmentId) =>+ server.getWithSilentNotFound<FAttachmentRow[]>(`attachments/${attachmentId}/all`),+ getAttachments: (noteId) => server.get<FAttachmentRow[]>(`notes/${noteId}/attachments`),+ getBlob: (entityType, entityId) =>+ server.getWithSilentNotFound<FBlobRow>(`${entityType}/${entityId}/blob`)+};+const froca = new FrocaImpl();export default froca;diff --git a/packages/share-theme/src/content/geomap_view.tsx b/packages/share-theme/src/content/geomap_view.tsxindex dab54bb8ea..667535d9d0 100644--- a/packages/share-theme/src/content/geomap_view.tsx+++ b/packages/share-theme/src/content/geomap_view.tsx@@ -10,6 +10,7 @@ import { ParentComponent } from "@triliumnext/client/src/widgets/react/react_utiimport { render } from "preact";import type { FrocaPayload } from "./calendar_view.js";+import { createShareFrocaSource } from "./share_froca_source.js";interface GeoMapPayload extends FrocaPayload {options: Record<string, OptionValue | null>;@@ -17,9 +18,10 @@ interface GeoMapPayload extends FrocaPayload {}/**- * Loads the map's notes into froca and the display options into `options`, then mounts the app's- * `GeoView` over them, under a component of its own as the app mounts every view. A `#readOnly` label added to the collection's note turns off every editing- * control the view has.+ * Loads the map's notes into froca, which reads any other note from the share, and the display+ * options into `options`, then mounts the app's `GeoView` over them, under a component of its own+ * as the app mounts every view. A `#readOnly` label added to the collection's note turns off every+ * editing control the view has.*/export default function mountGeoMap(container: HTMLElement, payload: GeoMapPayload) {const { options: optionValues, links, viewConfig, ...rows } = payload;@@ -27,6 +29,7 @@ export default function mountGeoMap(container: HTMLElement, payload: GeoMapPaylo.flatMap(([ name, value ]) => (value === null ? [] : [ [ name, value ] ]))));const noteId = container.dataset.noteId ?? "";+ froca.setSource(createShareFrocaSource(links));froca.addResp({...rows,attributes: [ ...rows.attributes, {diff --git a/packages/share-theme/src/content/share_froca_source.ts b/packages/share-theme/src/content/share_froca_source.tsnew file mode 100644index 0000000000..5fad501457--- /dev/null+++ b/packages/share-theme/src/content/share_froca_source.ts@@ -0,0 +1,34 @@+import type { FrocaSource, SubtreeResponse } from "@triliumnext/client/src/services/froca-interface.js";++/** Rows of notes as the share answers them, with the share link of each note. */+export interface ShareFrocaRows extends SubtreeResponse {+ links: Record<string, string>;+}++/**+ * Reads the notes, attachments and blobs froca lacks from the share's API, relative to the page,+ * and records the share link of each note it loads in `links`.+ */+export function createShareFrocaSource(links: Record<string, string>): FrocaSource {+ return {+ loadNotes: async (noteIds) => {+ const query = noteIds.map(encodeURIComponent).join(",");+ const rows = await getJson<ShareFrocaRows>(`api/tree?noteIds=${query}`);+ Object.assign(links, rows.links);+ return rows;+ },+ getSiblingAttachments: (attachmentId) =>+ getJson(`api/attachments/${encodeURIComponent(attachmentId)}/all`),+ getAttachments: (noteId) => getJson(`api/notes/${encodeURIComponent(noteId)}/attachments`),+ getBlob: (entityType, entityId) =>+ getJson(`api/${entityType}/${encodeURIComponent(entityId)}/blob`)+ };+}++async function getJson<T>(url: string): Promise<T> {+ const response = await fetch(url);+ if (!response.ok) {+ throw new Error(`${response.status} ${response.statusText}: ${url}`);+ }+ return await response.json() as T;+}diff --git a/packages/trilium-core/src/share/froca_payload.ts b/packages/trilium-core/src/share/froca_payload.tsindex 6ca9ac7076..1de724bea0 100644--- a/packages/trilium-core/src/share/froca_payload.ts+++ b/packages/trilium-core/src/share/froca_payload.ts@@ -1,35 +1,44 @@import { getShareLink } from "@triliumnext/share-theme/model/page";import * as sanitize from "../services/sanitizer.js";+import type SAttachment from "./shaca/entities/sattachment.js";import type SNote from "./shaca/entities/snote.js";/**- * The notes a collection view reads, as the rows the client's `froca.addResp()` takes, so that the- * share page builds the same `FNote`s the app does. `links` maps each note to its share link.+ * Notes as the rows the client's `froca.addResp()` takes, so that a shared page builds the same+ * `FNote`s the app does, with the share link of each note in `links`.*/-export interface FrocaPayload {+export interface FrocaRows {notes: ReturnType<typeof getNoteRow>[];branches: ReturnType<SNote["parentBranches"][number]["getPojo"]>[];attributes: ReturnType<SNote["ownedAttributes"][number]["getPojo"]>[];links: Record<string, string>;}+/** The notes a collection view reads, embedded in its page so the view draws without a request. */+export function buildFrocaPayload(note: SNote) {+ return buildFrocaRows([ note ]);+}+/**- * Collects `note`, its visible children and the notes their relations point to (templates and- * `#calendar:title` relations), with their owned attributes and the branches between them.- * Shaca holds only shared notes, so a relation to a note outside the share is left out.+ * Collects `roots`, their visible children and the notes their relations point to (templates and+ * `#calendar:title` relations), with their owned attributes and the branches between them. Shaca+ * holds only shared notes, so a relation to a note outside the share is left out, as is every note+ * that is protected or that `canAccess` refuses.*/-export function buildFrocaPayload(note: SNote): FrocaPayload {+export function buildFrocaRows(roots: SNote[], canAccess: (note: SNote) => boolean = () => true): FrocaRows {const notes = new Map<string, SNote>();const add = (candidate: SNote | null | undefined) => {- if (candidate && !candidate.isProtected) {+ if (candidate && !candidate.isProtected && canAccess(candidate)) {notes.set(candidate.noteId, candidate);}};- add(note);- for (const child of note.getVisibleChildNotes()) {- add(child);+ for (const root of roots) {+ add(root);+ for (const child of root.getVisibleChildNotes()) {+ add(child);+ }}for (const owner of [ ...notes.values() ]) {for (const relation of owner.ownedAttributes) {@@ -39,22 +48,47 @@ export function buildFrocaPayload(note: SNote): FrocaPayload {}}- const payload: FrocaPayload = { notes: [], branches: [], attributes: [], links: {} };+ const rows: FrocaRows = { notes: [], branches: [], attributes: [], links: {} };for (const included of notes.values()) {- payload.notes.push(getNoteRow(included));- payload.links[included.noteId] = getShareLink(included, sanitize.sanitizeUrl).href;+ rows.notes.push(getNoteRow(included));+ rows.links[included.noteId] = getShareLink(included, sanitize.sanitizeUrl).href;for (const branch of included.parentBranches) {if (notes.has(branch.parentNoteId)) {- payload.branches.push(branch.getPojo());+ rows.branches.push(branch.getPojo());}}for (const attribute of included.ownedAttributes) {if (attribute.type === "label" || notes.has(attribute.value)) {- payload.attributes.push(attribute.getPojo());+ rows.attributes.push(attribute.getPojo());}}}- return payload;+ return rows;+}++/** An attachment as the row `froca` reads attachments from. */+export function getAttachmentRow(attachment: SAttachment) {+ const pojo = attachment.getPojo();+ return {+ ...pojo,+ ownerId: attachment.ownerId,+ dateModified: pojo.utcDateModified,+ utcDateScheduledForErasureSince: null,+ contentLength: 0+ };+}++/** The content of a note or an attachment as the blob row `froca.getBlob()` reads. */+export function getBlobRow(entity: SNote | SAttachment) {+ const content = entity.hasStringContent() ? String(entity.getContent()) : null;+ const utcDateModified = "noteId" in entity ? entity.utcDateModified : entity.getPojo().utcDateModified;+ return {+ blobId: utcDateModified,+ content,+ contentLength: content?.length ?? 0,+ dateModified: utcDateModified,+ utcDateModified+ };}function getNoteRow(note: SNote) {diff --git a/packages/trilium-core/src/share/handlers.ts b/packages/trilium-core/src/share/handlers.tsindex 88e3a1e764..0e93b232f1 100644--- a/packages/trilium-core/src/share/handlers.ts+++ b/packages/trilium-core/src/share/handlers.ts@@ -8,6 +8,7 @@ import SearchContext from "../services/search/search_context.js";import { decodeBase64, decodeUtf8, encodeUtf8 } from "../services/utils/binary.js";import * as utils from "../services/utils/index.js";import { readShareTemplate, renderNoteContent } from "./content_renderer.js";+import { buildFrocaRows, getAttachmentRow, getBlobRow } from "./froca_payload.js";import { SHARE_ROUTE_PATHS, type ShareRoutePath } from "./route_paths.js";import { isShareReady } from "./share_provider.js";import type SAttachment from "./shaca/entities/sattachment.js";@@ -80,11 +81,16 @@ export function handleShareRequest(route: ShareRoute, req: ShareRequest): ShareRconst HANDLERS: Record<ShareRoutePath, (req: ShareRequest) => ShareReply> = {"/share/api/notes/:noteId/download": downloadNote,"/share/api/notes/:noteId/view": viewNote,+ "/share/api/notes/:noteId/attachments": getNoteAttachments,+ "/share/api/notes/:noteId/blob": getNoteBlob,"/share/api/notes/:noteId": getNote,"/share/api/notes": searchNotes,"/share/api/images/:noteId/:filename": getImage,"/share/api/attachments/:attachmentId/image/:filename": getAttachmentImage,"/share/api/attachments/:attachmentId/download": downloadAttachment,+ "/share/api/attachments/:attachmentId/all": getSiblingAttachments,+ "/share/api/attachments/:attachmentId/blob": getAttachmentBlob,+ "/share/api/tree": loadTree,"/share/": getShareRoot,"/share/:shareId": getShareNote};@@ -164,6 +170,41 @@ function getNote(req: ShareRequest): ShareReply {return jsonReply(200, note.getPojo(), noIndexHeaders(note));}+/**+ * The notes named by `?noteIds=` (comma-separated), as the rows the client's froca loads, so that an+ * app view on a shared page reads notes the page did not embed. Notes the caller cannot access are+ * left out.+ */+function loadTree(req: ShareRequest): ShareReply {+ const noteIds = String(req.query.noteIds ?? "").split(",").filter(Boolean);+ const canAccess = (note: SNote) => hasCredentialAccess(note, req);+ const notes = noteIds.flatMap((noteId) => {+ const note = shaca.getNote(noteId);+ return note ? [ note ] : [];+ });+ return jsonReply(200, buildFrocaRows(notes, canAccess));+}++function getNoteAttachments(req: ShareRequest): ShareReply {+ const note = checkNoteContentAccess(req.params.noteId ?? "", req);+ return jsonReply(200, note.getAttachments().map(getAttachmentRow));+}++function getNoteBlob(req: ShareRequest): ShareReply {+ const note = checkNoteContentAccess(req.params.noteId ?? "", req);+ return jsonReply(200, getBlobRow(note));+}++function getSiblingAttachments(req: ShareRequest): ShareReply {+ const attachment = checkAttachmentAccess(req.params.attachmentId ?? "", req);+ return jsonReply(200, attachment.note.getAttachments().map(getAttachmentRow));+}++function getAttachmentBlob(req: ShareRequest): ShareReply {+ const attachment = checkAttachmentAccess(req.params.attachmentId ?? "", req);+ return jsonReply(200, getBlobRow(attachment));+}+function downloadNote(req: ShareRequest): ShareReply {const note = checkNoteContentAccess(req.params.noteId ?? "", req);const filename = utils.formatDownloadTitle(note.title, note.type, note.mime);diff --git a/packages/trilium-core/src/share/route_paths.ts b/packages/trilium-core/src/share/route_paths.tsindex 3b51bd7ad0..88e68df7b6 100644--- a/packages/trilium-core/src/share/route_paths.ts+++ b/packages/trilium-core/src/share/route_paths.ts@@ -9,12 +9,17 @@export const SHARE_ROUTE_PATHS = ["/share/api/notes/:noteId/download","/share/api/notes/:noteId/view",+ "/share/api/notes/:noteId/attachments",+ "/share/api/notes/:noteId/blob","/share/api/notes/:noteId","/share/api/notes",// :filename is not used by trilium, but instead used for "save as" to assign a human-readable filename"/share/api/images/:noteId/:filename","/share/api/attachments/:attachmentId/image/:filename","/share/api/attachments/:attachmentId/download",+ "/share/api/attachments/:attachmentId/all",+ "/share/api/attachments/:attachmentId/blob",+ "/share/api/tree","/share/","/share/:shareId"] as const;