From 0390f30e1d8b201de1305a09c2a2262accf36ff6 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sun, 22 Mar 2026 21:56:11 -0400 Subject: [PATCH 01/17] Restore enterprise translations --- plugins/enterprise/client/translations.tsx | 112 +++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 plugins/enterprise/client/translations.tsx diff --git a/plugins/enterprise/client/translations.tsx b/plugins/enterprise/client/translations.tsx new file mode 100644 index 0000000000..ebc58a20e1 --- /dev/null +++ b/plugins/enterprise/client/translations.tsx @@ -0,0 +1,112 @@ +import { Trans } from "react-i18next"; + +export const Translations = () => ( + <> + + + + + + + + + + + + + + + + + + + + + + + + + + + + Space Settings -> Manage space -> Export space and choose to export as HTML with the "Normal Export" option.`} + /> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Glean in realtime.`} + /> + + + + priority@getoutline.com.`} + /> + + + + + +); From 0e978e1e3406fa9e164b400e2806c08a45d48e22 Mon Sep 17 00:00:00 2001 From: Apoorv Mishra Date: Mon, 23 Mar 2026 07:49:48 +0530 Subject: [PATCH 02/17] feat: highlight commented images (#11808) --- app/editor/index.tsx | 2 +- app/typings/styled-components.d.ts | 2 + shared/editor/components/Styles.ts | 4 ++ shared/editor/nodes/Image.tsx | 2 + shared/editor/plugins/CommentedImagePlugin.ts | 57 +++++++++++++++++++ shared/styles/theme.ts | 4 +- 6 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 shared/editor/plugins/CommentedImagePlugin.ts diff --git a/app/editor/index.tsx b/app/editor/index.tsx index d115975706..52d4af7fd4 100644 --- a/app/editor/index.tsx +++ b/app/editor/index.tsx @@ -944,7 +944,7 @@ const EditorContainer = styled(Styles)<{ a#comment-${props.focusedCommentId} ~ span.component-image div.image-wrapper { - outline: ${props.theme.commentMarkBackground} solid 2px; + outline: ${props.theme.commentedImageOutlineDark} solid 2px; } `} diff --git a/app/typings/styled-components.d.ts b/app/typings/styled-components.d.ts index dad22c2f4b..d844c40551 100644 --- a/app/typings/styled-components.d.ts +++ b/app/typings/styled-components.d.ts @@ -141,6 +141,8 @@ declare module "styled-components" { textDiffDeletedBackground: string; placeholder: string; commentMarkBackground: string; + commentedImageOutlineLight: string; + commentedImageOutlineDark: string; sidebarBackground: string; sidebarHoverBackground: string; sidebarActiveBackground: string; diff --git a/shared/editor/components/Styles.ts b/shared/editor/components/Styles.ts index 250132a163..8afdcf4c28 100644 --- a/shared/editor/components/Styles.ts +++ b/shared/editor/components/Styles.ts @@ -959,6 +959,10 @@ img.ProseMirror-separator { display: block; } +.image-commented .image-wrapper { + outline: ${props.theme.commentedImageOutlineLight} solid 2px; +} + // Removes forced paragraph spaces below images, this is needed to images // being inline nodes that are displayed like blocks .component-image + img.ProseMirror-separator, diff --git a/shared/editor/nodes/Image.tsx b/shared/editor/nodes/Image.tsx index 489e1d68b8..80da1b69fc 100644 --- a/shared/editor/nodes/Image.tsx +++ b/shared/editor/nodes/Image.tsx @@ -20,6 +20,7 @@ import { ImageSource } from "../lib/FileHelper"; import { DiagramPlaceholder } from "../components/DiagramPlaceholder"; import { addComment } from "../commands/comment"; import { addLink } from "../commands/link"; +import { commentedImagePlugin } from "../plugins/CommentedImagePlugin"; const imageSizeRegex = /\s=(\d+)?x(\d+)?$/; @@ -241,6 +242,7 @@ export default class Image extends SimpleImage { get plugins() { return [ ...super.plugins, + commentedImagePlugin(), new Plugin({ props: { handleKeyDown: (view, event) => { diff --git a/shared/editor/plugins/CommentedImagePlugin.ts b/shared/editor/plugins/CommentedImagePlugin.ts new file mode 100644 index 0000000000..4bc93bfcf9 --- /dev/null +++ b/shared/editor/plugins/CommentedImagePlugin.ts @@ -0,0 +1,57 @@ +import type { EditorState } from "prosemirror-state"; +import { Plugin } from "prosemirror-state"; +import { Decoration, DecorationSet } from "prosemirror-view"; + +/** + * Plugin that applies a light outline decoration to image nodes that have + * comment marks, providing a visual indicator that the image has been commented + * on. + */ +export class CommentedImagePlugin extends Plugin { + constructor() { + super({ + state: { + init: (_, state: EditorState) => ({ + decorations: this.createDecorations(state), + }), + apply: (tr, pluginState, _oldState, newState) => { + if (tr.docChanged) { + return { decorations: this.createDecorations(newState) }; + } + return pluginState; + }, + }, + props: { + decorations: (state) => { + const pluginState = this.getState(state); + return pluginState ? pluginState.decorations : null; + }, + }, + }); + } + + private createDecorations(state: EditorState) { + const decorations: Decoration[] = []; + + state.doc.descendants((node, pos) => { + if ( + node.type.name === "image" && + Array.isArray(node.attrs.marks) && + node.attrs.marks.some( + (mark: { type: string; attrs?: { resolved?: boolean } }) => + mark.type === "comment" && !mark.attrs?.resolved + ) + ) { + decorations.push( + Decoration.node(pos, pos + node.nodeSize, { + class: "image-commented", + }) + ); + } + }); + + return DecorationSet.create(state.doc, decorations); + } +} + +export const commentedImagePlugin = () => new CommentedImagePlugin(); diff --git a/shared/styles/theme.ts b/shared/styles/theme.ts index fd9e9f8b23..6fa379d895 100644 --- a/shared/styles/theme.ts +++ b/shared/styles/theme.ts @@ -71,7 +71,9 @@ const buildBaseTheme = (input: Partial) => { selected: colors.accent, textHighlight: "#FDEA9B", textHighlightForeground: colors.almostBlack, - commentMarkBackground: transparentize(0.5, "#2BC2FF"), + commentMarkBackground: transparentize(0.5, colors.brand.marine), + commentedImageOutlineDark: colors.brand.marine, + commentedImageOutlineLight: transparentize(0.7, colors.brand.marine), code: colors.lightBlack, codeComment: "#008000", codePunctuation: "#393a34", From 7dc1d12d3b285283533305bb380e1905f5e9c9e7 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Mon, 23 Mar 2026 08:08:24 -0400 Subject: [PATCH 03/17] feat: Support simplified mention syntax in markdown for MCP (#11851) * feat: Support simplified mention syntax in markdown for MCP clients Co-Authored-By: Claude Opus 4.6 * Restore translations * PR feedback --------- Co-authored-by: Claude Opus 4.6 --- server/routes/mcp/index.ts | 12 ++- server/validation.ts | 5 +- shared/editor/rules/mention.test.ts | 155 +++++++++++++++++++++++++++ shared/editor/rules/mention.ts | 44 ++++++-- shared/utils/parseMentionUrl.test.ts | 58 ++++++++++ shared/utils/parseMentionUrl.ts | 32 ++++-- 6 files changed, 288 insertions(+), 18 deletions(-) create mode 100644 shared/editor/rules/mention.test.ts create mode 100644 shared/utils/parseMentionUrl.test.ts diff --git a/server/routes/mcp/index.ts b/server/routes/mcp/index.ts index df5434e8fc..4c99f414c5 100644 --- a/server/routes/mcp/index.ts +++ b/server/routes/mcp/index.ts @@ -23,15 +23,21 @@ import { version } from "../../../package.json"; const app = new Koa(); const router = new Router(); +const defaultInstructions = `Document and collection markdown support @mentions using the syntax: @[Display Name](mention://user/userId). For example: @[John Doe](mention://user/c9a1b2e3-...). Use the list_users tool to find user IDs.`; + /** * Creates a fresh MCP server instance with tools filtered by the OAuth * scopes granted to the current token. * * @param scopes - the OAuth scopes granted to the access token. - * @param instructions - optional workspace guidance to send to clients. + * @param guidance - optional workspace guidance to append to default instructions. * @returns a configured McpServer ready to be connected to a transport. */ -function createMcpServer(scopes: string[], instructions?: string): McpServer { +function createMcpServer(scopes: string[], guidance?: string): McpServer { + const instructions = guidance + ? `${defaultInstructions}\n\n${guidance}` + : defaultInstructions; + const server = new McpServer( { name: "outline", @@ -41,7 +47,7 @@ function createMcpServer(scopes: string[], instructions?: string): McpServer { capabilities: { tools: {}, }, - ...(instructions ? { instructions } : {}), + instructions, } ); diff --git a/server/validation.ts b/server/validation.ts index 93166b50ab..ff8e18a2b9 100644 --- a/server/validation.ts +++ b/server/validation.ts @@ -251,9 +251,10 @@ export class ValidateURL { const { id, mentionType, modelId } = parseMentionUrl(url); return ( - id && - isUUID(id) && + (!id || isUUID(id)) && + !!mentionType && Object.values(MentionType).includes(mentionType as MentionType) && + !!modelId && isUUID(modelId) ); } catch (_err) { diff --git a/shared/editor/rules/mention.test.ts b/shared/editor/rules/mention.test.ts new file mode 100644 index 0000000000..5806668b68 --- /dev/null +++ b/shared/editor/rules/mention.test.ts @@ -0,0 +1,155 @@ +import markdownit from "markdown-it"; +import mentionRule from "./mention"; + +function findMentionTokens(tokens: ReturnType) { + const mentions: Array<{ + id: string | null; + type: string | null; + modelId: string | null; + label: string; + }> = []; + + for (const tok of tokens) { + if (tok.type === "inline" && tok.children) { + for (const child of tok.children) { + if (child.type === "mention") { + mentions.push({ + id: child.attrGet("id"), + type: child.attrGet("type"), + modelId: child.attrGet("modelId"), + label: child.content, + }); + } + } + } + } + + return mentions; +} + +describe("mention rule", () => { + const md = markdownit().use(mentionRule); + + describe("3-segment format (existing)", () => { + it("should parse a user mention", () => { + const result = md.parse( + "@[John Doe](mention://a1b2c3d4-e5f6-7890-abcd-ef1234567890/user/f0e1d2c3-b4a5-6789-0abc-def123456789)", + {} + ); + const mentions = findMentionTokens(result); + + expect(mentions).toHaveLength(1); + expect(mentions[0]).toEqual({ + id: "a1b2c3d4-e5f6-7890-abcd-ef1234567890", + type: "user", + modelId: "f0e1d2c3-b4a5-6789-0abc-def123456789", + label: "John Doe", + }); + }); + + it("should parse a group mention", () => { + const result = md.parse( + "@[Engineering](mention://a1b2c3d4-e5f6-7890-abcd-ef1234567890/group/f0e1d2c3-b4a5-6789-0abc-def123456789)", + {} + ); + const mentions = findMentionTokens(result); + + expect(mentions).toHaveLength(1); + expect(mentions[0].type).toBe("group"); + expect(mentions[0].label).toBe("Engineering"); + }); + }); + + describe("2-segment format (new)", () => { + it("should parse a user mention", () => { + const result = md.parse( + "@[John Doe](mention://user/f0e1d2c3-b4a5-6789-0abc-def123456789)", + {} + ); + const mentions = findMentionTokens(result); + + expect(mentions).toHaveLength(1); + expect(mentions[0].type).toBe("user"); + expect(mentions[0].modelId).toBe("f0e1d2c3-b4a5-6789-0abc-def123456789"); + expect(mentions[0].label).toBe("John Doe"); + // instanceId should be auto-generated + expect(mentions[0].id).toBeTruthy(); + expect(mentions[0].id).toMatch( + /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/ + ); + }); + + it("should parse a group mention", () => { + const result = md.parse("@[Engineering](mention://group/abc123)", {}); + const mentions = findMentionTokens(result); + + expect(mentions).toHaveLength(1); + expect(mentions[0].type).toBe("group"); + expect(mentions[0].modelId).toBe("abc123"); + expect(mentions[0].label).toBe("Engineering"); + }); + + it("should parse mention with single-word name", () => { + const result = md.parse("@[Alice](mention://user/abc123)", {}); + const mentions = findMentionTokens(result); + + expect(mentions).toHaveLength(1); + expect(mentions[0].label).toBe("Alice"); + }); + }); + + describe("mixed content", () => { + it("should parse mention within text", () => { + const result = md.parse( + "Hello @[John Doe](mention://user/abc123) please review", + {} + ); + const mentions = findMentionTokens(result); + + expect(mentions).toHaveLength(1); + expect(mentions[0].label).toBe("John Doe"); + }); + + it("should parse multiple mentions", () => { + const result = md.parse( + "@[Alice](mention://user/id1) and @[Bob](mention://user/id2)", + {} + ); + const mentions = findMentionTokens(result); + + expect(mentions).toHaveLength(2); + expect(mentions[0].label).toBe("Alice"); + expect(mentions[1].label).toBe("Bob"); + }); + + it("should parse mix of 2-segment and 3-segment mentions", () => { + const result = md.parse( + "@[Alice](mention://user/id1) and @[Bob](mention://inst-id/user/id2)", + {} + ); + const mentions = findMentionTokens(result); + + expect(mentions).toHaveLength(2); + expect(mentions[0].label).toBe("Alice"); + expect(mentions[0].id).toBeTruthy(); // auto-generated + expect(mentions[1].label).toBe("Bob"); + expect(mentions[1].id).toBe("inst-id"); + }); + }); + + describe("non-mentions", () => { + it("should not parse regular links as mentions", () => { + const result = md.parse("[John Doe](https://example.com)", {}); + const mentions = findMentionTokens(result); + + expect(mentions).toHaveLength(0); + }); + + it("should not parse links without @ prefix as mentions", () => { + const result = md.parse("[John Doe](mention://user/abc123)", {}); + const mentions = findMentionTokens(result); + + expect(mentions).toHaveLength(0); + }); + }); +}); diff --git a/shared/editor/rules/mention.ts b/shared/editor/rules/mention.ts index 8c5f15fa31..0f5ef0834f 100644 --- a/shared/editor/rules/mention.ts +++ b/shared/editor/rules/mention.ts @@ -1,5 +1,40 @@ import type { Token, StateCore } from "markdown-it"; import type MarkdownIt from "markdown-it"; +import { v4 as uuidv4 } from "uuid"; +import parseMentionUrl from "@shared/utils/parseMentionUrl"; + +/** + * Check whether a URL is a valid mention:// href. + * + * @param href the URL string to test. + * @returns true when the href is a recognised mention URL. + */ +function isMentionHref(href: string) { + const { mentionType, modelId } = parseMentionUrl(href); + return mentionType !== undefined && modelId !== undefined; +} + +/** + * Parse a mention:// href into the id, type and modelId needed by the editor. + * For 2-segment URLs (no instance id) a fresh UUID is generated. + * + * @param href the mention URL to parse. + * @returns the parsed components. + * @throws when the href is not a valid mention URL. + */ +function parseMentionHref(href: string): { + id: string; + type: string; + modelId: string; +} { + const { id, mentionType, modelId } = parseMentionUrl(href); + + if (!mentionType || !modelId) { + throw new Error(`Invalid mention href: ${href}`); + } + + return { id: id ?? uuidv4(), type: mentionType, modelId }; +} function renderMention(tokens: Token[], idx: number) { const id = tokens[idx].attrGet("id"); @@ -11,8 +46,6 @@ function renderMention(tokens: Token[], idx: number) { } function parseMentions(state: StateCore) { - const hrefRE = /^mention:\/\/([a-z0-9-]+)\/([a-z]+)\/([a-z0-9-]+)$/; - for (let i = 0; i < state.tokens.length; i++) { const tok = state.tokens[i]; if (!(tok.type === "inline" && tok.children)) { @@ -43,7 +76,7 @@ function parseMentions(state: StateCore) { // "link_open" token should have valid href const attr = openToken.attrs?.[0]; - if (!(attr && attr[0] === "href" && hrefRE.test(attr[1]))) { + if (!(attr && attr[0] === "href" && isMentionHref(attr[1]))) { return false; } @@ -57,11 +90,10 @@ function parseMentions(state: StateCore) { // remove "@" from preceding token precToken.content = precToken.content.slice(0, -1); - // href must be present, otherwise the hrefRE test in canChunkComposeMentionToken would've failed + // href must be present, otherwise the isMentionHref test would've failed // oxlint-disable-next-line @typescript-eslint/no-non-null-assertion const href = openToken.attrs![0][1]; - const matches = href.match(hrefRE); - const [id, mType, mId] = matches!.slice(1); + const { id, type: mType, modelId: mId } = parseMentionHref(href); const mentionToken = new state.Token("mention", "", 0); mentionToken.attrSet("id", id); diff --git a/shared/utils/parseMentionUrl.test.ts b/shared/utils/parseMentionUrl.test.ts new file mode 100644 index 0000000000..0e4d896f73 --- /dev/null +++ b/shared/utils/parseMentionUrl.test.ts @@ -0,0 +1,58 @@ +import parseMentionUrl from "./parseMentionUrl"; + +describe("parseMentionUrl", () => { + it("should parse 3-segment mention URL", () => { + expect( + parseMentionUrl( + "mention://9a17c1c8-d178-4350-9001-203a73070fcb/user/abc123def456" + ) + ).toEqual({ + id: "9a17c1c8-d178-4350-9001-203a73070fcb", + mentionType: "user", + modelId: "abc123def456", + }); + }); + + it("should parse 2-segment mention URL", () => { + expect(parseMentionUrl("mention://user/abc123def456")).toEqual({ + mentionType: "user", + modelId: "abc123def456", + }); + }); + + it("should parse 2-segment mention URL with UUID modelId", () => { + expect( + parseMentionUrl("mention://user/9a17c1c8-d178-4350-9001-203a73070fcb") + ).toEqual({ + mentionType: "user", + modelId: "9a17c1c8-d178-4350-9001-203a73070fcb", + }); + }); + + it("should parse group mention type", () => { + expect(parseMentionUrl("mention://group/abc123")).toEqual({ + mentionType: "group", + modelId: "abc123", + }); + }); + + it("should parse pull_request mention type with underscore", () => { + expect( + parseMentionUrl( + "mention://9a17c1c8-d178-4350-9001-203a73070fcb/pull_request/abc123" + ) + ).toEqual({ + id: "9a17c1c8-d178-4350-9001-203a73070fcb", + mentionType: "pull_request", + modelId: "abc123", + }); + }); + + it("should return empty object for invalid URL", () => { + expect(parseMentionUrl("https://example.com")).toEqual({}); + }); + + it("should return empty object for empty string", () => { + expect(parseMentionUrl("")).toEqual({}); + }); +}); diff --git a/shared/utils/parseMentionUrl.ts b/shared/utils/parseMentionUrl.ts index f4ab806074..f746f93dc4 100644 --- a/shared/utils/parseMentionUrl.ts +++ b/shared/utils/parseMentionUrl.ts @@ -1,12 +1,30 @@ -const parseMentionUrl = (url: string) => { - const matches = url.match( - /^mention:\/\/([a-z0-9-]+)\/([a-z]+)\/([a-z0-9-]+)$/ +/** + * Parse a mention:// URL into its components. + * + * Supports both the 3-segment format (mention://id/type/modelId) and the + * 2-segment format (mention://type/modelId). + * + * @param url the mention URL to parse. + * @returns the parsed components, or an empty object if the URL is invalid. + */ +const parseMentionUrl = ( + url: string +): { id?: string; mentionType?: string; modelId?: string } => { + const match3 = url.match( + /^mention:\/\/([a-z0-9-]+)\/([a-z_]+)\/([a-z0-9-]+)$/ ); - if (!matches) { - return {}; + if (match3) { + const [id, mentionType, modelId] = match3.slice(1); + return { id, mentionType, modelId }; } - const [id, mentionType, modelId] = matches.slice(1); - return { id, mentionType, modelId }; + + const match2 = url.match(/^mention:\/\/([a-z_]+)\/([a-z0-9-]+)$/); + if (match2) { + const [mentionType, modelId] = match2.slice(1); + return { mentionType, modelId }; + } + + return {}; }; export default parseMentionUrl; From 33d8e41e413270aa3093d1fb66b7b0ae62f6c22a Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Mon, 23 Mar 2026 18:53:34 -0400 Subject: [PATCH 04/17] fix: Sub-table header sticky behavior (#11857) --- shared/editor/components/Styles.ts | 14 +++++++------- shared/editor/nodes/TableView.ts | 8 +++++++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/shared/editor/components/Styles.ts b/shared/editor/components/Styles.ts index 8afdcf4c28..7a2b18b7ed 100644 --- a/shared/editor/components/Styles.ts +++ b/shared/editor/components/Styles.ts @@ -1953,11 +1953,6 @@ table { font-weight: 500; } - tr:first-child { - position: relative; - z-index: 2; - } - tr:first-child th, tr:first-child td { border-top: 0; @@ -2314,7 +2309,12 @@ table { } .${EditorStyleHelper.tableStickyHeader} { - tr:first-child th { + > .${EditorStyleHelper.tableScrollable} > table > tbody > tr:first-child { + position: relative; + z-index: 2; + } + + > .${EditorStyleHelper.tableScrollable} > table > tbody > tr:first-child > th { transform: translateY(calc(var(--header-offset, 64px) + var(--sticky-scroll-offset, 0px))); border-bottom: 1px solid ${props.theme.divider}; @@ -2330,7 +2330,7 @@ table { } } - .${EditorStyleHelper.tableGrip} { + > .${EditorStyleHelper.tableGrip} { display: none; } } diff --git a/shared/editor/nodes/TableView.ts b/shared/editor/nodes/TableView.ts index deb8e8619d..cb5e68c1c6 100644 --- a/shared/editor/nodes/TableView.ts +++ b/shared/editor/nodes/TableView.ts @@ -114,7 +114,8 @@ export class TableView extends ProsemirrorTableView { private static readonly HEADER_HEIGHT = 60; /** - * Sets up the scroll listener for sticky header behavior. + * Sets up the scroll listener for sticky header behavior. Nested tables + * (tables within another table) are excluded from sticky header behavior. */ private setupStickyHeader() { if (!isBrowser) { @@ -123,6 +124,11 @@ export class TableView extends ProsemirrorTableView { // Defer setup to ensure DOM is fully rendered setTimeout(() => { + // Skip sticky header for nested tables + if (this.dom.closest(`table .${EditorStyleHelper.table}`)) { + return; + } + this.scrollHandler = () => { this.updateStickyHeader(); }; From 84aed78ee233c44e05c249a9e4ebccf1f1646d1c Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Mon, 23 Mar 2026 18:53:37 -0400 Subject: [PATCH 05/17] fix: Improve performance when editing titles in large open document trees (#11858) --- .../Sidebar/components/DocumentLink.tsx | 43 ++++++++++--------- .../Sidebar/hooks/useCollectionDocuments.ts | 38 +++++++--------- shared/utils/collections.ts | 24 ++++++++--- shared/utils/shallowEqual.ts | 23 ++++++++++ 4 files changed, 80 insertions(+), 48 deletions(-) create mode 100644 shared/utils/shallowEqual.ts diff --git a/app/components/Sidebar/components/DocumentLink.tsx b/app/components/Sidebar/components/DocumentLink.tsx index 1b1603cedd..eaccd07cf1 100644 --- a/app/components/Sidebar/components/DocumentLink.tsx +++ b/app/components/Sidebar/components/DocumentLink.tsx @@ -265,27 +265,30 @@ function InnerDocumentLink( }; }); - const nodeChildren = React.useMemo(() => { - const insertDraftDocument = - activeDocument?.isDraft && - activeDocument?.isActive && - activeDocument?.parentDocumentId === node.id; + const insertDraftChild = !!( + activeDocument?.isDraft && + activeDocument?.isActive && + activeDocument?.parentDocumentId === node.id + ); - return collection && insertDraftDocument - ? sortNavigationNodes( - [activeDocument?.asNavigationNode, ...node.children], - collection.sort, - false - ) - : node.children; - }, [ - activeDocument?.isActive, - activeDocument?.isDraft, - activeDocument?.parentDocumentId, - activeDocument?.asNavigationNode, - collection, - node, - ]); + // Only subscribe to asNavigationNode when this node is the parent of an + // active draft. This avoids every DocumentLink observer re-rendering on + // every title keystroke. + const draftNavNode = insertDraftChild + ? activeDocument?.asNavigationNode + : undefined; + + const nodeChildren = React.useMemo( + () => + collection && draftNavNode + ? sortNavigationNodes( + [draftNavNode, ...node.children], + collection.sort, + false + ) + : node.children, + [draftNavNode, collection, node] + ); const doc = documents.get(node.id); const title = doc?.title || node.title || t("Untitled"); diff --git a/app/components/Sidebar/hooks/useCollectionDocuments.ts b/app/components/Sidebar/hooks/useCollectionDocuments.ts index 9574db3ef8..a886d4f158 100644 --- a/app/components/Sidebar/hooks/useCollectionDocuments.ts +++ b/app/components/Sidebar/hooks/useCollectionDocuments.ts @@ -7,38 +7,32 @@ export default function useCollectionDocuments( collection: Collection | undefined, activeDocument: Document | undefined ) { - const insertDraftDocument = useMemo( - () => - activeDocument && - activeDocument.isActive && - activeDocument.isDraft && - activeDocument.collectionId === collection?.id && - !activeDocument.parentDocumentId, - [ - activeDocument?.isActive, - activeDocument?.isDraft, - activeDocument?.collectionId, - activeDocument?.parentDocumentId, - collection?.id, - ] + const insertDraftDocument = !!( + activeDocument && + activeDocument.isActive && + activeDocument.isDraft && + activeDocument.collectionId === collection?.id && + !activeDocument.parentDocumentId ); + // Only subscribe to asNavigationNode when we actually need to insert a draft + // into the sorted list. This avoids every CollectionLinkChildren observer + // re-rendering on every title keystroke. + const draftNavNode = insertDraftDocument + ? activeDocument?.asNavigationNode + : undefined; + return useMemo(() => { if (!collection?.sortedDocuments) { return undefined; } - return insertDraftDocument && activeDocument + return draftNavNode ? sortNavigationNodes( - [activeDocument.asNavigationNode, ...collection.sortedDocuments], + [draftNavNode, ...collection.sortedDocuments], collection.sort, false ) : collection.sortedDocuments; - }, [ - insertDraftDocument, - activeDocument?.asNavigationNode, - collection?.sortedDocuments, - collection?.sort, - ]); + }, [draftNavNode, collection?.sortedDocuments, collection?.sort]); } diff --git a/shared/utils/collections.ts b/shared/utils/collections.ts index b153f539fb..d0d9b3c6e3 100644 --- a/shared/utils/collections.ts +++ b/shared/utils/collections.ts @@ -1,4 +1,5 @@ import type { NavigationNode } from "../types"; +import shallowEqual from "./shallowEqual"; import naturalSort from "./naturalSort"; type Sort = { @@ -21,12 +22,23 @@ export const sortNavigationNodes = ( direction: sort.direction, }); - return orderedDocs.map((node) => ({ - ...node, - children: sortChildren - ? sortNavigationNodes(node.children, sort, sortChildren) - : node.children, - })); + if (!sortChildren) { + return orderedDocs; + } + + return orderedDocs.map((node) => { + const sortedChildren = sortNavigationNodes( + node.children, + sort, + sortChildren + ); + // Preserve the original node reference if children order didn't change. + // This allows React.memo to skip re-renders of unchanged tree nodes. + if (shallowEqual(sortedChildren, node.children)) { + return node; + } + return { ...node, children: sortedChildren }; + }); }; export const colorPalette = [ diff --git a/shared/utils/shallowEqual.ts b/shared/utils/shallowEqual.ts new file mode 100644 index 0000000000..6df0cbe43a --- /dev/null +++ b/shared/utils/shallowEqual.ts @@ -0,0 +1,23 @@ +/** + * Check if two arrays have the same elements in the same order by reference. + * Uses strict equality (===) rather than deep comparison, so object identity + * is preserved — important for React.memo optimizations. + * + * @param a first array. + * @param b second array. + * @returns true if the arrays are shallowly equal. + */ +export default function shallowEqual(a: T[], b: T[]): boolean { + if (a === b) { + return true; + } + if (a.length !== b.length) { + return false; + } + for (let i = 0; i < a.length; i++) { + if (a[i] !== b[i]) { + return false; + } + } + return true; +} From f1e5a7cfa76aa0dc95a52343fcb7429967a8e4ee Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Mon, 23 Mar 2026 18:54:13 -0400 Subject: [PATCH 06/17] Fix passkey login 400 error when authenticatorAttachment is undefined (#11856) * Initial plan * Fix passkey login 400 error when authenticatorAttachment is undefined Co-authored-by: tommoor <380914+tommoor@users.noreply.github.com> Agent-Logs-Url: https://github.com/outline/outline/sessions/b7ea5777-cd06-41e7-a796-70ea083dfc34 --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: tommoor <380914+tommoor@users.noreply.github.com> --- app/scenes/Login/components/AuthenticationProvider.tsx | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/app/scenes/Login/components/AuthenticationProvider.tsx b/app/scenes/Login/components/AuthenticationProvider.tsx index 0dad1a8a98..e5c275ecc1 100644 --- a/app/scenes/Login/components/AuthenticationProvider.tsx +++ b/app/scenes/Login/components/AuthenticationProvider.tsx @@ -89,12 +89,16 @@ function AuthenticationProvider(props: Props) { // Populate hidden form fields with authentication data if (formRef.current) { - const createInputs = (obj: any, prefix = "") => { + const createInputs = (obj: Record, prefix = "") => { Object.entries(obj).forEach(([key, value]) => { + if (value === undefined || value === null) { + return; + } + const fieldName = prefix ? `${prefix}[${key}]` : key; - if (value && typeof value === "object" && !Array.isArray(value)) { - createInputs(value, fieldName); + if (typeof value === "object" && !Array.isArray(value)) { + createInputs(value as Record, fieldName); } else { // Create hidden input for primitive values const input = document.createElement("input"); From 793804cd0da6a5782d203d683158282cc8d29b5b Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Mon, 23 Mar 2026 20:21:43 -0400 Subject: [PATCH 07/17] feat: Strip comments from presentation mode (#11860) * feat: Strip comment marks from documents in presentation mode Move removeMarks to shared ProsemirrorHelper and use it to strip comment marks before rendering slides. Make server ProsemirrorHelper extend the shared class to eliminate duplication and remove SharedProsemirrorHelper imports from server code. Co-Authored-By: Claude Opus 4.6 * fix: Use Set for mark lookup and cloneDeep for browser compat Use a Set for O(1) mark lookups in removeMarks traversal. Replace structuredClone with lodash/cloneDeep to support older browsers that lack the native API. Co-Authored-By: Claude Opus 4.6 * test: Add tests for ProsemirrorHelper.removeMarks Co-Authored-By: Claude Opus 4.6 --------- Co-authored-by: Claude Opus 4.6 --- .../Document/components/PresentationMode.tsx | 14 +- server/commands/documentCreator.ts | 3 +- .../models/helpers/ProseMirrorHelper.test.ts | 3 +- server/models/helpers/ProsemirrorHelper.tsx | 34 +---- server/models/helpers/TextHelper.test.ts | 3 +- server/queues/tasks/APIImportTask.ts | 7 +- server/utils/DocumentConverter.ts | 3 +- shared/utils/ProsemirrorHelper.test.ts | 144 ++++++++++++++++++ shared/utils/ProsemirrorHelper.ts | 30 +++- 9 files changed, 196 insertions(+), 45 deletions(-) diff --git a/app/scenes/Document/components/PresentationMode.tsx b/app/scenes/Document/components/PresentationMode.tsx index 71692daff2..345a329406 100644 --- a/app/scenes/Document/components/PresentationMode.tsx +++ b/app/scenes/Document/components/PresentationMode.tsx @@ -7,7 +7,9 @@ import Icon from "@shared/components/Icon"; import { richExtensions } from "@shared/editor/nodes"; import { canUseElementFullscreen } from "@shared/utils/browser"; import { s, depths, hover } from "@shared/styles"; +import cloneDeep from "lodash/cloneDeep"; import type { ProsemirrorData } from "@shared/types"; +import { ProsemirrorHelper } from "@shared/utils/ProsemirrorHelper"; import { colorPalette } from "@shared/utils/collections"; import Editor from "~/components/Editor"; import NudeButton from "~/components/NudeButton"; @@ -130,8 +132,16 @@ function PresentationMode({ title, icon, iconColor, data, onClose }: Props) { const supportsFullscreen = React.useMemo(() => canUseElementFullscreen(), []); const isIdle = useIdle(3000, idleEvents); + const strippedData = React.useMemo( + () => + ProsemirrorHelper.removeMarks(cloneDeep(data), [ + "comment", + ]) as ProsemirrorData, + [data] + ); + const slides = React.useMemo(() => { - const result = splitIntoSlides(data, title, icon, iconColor); + const result = splitIntoSlides(strippedData, title, icon, iconColor); const contentSlides = result.filter((s) => s.type === "content"); const hasContent = contentSlides.length > 0 && @@ -144,7 +154,7 @@ function PresentationMode({ title, icon, iconColor, data, onClose }: Props) { } return result; - }, [data, title, icon, iconColor]); + }, [strippedData, title, icon, iconColor]); const totalSlides = slides.length; diff --git a/server/commands/documentCreator.ts b/server/commands/documentCreator.ts index 2bddde1640..b2937a62f9 100644 --- a/server/commands/documentCreator.ts +++ b/server/commands/documentCreator.ts @@ -1,5 +1,4 @@ import type { Optional } from "utility-types"; -import { ProsemirrorHelper as SharedProsemirrorHelper } from "@shared/utils/ProsemirrorHelper"; import { TextHelper } from "@shared/utils/TextHelper"; import { Document, type Template } from "@server/models"; import { DocumentHelper } from "@server/models/helpers/DocumentHelper"; @@ -94,7 +93,7 @@ export default async function documentCreator( : text ? ProsemirrorHelper.toProsemirror(text).toJSON() : template - ? SharedProsemirrorHelper.replaceTemplateVariables( + ? ProsemirrorHelper.replaceTemplateVariables( await DocumentHelper.toJSON(template), user ) diff --git a/server/models/helpers/ProseMirrorHelper.test.ts b/server/models/helpers/ProseMirrorHelper.test.ts index f3668b665e..0dfb697dc8 100644 --- a/server/models/helpers/ProseMirrorHelper.test.ts +++ b/server/models/helpers/ProseMirrorHelper.test.ts @@ -2,7 +2,6 @@ import { faker } from "@faker-js/faker"; import type { DeepPartial } from "utility-types"; import type { ProsemirrorData } from "@shared/types"; import { MentionType } from "@shared/types"; -import { ProsemirrorHelper as SharedProsemirrorHelper } from "@shared/utils/ProsemirrorHelper"; import { createContext } from "@server/context"; import { buildProseMirrorDoc, buildUser } from "@server/test/factories"; import type { MentionAttrs } from "./ProsemirrorHelper"; @@ -973,7 +972,7 @@ describe("ProsemirrorHelper", () => { }, ]); - const images = SharedProsemirrorHelper.getImages(doc); + const images = ProsemirrorHelper.getImages(doc); expect(images.length).toBe(1); expect(images[0].attrs.src).toBe("https://example.com/image.png"); expect(images[0].attrs.alt).toBe("Test image"); diff --git a/server/models/helpers/ProsemirrorHelper.tsx b/server/models/helpers/ProsemirrorHelper.tsx index 238cf55513..f067dc7c5f 100644 --- a/server/models/helpers/ProsemirrorHelper.tsx +++ b/server/models/helpers/ProsemirrorHelper.tsx @@ -21,6 +21,7 @@ import { attachmentRedirectRegex, ProsemirrorHelper as SharedProsemirrorHelper, } from "@shared/utils/ProsemirrorHelper"; + import parseDocumentSlug from "@shared/utils/parseDocumentSlug"; import { isRTL } from "@shared/utils/rtl"; import { isInternalUrl } from "@shared/utils/urls"; @@ -62,7 +63,7 @@ export type MentionAttrs = { }; @trace() -export class ProsemirrorHelper { +export class ProsemirrorHelper extends SharedProsemirrorHelper { /** * Returns the input text as a Y.Doc. * @@ -255,33 +256,6 @@ export class ProsemirrorHelper { return blockNode ? doc.copy(Fragment.fromArray([blockNode])) : undefined; } - /** - * Removes all marks from the node that match the given types. - * - * @param data The ProsemirrorData object to remove marks from - * @param marks The mark types to remove - * @returns The content with marks removed - */ - static removeMarks(doc: Node | ProsemirrorData, marks: string[]) { - const json = "toJSON" in doc ? (doc.toJSON() as ProsemirrorData) : doc; - - function removeMarksInner(node: ProsemirrorData) { - if (node.marks) { - node.marks = node.marks.filter((mark) => !marks.includes(mark.type)); - } - if (node.attrs?.marks) { - node.attrs.marks = (node.attrs.marks as { type: string }[])?.filter( - (mark) => !marks.includes(mark.type) - ); - } - if (node.content) { - node.content.forEach(removeMarksInner); - } - return node; - } - return removeMarksInner(json); - } - static async replaceInternalUrls( doc: Node | ProsemirrorData, basePath: string @@ -875,8 +849,8 @@ export class ProsemirrorHelper { doc: Node, user: User ): Promise { - const images = SharedProsemirrorHelper.getImages(doc); - const videos = SharedProsemirrorHelper.getVideos(doc); + const images = ProsemirrorHelper.getImages(doc); + const videos = ProsemirrorHelper.getVideos(doc); const nodes = [...images, ...videos]; if (!nodes.length) { diff --git a/server/models/helpers/TextHelper.test.ts b/server/models/helpers/TextHelper.test.ts index 1105392292..4be7e6f509 100644 --- a/server/models/helpers/TextHelper.test.ts +++ b/server/models/helpers/TextHelper.test.ts @@ -1,4 +1,3 @@ -import { ProsemirrorHelper as SharedProsemirrorHelper } from "@shared/utils/ProsemirrorHelper"; import { createContext } from "@server/context"; import { buildProseMirrorDoc, buildUser } from "@server/test/factories"; import { ProsemirrorHelper } from "./ProsemirrorHelper"; @@ -43,7 +42,7 @@ describe("ProsemirrorHelper", () => { }, ]); - const images = SharedProsemirrorHelper.getImages(doc); + const images = ProsemirrorHelper.getImages(doc); expect(images.length).toBe(1); expect(images[0].attrs.src).toBe("https://example.com/image.png"); expect(images[0].attrs.alt).toBe("Test image"); diff --git a/server/queues/tasks/APIImportTask.ts b/server/queues/tasks/APIImportTask.ts index 7d1b0ab9c9..334347199e 100644 --- a/server/queues/tasks/APIImportTask.ts +++ b/server/queues/tasks/APIImportTask.ts @@ -13,7 +13,6 @@ import type { ProsemirrorDoc, } from "@shared/types"; import { AttachmentPreset, ImportState, ImportTaskState } from "@shared/types"; -import { ProsemirrorHelper as SharedProseMirrorHelper } from "@shared/utils/ProsemirrorHelper"; import { createContext } from "@server/context"; import { schema } from "@server/editor"; import Logger from "@server/logging/Logger"; @@ -262,9 +261,9 @@ export default abstract class APIImportTask< }): Promise { const docNode = ProsemirrorHelper.toProsemirror(doc); const nodes = [ - ...SharedProseMirrorHelper.getImages(docNode), - ...SharedProseMirrorHelper.getVideos(docNode), - ...SharedProseMirrorHelper.getAttachments(docNode), + ...ProsemirrorHelper.getImages(docNode), + ...ProsemirrorHelper.getVideos(docNode), + ...ProsemirrorHelper.getAttachments(docNode), ]; if (!nodes.length) { diff --git a/server/utils/DocumentConverter.ts b/server/utils/DocumentConverter.ts index 1e9ab3662f..91ac6cf9fc 100644 --- a/server/utils/DocumentConverter.ts +++ b/server/utils/DocumentConverter.ts @@ -6,7 +6,6 @@ import mammoth from "mammoth"; import type { Node } from "prosemirror-model"; import { DOMParser as ProsemirrorDOMParser } from "prosemirror-model"; import yaml from "js-yaml"; -import { ProsemirrorHelper as SharedProsemirrorHelper } from "@shared/utils/ProsemirrorHelper"; import { schema, serializer } from "@server/editor"; import { FileImportError } from "@server/errors"; import { trace, traceFunction } from "@server/logging/tracing"; @@ -55,7 +54,7 @@ export class DocumentConverter { // Extract title from first H1 heading let title = ""; - const headings = SharedProsemirrorHelper.getHeadings(doc); + const headings = ProsemirrorHelper.getHeadings(doc); if (headings.length > 0 && headings[0].level === 1) { title = headings[0].title; doc = ProsemirrorHelper.removeFirstHeading(doc); diff --git a/shared/utils/ProsemirrorHelper.test.ts b/shared/utils/ProsemirrorHelper.test.ts index 33c295ec8a..e9f0759353 100644 --- a/shared/utils/ProsemirrorHelper.test.ts +++ b/shared/utils/ProsemirrorHelper.test.ts @@ -234,4 +234,148 @@ describe("ProsemirrorHelper", () => { ]); }); }); + + describe("removeMarks", () => { + it("should remove specified mark types from text nodes", () => { + const doc: ProsemirrorData = { + type: "doc", + content: [ + { + type: "paragraph", + content: [ + { + type: "text", + text: "hello", + marks: [ + { type: "comment", attrs: { id: "c1" } }, + { type: "bold" }, + ], + }, + ], + }, + ], + }; + + const result = ProsemirrorHelper.removeMarks(doc, ["comment"]); + expect(result.content![0].content![0].marks).toEqual([{ type: "bold" }]); + }); + + it("should remove marks from nested content", () => { + const doc: ProsemirrorData = { + type: "doc", + content: [ + { + type: "blockquote", + content: [ + { + type: "paragraph", + content: [ + { + type: "text", + text: "nested", + marks: [{ type: "comment", attrs: { id: "c1" } }], + }, + ], + }, + ], + }, + ], + }; + + const result = ProsemirrorHelper.removeMarks(doc, ["comment"]); + expect(result.content![0].content![0].content![0].marks).toEqual([]); + }); + + it("should remove marks from node attrs.marks", () => { + const doc: ProsemirrorData = { + type: "doc", + content: [ + { + type: "image", + attrs: { + src: "test.png", + marks: [ + { type: "comment", attrs: { id: "c1" } }, + { type: "link", attrs: { href: "url" } }, + ], + }, + }, + ], + }; + + const result = ProsemirrorHelper.removeMarks(doc, ["comment"]); + expect(result.content![0].attrs!.marks).toEqual([ + { type: "link", attrs: { href: "url" } }, + ]); + }); + + it("should remove multiple mark types at once", () => { + const doc: ProsemirrorData = { + type: "doc", + content: [ + { + type: "paragraph", + content: [ + { + type: "text", + text: "hello", + marks: [ + { type: "comment", attrs: { id: "c1" } }, + { type: "bold" }, + { type: "highlight" }, + ], + }, + ], + }, + ], + }; + + const result = ProsemirrorHelper.removeMarks(doc, [ + "comment", + "highlight", + ]); + expect(result.content![0].content![0].marks).toEqual([{ type: "bold" }]); + }); + + it("should leave nodes unchanged when no marks match", () => { + const doc: ProsemirrorData = { + type: "doc", + content: [ + { + type: "paragraph", + content: [ + { + type: "text", + text: "hello", + marks: [{ type: "bold" }], + }, + ], + }, + ], + }; + + const result = ProsemirrorHelper.removeMarks(doc, ["comment"]); + expect(result.content![0].content![0].marks).toEqual([{ type: "bold" }]); + }); + + it("should handle nodes with no marks", () => { + const doc: ProsemirrorData = { + type: "doc", + content: [ + { + type: "paragraph", + content: [ + { + type: "text", + text: "plain", + }, + ], + }, + ], + }; + + const result = ProsemirrorHelper.removeMarks(doc, ["comment"]); + expect(result.content![0].content![0].marks).toBeUndefined(); + }); + }); }); diff --git a/shared/utils/ProsemirrorHelper.ts b/shared/utils/ProsemirrorHelper.ts index d452fcfa9b..bb502c5d42 100644 --- a/shared/utils/ProsemirrorHelper.ts +++ b/shared/utils/ProsemirrorHelper.ts @@ -48,10 +48,38 @@ export const attachmentPublicRegex = /public\/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\/(?[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/gi; export class ProsemirrorHelper { + /** + * Remove specific mark types from all nodes in the document. + * + * @param doc the prosemirror document or JSON data. + * @param marks the mark type names to remove. + * @returns the document data with specified marks removed. + */ + static removeMarks(doc: Node | ProsemirrorData, marks: string[]) { + const json = "toJSON" in doc ? (doc.toJSON() as ProsemirrorData) : doc; + const markSet = new Set(marks); + + function removeMarksInner(node: ProsemirrorData) { + if (node.marks) { + node.marks = node.marks.filter((mark) => !markSet.has(mark.type)); + } + if (node.attrs?.marks) { + node.attrs.marks = (node.attrs.marks as { type: string }[])?.filter( + (mark) => !markSet.has(mark.type) + ); + } + if (node.content) { + node.content.forEach(removeMarksInner); + } + return node; + } + return removeMarksInner(json); + } + /** * Get a new empty document. * - * @returns A new empty document as JSON. + * @returns a new empty document as JSON. */ static getEmptyDocument(): ProsemirrorData { return { From c2ccdb6fd4d3341780425cfde0d9fbbbd338d382 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Tue, 24 Mar 2026 22:54:43 -0400 Subject: [PATCH 08/17] fix: Prevent registration of duplicate passkeys on the same device (#11870) Co-authored-by: Claude Opus 4.6 --- plugins/passkeys/server/auth/passkeys.ts | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/plugins/passkeys/server/auth/passkeys.ts b/plugins/passkeys/server/auth/passkeys.ts index 56e928c305..c7b4fd4405 100644 --- a/plugins/passkeys/server/auth/passkeys.ts +++ b/plugins/passkeys/server/auth/passkeys.ts @@ -95,12 +95,20 @@ router.post( const { user } = ctx.state.auth; authorize(user, "createUserPasskey", user.team); + // Fetch existing passkeys to exclude them from registration + const existingPasskeys = await UserPasskey.findAll({ + where: { userId: user.id }, + }); + const options = await generateRegistrationOptions({ rpName, rpID: getRpID(ctx), userID: isoBase64URL.toBuffer(user.id), userName: user.email || user.name, - // Don't exclude credentials, so we can detect if one is already registered (optional) + excludeCredentials: existingPasskeys.map((pk) => ({ + id: pk.credentialId, + transports: pk.transports as AuthenticatorTransportFuture[], + })), authenticatorSelection: { residentKey: "preferred", userVerification: "preferred", @@ -154,6 +162,7 @@ router.post( } const { verified, registrationInfo } = verification; + const ZERO_AAGUID = "00000000-0000-0000-0000-000000000000"; if (verified && registrationInfo) { const { credential, aaguid } = registrationInfo; @@ -166,7 +175,7 @@ router.post( const userAgent = ctx.request.get("user-agent"); const transports = body.response.transports || []; - // Check if already exists + // Check if already exists by credential ID const existing = await UserPasskey.findOne({ where: { credentialId: credentialIdBase64 }, }); @@ -183,6 +192,17 @@ router.post( aaguid, }); } else { + // Check if user already has a passkey from the same authenticator + if (aaguid && aaguid !== ZERO_AAGUID) { + const duplicateDevice = await UserPasskey.findOne({ + where: { userId: user.id, aaguid }, + }); + + if (duplicateDevice) { + throw ValidationError("You already have a passkey on this device"); + } + } + await UserPasskey.createWithCtx(ctx, { userId: user.id, credentialId: credentialIdBase64, From 979d9a412d7b1428cd42bef8a92fd1767c375928 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Wed, 25 Mar 2026 22:59:57 -0400 Subject: [PATCH 09/17] Mermaid improvements (#11874) * fix: Upgrade mermaid to 11.13.0 Includes a fix for incorrect viewBox casing in Radar and Packet diagram renderers (mermaid-js/mermaid#7076) and other improvements. Co-Authored-By: Claude Opus 4.6 * fix: Use visibility:hidden for mermaid rendering element Instead of positioning the temporary render element offscreen at -9999px, use visibility:hidden with position:fixed so the browser computes correct bounding boxes for SVG elements. Offscreen elements can produce incorrect getBBox() results, leading to wrong viewBox dimensions and diagrams rendering too big or too small. Fixes #11782 Co-Authored-By: Claude Opus 4.6 * Add session storage for generated diagrams to reduce relayout * fix: Use LRU eviction for mermaid sessionStorage cache Track access order via a dedicated LRU index key so the cache evicts least-recently-used entries rather than arbitrary ones. Co-Authored-By: Claude Opus 4.6 --------- Co-authored-by: Claude Opus 4.6 --- package.json | 2 +- shared/editor/extensions/Mermaid.ts | 84 ++++++++++-- shared/utils/string.test.ts | 19 +++ shared/utils/string.ts | 14 ++ yarn.lock | 192 +++++++++++++++------------- 5 files changed, 208 insertions(+), 103 deletions(-) create mode 100644 shared/utils/string.test.ts diff --git a/package.json b/package.json index d45090b30a..ee169b0bc7 100644 --- a/package.json +++ b/package.json @@ -169,7 +169,7 @@ "markdown-it": "^14.1.0", "markdown-it-container": "^3.0.0", "markdown-it-emoji": "^3.0.0", - "mermaid": "11.12.1", + "mermaid": "11.13.0", "mime-types": "^3.0.1", "mobx": "^4.15.4", "mobx-react": "^6.3.1", diff --git a/shared/editor/extensions/Mermaid.ts b/shared/editor/extensions/Mermaid.ts index 1361af6513..af93e10365 100644 --- a/shared/editor/extensions/Mermaid.ts +++ b/shared/editor/extensions/Mermaid.ts @@ -15,6 +15,7 @@ import { findParentNode } from "../queries/findParentNode"; import type { NodeWithPos } from "../types"; import type { Editor } from "../../../app/editor"; import { LightboxImageFactory } from "../lib/Lightbox"; +import { hashString } from "../../utils/string"; import { sanitizeUrl } from "../../utils/urls"; export const pluginKey = new PluginKey("mermaid"); @@ -25,21 +26,72 @@ export type MermaidState = { editingId?: string; }; +const STORAGE_PREFIX = "mermaid:"; +const MAX_STORAGE_ENTRIES = 20; + class Cache { - static get(key: string) { - return this.data.get(key); + /** Get a cached SVG by diagram text and theme. */ + static get(key: string): string | undefined { + try { + const hash = hashString(key); + const value = sessionStorage.getItem(STORAGE_PREFIX + hash); + if (value) { + this.touchLru(hash); + return value; + } + } catch { + // sessionStorage unavailable + } + return undefined; } + /** Cache a rendered SVG in sessionStorage. */ static set(key: string, value: string) { - this.data.set(key, value); - - if (this.data.size > this.maxSize) { - this.data.delete(this.data.keys().next().value); + try { + const hash = hashString(key); + this.touchLru(hash); + this.pruneStorage(); + sessionStorage.setItem(STORAGE_PREFIX + hash, value); + } catch { + // sessionStorage full or unavailable } } - private static maxSize = 20; - private static data: Map = new Map(); + /** Move or append a hash to the end (most recent) of the LRU list. */ + private static touchLru(hash: string) { + const lru = this.getLru(); + const idx = lru.indexOf(hash); + if (idx !== -1) { + lru.splice(idx, 1); + } + lru.push(hash); + sessionStorage.setItem(STORAGE_PREFIX + "lru", JSON.stringify(lru)); + } + + /** Evict least-recently-used entries when over the limit. */ + private static pruneStorage() { + const lru = this.getLru(); + + while (lru.length > MAX_STORAGE_ENTRIES) { + const evict = lru.shift()!; + sessionStorage.removeItem(STORAGE_PREFIX + evict); + } + + sessionStorage.setItem(STORAGE_PREFIX + "lru", JSON.stringify(lru)); + } + + /** Read the LRU order list from sessionStorage. */ + private static getLru(): string[] { + try { + const raw = sessionStorage.getItem(STORAGE_PREFIX + "lru"); + if (raw) { + return JSON.parse(raw); + } + } catch { + // corrupted or unavailable + } + return []; + } } let mermaid: typeof MermaidUnsafe; @@ -104,16 +156,20 @@ class MermaidRenderer { return; } - // Create a temporary element that will render the diagram off-screen. This is necessary - // as Mermaid will error if the element is not visible or the element is removed while the - // diagram is being rendered. + // Create a temporary element for rendering. We use visibility:hidden instead of + // offscreen positioning so the browser computes correct bounding boxes for SVG + // elements — offscreen elements can produce incorrect getBBox() results, leading + // to wrong viewBox dimensions (see mermaid-js/mermaid#6146). const renderElement = document.createElement("div"); const tempId = "offscreen-mermaid-" + Math.random().toString(36).substr(2, 9); renderElement.id = tempId; - renderElement.style.position = "absolute"; - renderElement.style.left = "-9999px"; - renderElement.style.top = "-9999px"; + renderElement.style.position = "fixed"; + renderElement.style.visibility = "hidden"; + renderElement.style.top = "0"; + renderElement.style.left = "0"; + renderElement.style.width = "100%"; + renderElement.style.zIndex = "-1"; document.body.appendChild(renderElement); try { diff --git a/shared/utils/string.test.ts b/shared/utils/string.test.ts new file mode 100644 index 0000000000..f9b84606ac --- /dev/null +++ b/shared/utils/string.test.ts @@ -0,0 +1,19 @@ +import { hashString } from "./string"; + +describe("hashString", () => { + it("returns a hex string", () => { + expect(hashString("hello")).toMatch(/^[0-9a-f]+$/); + }); + + it("returns consistent results for the same input", () => { + expect(hashString("test")).toBe(hashString("test")); + }); + + it("returns different hashes for different inputs", () => { + expect(hashString("abc")).not.toBe(hashString("def")); + }); + + it("handles empty string", () => { + expect(hashString("")).toMatch(/^[0-9a-f]+$/); + }); +}); diff --git a/shared/utils/string.ts b/shared/utils/string.ts index 3079bb681c..ef5ae9c614 100644 --- a/shared/utils/string.ts +++ b/shared/utils/string.ts @@ -1,3 +1,17 @@ +/** + * Simple string hash using the djb2 algorithm, returns a hex string. + * + * @param str the string to hash. + * @returns a hex-encoded 32-bit hash. + */ +export function hashString(str: string): string { + let hash = 5381; + for (let i = 0; i < str.length; i++) { + hash = ((hash << 5) + hash + str.charCodeAt(i)) | 0; + } + return (hash >>> 0).toString(16); +} + /** * Returns the index of the first occurrence of a substring in a string that matches a regular expression. * diff --git a/yarn.lock b/yarn.lock index ae5b33ff25..65fc9a9c27 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2897,45 +2897,45 @@ __metadata: languageName: node linkType: hard -"@chevrotain/cst-dts-gen@npm:11.0.3": - version: 11.0.3 - resolution: "@chevrotain/cst-dts-gen@npm:11.0.3" +"@chevrotain/cst-dts-gen@npm:11.1.2": + version: 11.1.2 + resolution: "@chevrotain/cst-dts-gen@npm:11.1.2" dependencies: - "@chevrotain/gast": "npm:11.0.3" - "@chevrotain/types": "npm:11.0.3" - lodash-es: "npm:4.17.21" - checksum: 10c0/9e945a0611386e4e08af34c2d0b3af36c1af08f726b58145f11310f2aeafcb2d65264c06ec65a32df6b6a65771e6a55be70580c853afe3ceb51487e506967104 + "@chevrotain/gast": "npm:11.1.2" + "@chevrotain/types": "npm:11.1.2" + lodash-es: "npm:4.17.23" + checksum: 10c0/372a9573a404a1d717c92875024588a53d1eb078f12d2cd1d79d9c2c888c9b429eb62bbc85501b8ff168c14b22a675da99d97bb39b0a774e9fee000dc60fd8ff languageName: node linkType: hard -"@chevrotain/gast@npm:11.0.3": - version: 11.0.3 - resolution: "@chevrotain/gast@npm:11.0.3" +"@chevrotain/gast@npm:11.1.2": + version: 11.1.2 + resolution: "@chevrotain/gast@npm:11.1.2" dependencies: - "@chevrotain/types": "npm:11.0.3" - lodash-es: "npm:4.17.21" - checksum: 10c0/54fc44d7b4a7b0323f49d957dd88ad44504922d30cb226d93b430b0e09925efe44e0726068581d777f423fabfb878a2238ed2c87b690c0c0014ebd12b6968354 + "@chevrotain/types": "npm:11.1.2" + lodash-es: "npm:4.17.23" + checksum: 10c0/540bfc9270d752f398b29efe9c89bb907d2984a47db4308a943e50c1cbd261ee13ecbef15c0e07808cf476d835bc36e65854db0bd214c277296cc14013eca8f4 languageName: node linkType: hard -"@chevrotain/regexp-to-ast@npm:11.0.3": - version: 11.0.3 - resolution: "@chevrotain/regexp-to-ast@npm:11.0.3" - checksum: 10c0/6939c5c94fbfb8c559a4a37a283af5ded8e6147b184a7d7bcf5ad1404d9d663c78d81602bd8ea8458ec497358a9e1671541099c511835d0be2cad46f00c62b3f +"@chevrotain/regexp-to-ast@npm:11.1.2": + version: 11.1.2 + resolution: "@chevrotain/regexp-to-ast@npm:11.1.2" + checksum: 10c0/645f02ac94cb33e04c10547b197762c6936c7b7668966240684795ce131cb515a76b63e0cc500e787d669f1a36bd2f902ec518f27843ec857ecf9043c717527e languageName: node linkType: hard -"@chevrotain/types@npm:11.0.3": - version: 11.0.3 - resolution: "@chevrotain/types@npm:11.0.3" - checksum: 10c0/72fe8f0010ebef848e47faea14a88c6fdc3cdbafaef6b13df4a18c7d33249b1b675e37b05cb90a421700c7016dae7cd4187ab6b549e176a81cea434f69cd2503 +"@chevrotain/types@npm:11.1.2": + version: 11.1.2 + resolution: "@chevrotain/types@npm:11.1.2" + checksum: 10c0/c0c4679a3d407df34e18d5adfa7ac599b4a2bfddbf68da6e43678b9b3e16ab911de7766b37b9fc466261c3dead3db1b620e2e344f800fa9f0f381720475eda8f languageName: node linkType: hard -"@chevrotain/utils@npm:11.0.3": - version: 11.0.3 - resolution: "@chevrotain/utils@npm:11.0.3" - checksum: 10c0/b31972d1b2d444eef1499cf9b7576fc1793e8544910de33a3c18e07c270cfad88067f175d0ee63e7bc604713ebed647f8190db45cc8311852cd2d4fe2ef14068 +"@chevrotain/utils@npm:11.1.2": + version: 11.1.2 + resolution: "@chevrotain/utils@npm:11.1.2" + checksum: 10c0/72989e7051781b9084252486712844c55e3b454318c7da4a5f6ded28dcd3947ba2882773a6bf09b7e744599e8c8025df8d3de0d487121734e6edb66999450438 languageName: node linkType: hard @@ -3666,7 +3666,7 @@ __metadata: languageName: node linkType: hard -"@iconify/utils@npm:^3.0.1": +"@iconify/utils@npm:^3.0.2": version: 3.1.0 resolution: "@iconify/utils@npm:3.1.0" dependencies: @@ -4389,12 +4389,12 @@ __metadata: languageName: node linkType: hard -"@mermaid-js/parser@npm:^0.6.3": - version: 0.6.3 - resolution: "@mermaid-js/parser@npm:0.6.3" +"@mermaid-js/parser@npm:^1.0.1": + version: 1.0.1 + resolution: "@mermaid-js/parser@npm:1.0.1" dependencies: - langium: "npm:3.3.1" - checksum: 10c0/9711174ff31f32d93c8da03ed6b1a1380f5ccfb27ffcdfaf42236da4b381aa0602752b3afc7893582d5ccdfc79b0465c69afe963b825328049575831f4ddd28e + langium: "npm:^4.0.0" + checksum: 10c0/34231cb63412ddbb3c8c6dba6366b958f98b66293b2e5748d3bd6c286acde47862a6ac86b2f6a33e7b5dee6a1895035af06ee48863c787bc20415b89fe77d705 languageName: node linkType: hard @@ -9039,6 +9039,21 @@ __metadata: languageName: node linkType: hard +"@upsetjs/venn.js@npm:^2.0.0": + version: 2.0.0 + resolution: "@upsetjs/venn.js@npm:2.0.0" + dependencies: + d3-selection: "npm:^3.0.0" + d3-transition: "npm:^3.0.1" + dependenciesMeta: + d3-selection: + optional: true + d3-transition: + optional: true + checksum: 10c0/b12014d94708ab4df7f5a4b6205c6f23ff235cca2ffe91df3314862b109b826e52f9020c2a2f7527d3712d21c578d6db9cdb60ce46a528739cc18e58d111f724 + languageName: node + linkType: hard + "@vitejs/plugin-react-oxc@npm:^0.2.3": version: 0.2.3 resolution: "@vitejs/plugin-react-oxc@npm:0.2.3" @@ -10288,7 +10303,7 @@ __metadata: languageName: node linkType: hard -"chevrotain-allstar@npm:~0.3.0": +"chevrotain-allstar@npm:~0.3.1": version: 0.3.1 resolution: "chevrotain-allstar@npm:0.3.1" dependencies: @@ -10299,17 +10314,17 @@ __metadata: languageName: node linkType: hard -"chevrotain@npm:~11.0.3": - version: 11.0.3 - resolution: "chevrotain@npm:11.0.3" +"chevrotain@npm:~11.1.1": + version: 11.1.2 + resolution: "chevrotain@npm:11.1.2" dependencies: - "@chevrotain/cst-dts-gen": "npm:11.0.3" - "@chevrotain/gast": "npm:11.0.3" - "@chevrotain/regexp-to-ast": "npm:11.0.3" - "@chevrotain/types": "npm:11.0.3" - "@chevrotain/utils": "npm:11.0.3" - lodash-es: "npm:4.17.21" - checksum: 10c0/ffd425fa321e3f17e9833d7f44cd39f2743f066e92ca74b226176080ca5d455f853fe9091cdfd86354bd899d85c08b3bdc3f55b267e7d07124b048a88349765f + "@chevrotain/cst-dts-gen": "npm:11.1.2" + "@chevrotain/gast": "npm:11.1.2" + "@chevrotain/regexp-to-ast": "npm:11.1.2" + "@chevrotain/types": "npm:11.1.2" + "@chevrotain/utils": "npm:11.1.2" + lodash-es: "npm:4.17.23" + checksum: 10c0/7f0b5780035c582d4c620c81e1fbb58c9f41a69f1c7efdae96819c7bc0928ddb4f046bb8239e71539f383b3b8ce460bd11f44b5fb5107e1d45a0cc91bd6a4198 languageName: node linkType: hard @@ -11074,7 +11089,7 @@ __metadata: languageName: node linkType: hard -"cytoscape@npm:^3.29.3": +"cytoscape@npm:^3.33.1": version: 3.33.1 resolution: "cytoscape@npm:3.33.1" checksum: 10c0/dffcf5f74df4d91517c4faf394df880d8283ce76edef19edba0c762941cf4f18daf7c4c955ec50c794f476ace39ad4394f8c98483222bd2682e1fd206e976411 @@ -11318,7 +11333,7 @@ __metadata: languageName: node linkType: hard -"d3-selection@npm:2 - 3, d3-selection@npm:3": +"d3-selection@npm:2 - 3, d3-selection@npm:3, d3-selection@npm:^3.0.0": version: 3.0.0 resolution: "d3-selection@npm:3.0.0" checksum: 10c0/e59096bbe8f0cb0daa1001d9bdd6dbc93a688019abc97d1d8b37f85cd3c286a6875b22adea0931b0c88410d025563e1643019161a883c516acf50c190a11b56b @@ -11368,7 +11383,7 @@ __metadata: languageName: node linkType: hard -"d3-transition@npm:2 - 3, d3-transition@npm:3": +"d3-transition@npm:2 - 3, d3-transition@npm:3, d3-transition@npm:^3.0.1": version: 3.0.1 resolution: "d3-transition@npm:3.0.1" dependencies: @@ -11434,13 +11449,13 @@ __metadata: languageName: node linkType: hard -"dagre-d3-es@npm:7.0.13": - version: 7.0.13 - resolution: "dagre-d3-es@npm:7.0.13" +"dagre-d3-es@npm:7.0.14": + version: 7.0.14 + resolution: "dagre-d3-es@npm:7.0.14" dependencies: d3: "npm:^7.9.0" lodash-es: "npm:^4.17.21" - checksum: 10c0/4eca80dbbad4075311e3853930f99486024785b54210541796d4216140d91744738ee51125e2692c3532af148fbc2e690171750583916ed2ad553150abb198c7 + checksum: 10c0/0dc91fc79300eb0a4eab5a48a76c2baf3ce439c389d19e2f015729bb57dafd75e1e9a4c2880daf016e81ee45caca7b21745c13b23b6cd2a786ce84767e88323e languageName: node linkType: hard @@ -11515,10 +11530,10 @@ __metadata: languageName: node linkType: hard -"dayjs@npm:^1.11.18": - version: 1.11.19 - resolution: "dayjs@npm:1.11.19" - checksum: 10c0/7d8a6074a343f821f81ea284d700bd34ea6c7abbe8d93bce7aba818948957c1b7f56131702e5e890a5622cdfc05dcebe8aed0b8313bdc6838a594d7846b0b000 +"dayjs@npm:^1.11.19": + version: 1.11.20 + resolution: "dayjs@npm:1.11.20" + checksum: 10c0/8af525e2aa100c8db9923d706c42b2b2d30579faf89456619413a5c10916efc92c2b166e193c27c02eb3174b30aa440ee1e7b72b0a2876b3da651d204db848a0 languageName: node linkType: hard @@ -11833,15 +11848,15 @@ __metadata: languageName: node linkType: hard -"dompurify@npm:^3.2.5": - version: 3.3.1 - resolution: "dompurify@npm:3.3.1" +"dompurify@npm:^3.3.1": + version: 3.3.3 + resolution: "dompurify@npm:3.3.3" dependencies: "@types/trusted-types": "npm:^2.0.7" dependenciesMeta: "@types/trusted-types": optional: true - checksum: 10c0/fa0a8c55a436ba0d54389195e3d2337e311f56de709a2fc9efc98dbbc7746fa53bb4b74b6ac043b77a279a8f2ebd8685f0ebaa6e58c9e32e92051d529bc0baf8 + checksum: 10c0/097c14a21a3f6cb95beded9ecd255f7c3512c42767b048390c747b0fe35736f6a71e02320fc50a9ac2be645834b463e4760915d595d502a56452daf339d0ea9c languageName: node linkType: hard @@ -15645,7 +15660,7 @@ __metadata: languageName: node linkType: hard -"katex@npm:^0.16.22, katex@npm:^0.16.25": +"katex@npm:^0.16.25": version: 0.16.27 resolution: "katex@npm:0.16.27" dependencies: @@ -15889,16 +15904,16 @@ __metadata: languageName: node linkType: hard -"langium@npm:3.3.1": - version: 3.3.1 - resolution: "langium@npm:3.3.1" +"langium@npm:^4.0.0": + version: 4.2.1 + resolution: "langium@npm:4.2.1" dependencies: - chevrotain: "npm:~11.0.3" - chevrotain-allstar: "npm:~0.3.0" + chevrotain: "npm:~11.1.1" + chevrotain-allstar: "npm:~0.3.1" vscode-languageserver: "npm:~9.0.1" vscode-languageserver-textdocument: "npm:~1.0.11" - vscode-uri: "npm:~3.0.8" - checksum: 10c0/0c54803068addb0f7c16a57fdb2db2e5d4d9a21259d477c3c7d0587c2c2f65a313f9eeef3c95ac1c2e41cd11d4f2eaf620d2c03fe839a3350ffee59d2b4c7647 + vscode-uri: "npm:~3.1.0" + checksum: 10c0/19ddf79cc3c435ec70f8eb50de255571711db7cea89d171cf80bc97e7ed73d4d0bb6b4215899df8369fa6d0e17f442f30af4ec2e9657041bf2f93be1310ba50a languageName: node linkType: hard @@ -16225,10 +16240,10 @@ __metadata: languageName: node linkType: hard -"lodash-es@npm:4.17.21, lodash-es@npm:^4.17.21": - version: 4.17.21 - resolution: "lodash-es@npm:4.17.21" - checksum: 10c0/fb407355f7e6cd523a9383e76e6b455321f0f153a6c9625e21a8827d10c54c2a2341bd2ae8d034358b60e07325e1330c14c224ff582d04612a46a4f0479ff2f2 +"lodash-es@npm:4.17.23, lodash-es@npm:^4.17.21, lodash-es@npm:^4.17.23": + version: 4.17.23 + resolution: "lodash-es@npm:4.17.23" + checksum: 10c0/3150fb6660c14c7a6b5f23bd11597d884b140c0e862a17fdb415aaa5ef7741523182904a6b7929f04e5f60a11edb5a79499eb448734381c99ffb3c4734beeddd languageName: node linkType: hard @@ -16598,7 +16613,7 @@ __metadata: languageName: node linkType: hard -"marked@npm:^16.2.1": +"marked@npm:^16.3.0": version: 16.4.2 resolution: "marked@npm:16.4.2" bin: @@ -16673,31 +16688,32 @@ __metadata: languageName: node linkType: hard -"mermaid@npm:11.12.1": - version: 11.12.1 - resolution: "mermaid@npm:11.12.1" +"mermaid@npm:11.13.0": + version: 11.13.0 + resolution: "mermaid@npm:11.13.0" dependencies: "@braintree/sanitize-url": "npm:^7.1.1" - "@iconify/utils": "npm:^3.0.1" - "@mermaid-js/parser": "npm:^0.6.3" + "@iconify/utils": "npm:^3.0.2" + "@mermaid-js/parser": "npm:^1.0.1" "@types/d3": "npm:^7.4.3" - cytoscape: "npm:^3.29.3" + "@upsetjs/venn.js": "npm:^2.0.0" + cytoscape: "npm:^3.33.1" cytoscape-cose-bilkent: "npm:^4.1.0" cytoscape-fcose: "npm:^2.2.0" d3: "npm:^7.9.0" d3-sankey: "npm:^0.12.3" - dagre-d3-es: "npm:7.0.13" - dayjs: "npm:^1.11.18" - dompurify: "npm:^3.2.5" - katex: "npm:^0.16.22" + dagre-d3-es: "npm:7.0.14" + dayjs: "npm:^1.11.19" + dompurify: "npm:^3.3.1" + katex: "npm:^0.16.25" khroma: "npm:^2.1.0" - lodash-es: "npm:^4.17.21" - marked: "npm:^16.2.1" + lodash-es: "npm:^4.17.23" + marked: "npm:^16.3.0" roughjs: "npm:^4.6.6" stylis: "npm:^4.3.6" ts-dedent: "npm:^2.2.0" uuid: "npm:^11.1.0" - checksum: 10c0/0dd07a5986bb25ca038f68f7a187a0b6ccf5fa8c738603c3145bd4b6289d50312053818f52617cf575a6652e6c57809ae115c016725f7ce446496be5971a150a + checksum: 10c0/9d908314d26cdb23d8fbb6a1183f3dc48440efa2cf25f31fd5f75ef3f72e973ef7c3b77768eb8399898035366d2b4a3a2bf0a0de9e35757848754935bf1288c0 languageName: node linkType: hard @@ -17734,7 +17750,7 @@ __metadata: markdown-it: "npm:^14.1.0" markdown-it-container: "npm:^3.0.0" markdown-it-emoji: "npm:^3.0.0" - mermaid: "npm:11.12.1" + mermaid: "npm:11.13.0" mime-types: "npm:^3.0.1" mobx: "npm:^4.15.4" mobx-react: "npm:^6.3.1" @@ -22534,10 +22550,10 @@ __metadata: languageName: node linkType: hard -"vscode-uri@npm:~3.0.8": - version: 3.0.8 - resolution: "vscode-uri@npm:3.0.8" - checksum: 10c0/f7f217f526bf109589969fe6e66b71e70b937de1385a1d7bb577ca3ee7c5e820d3856a86e9ff2fa9b7a0bc56a3dd8c3a9a557d3fedd7df414bc618d5e6b567f9 +"vscode-uri@npm:~3.1.0": + version: 3.1.0 + resolution: "vscode-uri@npm:3.1.0" + checksum: 10c0/5f6c9c10fd9b1664d71fab4e9fbbae6be93c7f75bb3a1d9d74399a88ab8649e99691223fd7cef4644376cac6e94fa2c086d802521b9a8e31c5af3e60f0f35624 languageName: node linkType: hard From b91d9e9a727b025549d23fa426eb8ebf5e812200 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Wed, 25 Mar 2026 23:01:26 -0400 Subject: [PATCH 10/17] feat: Extract search into pluggable provider system (#11448) * feat: Extract search into pluggable provider system Refactors the monolithic SearchHelper into a pluggable search provider architecture, enabling alternative search backends (Elasticsearch, Turbopuffer, etc.) while preserving PostgreSQL full-text search as the default. The SEARCH_PROVIDER env var selects the active provider. - Add BaseSearchProvider abstract class and SearchProviderManager - Add Hook.SearchProvider to the plugin system - Move PostgreSQL search logic into plugins/postgres-search/ - Add SearchIndexProcessor for event-driven index sync - Update all callers to use the provider manager directly - Keep SearchHelper as a deprecated thin wrapper for backwards compat Closes #11347 Co-Authored-By: Claude Opus 4.6 * refactor: Remove deprecated SearchHelper wrapper All callers now use SearchProviderManager directly, so the thin delegation wrapper is no longer needed. Co-Authored-By: Claude Opus 4.6 * refactor: Rename postgres-search plugin to search-postgres Renames the plugin folder and id so that future search provider plugins (e.g. search-elasticsearch, search-turbopuffer) will be colocated alphabetically. Co-Authored-By: Claude Opus 4.6 * refactor: Remove special-case plugin import from SearchProviderManager Make PluginManager.loadPlugins resilient to individual plugin load failures so SearchProviderManager can use the standard getHooks path without needing to directly import the search-postgres plugin. Co-Authored-By: Claude Opus 4.6 * test: Add missing search provider tests for full coverage parity Adds all tests that existed in the old SearchHelper.test.ts but were missing from PostgresSearchProvider.test.ts, including searchTitlesForUser status filters, collection filtering, group memberships, and sorting tests. Co-Authored-By: Claude Opus 4.6 * feedback --------- Co-authored-by: Claude Opus 4.6 --- app/stores/UnfurlsStore.ts | 6 +- plugins/search-postgres/plugin.json | 6 + .../server/PostgresSearchProvider.test.ts | 139 ++++++------- .../server/PostgresSearchProvider.ts | 186 ++++++++++-------- plugins/search-postgres/server/index.ts | 13 ++ plugins/slack/server/api/hooks.ts | 4 +- server/env.ts | 8 + .../DocumentArchivedProcessor.test.ts | 6 +- .../processors/SearchIndexProcessor.test.ts | 82 ++++++++ .../queues/processors/SearchIndexProcessor.ts | 139 +++++++++++++ server/routes/api/documents/documents.ts | 29 +-- server/routes/api/suggestions/suggestions.ts | 6 +- server/tools/documents.ts | 6 +- server/utils/BaseSearchProvider.ts | 149 ++++++++++++++ server/utils/PluginManager.ts | 14 +- server/utils/SearchProviderManager.ts | 49 +++++ shared/types.ts | 7 + 17 files changed, 667 insertions(+), 182 deletions(-) create mode 100644 plugins/search-postgres/plugin.json rename server/models/helpers/SearchHelper.test.ts => plugins/search-postgres/server/PostgresSearchProvider.test.ts (89%) rename server/models/helpers/SearchHelper.ts => plugins/search-postgres/server/PostgresSearchProvider.ts (83%) create mode 100644 plugins/search-postgres/server/index.ts create mode 100644 server/queues/processors/SearchIndexProcessor.test.ts create mode 100644 server/queues/processors/SearchIndexProcessor.ts create mode 100644 server/utils/BaseSearchProvider.ts create mode 100644 server/utils/SearchProviderManager.ts diff --git a/app/stores/UnfurlsStore.ts b/app/stores/UnfurlsStore.ts index 9351260c56..17a819b97d 100644 --- a/app/stores/UnfurlsStore.ts +++ b/app/stores/UnfurlsStore.ts @@ -24,7 +24,11 @@ class UnfurlsStore extends Store> { }): Promise | undefined> => { try { const protocol = new URL(url).protocol; - if (protocol !== "http:" && protocol !== "https:" && protocol !== "mention:") { + if ( + protocol !== "http:" && + protocol !== "https:" && + protocol !== "mention:" + ) { return; } } catch (_err) { diff --git a/plugins/search-postgres/plugin.json b/plugins/search-postgres/plugin.json new file mode 100644 index 0000000000..d87ccf91ba --- /dev/null +++ b/plugins/search-postgres/plugin.json @@ -0,0 +1,6 @@ +{ + "id": "search-postgres", + "name": "PostgreSQL Search", + "priority": 0, + "description": "Full-text search powered by PostgreSQL tsvector." +} diff --git a/server/models/helpers/SearchHelper.test.ts b/plugins/search-postgres/server/PostgresSearchProvider.test.ts similarity index 89% rename from server/models/helpers/SearchHelper.test.ts rename to plugins/search-postgres/server/PostgresSearchProvider.test.ts index 0916d2113b..d4b07acde3 100644 --- a/server/models/helpers/SearchHelper.test.ts +++ b/plugins/search-postgres/server/PostgresSearchProvider.test.ts @@ -4,7 +4,6 @@ import { SortFilter, StatusFilter, } from "@shared/types"; -import SearchHelper from "@server/models/helpers/SearchHelper"; import { buildDocument, buildDraftDocument, @@ -14,15 +13,19 @@ import { buildShare, buildGroup, } from "@server/test/factories"; -import UserMembership from "../UserMembership"; -import GroupMembership from "../GroupMembership"; +import UserMembership from "@server/models/UserMembership"; +import GroupMembership from "@server/models/GroupMembership"; +import SearchProviderManager from "@server/utils/SearchProviderManager"; +import PostgresSearchProvider from "./PostgresSearchProvider"; + +const provider = SearchProviderManager.getProvider(); beforeEach(async () => { jest.resetAllMocks(); await buildDocument(); }); -describe("SearchHelper", () => { +describe("PostgresSearchProvider", () => { describe("#searchForTeam", () => { it("should return search results from public collections", async () => { const team = await buildTeam(); @@ -34,7 +37,7 @@ describe("SearchHelper", () => { collectionId: collection.id, title: "test", }); - const { results } = await SearchHelper.searchForTeam(team, { + const { results } = await provider.searchForTeam(team, { query: "test", }); expect(results.length).toBe(1); @@ -58,7 +61,7 @@ describe("SearchHelper", () => { title: "document 2", }), ]); - const { results } = await SearchHelper.searchForTeam(team); + const { results } = await provider.searchForTeam(team); expect(results.length).toBe(2); expect(results.map((r) => r.document.id).sort()).toEqual( documents.map((doc) => doc.id).sort() @@ -76,7 +79,7 @@ describe("SearchHelper", () => { collectionId: collection.id, title: "test", }); - const { results } = await SearchHelper.searchForTeam(team, { + const { results } = await provider.searchForTeam(team, { query: "test", }); expect(results.length).toBe(0); @@ -93,7 +96,7 @@ describe("SearchHelper", () => { collectionId: collection.id, title: "test", }); - const { results } = await SearchHelper.searchForTeam(team, { + const { results } = await provider.searchForTeam(team, { query: "test", collectionId: collection.id, }); @@ -122,7 +125,7 @@ describe("SearchHelper", () => { includeChildDocuments: true, }); - const { results } = await SearchHelper.searchForTeam(team, { + const { results } = await provider.searchForTeam(team, { query: "test", collectionId: collection.id, share, @@ -132,7 +135,7 @@ describe("SearchHelper", () => { it("should handle no collections", async () => { const team = await buildTeam(); - const { results } = await SearchHelper.searchForTeam(team, { + const { results } = await provider.searchForTeam(team, { query: "test", }); expect(results.length).toBe(0); @@ -148,7 +151,7 @@ describe("SearchHelper", () => { collectionId: collection.id, title: "test with backslash \\", }); - const { results } = await SearchHelper.searchForTeam(team, { + const { results } = await provider.searchForTeam(team, { query: "test with backslash \\", }); expect(results.length).toBe(1); @@ -170,7 +173,7 @@ describe("SearchHelper", () => { collectionId: collection.id, title: "test number 2", }); - const { total } = await SearchHelper.searchForTeam(team, { + const { total } = await provider.searchForTeam(team, { query: "test", }); expect(total).toBe(2); @@ -188,7 +191,7 @@ describe("SearchHelper", () => { }); document.title = "change"; await document.save(); - const { total } = await SearchHelper.searchForTeam(team, { + const { total } = await provider.searchForTeam(team, { query: "test number", }); expect(total).toBe(1); @@ -206,7 +209,7 @@ describe("SearchHelper", () => { }); document.title = "change"; await document.save(); - const { total } = await SearchHelper.searchForTeam(team, { + const { total } = await provider.searchForTeam(team, { query: "title doesn't exist", }); expect(total).toBe(0); @@ -234,7 +237,7 @@ describe("SearchHelper", () => { deletedAt: new Date(), title: "test", }); - const { results } = await SearchHelper.searchForUser(user, { + const { results } = await provider.searchForUser(user, { query: "test", }); expect(results.length).toBe(1); @@ -263,7 +266,7 @@ describe("SearchHelper", () => { title: "document 2", }), ]); - const { results } = await SearchHelper.searchForUser(user); + const { results } = await provider.searchForUser(user); expect(results.length).toBe(2); expect(results.map((r) => r.document.id).sort()).toEqual( documents.map((doc) => doc.id).sort() @@ -291,7 +294,7 @@ describe("SearchHelper", () => { title: "document 2", }), ]); - const { results } = await SearchHelper.searchForUser(user, { + const { results } = await provider.searchForUser(user, { collectionId: collection.id, }); expect(results.length).toBe(2); @@ -339,7 +342,7 @@ describe("SearchHelper", () => { title: "document 2 in collection 2", }), ]); - const { results } = await SearchHelper.searchForUser(user, { + const { results } = await provider.searchForUser(user, { collectionId: collection1.id, }); expect(results.length).toBe(2); @@ -351,7 +354,7 @@ describe("SearchHelper", () => { it("should handle no collections", async () => { const team = await buildTeam(); const user = await buildUser({ teamId: team.id }); - const { results } = await SearchHelper.searchForUser(user, { + const { results } = await provider.searchForUser(user, { query: "test", }); expect(results.length).toBe(0); @@ -381,7 +384,7 @@ describe("SearchHelper", () => { title: "test", archivedAt: new Date(), }); - const { results } = await SearchHelper.searchForUser(user, { + const { results } = await provider.searchForUser(user, { query: "test", statusFilter: [StatusFilter.Draft], }); @@ -406,7 +409,7 @@ describe("SearchHelper", () => { permission: DocumentPermission.Read, }); - const { results } = await SearchHelper.searchForUser(user, { + const { results } = await provider.searchForUser(user, { query: "test", statusFilter: [StatusFilter.Published, StatusFilter.Archived], }); @@ -437,7 +440,7 @@ describe("SearchHelper", () => { title: "test", archivedAt: new Date(), }); - const { results } = await SearchHelper.searchForUser(user, { + const { results } = await provider.searchForUser(user, { query: "test", statusFilter: [StatusFilter.Published], }); @@ -474,7 +477,7 @@ describe("SearchHelper", () => { title: "test", archivedAt: new Date(), }); - const { results } = await SearchHelper.searchForUser(user, { + const { results } = await provider.searchForUser(user, { query: "test", statusFilter: [StatusFilter.Archived], }); @@ -502,7 +505,7 @@ describe("SearchHelper", () => { title: "test", archivedAt: new Date(), }); - const { results } = await SearchHelper.searchForUser(user, { + const { results } = await provider.searchForUser(user, { query: "test", statusFilter: [StatusFilter.Archived, StatusFilter.Published], }); @@ -530,7 +533,7 @@ describe("SearchHelper", () => { title: "archived not draft", archivedAt: new Date(), }); - const { results } = await SearchHelper.searchForUser(user, { + const { results } = await provider.searchForUser(user, { query: "draft", statusFilter: [StatusFilter.Published, StatusFilter.Draft], }); @@ -558,7 +561,7 @@ describe("SearchHelper", () => { title: "archived not draft", archivedAt: new Date(), }); - const { results } = await SearchHelper.searchForUser(user, { + const { results } = await provider.searchForUser(user, { query: "draft", statusFilter: [StatusFilter.Draft, StatusFilter.Archived], }); @@ -584,7 +587,7 @@ describe("SearchHelper", () => { collectionId: collection.id, title: "test number 2", }); - const { total } = await SearchHelper.searchForUser(user, { + const { total } = await provider.searchForUser(user, { query: "test", }); expect(total).toBe(2); @@ -605,7 +608,7 @@ describe("SearchHelper", () => { }); document.title = "change"; await document.save(); - const { total } = await SearchHelper.searchForUser(user, { + const { total } = await provider.searchForUser(user, { query: "test number", }); expect(total).toBe(1); @@ -626,7 +629,7 @@ describe("SearchHelper", () => { }); document.title = "change"; await document.save(); - const { total } = await SearchHelper.searchForUser(user, { + const { total } = await provider.searchForUser(user, { query: "title doesn't exist", }); expect(total).toBe(0); @@ -647,7 +650,7 @@ describe("SearchHelper", () => { }); document.title = "change"; await document.save(); - const { total } = await SearchHelper.searchForUser(user, { + const { total } = await provider.searchForUser(user, { query: `"test number"`, }); expect(total).toBe(1); @@ -668,7 +671,7 @@ describe("SearchHelper", () => { }); document.title = "change"; await document.save(); - const { total } = await SearchHelper.searchForUser(user, { + const { total } = await provider.searchForUser(user, { query: "env: ", }); expect(total).toBe(1); @@ -681,7 +684,7 @@ describe("SearchHelper", () => { const collection = await buildCollection({ userId: otherUser.id, teamId: team.id, - permission: null, // private collection + permission: null, }); const document = await buildDocument({ userId: otherUser.id, @@ -690,7 +693,6 @@ describe("SearchHelper", () => { title: "group test document", }); - // Document with no access should not appear in results await buildDocument({ userId: otherUser.id, teamId: team.id, @@ -698,7 +700,6 @@ describe("SearchHelper", () => { title: "group test document 2", }); - // Create a group and add the user to it const group = await buildGroup({ teamId: team.id, }); @@ -708,14 +709,13 @@ describe("SearchHelper", () => { }, }); - // Add group membership to the document await GroupMembership.create({ createdById: otherUser.id, groupId: group.id, documentId: document.id, }); - const { results } = await SearchHelper.searchForUser(user, { + const { results } = await provider.searchForUser(user, { query: "group test", }); @@ -739,7 +739,7 @@ describe("SearchHelper", () => { collectionId: collection.id, title: "test", }); - const documents = await SearchHelper.searchTitlesForUser(user, { + const documents = await provider.searchTitlesForUser(user, { query: "test", }); expect(documents.length).toBe(1); @@ -774,7 +774,7 @@ describe("SearchHelper", () => { collectionId: collection1.id, title: "test", }); - const documents = await SearchHelper.searchTitlesForUser(user, { + const documents = await provider.searchTitlesForUser(user, { query: "test", collectionId: collection.id, }); @@ -785,7 +785,7 @@ describe("SearchHelper", () => { it("should handle no collections", async () => { const team = await buildTeam(); const user = await buildUser({ teamId: team.id }); - const documents = await SearchHelper.searchTitlesForUser(user, { + const documents = await provider.searchTitlesForUser(user, { query: "test", }); expect(documents.length).toBe(0); @@ -815,7 +815,7 @@ describe("SearchHelper", () => { title: "test", archivedAt: new Date(), }); - const documents = await SearchHelper.searchTitlesForUser(user, { + const documents = await provider.searchTitlesForUser(user, { query: "test", statusFilter: [StatusFilter.Draft], }); @@ -846,7 +846,7 @@ describe("SearchHelper", () => { title: "test", archivedAt: new Date(), }); - const documents = await SearchHelper.searchTitlesForUser(user, { + const documents = await provider.searchTitlesForUser(user, { query: "test", statusFilter: [StatusFilter.Published], }); @@ -883,7 +883,7 @@ describe("SearchHelper", () => { title: "test", archivedAt: new Date(), }); - const documents = await SearchHelper.searchTitlesForUser(user, { + const documents = await provider.searchTitlesForUser(user, { query: "test", statusFilter: [StatusFilter.Archived], }); @@ -911,7 +911,7 @@ describe("SearchHelper", () => { title: "test", archivedAt: new Date(), }); - const documents = await SearchHelper.searchTitlesForUser(user, { + const documents = await provider.searchTitlesForUser(user, { query: "test", statusFilter: [StatusFilter.Archived, StatusFilter.Published], }); @@ -939,7 +939,7 @@ describe("SearchHelper", () => { title: "archived not draft", archivedAt: new Date(), }); - const documents = await SearchHelper.searchTitlesForUser(user, { + const documents = await provider.searchTitlesForUser(user, { query: "draft", statusFilter: [StatusFilter.Published, StatusFilter.Draft], }); @@ -967,7 +967,7 @@ describe("SearchHelper", () => { title: "archived not draft", archivedAt: new Date(), }); - const documents = await SearchHelper.searchTitlesForUser(user, { + const documents = await provider.searchTitlesForUser(user, { query: "draft", statusFilter: [StatusFilter.Draft, StatusFilter.Archived], }); @@ -981,7 +981,7 @@ describe("SearchHelper", () => { const collection = await buildCollection({ userId: otherUser.id, teamId: team.id, - permission: null, // private collection + permission: null, }); const document = await buildDocument({ userId: otherUser.id, @@ -990,7 +990,6 @@ describe("SearchHelper", () => { title: "group title test document", }); - // Document with no access should not appear in results await buildDocument({ userId: otherUser.id, teamId: team.id, @@ -998,7 +997,6 @@ describe("SearchHelper", () => { title: "group title test document 2", }); - // Create a group and add the user to it const group = await buildGroup({ teamId: team.id, }); @@ -1008,14 +1006,13 @@ describe("SearchHelper", () => { }, }); - // Add group membership to the document await GroupMembership.create({ createdById: otherUser.id, groupId: group.id, documentId: document.id, }); - const documents = await SearchHelper.searchTitlesForUser(user, { + const documents = await provider.searchTitlesForUser(user, { query: "group title", }); @@ -1039,7 +1036,7 @@ describe("SearchHelper", () => { name: "Other Collection", }); - const results = await SearchHelper.searchCollectionsForUser(user, { + const results = await provider.searchCollectionsForUser(user, { query: "test", }); @@ -1061,7 +1058,7 @@ describe("SearchHelper", () => { name: "Beta", }); - const results = await SearchHelper.searchCollectionsForUser(user); + const results = await provider.searchCollectionsForUser(user); expect(results.length).toBe(2); expect(results[0].id).toBe(collection1.id); @@ -1096,7 +1093,7 @@ describe("SearchHelper", () => { title: "Beta Document", }); - const { results } = await SearchHelper.searchForUser(user, { + const { results } = await provider.searchForUser(user, { sort: SortFilter.Title, direction: DirectionFilter.ASC, }); @@ -1133,7 +1130,7 @@ describe("SearchHelper", () => { title: "Beta Document", }); - const { results } = await SearchHelper.searchForUser(user, { + const { results } = await provider.searchForUser(user, { sort: SortFilter.Title, direction: DirectionFilter.DESC, }); @@ -1176,7 +1173,7 @@ describe("SearchHelper", () => { updatedAt: new Date("2023-12-01"), }); - const { results } = await SearchHelper.searchForUser(user, { + const { results } = await provider.searchForUser(user, { sort: SortFilter.CreatedAt, direction: DirectionFilter.ASC, }); @@ -1216,7 +1213,7 @@ describe("SearchHelper", () => { updatedAt: new Date("2023-06-01"), }); - const { results } = await SearchHelper.searchForUser(user); + const { results } = await provider.searchForUser(user); expect(results.length).toBe(3); expect(results[0].document.id).toBe(doc2.id); @@ -1252,7 +1249,7 @@ describe("SearchHelper", () => { updatedAt: new Date("2023-01-01"), }); - const { results } = await SearchHelper.searchForUser(user, { + const { results } = await provider.searchForUser(user, { query: "search", }); @@ -1288,7 +1285,7 @@ describe("SearchHelper", () => { updatedAt: new Date("2025-12-01"), }); - const { results } = await SearchHelper.searchForUser(user, { + const { results } = await provider.searchForUser(user, { query: "search", sort: SortFilter.UpdatedAt, direction: DirectionFilter.DESC, @@ -1326,7 +1323,7 @@ describe("SearchHelper", () => { }); // Without popularity boost, pure relevance should win - const { results: withoutBoost } = await SearchHelper.searchForTeam(team, { + const { results: withoutBoost } = await provider.searchForTeam(team, { query: "testing", usePopularityBoost: false, }); @@ -1335,7 +1332,7 @@ describe("SearchHelper", () => { expect(withoutBoost[0].document.id).toBe(relevantDoc.id); // With popularity boost, the popular document may rank higher - const { results: withBoost } = await SearchHelper.searchForTeam(team, { + const { results: withBoost } = await provider.searchForTeam(team, { query: "testing", usePopularityBoost: true, }); @@ -1350,22 +1347,28 @@ describe("SearchHelper", () => { describe("webSearchQuery", () => { it("should correctly sanitize query", () => { - expect(SearchHelper.webSearchQuery("one/two")).toBe("one/two:*"); - expect(SearchHelper.webSearchQuery("one\\two")).toBe("one\\\\two:*"); - expect(SearchHelper.webSearchQuery("test''")).toBe("test"); + expect(PostgresSearchProvider.webSearchQuery("one/two")).toBe( + "one/two:*" + ); + expect(PostgresSearchProvider.webSearchQuery("one\\two")).toBe( + "one\\\\two:*" + ); + expect(PostgresSearchProvider.webSearchQuery("test''")).toBe("test"); }); it("should wildcard unquoted queries", () => { - expect(SearchHelper.webSearchQuery("test")).toBe("test:*"); - expect(SearchHelper.webSearchQuery("'")).toBe(""); - expect(SearchHelper.webSearchQuery("'quoted'")).toBe(`"quoted":*`); + expect(PostgresSearchProvider.webSearchQuery("test")).toBe("test:*"); + expect(PostgresSearchProvider.webSearchQuery("'")).toBe(""); + expect(PostgresSearchProvider.webSearchQuery("'quoted'")).toBe( + `"quoted":*` + ); }); it("should wildcard multi-word queries", () => { - expect(SearchHelper.webSearchQuery("this is a test")).toBe( + expect(PostgresSearchProvider.webSearchQuery("this is a test")).toBe( "this&is&a&test:*" ); }); it("should not wildcard quoted queries", () => { - expect(SearchHelper.webSearchQuery(`"this is a test"`)).toBe( + expect(PostgresSearchProvider.webSearchQuery(`"this is a test"`)).toBe( `"this<->is<->a<->test"` ); }); diff --git a/server/models/helpers/SearchHelper.ts b/plugins/search-postgres/server/PostgresSearchProvider.ts similarity index 83% rename from server/models/helpers/SearchHelper.ts rename to plugins/search-postgres/server/PostgresSearchProvider.ts index 85ee0fe075..f80f2f59c6 100644 --- a/server/models/helpers/SearchHelper.ts +++ b/plugins/search-postgres/server/PostgresSearchProvider.ts @@ -11,63 +11,23 @@ import type { WhereOptions, } from "sequelize"; import { Op, Sequelize } from "sequelize"; -import type { DateFilter } from "@shared/types"; -import { DirectionFilter, SortFilter } from "@shared/types"; -import { StatusFilter } from "@shared/types"; +import type { SearchableModel } from "@shared/types"; +import { DirectionFilter, SortFilter, StatusFilter } from "@shared/types"; import { regexIndexOf, regexLastIndexOf } from "@shared/utils/string"; import { getUrls } from "@shared/utils/urls"; import { ValidationError } from "@server/errors"; import Collection from "@server/models/Collection"; +import type Comment from "@server/models/Comment"; import Document from "@server/models/Document"; -import type Share from "@server/models/Share"; import Team from "@server/models/Team"; import User from "@server/models/User"; +import { DocumentHelper } from "@server/models/helpers/DocumentHelper"; import { sequelize } from "@server/storage/database"; -import { DocumentHelper } from "./DocumentHelper"; - -type SearchResponse = { - results: { - /** The search ranking, for sorting results */ - ranking: number; - /** A snippet of contextual text around the search result */ - context?: string; - /** The document result */ - document: Document; - }[]; - /** The total number of results for the search query without pagination */ - total: number; -}; - -type SearchOptions = { - /** The query limit for pagination */ - limit?: number; - /** The query offset for pagination */ - offset?: number; - /** The text to search for */ - query?: string; - /** Limit results to a collection. Authorization is presumed to have been done before passing to this helper. */ - collectionId?: string | null; - /** Limit results to a shared document. */ - share?: Share; - /** Limit results to a date range. */ - dateFilter?: DateFilter; - /** Status of the documents to return */ - statusFilter?: StatusFilter[]; - /** Limit results to a list of documents. */ - documentIds?: string[]; - /** Limit results to a list of users that collaborated on the document. */ - collaboratorIds?: string[]; - /** The minimum number of words to be returned in the contextual snippet */ - snippetMinWords?: number; - /** The maximum number of words to be returned in the contextual snippet */ - snippetMaxWords?: number; - /** The field to sort results by */ - sort?: SortFilter; - /** The sort direction */ - direction?: DirectionFilter; - /** Whether to boost results by popularity score. Defaults to true. */ - usePopularityBoost?: boolean; -}; +import type { + SearchOptions, + SearchResponse, +} from "@server/utils/BaseSearchProvider"; +import { BaseSearchProvider } from "@server/utils/BaseSearchProvider"; type RankedDocument = Document & { id: string; @@ -76,24 +36,31 @@ type RankedDocument = Document & { }; }; -export default class SearchHelper { +/** + * Search provider that uses PostgreSQL full-text search via tsvector. + * Indexing is handled by database triggers, so index/remove/updateMetadata + * are no-ops. + */ +export default class PostgresSearchProvider extends BaseSearchProvider { + id = "postgres"; + /** * The maximum length of a search query. */ public static maxQueryLength = 1000; /** - * Cached regex pattern for single quotes to avoid recompilation + * Cached regex pattern for single quotes to avoid recompilation. */ private static readonly SINGLE_QUOTE_REGEX = /'+/g; /** - * Cached regex pattern for quoted queries + * Cached regex pattern for quoted queries. */ private static readonly QUOTED_QUERY_REGEX = /"([^"]*)"/g; /** - * Cached regex pattern for break characters + * Cached regex pattern for break characters. */ private static readonly BREAK_CHARS_REGEX = new RegExp( `[ .,"'\n。!?!?…]`, @@ -101,7 +68,7 @@ export default class SearchHelper { ); /** - * Cached stop words set for efficient lookup + * Cached stop words set for efficient lookup. * Based on: https://github.com/postgres/postgres/blob/fc0d0ce978752493868496be6558fa17b7c4c3cf/src/backend/snowball/stopwords/english.stop */ private static readonly STOP_WORDS = new Set([ @@ -215,13 +182,13 @@ export default class SearchHelper { "should", ]); - public static async searchForTeam( + async searchForTeam( team: Team, options: SearchOptions = {} ): Promise { const { limit = 15, offset = 0, query } = options; - const where = await this.buildWhere(team, { + const where = await PostgresSearchProvider.buildWhere(team, { ...options, statusFilter: [...(options.statusFilter || []), StatusFilter.Published], }); @@ -256,7 +223,7 @@ export default class SearchHelper { }); } - const findOptions = this.buildFindOptions({ + const findOptions = PostgresSearchProvider.buildFindOptions({ query, sort: options.sort, direction: options.direction, @@ -292,7 +259,7 @@ export default class SearchHelper { ], }); - return this.buildResponse({ + return PostgresSearchProvider.buildResponse({ query, results, documents, @@ -306,12 +273,12 @@ export default class SearchHelper { } } - public static async searchTitlesForUser( + async searchTitlesForUser( user: User, options: SearchOptions = {} ): Promise { const { limit = 15, offset = 0, query, ...rest } = options; - const where = await this.buildWhere(user, rest); + const where = await PostgresSearchProvider.buildWhere(user, rest); if (query) { where[Op.and].push({ @@ -379,7 +346,7 @@ export default class SearchHelper { }); } - public static async searchCollectionsForUser( + async searchCollectionsForUser( user: User, options: SearchOptions = {} ): Promise { @@ -408,15 +375,15 @@ export default class SearchHelper { }); } - public static async searchForUser( + async searchForUser( user: User, options: SearchOptions = {} ): Promise { const { limit = 15, offset = 0, query } = options; - const where = await this.buildWhere(user, options); + const where = await PostgresSearchProvider.buildWhere(user, options); - const findOptions = this.buildFindOptions({ + const findOptions = PostgresSearchProvider.buildFindOptions({ query, sort: options.sort, direction: options.direction, @@ -484,7 +451,7 @@ export default class SearchHelper { : countQuery, ]); - return this.buildResponse({ + return PostgresSearchProvider.buildResponse({ query, results, documents, @@ -498,6 +465,49 @@ export default class SearchHelper { } } + /** + * No-op for PostgreSQL — indexing is handled by database triggers. + * + * @param _model - unused. + * @param _item - unused. + */ + async index( + _model: SearchableModel, + _item: Document | Collection | Comment + ): Promise { + // PostgreSQL uses tsvector triggers for indexing + } + + /** + * No-op for PostgreSQL — removal is handled by database cascades. + * + * @param _model - unused. + * @param _id - unused. + * @param _teamId - unused. + */ + async remove( + _model: SearchableModel, + _id: string, + _teamId: string + ): Promise { + // PostgreSQL handles removal via cascading deletes + } + + /** + * No-op for PostgreSQL — metadata is stored in the same tables. + * + * @param _model - unused. + * @param _id - unused. + * @param _metadata - unused. + */ + async updateMetadata( + _model: SearchableModel, + _id: string, + _metadata: Record + ): Promise { + // PostgreSQL metadata lives in the same row as the document + } + private static buildFindOptions({ query, sort, @@ -519,7 +529,7 @@ export default class SearchHelper { : `ts_rank("searchVector", to_tsquery('english', :query))`; attributes.push([Sequelize.literal(rankExpression), "searchRanking"]); - replacements["query"] = this.webSearchQuery(query); + replacements["query"] = PostgresSearchProvider.webSearchQuery(query); } // When searching with a query and no explicit sort, prioritize search @@ -551,8 +561,10 @@ export default class SearchHelper { private static buildResultContext(document: Document, query: string) { // Reset regex lastIndex to avoid state issues with global regex - this.QUOTED_QUERY_REGEX.lastIndex = 0; - const quotedQueries = Array.from(query.matchAll(this.QUOTED_QUERY_REGEX)); + PostgresSearchProvider.QUOTED_QUERY_REGEX.lastIndex = 0; + const quotedQueries = Array.from( + query.matchAll(PostgresSearchProvider.QUOTED_QUERY_REGEX) + ); const text = DocumentHelper.toPlainText(document); // Regex to highlight quoted queries as ts_headline will not do this by default due to stemming. @@ -562,7 +574,7 @@ export default class SearchHelper { fullMatchRegex.source, ...(quotedQueries.length ? quotedQueries.map((match) => escapeRegExp(match[1])) - : this.removeStopWords(query) + : PostgresSearchProvider.removeStopWords(query) .trim() .split(" ") .map((match) => `\\b${escapeRegExp(match)}\\b`)), @@ -571,8 +583,8 @@ export default class SearchHelper { ); // Reset regex lastIndex to avoid state issues with global regex - this.BREAK_CHARS_REGEX.lastIndex = 0; - const breakCharsRegex = this.BREAK_CHARS_REGEX; + PostgresSearchProvider.BREAK_CHARS_REGEX.lastIndex = 0; + const breakCharsRegex = PostgresSearchProvider.BREAK_CHARS_REGEX; // chop text around the first match, prefer the first full match if possible. const fullMatchIndex = text.search(fullMatchRegex); @@ -715,15 +727,17 @@ export default class SearchHelper { let likelyUrls = getUrls(options.query); // remove likely urls, and escape the rest of the query. - let limitedQuery = this.escapeQuery( + let limitedQuery = PostgresSearchProvider.escapeQuery( likelyUrls .reduce((q, url) => q.replace(url, ""), options.query) - .slice(0, this.maxQueryLength) + .slice(0, PostgresSearchProvider.maxQueryLength) .trim() ); // Escape the URLs - likelyUrls = likelyUrls.map((url) => this.escapeQuery(url)); + likelyUrls = likelyUrls.map((url) => + PostgresSearchProvider.escapeQuery(url) + ); // Extract quoted queries and add them to the where clause, up to a maximum of 3 total. const quotedQueries = Array.from(limitedQuery.matchAll(/"([^"]*)"/g)).map( @@ -785,7 +799,9 @@ export default class SearchHelper { return { ranking: result.dataValues.searchRanking, - context: query ? this.buildResultContext(document, query) : undefined, + context: query + ? PostgresSearchProvider.buildResultContext(document, query) + : undefined, document, }; }), @@ -794,22 +810,26 @@ export default class SearchHelper { } /** - * Convert a user search query into a format that can be used by Postgres + * Convert a user search query into a format that can be used by Postgres. * - * @param query The user search query - * @returns The query formatted for Postgres ts_query + * @param query - the user search query. + * @returns the query formatted for Postgres ts_query. */ public static webSearchQuery(query: string): string { // limit length of search queries as we're using regex against untrusted input - let limitedQuery = this.escapeQuery(query.slice(0, this.maxQueryLength)); + let limitedQuery = PostgresSearchProvider.escapeQuery( + query.slice(0, PostgresSearchProvider.maxQueryLength) + ); const quotedSearch = limitedQuery.startsWith('"') && limitedQuery.endsWith('"'); // Replace single quote characters with &. // Reset regex lastIndex to avoid state issues with global regex - this.SINGLE_QUOTE_REGEX.lastIndex = 0; - const singleQuotes = limitedQuery.matchAll(this.SINGLE_QUOTE_REGEX); + PostgresSearchProvider.SINGLE_QUOTE_REGEX.lastIndex = 0; + const singleQuotes = limitedQuery.matchAll( + PostgresSearchProvider.SINGLE_QUOTE_REGEX + ); for (const match of singleQuotes) { if ( @@ -851,11 +871,9 @@ export default class SearchHelper { } private static removeStopWords(query: string): string { - // Based on: - // https://github.com/postgres/postgres/blob/fc0d0ce978752493868496be6558fa17b7c4c3cf/src/backend/snowball/stopwords/english.stop return query .split(" ") - .filter((word) => !this.STOP_WORDS.has(word)) + .filter((word) => !PostgresSearchProvider.STOP_WORDS.has(word)) .join(" "); } } diff --git a/plugins/search-postgres/server/index.ts b/plugins/search-postgres/server/index.ts new file mode 100644 index 0000000000..7ec4f0eb3a --- /dev/null +++ b/plugins/search-postgres/server/index.ts @@ -0,0 +1,13 @@ +import { PluginManager, Hook } from "@server/utils/PluginManager"; +import config from "../plugin.json"; +import PostgresSearchProvider from "./PostgresSearchProvider"; + +const provider = new PostgresSearchProvider(); + +PluginManager.add([ + { + ...config, + type: Hook.SearchProvider, + value: provider, + }, +]); diff --git a/plugins/slack/server/api/hooks.ts b/plugins/slack/server/api/hooks.ts index aff6f7b9e4..aceaf31b75 100644 --- a/plugins/slack/server/api/hooks.ts +++ b/plugins/slack/server/api/hooks.ts @@ -23,7 +23,7 @@ import { AuthenticationProvider, Comment, } from "@server/models"; -import SearchHelper from "@server/models/helpers/SearchHelper"; +import SearchProviderManager from "@server/utils/SearchProviderManager"; import { can } from "@server/policies"; import type { APIContext } from "@server/types"; import { safeEqual } from "@server/utils/crypto"; @@ -238,7 +238,7 @@ router.post( return; } - const { results, total } = await SearchHelper.searchForUser(user, options); + const { results, total } = await SearchProviderManager.getProvider().searchForUser(user, options); await SearchQuery.create({ userId: user ? user.id : null, diff --git a/server/env.ts b/server/env.ts index 7d97fbe0ed..8e6b5085e8 100644 --- a/server/env.ts +++ b/server/env.ts @@ -772,6 +772,14 @@ export class Environment { environment.ALLOWED_PRIVATE_IP_ADDRESSES ); + /** + * The search provider to use. Defaults to "postgres" which uses PostgreSQL + * full-text search. Alternative providers can be registered via plugins. + */ + @IsOptional() + public SEARCH_PROVIDER = + this.toOptionalString(environment.SEARCH_PROVIDER) ?? "postgres"; + /** * The product name */ diff --git a/server/queues/processors/DocumentArchivedProcessor.test.ts b/server/queues/processors/DocumentArchivedProcessor.test.ts index 59560044d0..550425d5bb 100644 --- a/server/queues/processors/DocumentArchivedProcessor.test.ts +++ b/server/queues/processors/DocumentArchivedProcessor.test.ts @@ -16,7 +16,7 @@ describe("DocumentArchivedProcessor", () => { userId: user.id, documentId: document.id, }); - + // Verify the star exists expect( await Star.count({ @@ -56,7 +56,7 @@ describe("DocumentArchivedProcessor", () => { teamId: actor.teamId, userId: actor.id, }); - + // Create stars for both users await buildStar({ userId: actor.id, @@ -95,7 +95,7 @@ describe("DocumentArchivedProcessor", () => { }, }) ).toBe(0); - + // Verify the other user's star still exists expect( await Star.count({ diff --git a/server/queues/processors/SearchIndexProcessor.test.ts b/server/queues/processors/SearchIndexProcessor.test.ts new file mode 100644 index 0000000000..5128a0851b --- /dev/null +++ b/server/queues/processors/SearchIndexProcessor.test.ts @@ -0,0 +1,82 @@ +import { SearchableModel } from "@shared/types"; +import { + buildDocument, + buildCollection, + buildUser, +} from "@server/test/factories"; +import SearchProviderManager from "@server/utils/SearchProviderManager"; +import SearchIndexProcessor from "./SearchIndexProcessor"; + +const processor = new SearchIndexProcessor(); + +describe("SearchIndexProcessor", () => { + it("should have the expected applicable events", () => { + expect(SearchIndexProcessor.applicableEvents).toContain( + "documents.publish" + ); + expect(SearchIndexProcessor.applicableEvents).toContain( + "documents.update.delayed" + ); + expect(SearchIndexProcessor.applicableEvents).toContain( + "documents.permanent_delete" + ); + expect(SearchIndexProcessor.applicableEvents).toContain( + "collections.create" + ); + expect(SearchIndexProcessor.applicableEvents).toContain("comments.create"); + expect(SearchIndexProcessor.applicableEvents).toContain("comments.delete"); + }); + + it("should call provider.index for documents.publish", async () => { + const user = await buildUser(); + const collection = await buildCollection({ + teamId: user.teamId, + userId: user.id, + }); + const document = await buildDocument({ + teamId: user.teamId, + collectionId: collection.id, + userId: user.id, + }); + + const provider = SearchProviderManager.getProvider(); + const indexSpy = jest.spyOn(provider, "index"); + + await processor.perform({ + name: "documents.publish", + documentId: document.id, + collectionId: collection.id, + teamId: user.teamId, + actorId: user.id, + } as any); + + expect(indexSpy).toHaveBeenCalledWith( + SearchableModel.Document, + expect.objectContaining({ id: document.id }) + ); + + indexSpy.mockRestore(); + }); + + it("should call provider.remove for documents.permanent_delete", async () => { + const user = await buildUser(); + const provider = SearchProviderManager.getProvider(); + const removeSpy = jest.spyOn(provider, "remove"); + + await processor.perform({ + name: "documents.permanent_delete", + documentId: "deleted-doc-id", + collectionId: "some-collection-id", + teamId: user.teamId, + actorId: user.id, + } as any); + + expect(removeSpy).toHaveBeenCalledWith( + SearchableModel.Document, + "deleted-doc-id", + user.teamId + ); + + removeSpy.mockRestore(); + }); +}); diff --git a/server/queues/processors/SearchIndexProcessor.ts b/server/queues/processors/SearchIndexProcessor.ts new file mode 100644 index 0000000000..db6b26fc8a --- /dev/null +++ b/server/queues/processors/SearchIndexProcessor.ts @@ -0,0 +1,139 @@ +import { SearchableModel } from "@shared/types"; +import { Document, Collection, Comment } from "@server/models"; +import BaseProcessor from "@server/queues/processors/BaseProcessor"; +import type { + DocumentEvent, + DocumentMovedEvent, + CollectionEvent, + CommentEvent, + CommentUpdateEvent, + Event, +} from "@server/types"; +import SearchProviderManager from "@server/utils/SearchProviderManager"; + +/** + * Processor that keeps the search index in sync with data changes. + * For PostgreSQL this is largely a no-op since tsvector triggers handle + * indexing, but external providers (Elasticsearch, etc.) rely on these + * events to maintain their indexes. + */ +export default class SearchIndexProcessor extends BaseProcessor { + static applicableEvents: Event["name"][] = [ + "documents.publish", + "documents.update.delayed", + "documents.archive", + "documents.unarchive", + "documents.delete", + "documents.permanent_delete", + "documents.move", + "collections.create", + "collections.update", + "collections.delete", + "comments.create", + "comments.update", + "comments.delete", + ]; + + async perform( + event: DocumentEvent | DocumentMovedEvent | CollectionEvent | CommentEvent + ): Promise { + const provider = SearchProviderManager.getProvider(); + + // When using the built-in Postgres search provider, tsvector triggers + // handle indexing directly and the provider methods are effectively no-ops for now. + if (process.env.SEARCH_PROVIDER === "postgres") { + return; + } + + switch (event.name) { + case "documents.publish": + case "documents.update.delayed": + case "documents.unarchive": { + const document = await Document.findByPk( + (event as DocumentEvent).documentId + ); + if (document) { + await provider.index(SearchableModel.Document, document); + } + break; + } + + case "documents.archive": + case "documents.delete": { + const document = await Document.findByPk( + (event as DocumentEvent).documentId, + { paranoid: false } + ); + if (document) { + await provider.updateMetadata(SearchableModel.Document, document.id, { + archivedAt: document.archivedAt, + deletedAt: document.deletedAt, + }); + } + break; + } + + case "documents.permanent_delete": { + await provider.remove( + SearchableModel.Document, + (event as DocumentEvent).documentId, + event.teamId + ); + break; + } + + case "documents.move": { + const movedEvent = event as DocumentMovedEvent; + for (const documentId of movedEvent.data.documentIds) { + await provider.updateMetadata(SearchableModel.Document, documentId, { + collectionId: movedEvent.collectionId, + }); + } + break; + } + + case "collections.create": + case "collections.update": { + const collection = await Collection.findByPk( + (event as CollectionEvent).collectionId + ); + if (collection) { + await provider.index(SearchableModel.Collection, collection); + } + break; + } + + case "collections.delete": { + await provider.remove( + SearchableModel.Collection, + (event as CollectionEvent).collectionId, + event.teamId + ); + break; + } + + case "comments.create": + case "comments.update": { + const comment = await Comment.findByPk( + (event as CommentEvent | CommentUpdateEvent).modelId + ); + if (comment) { + await provider.index(SearchableModel.Comment, comment); + } + break; + } + + case "comments.delete": { + await provider.remove( + SearchableModel.Comment, + (event as CommentEvent).modelId, + event.teamId + ); + break; + } + + default: + break; + } + } +} diff --git a/server/routes/api/documents/documents.ts b/server/routes/api/documents/documents.ts index eb2f5c47b0..d8b9aeac2a 100644 --- a/server/routes/api/documents/documents.ts +++ b/server/routes/api/documents/documents.ts @@ -64,7 +64,7 @@ import { import AttachmentHelper from "@server/models/helpers/AttachmentHelper"; import { DocumentHelper } from "@server/models/helpers/DocumentHelper"; import { ProsemirrorHelper } from "@server/models/helpers/ProsemirrorHelper"; -import SearchHelper from "@server/models/helpers/SearchHelper"; +import SearchProviderManager from "@server/utils/SearchProviderManager"; import { TextHelper } from "@server/models/helpers/TextHelper"; import { authorize, cannot } from "@server/policies"; import { @@ -1015,17 +1015,18 @@ router.post( collaboratorIds = [userId]; } - const documents = await SearchHelper.searchTitlesForUser(user, { - query, - dateFilter, - statusFilter, - collectionId, - collaboratorIds, - offset, - limit, - sort: sort as SortFilter, - direction: direction as DirectionFilter, - }); + const documents = + await SearchProviderManager.getProvider().searchTitlesForUser(user, { + query, + dateFilter, + statusFilter, + collectionId, + collaboratorIds, + offset, + limit, + sort: sort as SortFilter, + direction: direction as DirectionFilter, + }); const policies = presentPolicies(user, documents); const data = await presentDocuments(ctx, documents); @@ -1099,7 +1100,7 @@ router.post( const team = await share.$get("team"); invariant(team, "Share must belong to a team"); - response = await SearchHelper.searchForTeam(team, { + response = await SearchProviderManager.getProvider().searchForTeam(team, { query, collectionId: collection?.id || document?.collectionId, share, @@ -1145,7 +1146,7 @@ router.post( collaboratorIds = [userId]; } - response = await SearchHelper.searchForUser(user, { + response = await SearchProviderManager.getProvider().searchForUser(user, { query, collaboratorIds, collectionId, diff --git a/server/routes/api/suggestions/suggestions.ts b/server/routes/api/suggestions/suggestions.ts index 84a06da749..5ee394bda6 100644 --- a/server/routes/api/suggestions/suggestions.ts +++ b/server/routes/api/suggestions/suggestions.ts @@ -5,7 +5,7 @@ import { StatusFilter } from "@shared/types"; import auth from "@server/middlewares/authentication"; import validate from "@server/middlewares/validate"; import { Group, User } from "@server/models"; -import SearchHelper from "@server/models/helpers/SearchHelper"; +import SearchProviderManager from "@server/utils/SearchProviderManager"; import { can } from "@server/policies"; import { presentDocuments, @@ -29,7 +29,7 @@ router.post( const actor = ctx.state.auth.user; const [documents, users, groups, collections] = await Promise.all([ - SearchHelper.searchTitlesForUser(actor, { + SearchProviderManager.getProvider().searchTitlesForUser(actor, { query, offset, limit, @@ -74,7 +74,7 @@ router.post( offset, limit, }), - SearchHelper.searchCollectionsForUser(actor, { query, offset, limit }), + SearchProviderManager.getProvider().searchCollectionsForUser(actor, { query, offset, limit }), ]); ctx.body = { diff --git a/server/tools/documents.ts b/server/tools/documents.ts index 1a79ef8693..8589ea5cff 100644 --- a/server/tools/documents.ts +++ b/server/tools/documents.ts @@ -7,7 +7,6 @@ import documentUpdater from "@server/commands/documentUpdater"; import { Op } from "sequelize"; import { Collection, Document } from "@server/models"; import { sequelize } from "@server/storage/database"; -import SearchHelper from "@server/models/helpers/SearchHelper"; import { authorize } from "@server/policies"; import { presentDocument } from "@server/presenters"; import AuthenticationHelper from "@shared/helpers/AuthenticationHelper"; @@ -22,6 +21,7 @@ import { withTracing, } from "./util"; import { TextEditMode } from "@shared/types"; +import SearchProviderManager from "@server/utils/SearchProviderManager"; /** * Registers document-related MCP tools on the given server, filtered by @@ -93,6 +93,8 @@ export function documentTools(server: McpServer, scopes: string[]) { } if (query) { + const searchProvider = SearchProviderManager.getProvider(); + // If the query looks like a document ID or urlId, try direct // lookup first so exact matches appear at the top of results. let exactMatch: Document | null = null; @@ -109,7 +111,7 @@ export function documentTools(server: McpServer, scopes: string[]) { } } - const { results } = await SearchHelper.searchForUser(user, { + const { results } = await searchProvider.searchForUser(user, { query, collectionId, offset: effectiveOffset, diff --git a/server/utils/BaseSearchProvider.ts b/server/utils/BaseSearchProvider.ts new file mode 100644 index 0000000000..8a2ef5365a --- /dev/null +++ b/server/utils/BaseSearchProvider.ts @@ -0,0 +1,149 @@ +import type { DateFilter } from "@shared/types"; +import type { SearchableModel } from "@shared/types"; +import type { DirectionFilter, SortFilter, StatusFilter } from "@shared/types"; +import type Collection from "@server/models/Collection"; +import type Comment from "@server/models/Comment"; +import type Document from "@server/models/Document"; +import type Share from "@server/models/Share"; +import type Team from "@server/models/Team"; +import type User from "@server/models/User"; + +export interface SearchResponse { + results: { + /** The search ranking, for sorting results. */ + ranking: number; + /** A snippet of contextual text around the search result. */ + context?: string; + /** The document result. */ + document: Document; + }[]; + /** The total number of results for the search query without pagination. */ + total: number; +} + +export interface SearchOptions { + /** The query limit for pagination. */ + limit?: number; + /** The query offset for pagination. */ + offset?: number; + /** The text to search for. */ + query?: string; + /** Limit results to a collection. Authorization is presumed to have been done before passing to this provider. */ + collectionId?: string | null; + /** Limit results to a shared document. */ + share?: Share; + /** Limit results to a date range. */ + dateFilter?: DateFilter; + /** Status of the documents to return. */ + statusFilter?: StatusFilter[]; + /** Limit results to a list of documents. */ + documentIds?: string[]; + /** Limit results to a list of users that collaborated on the document. */ + collaboratorIds?: string[]; + /** The minimum number of words to be returned in the contextual snippet. */ + snippetMinWords?: number; + /** The maximum number of words to be returned in the contextual snippet. */ + snippetMaxWords?: number; + /** The field to sort results by. */ + sort?: SortFilter; + /** The sort direction. */ + direction?: DirectionFilter; + /** Whether to boost results by popularity score. Defaults to true. */ + usePopularityBoost?: boolean; +} + +/** + * Abstract base class for search providers. Implementations handle full-text + * search, title search, collection search, and index management. + */ +export abstract class BaseSearchProvider { + /** Unique identifier for this provider, matched against `SEARCH_PROVIDER` env var. */ + abstract id: string; + + /** + * Perform a full-text search scoped to a user's accessible documents. + * + * @param user - the user performing the search. + * @param options - search options. + * @returns search results with ranking and context. + */ + abstract searchForUser( + user: User, + options?: SearchOptions + ): Promise; + + /** + * Perform a full-text search scoped to a team (used for shared document search). + * + * @param team - the team to search within. + * @param options - search options. + * @returns search results with ranking and context. + */ + abstract searchForTeam( + team: Team, + options?: SearchOptions + ): Promise; + + /** + * Search document titles for a user (used for link suggestions, quick search). + * + * @param user - the user performing the search. + * @param options - search options. + * @returns matching documents. + */ + abstract searchTitlesForUser( + user: User, + options?: SearchOptions + ): Promise; + + /** + * Search collections for a user. + * + * @param user - the user performing the search. + * @param options - search options. + * @returns matching collections. + */ + abstract searchCollectionsForUser( + user: User, + options?: SearchOptions + ): Promise; + + /** + * Index or re-index a searchable item. For providers that rely on database + * triggers (e.g. PostgreSQL tsvector), this may be a no-op. + * + * @param model - the type of model being indexed. + * @param item - the model instance to index. + */ + abstract index( + model: SearchableModel, + item: Document | Collection | Comment + ): Promise; + + /** + * Remove an item from the search index. + * + * @param model - the type of model being removed. + * @param id - the id of the item to remove. + * @param teamId - the team id the item belongs to. + */ + abstract remove( + model: SearchableModel, + id: string, + teamId: string + ): Promise; + + /** + * Update metadata for an indexed item without re-indexing the full content. + * Useful for permission changes, moves, archive/unarchive. + * + * @param model - the type of model being updated. + * @param id - the id of the item to update. + * @param metadata - the metadata fields to update. + */ + abstract updateMetadata( + model: SearchableModel, + id: string, + metadata: Record + ): Promise; +} diff --git a/server/utils/PluginManager.ts b/server/utils/PluginManager.ts index 5851b9a21a..b39de18478 100644 --- a/server/utils/PluginManager.ts +++ b/server/utils/PluginManager.ts @@ -11,6 +11,7 @@ import type { BaseTask } from "@server/queues/tasks/base/BaseTask"; import type { UnfurlSignature, UninstallSignature } from "@server/types"; import type { BaseIssueProvider } from "./BaseIssueProvider"; import type { GroupSyncProvider } from "./GroupSyncProvider"; +import type { BaseSearchProvider } from "./BaseSearchProvider"; export enum PluginPriority { VeryHigh = 0, @@ -29,6 +30,7 @@ export enum Hook { EmailTemplate = "emailTemplate", IssueProvider = "issueProvider", Processor = "processor", + SearchProvider = "searchProvider", Task = "task", UnfurlProvider = "unfurl", Uninstall = "uninstall", @@ -45,6 +47,7 @@ type PluginValueMap = { [Hook.EmailTemplate]: typeof BaseEmail; [Hook.IssueProvider]: BaseIssueProvider; [Hook.Processor]: typeof BaseProcessor; + [Hook.SearchProvider]: BaseSearchProvider; [Hook.Task]: typeof BaseTask; [Hook.Uninstall]: UninstallSignature; [Hook.UnfurlProvider]: { unfurl: UnfurlSignature; cacheExpiry: number }; @@ -106,9 +109,10 @@ export class PluginManager { /** * Returns all the plugins of a given type in order of priority. + * Triggers loading of all plugins from disk if not already loaded. * - * @param type The type of plugin to filter by - * @returns A list of plugins + * @param type - the type of plugin to filter by. + * @returns a list of plugins. */ public static getHooks(type: T) { this.loadPlugins(); @@ -139,9 +143,9 @@ export class PluginManager { glob .sync(path.join(rootDir, "plugins/*/server/!(*.test|schema).[jt]s")) - .forEach((filePath: string) => { - require(path.join(process.cwd(), filePath)); - }); + .forEach((filePath: string) => + require(path.join(process.cwd(), filePath)) + ); this.loaded = true; } diff --git a/server/utils/SearchProviderManager.ts b/server/utils/SearchProviderManager.ts new file mode 100644 index 0000000000..e4fb664864 --- /dev/null +++ b/server/utils/SearchProviderManager.ts @@ -0,0 +1,49 @@ +import env from "@server/env"; +import Logger from "@server/logging/Logger"; +import type { BaseSearchProvider } from "./BaseSearchProvider"; +import { Hook, PluginManager } from "./PluginManager"; + +/** + * Manages selection and caching of the active search provider based on the + * `SEARCH_PROVIDER` environment variable. + */ +export default class SearchProviderManager { + private static cachedProvider: BaseSearchProvider | undefined; + + /** + * Returns the active search provider. The provider is determined by matching + * `SEARCH_PROVIDER` env var against registered `Hook.SearchProvider` plugins. + * + * @returns the active search provider instance. + * @throws if no matching provider is found. + */ + public static getProvider(): BaseSearchProvider { + if (this.cachedProvider) { + return this.cachedProvider; + } + + const providerId = env.SEARCH_PROVIDER; + const plugins = PluginManager.getHooks(Hook.SearchProvider); + + for (const plugin of plugins) { + if (plugin.value.id === providerId) { + this.cachedProvider = plugin.value; + Logger.debug("plugins", `Using search provider: ${plugin.value.id}`); + return this.cachedProvider; + } + } + + throw new Error( + `Search provider "${providerId}" not found. Available providers: ${plugins + .map((p) => p.value.id) + .join(", ")}` + ); + } + + /** + * Reset the cached provider. Useful for testing. + */ + public static reset(): void { + this.cachedProvider = undefined; + } +} diff --git a/shared/types.ts b/shared/types.ts index de6b1ee535..f50d0620ec 100644 --- a/shared/types.ts +++ b/shared/types.ts @@ -32,6 +32,13 @@ export enum DirectionFilter { DESC = "DESC", } +/** Model types that support search indexing. */ +export enum SearchableModel { + Document = "document", + Collection = "collection", + Comment = "comment", +} + export enum CollectionStatusFilter { Archived = "archived", } From 45b2f6e2222d6f00a61752f3e5857c9526409e82 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Thu, 26 Mar 2026 00:15:28 -0400 Subject: [PATCH 11/17] fix: read-only scoped API keys cannot access MCP (#11875) --- server/models/ApiKey.test.ts | 19 ++++++++ server/models/ApiKey.ts | 6 +++ .../models/oauth/OAuthAuthentication.test.ts | 48 +++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 server/models/oauth/OAuthAuthentication.test.ts diff --git a/server/models/ApiKey.test.ts b/server/models/ApiKey.test.ts index 8b62cf6545..09a53e8018 100644 --- a/server/models/ApiKey.test.ts +++ b/server/models/ApiKey.test.ts @@ -1,4 +1,5 @@ import { randomString } from "@shared/random"; +import { Scope } from "@shared/types"; import { buildApiKey } from "@server/test/factories"; import ApiKey from "./ApiKey"; @@ -110,5 +111,23 @@ describe("#ApiKey", () => { expect(apiKey.canAccess("/api/documents.create")).toBe(false); expect(apiKey.canAccess("/api/collections.create")).toBe(false); }); + + it("should allow MCP access for scoped API keys", async () => { + const apiKey = await buildApiKey({ + name: "Dev", + scope: [Scope.Read], + }); + + expect(apiKey.canAccess("/mcp")).toBe(true); + expect(apiKey.canAccess("/mcp/")).toBe(true); + }); + + it("should allow MCP access for unscoped API keys", async () => { + const apiKey = await buildApiKey({ + name: "Dev", + }); + + expect(apiKey.canAccess("/mcp")).toBe(true); + }); }); }); diff --git a/server/models/ApiKey.ts b/server/models/ApiKey.ts index d9ccf42a07..cb3a7d5770 100644 --- a/server/models/ApiKey.ts +++ b/server/models/ApiKey.ts @@ -176,6 +176,12 @@ class ApiKey extends ParanoidModel< return true; } + // MCP endpoint access is allowed if the key has any valid scope. + // Fine-grained scope enforcement happens at the tool level. + if (path.startsWith("/mcp")) { + return this.scope.length > 0; + } + return AuthenticationHelper.canAccess(path, this.scope); }; } diff --git a/server/models/oauth/OAuthAuthentication.test.ts b/server/models/oauth/OAuthAuthentication.test.ts new file mode 100644 index 0000000000..978edf079e --- /dev/null +++ b/server/models/oauth/OAuthAuthentication.test.ts @@ -0,0 +1,48 @@ +import { Scope } from "@shared/types"; +import { buildOAuthAuthentication, buildUser } from "@server/test/factories"; + +describe("OAuthAuthentication", () => { + describe("canAccess", () => { + it("should allow MCP access for scoped tokens", async () => { + const user = await buildUser(); + const authentication = await buildOAuthAuthentication({ + user, + scope: [Scope.Read], + }); + + expect(authentication.canAccess("/mcp")).toBe(true); + expect(authentication.canAccess("/mcp/")).toBe(true); + }); + + it("should deny MCP access for tokens with empty scope", async () => { + const user = await buildUser(); + const authentication = await buildOAuthAuthentication({ + user, + scope: [], + }); + + expect(authentication.canAccess("/mcp")).toBe(false); + }); + + it("should always allow the revoke endpoint", async () => { + const user = await buildUser(); + const authentication = await buildOAuthAuthentication({ + user, + scope: [Scope.Read], + }); + + expect(authentication.canAccess("/oauth/revoke")).toBe(true); + }); + + it("should check scopes for API paths", async () => { + const user = await buildUser(); + const authentication = await buildOAuthAuthentication({ + user, + scope: [Scope.Read], + }); + + expect(authentication.canAccess("/api/documents.list")).toBe(true); + expect(authentication.canAccess("/api/documents.update")).toBe(false); + }); + }); +}); From c91b839d229159348ff08878512f01a85d414c6c Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Fri, 27 Mar 2026 08:42:24 -0400 Subject: [PATCH 12/17] fix: Unable to resize imported image from docx (#11878) * fix: Mammoth converts docx images to tags with base64 data URIs but no width/height attributes * fix: Bound memory usage and prevent infinite loop in image dimension parsing Decode only a 64 KB prefix of base64 data URIs instead of the full payload, cap the JPEG marker scan at 64 KB, and bail on malformed segment lengths (< 2 or overflowing the buffer) to prevent an infinite loop on truncated data. Co-Authored-By: Claude Opus 4.6 (1M context) --------- Co-authored-by: Claude Opus 4.6 (1M context) --- server/utils/DocumentConverter.test.ts | 104 ++++++++++++++++++++++ server/utils/DocumentConverter.ts | 118 +++++++++++++++++++++++++ 2 files changed, 222 insertions(+) diff --git a/server/utils/DocumentConverter.test.ts b/server/utils/DocumentConverter.test.ts index 3857992a07..594c3e0c2a 100644 --- a/server/utils/DocumentConverter.test.ts +++ b/server/utils/DocumentConverter.test.ts @@ -395,5 +395,109 @@ Content`; expect(image.attrs.width).toBe(400); expect(image.attrs.height).toBe(300); }); + + it("should extract dimensions from PNG data URI images", () => { + // Minimal 2x3 PNG (IHDR: width=2, height=3) + const pngBuffer = Buffer.alloc(33); + // PNG signature + Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]).copy( + pngBuffer + ); + // IHDR chunk length (13 bytes) + pngBuffer.writeUInt32BE(13, 8); + // "IHDR" + Buffer.from("IHDR").copy(pngBuffer, 12); + // Width = 200 + pngBuffer.writeUInt32BE(200, 16); + // Height = 150 + pngBuffer.writeUInt32BE(150, 20); + + const base64 = pngBuffer.toString("base64"); + const html = `

`; + + const doc = DocumentConverter.htmlToProsemirror(html); + + const paragraph = doc.content.child(0); + const image = paragraph.content.child(0); + expect(image.type.name).toBe("image"); + expect(image.attrs.width).toBe(200); + expect(image.attrs.height).toBe(150); + }); + + it("should extract dimensions from JPEG data URI images", () => { + // Minimal JPEG with SOF0 marker + const jpegBuffer = Buffer.alloc(20); + // JPEG SOI marker + jpegBuffer[0] = 0xff; + jpegBuffer[1] = 0xd8; + // SOF0 marker + jpegBuffer[2] = 0xff; + jpegBuffer[3] = 0xc0; + // Segment length + jpegBuffer.writeUInt16BE(17, 4); + // Precision + jpegBuffer[6] = 8; + // Height = 300 + jpegBuffer.writeUInt16BE(300, 7); + // Width = 400 + jpegBuffer.writeUInt16BE(400, 9); + + const base64 = jpegBuffer.toString("base64"); + const html = `

`; + + const doc = DocumentConverter.htmlToProsemirror(html); + + const paragraph = doc.content.child(0); + const image = paragraph.content.child(0); + expect(image.type.name).toBe("image"); + expect(image.attrs.width).toBe(400); + expect(image.attrs.height).toBe(300); + }); + + it("should extract dimensions from GIF data URI images", () => { + // Minimal GIF header + const gifBuffer = Buffer.alloc(10); + // GIF signature + Buffer.from("GIF89a").copy(gifBuffer); + // Width = 320 (little-endian) + gifBuffer.writeUInt16LE(320, 6); + // Height = 240 (little-endian) + gifBuffer.writeUInt16LE(240, 8); + + const base64 = gifBuffer.toString("base64"); + const html = `

`; + + const doc = DocumentConverter.htmlToProsemirror(html); + + const paragraph = doc.content.child(0); + const image = paragraph.content.child(0); + expect(image.type.name).toBe("image"); + expect(image.attrs.width).toBe(320); + expect(image.attrs.height).toBe(240); + }); + + it("should not override existing width/height on data URI images", () => { + // PNG with dimensions 200x150 but HTML attributes say 100x75 + const pngBuffer = Buffer.alloc(33); + Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]).copy( + pngBuffer + ); + pngBuffer.writeUInt32BE(13, 8); + Buffer.from("IHDR").copy(pngBuffer, 12); + pngBuffer.writeUInt32BE(200, 16); + pngBuffer.writeUInt32BE(150, 20); + + const base64 = pngBuffer.toString("base64"); + const html = `

`; + + const doc = DocumentConverter.htmlToProsemirror(html); + + const paragraph = doc.content.child(0); + const image = paragraph.content.child(0); + expect(image.type.name).toBe("image"); + // Should use the HTML attributes, not the parsed dimensions + expect(image.attrs.width).toBe(100); + expect(image.attrs.height).toBe(75); + }); }); }); diff --git a/server/utils/DocumentConverter.ts b/server/utils/DocumentConverter.ts index 91ac6cf9fc..28878f446f 100644 --- a/server/utils/DocumentConverter.ts +++ b/server/utils/DocumentConverter.ts @@ -147,6 +147,30 @@ export class DocumentConverter { const calculatedHeight = Math.round(parseInt(dataHeight) / ratio); img.setAttribute("height", String(calculatedHeight)); } + + // Extract dimensions from data URI images that lack width/height + // (e.g. images embedded by mammoth during docx import). + // Only decode a small prefix of the base64 data — headers for all + // supported formats live within the first 64 KB of the file. + if (!img.getAttribute("width") && !img.getAttribute("height")) { + const src = img.getAttribute("src") || ""; + if (src.startsWith("data:") && src.includes(";base64,")) { + const base64Start = src.indexOf(";base64,") + 8; + // 4 base64 chars → 3 bytes; decode at most ~64 KB of image data. + const maxBase64Chars = Math.ceil(65536 / 3) * 4; + const base64Prefix = src.slice( + base64Start, + base64Start + maxBase64Chars + ); + const dimensions = this.getImageDimensionsFromBuffer( + Buffer.from(base64Prefix, "base64") + ); + if (dimensions) { + img.setAttribute("width", String(dimensions.width)); + img.setAttribute("height", String(dimensions.height)); + } + } + } }); } @@ -443,4 +467,98 @@ export class DocumentConverter { return yamlCodeblock + remainingContent; } + + /** + * Parse image dimensions from a binary buffer. Supports PNG, JPEG, and GIF. + * + * @param buffer The image data. + * @returns The width and height if parseable, otherwise undefined. + */ + private static getImageDimensionsFromBuffer( + buffer: Buffer + ): { width: number; height: number } | undefined { + try { + // PNG: signature + IHDR chunk + if ( + buffer.length >= 24 && + buffer[0] === 0x89 && + buffer[1] === 0x50 && + buffer[2] === 0x4e && + buffer[3] === 0x47 + ) { + return { + width: buffer.readUInt32BE(16), + height: buffer.readUInt32BE(20), + }; + } + + // GIF: signature + logical screen descriptor + if ( + buffer.length >= 10 && + buffer[0] === 0x47 && + buffer[1] === 0x49 && + buffer[2] === 0x46 + ) { + return { + width: buffer.readUInt16LE(6), + height: buffer.readUInt16LE(8), + }; + } + + // JPEG: scan for SOF marker (cap at 64 KB to bound work) + if (buffer.length >= 2 && buffer[0] === 0xff && buffer[1] === 0xd8) { + const scanLimit = Math.min(buffer.length, 65536); + let offset = 2; + while (offset + 1 < scanLimit) { + if (buffer[offset] !== 0xff) { + offset++; + continue; + } + const marker = buffer[offset + 1]; + offset += 2; + + // Standalone markers without a payload + if ( + marker === 0x00 || + marker === 0x01 || + (marker >= 0xd0 && marker <= 0xd9) + ) { + continue; + } + + if (offset + 2 > scanLimit) { + break; + } + const segmentLength = buffer.readUInt16BE(offset); + + // SOF markers contain the frame dimensions — check before + // the advance guard since this returns immediately. + if ( + (marker >= 0xc0 && marker <= 0xc3) || + (marker >= 0xc5 && marker <= 0xc7) || + (marker >= 0xc9 && marker <= 0xcb) || + (marker >= 0xcd && marker <= 0xcf) + ) { + if (offset + 7 <= buffer.length) { + return { + height: buffer.readUInt16BE(offset + 3), + width: buffer.readUInt16BE(offset + 5), + }; + } + break; + } + + // Length includes itself and must be >= 2; bail on malformed data. + if (segmentLength < 2 || offset + segmentLength > buffer.length) { + break; + } + + offset += segmentLength; + } + } + } catch { + // Return undefined if parsing fails + } + return undefined; + } } From 0b213bd6b8fc59a9e2679b8dadacba579a3ae3c4 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Fri, 27 Mar 2026 08:42:32 -0400 Subject: [PATCH 13/17] feat: Map document creator to existing users during JSON import (#11879) * feat: Map creator/updater IDs to existing users during JSON import When importing documents from JSON, resolve the original document author to an internal user by matching on user ID first, then email, falling back to the importing user. Results are cached to avoid redundant queries. Co-Authored-By: Claude Opus 4.6 * fix: Add negative caching for user resolution during import Cache misses (not just hits) in resolveUserId so that repeated lookups for users that don't exist in the target team are served from cache instead of hitting the database for every document. Co-Authored-By: Claude Opus 4.6 * docs: Fix resolveUserId JSDoc to match actual behavior Co-Authored-By: Claude Opus 4.6 --------- Co-authored-by: Claude Opus 4.6 --- server/commands/documentCreator.ts | 8 +- server/queues/tasks/ImportJSONTask.test.ts | 164 ++++++++++++++++++--- server/queues/tasks/ImportTask.ts | 64 ++++++++ 3 files changed, 211 insertions(+), 25 deletions(-) diff --git a/server/commands/documentCreator.ts b/server/commands/documentCreator.ts index b2937a62f9..75f57ab361 100644 --- a/server/commands/documentCreator.ts +++ b/server/commands/documentCreator.ts @@ -25,6 +25,8 @@ type Props = Optional< | "publishedAt" | "createdAt" | "updatedAt" + | "createdById" + | "lastModifiedById" > > & { state?: Buffer; @@ -58,6 +60,8 @@ export default async function documentCreator( editorVersion, publishedAt, sourceMetadata, + createdById, + lastModifiedById, }: Props ): Promise { const { user } = ctx.state.auth; @@ -108,8 +112,8 @@ export default async function documentCreator( teamId: user.teamId, createdAt, updatedAt: updatedAt ?? createdAt, - lastModifiedById: user.id, - createdById: user.id, + lastModifiedById: lastModifiedById ?? createdById ?? user.id, + createdById: createdById ?? user.id, templateId, publishedAt, importId, diff --git a/server/queues/tasks/ImportJSONTask.test.ts b/server/queues/tasks/ImportJSONTask.test.ts index 936bd823c8..29767dfa81 100644 --- a/server/queues/tasks/ImportJSONTask.test.ts +++ b/server/queues/tasks/ImportJSONTask.test.ts @@ -1,37 +1,155 @@ import path from "node:path"; -import { FileOperation } from "@server/models"; -import { buildFileOperation } from "@server/test/factories"; +import { FileOperation, User } from "@server/models"; +import { + buildFileOperation, + buildUser, + buildTeam, + buildAdmin, +} from "@server/test/factories"; import ImportJSONTask from "./ImportJSONTask"; +// The fixture has these values for both documents: +// createdById: "ccec260a-e060-4925-ade8-17cfabaf2cac" +// createdByEmail: "hmac.devo@gmail.com" +const fixtureCreatedById = "ccec260a-e060-4925-ade8-17cfabaf2cac"; +const fixtureCreatedByEmail = "hmac.devo@gmail.com"; + +const fixturePath = path.resolve( + __dirname, + "..", + "..", + "test", + "fixtures", + "outline-json.zip" +); + +function mockHandle(fileOperation: FileOperation) { + Object.defineProperty(fileOperation, "handle", { + get() { + return { + path: fixturePath, + cleanup: async () => {}, + }; + }, + }); + jest.spyOn(FileOperation, "findByPk").mockResolvedValue(fileOperation); +} + describe("ImportJSONTask", () => { it("should import the documents, attachments", async () => { const fileOperation = await buildFileOperation(); - Object.defineProperty(fileOperation, "handle", { - get() { - return { - path: path.resolve( - __dirname, - "..", - "..", - "test", - "fixtures", - "outline-json.zip" - ), - cleanup: async () => {}, - }; - }, - }); - jest.spyOn(FileOperation, "findByPk").mockResolvedValue(fileOperation); - - const props = { - fileOperationId: fileOperation.id, - }; + mockHandle(fileOperation); const task = new ImportJSONTask(); - const response = await task.perform(props); + const response = await task.perform({ + fileOperationId: fileOperation.id, + }); expect(response.collections.size).toEqual(1); expect(response.documents.size).toEqual(2); expect(response.attachments.size).toEqual(1); }); + + describe("user mapping", () => { + it("should map createdById to an existing user by ID", async () => { + // Ensure a user exists with the fixture's createdById, handling the + // case where it may already exist from a prior test run. + let originalAuthor = await User.findByPk(fixtureCreatedById); + const teamId = originalAuthor?.teamId ?? (await buildTeam()).id; + + if (!originalAuthor) { + originalAuthor = await buildUser({ + id: fixtureCreatedById, + teamId, + }); + } + + const admin = await buildAdmin({ teamId }); + const fileOperation = await buildFileOperation({ + userId: admin.id, + teamId, + }); + mockHandle(fileOperation); + + const task = new ImportJSONTask(); + const response = await task.perform({ + fileOperationId: fileOperation.id, + }); + + for (const document of response.documents.values()) { + expect(document.createdById).toEqual(originalAuthor.id); + expect(document.lastModifiedById).toEqual(originalAuthor.id); + } + }); + + it("should fall back to email matching when ID does not match", async () => { + const team = await buildTeam(); + // User has matching email but a different ID + const originalAuthor = await buildUser({ + teamId: team.id, + email: fixtureCreatedByEmail, + }); + const admin = await buildAdmin({ teamId: team.id }); + const fileOperation = await buildFileOperation({ + userId: admin.id, + teamId: team.id, + }); + mockHandle(fileOperation); + + const task = new ImportJSONTask(); + const response = await task.perform({ + fileOperationId: fileOperation.id, + }); + + for (const document of response.documents.values()) { + expect(document.createdById).toEqual(originalAuthor.id); + expect(document.lastModifiedById).toEqual(originalAuthor.id); + } + }); + + it("should fall back to importing user when no match is found", async () => { + const team = await buildTeam(); + const admin = await buildAdmin({ teamId: team.id }); + const fileOperation = await buildFileOperation({ + userId: admin.id, + teamId: team.id, + }); + mockHandle(fileOperation); + + const task = new ImportJSONTask(); + const response = await task.perform({ + fileOperationId: fileOperation.id, + }); + + for (const document of response.documents.values()) { + expect(document.createdById).toEqual(admin.id); + expect(document.lastModifiedById).toEqual(admin.id); + } + }); + + it("should not match users from a different team", async () => { + const team = await buildTeam(); + const otherTeam = await buildTeam(); + // Create user with matching email in a different team + await buildUser({ + teamId: otherTeam.id, + email: fixtureCreatedByEmail, + }); + const admin = await buildAdmin({ teamId: team.id }); + const fileOperation = await buildFileOperation({ + userId: admin.id, + teamId: team.id, + }); + mockHandle(fileOperation); + + const task = new ImportJSONTask(); + const response = await task.perform({ + fileOperationId: fileOperation.id, + }); + + for (const document of response.documents.values()) { + expect(document.createdById).toEqual(admin.id); + } + }); + }); }); diff --git a/server/queues/tasks/ImportTask.ts b/server/queues/tasks/ImportTask.ts index 04ba669749..e3569b6f65 100644 --- a/server/queues/tasks/ImportTask.ts +++ b/server/queues/tasks/ImportTask.ts @@ -305,6 +305,7 @@ export default abstract class ImportTask extends BaseTask { const collections = new Map(); const documents = new Map(); const attachments = new Map(); + const userIdCache = new Map(); const user = await User.findByPk(fileOperation.userId, { rejectOnEmpty: true, @@ -437,6 +438,13 @@ export default abstract class ImportTask extends BaseTask { ); } + const resolvedUserId = + (await this.resolveUserId( + item, + fileOperation.teamId, + userIdCache + )) ?? fileOperation.userId; + const document = await documentCreator(ctx, { sourceMetadata: { fileName: path.basename(item.path), @@ -457,6 +465,8 @@ export default abstract class ImportTask extends BaseTask { publishedAt: item.updatedAt ?? item.createdAt ?? new Date(), parentDocumentId: item.parentDocumentId, importId: fileOperation.id, + createdById: resolvedUserId, + lastModifiedById: resolvedUserId, }); documents.set(item.id, document); @@ -535,6 +545,60 @@ export default abstract class ImportTask extends BaseTask { }; } + /** + * Resolves the original document author to an internal user, using a cache + * to avoid redundant database queries. Attempts to match by user ID first, + * then by email. Both hits and misses are cached. + * + * @param item the document import item containing createdById and createdByEmail. + * @param teamId the team ID to scope the lookup to. + * @param cache a map used to cache resolved user IDs across calls. + * @returns the resolved user ID, or undefined if no match was found. + */ + private async resolveUserId( + item: { createdById?: string; createdByEmail?: string | null }, + teamId: string, + cache: Map + ): Promise { + if (item.createdById) { + const cacheKey = `id:${item.createdById}`; + if (cache.has(cacheKey)) { + return cache.get(cacheKey); + } + + const user = await User.findOne({ + where: { id: item.createdById, teamId }, + }); + if (user) { + cache.set(cacheKey, user.id); + return user.id; + } + cache.set(cacheKey, undefined); + } + + if (item.createdByEmail) { + const email = item.createdByEmail.toLowerCase().trim(); + const cacheKey = `email:${email}`; + if (cache.has(cacheKey)) { + return cache.get(cacheKey); + } + + const user = await User.findOne({ + where: { email, teamId }, + }); + if (user) { + cache.set(cacheKey, user.id); + if (item.createdById) { + cache.set(`id:${item.createdById}`, user.id); + } + return user.id; + } + cache.set(cacheKey, undefined); + } + + return undefined; + } + private async preprocessDocUrlIds(data: StructuredImportData) { for (const doc of data.documents) { // check DB only if urlId is present in the input. From 3cc85f1cdf6b256aa6c53d09c25904150de130a7 Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Fri, 27 Mar 2026 17:05:21 -0400 Subject: [PATCH 14/17] Fix DocumentMove dialog hiding siblings and nieces/nephews as move targets (#11885) * Initial plan * fix: show siblings and descendants in DocumentMove dialog The filterSourceDocument function was incorrectly removing the document's parent node from the navigation tree, which also hid all siblings (children of the same parent) and their descendants. Instead, only the document itself and its own descendants are now excluded (to prevent circular references). The parent is kept in the tree so siblings remain visible as valid move targets. Agent-Logs-Url: https://github.com/outline/outline/sessions/12574f1c-7a7c-45a0-8444-19e24aa10782 Co-authored-by: tommoor <380914+tommoor@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: tommoor <380914+tommoor@users.noreply.github.com> --- .../DocumentExplorer/DocumentMove.tsx | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/app/components/DocumentExplorer/DocumentMove.tsx b/app/components/DocumentExplorer/DocumentMove.tsx index 4444b33c38..5a05f74474 100644 --- a/app/components/DocumentExplorer/DocumentMove.tsx +++ b/app/components/DocumentExplorer/DocumentMove.tsx @@ -3,6 +3,7 @@ import { useState, useMemo } from "react"; import { useTranslation, Trans } from "react-i18next"; import { toast } from "sonner"; import type { NavigationNode } from "@shared/types"; +import { descendants, flattenTree } from "@shared/utils/tree"; import type Document from "~/models/Document"; import Button from "~/components/Button"; import Text from "~/components/Text"; @@ -23,13 +24,23 @@ function DocumentMove({ document }: Props) { const [selectedPath, selectPath] = useState(null); const items = useMemo(() => { - // Recursively filter out the document itself and its existing parent doc, if any. + // Collect the IDs of the document itself and all of its descendants so they + // can be excluded from the move targets (moving to self or a descendant + // would create a cycle; moving to the exact same location is a no-op). + const allNodes = collectionTrees.flatMap(flattenTree); + const sourceNode = allNodes.find((node) => node.id === document.id); + const excludedIds = new Set([document.id]); + if (sourceNode) { + descendants(sourceNode).forEach((n) => excludedIds.add(n.id)); + } + + // Recursively filter out the document itself and its descendants. + // The document's current parent is intentionally kept so that siblings + // remain visible as valid move targets. const filterSourceDocument = (node: NavigationNode): NavigationNode => ({ ...node, children: node.children - ?.filter( - (c) => c.id !== document.id && c.id !== document.parentDocumentId - ) + ?.filter((c) => !excludedIds.has(c.id)) .map(filterSourceDocument), }); @@ -43,7 +54,7 @@ function DocumentMove({ document }: Props) { ); return nodes; - }, [policies, collectionTrees, document.id, document.parentDocumentId]); + }, [policies, collectionTrees, document.id]); const move = async () => { if (!selectedPath) { From df5dd0b98d1e4d23c768ea746e2171f3ae74b597 Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Fri, 27 Mar 2026 19:55:15 -0400 Subject: [PATCH 15/17] Fix custom team logo not appearing in link previews for public shares (#11872) * Initial plan * fix: resolve team avatar to signed URL for public share link previews Co-authored-by: tommoor <380914+tommoor@users.noreply.github.com> Agent-Logs-Url: https://github.com/outline/outline/sessions/3632734e-1bb5-4705-bdcd-a2ccbb211af8 * refactor: move avatar URL resolution to Team.publicAvatarUrl() Co-authored-by: tommoor <380914+tommoor@users.noreply.github.com> Agent-Logs-Url: https://github.com/outline/outline/sessions/a2191be3-0533-459a-8366-602bb798a60e * test: add Team.publicAvatarUrl model tests; update JSDoc Co-authored-by: tommoor <380914+tommoor@users.noreply.github.com> Agent-Logs-Url: https://github.com/outline/outline/sessions/7609501c-a4d1-44ea-a7bf-fa6fd8e7c999 * test: fix Team.publicAvatarUrl tests to use actual attachment URLs Co-authored-by: tommoor <380914+tommoor@users.noreply.github.com> Agent-Logs-Url: https://github.com/outline/outline/sessions/0a768f8b-0dd8-4e7a-a50d-873af58aab28 --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: tommoor <380914+tommoor@users.noreply.github.com> --- server/models/Team.test.ts | 51 +++++++++++++++++++++++++++++++++++++- server/models/Team.ts | 34 +++++++++++++++++++++++++ server/routes/app.ts | 4 ++- 3 files changed, 87 insertions(+), 2 deletions(-) diff --git a/server/models/Team.test.ts b/server/models/Team.test.ts index 25665bea59..9ece16cbe2 100644 --- a/server/models/Team.test.ts +++ b/server/models/Team.test.ts @@ -1,4 +1,5 @@ -import { buildTeam, buildCollection } from "@server/test/factories"; +import { randomUUID } from "node:crypto"; +import { buildTeam, buildCollection, buildAttachment } from "@server/test/factories"; describe("Team", () => { describe("collectionIds", () => { @@ -40,4 +41,52 @@ describe("Team", () => { expect(team.previousSubdomains?.[1]).toEqual(subdomain); }); }); + + describe("publicAvatarUrl", () => { + it("should return null when no avatarUrl is set", async () => { + const team = await buildTeam({ avatarUrl: null }); + const result = await team.publicAvatarUrl(); + expect(result).toBeNull(); + }); + + it("should return external URL unchanged", async () => { + const url = "https://example.com/logo.png"; + const team = await buildTeam({ avatarUrl: url }); + const result = await team.publicAvatarUrl(); + expect(result).toEqual(url); + }); + + it("should return signed URL for private-bucket attachment redirect", async () => { + const team = await buildTeam(); + const attachment = await buildAttachment({ + teamId: team.id, + acl: "private", + }); + + await team.update({ + avatarUrl: `/api/attachments.redirect?id=${attachment.id}`, + }); + + const result = await team.publicAvatarUrl(); + expect(result).toEqual(await attachment.signedUrl); + }); + + it("should return canonical URL for public-bucket attachment redirect", async () => { + const team = await buildTeam(); + const id = randomUUID(); + const attachment = await buildAttachment({ + id, + teamId: team.id, + key: `avatars/${team.id}/${id}/logo.png`, + acl: "public-read", + }); + + await team.update({ + avatarUrl: `/api/attachments.redirect?id=${attachment.id}`, + }); + + const result = await team.publicAvatarUrl(); + expect(result).toEqual(attachment.canonicalUrl); + }); + }); }); diff --git a/server/models/Team.ts b/server/models/Team.ts index 80849d9856..6fa258cab5 100644 --- a/server/models/Team.ts +++ b/server/models/Team.ts @@ -29,6 +29,7 @@ import { TeamPreferenceDefaults } from "@shared/constants"; import type { TeamPreferences } from "@shared/types"; import { TeamPreference, UserRole } from "@shared/types"; import { getBaseDomain, RESERVED_SUBDOMAINS } from "@shared/utils/domains"; +import { attachmentRedirectRegex } from "@shared/utils/ProsemirrorHelper"; import { parseEmail } from "@shared/utils/email"; import { TeamValidation } from "@shared/validations"; import env from "@server/env"; @@ -57,6 +58,8 @@ export enum TeamFlag { MarkedSafe = "markedSafe", } +const avatarRedirectPattern = new RegExp(attachmentRedirectRegex.source, "i"); + @Scopes(() => ({ withDomains: { include: [{ model: TeamDomain }], @@ -145,6 +148,37 @@ class Team extends ParanoidModel< this.setDataValue("avatarUrl", value); } + /** + * Returns a directly-accessible URL for the team's avatar suitable for use + * in contexts without authentication. Attachment is loaded and a signed (or + * canonical) URL is returned; any other URL is returned unchanged. + * + * @returns A promise resolving to a direct URL, or null when no avatar is set. + */ + async publicAvatarUrl(): Promise { + const url = this.avatarUrl; + if (!url) { + return null; + } + + const match = avatarRedirectPattern.exec(url); + if (!match?.groups?.id) { + return url; + } + + const attachment = await Attachment.findOne({ + where: { id: match.groups.id, teamId: this.id }, + }); + + if (!attachment) { + return url; + } + + return attachment.isStoredInPublicBucket + ? attachment.canonicalUrl + : await attachment.signedUrl; + } + @Default(true) @Column sharing: boolean; diff --git a/server/routes/app.ts b/server/routes/app.ts index b6fbaad024..b598752f29 100644 --- a/server/routes/app.ts +++ b/server/routes/app.ts @@ -340,7 +340,9 @@ export const renderShare = async (ctx: Context, next: Next) => { (publicBranding && team?.description ? team.description : undefined), content, shortcutIcon: - publicBranding && team?.avatarUrl ? team.avatarUrl : undefined, + publicBranding && team?.avatarUrl + ? (await team.publicAvatarUrl()) ?? undefined + : undefined, analytics, isShare: true, rootShareId, From 27dc02aad1258a58664166bf0de9d504d5a54a24 Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Sat, 28 Mar 2026 15:08:34 -0400 Subject: [PATCH 16/17] Add anchor text to MCP comment tool responses (#11886) * Initial plan * Add comment anchor text to MCP comment tool responses Agent-Logs-Url: https://github.com/outline/outline/sessions/294b6510-996f-4a86-a7d6-7ed1c336fc19 Co-authored-by: tommoor <380914+tommoor@users.noreply.github.com> * Address PR review: fix auth gap, cache marks, add anchorText tests - Always authorize read access in update_comment before exposing anchor text - Cache comment marks per document in list_comments to avoid O(n * docSize) - Add 4 MCP tests verifying anchorText presence/absence in responses Co-Authored-By: Claude Opus 4.6 --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: tommoor <380914+tommoor@users.noreply.github.com> Co-authored-by: Tom Moor Co-authored-by: Claude Opus 4.6 --- server/presenters/comment.ts | 22 +++-- server/routes/mcp/index.test.ts | 144 ++++++++++++++++++++++++++++++++ server/tools/comments.ts | 39 ++++++++- 3 files changed, 193 insertions(+), 12 deletions(-) diff --git a/server/presenters/comment.ts b/server/presenters/comment.ts index 86a4b8a371..18daa8caaf 100644 --- a/server/presenters/comment.ts +++ b/server/presenters/comment.ts @@ -1,4 +1,7 @@ -import { ProsemirrorHelper } from "@shared/utils/ProsemirrorHelper"; +import { + ProsemirrorHelper, + type CommentMark, +} from "@shared/utils/ProsemirrorHelper"; import type { Comment } from "@server/models"; import { DocumentHelper } from "@server/models/helpers/DocumentHelper"; import presentUser from "./user"; @@ -6,22 +9,23 @@ import presentUser from "./user"; type Options = { /** Whether to include anchor text, if it exists */ includeAnchorText?: boolean; + /** Precomputed comment marks to avoid reparsing the document. */ + commentMarks?: CommentMark[]; }; export default function present( comment: Comment, - { includeAnchorText }: Options = {} + { includeAnchorText, commentMarks }: Options = {} ) { let anchorText: string | undefined; if (includeAnchorText && comment.document) { - const commentMarks = ProsemirrorHelper.getComments( - DocumentHelper.toProsemirror(comment.document) - ); - anchorText = ProsemirrorHelper.getAnchorTextForComment( - commentMarks, - comment.id - ); + const marks = + commentMarks ?? + ProsemirrorHelper.getComments( + DocumentHelper.toProsemirror(comment.document) + ); + anchorText = ProsemirrorHelper.getAnchorTextForComment(marks, comment.id); } return { diff --git a/server/routes/mcp/index.test.ts b/server/routes/mcp/index.test.ts index 09d48068ca..782a832a0d 100644 --- a/server/routes/mcp/index.test.ts +++ b/server/routes/mcp/index.test.ts @@ -1,10 +1,12 @@ import { Scope, TeamPreference } from "@shared/types"; +import type { ProsemirrorData } from "@shared/types"; import { buildUser, buildAdmin, buildCollection, buildDocument, buildComment, + buildCommentMark, buildOAuthAuthentication, } from "@server/test/factories"; import { getTestServer } from "@server/test/support"; @@ -620,6 +622,148 @@ describe("POST /mcp/", () => { expect(data.text).toContain("Updated comment text"); }); + it("list_comments includes anchorText when comment is anchored", async () => { + const { user, accessToken } = await buildOAuthUser(); + const collection = await buildCollection({ + teamId: user.teamId, + userId: user.id, + }); + const document = await buildDocument({ + teamId: user.teamId, + userId: user.id, + collectionId: collection.id, + }); + const comment = await buildComment({ + userId: user.id, + documentId: document.id, + }); + + const anchorText = "highlighted text"; + const content = { + type: "doc", + content: [ + { + type: "paragraph", + content: [ + { + type: "text", + text: anchorText, + marks: [buildCommentMark({ id: comment.id, userId: user.id })], + }, + ], + }, + ], + } as ProsemirrorData; + await document.update({ content }); + + const res = await callMcpTool(server, accessToken, "list_comments", { + documentId: document.id, + }); + const data = (res?.result?.content ?? []).map((c: { text: string }) => + JSON.parse(c.text) + ); + + const match = data.find((c: { id: string }) => c.id === comment.id) as { + anchorText: string; + }; + expect(match).toBeDefined(); + expect(match.anchorText).toEqual(anchorText); + }); + + it("list_comments returns undefined anchorText for non-anchored comment", async () => { + const { user, accessToken } = await buildOAuthUser(); + const collection = await buildCollection({ + teamId: user.teamId, + userId: user.id, + }); + const document = await buildDocument({ + teamId: user.teamId, + userId: user.id, + collectionId: collection.id, + }); + await buildComment({ + userId: user.id, + documentId: document.id, + }); + + const res = await callMcpTool(server, accessToken, "list_comments", { + documentId: document.id, + }); + const data = (res?.result?.content ?? []).map((c: { text: string }) => + JSON.parse(c.text) + ); + + expect(data.length).toBeGreaterThanOrEqual(1); + expect(data[0].anchorText).toBeUndefined(); + }); + + it("create_comment includes anchorText in response", async () => { + const { user, accessToken } = await buildOAuthUser(); + const collection = await buildCollection({ + teamId: user.teamId, + userId: user.id, + }); + const document = await buildDocument({ + teamId: user.teamId, + userId: user.id, + collectionId: collection.id, + }); + + const res = await callMcpTool(server, accessToken, "create_comment", { + documentId: document.id, + text: "A new comment", + }); + const data = JSON.parse(res?.result?.content?.[0]?.text ?? "{}"); + + // New comments have no anchor mark in the document, so anchorText is undefined + expect(data.id).toBeDefined(); + expect(data.anchorText).toBeUndefined(); + }); + + it("update_comment includes anchorText in response", async () => { + const { user, accessToken } = await buildOAuthUser(); + const collection = await buildCollection({ + teamId: user.teamId, + userId: user.id, + }); + const document = await buildDocument({ + teamId: user.teamId, + userId: user.id, + collectionId: collection.id, + }); + const comment = await buildComment({ + userId: user.id, + documentId: document.id, + }); + + const anchorText = "anchored content"; + const content = { + type: "doc", + content: [ + { + type: "paragraph", + content: [ + { + type: "text", + text: anchorText, + marks: [buildCommentMark({ id: comment.id, userId: user.id })], + }, + ], + }, + ], + } as ProsemirrorData; + await document.update({ content }); + + const res = await callMcpTool(server, accessToken, "update_comment", { + id: comment.id, + text: "Updated text", + }); + const data = JSON.parse(res?.result?.content?.[0]?.text ?? "{}"); + + expect(data.id).toEqual(comment.id); + expect(data.anchorText).toEqual(anchorText); + }); + it("delete_comment deletes own comment", async () => { const { user, accessToken } = await buildOAuthUser(); const collection = await buildCollection({ diff --git a/server/tools/comments.ts b/server/tools/comments.ts index 5fd03814e5..4056598933 100644 --- a/server/tools/comments.ts +++ b/server/tools/comments.ts @@ -4,7 +4,10 @@ import type { FindOptions, WhereOptions } from "sequelize"; import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js"; import { CommentStatusFilter } from "@shared/types"; +import { ProsemirrorHelper } from "@shared/utils/ProsemirrorHelper"; +import type { CommentMark } from "@shared/utils/ProsemirrorHelper"; import { commentParser } from "@server/editor"; +import { DocumentHelper } from "@server/models/helpers/DocumentHelper"; import { Comment, Collection, Document } from "@server/models"; import { authorize } from "@server/policies"; import { presentComment } from "@server/presenters"; @@ -23,10 +26,17 @@ import { * ProseMirror JSON. * * @param comment - the comment model instance. + * @param commentMarks - optional precomputed comment marks to avoid reparsing. * @returns the presented comment with an additional `text` field. */ -function presentCommentWithText(comment: Comment) { - const presented = presentComment(comment); +function presentCommentWithText( + comment: Comment, + commentMarks?: CommentMark[] +) { + const presented = presentComment(comment, { + includeAnchorText: true, + commentMarks, + }); return { ...presented, text: comment.toPlainText(), @@ -182,7 +192,25 @@ export function commentTools(server: McpServer, scopes: string[]) { }); } - const presented = comments.map(presentCommentWithText); + // Precompute comment marks per document to avoid reparsing + // the same document for every comment. + const marksCache = new Map(); + const presented = comments.map((comment) => { + const doc = comment.document; + let marks: CommentMark[] | undefined; + if (doc) { + if (!marksCache.has(doc.id)) { + marksCache.set( + doc.id, + ProsemirrorHelper.getComments( + DocumentHelper.toProsemirror(doc) + ) + ); + } + marks = marksCache.get(doc.id); + } + return presentCommentWithText(comment, marks); + }); return success(presented); } catch (err) { return error(err); @@ -238,6 +266,7 @@ export function commentTools(server: McpServer, scopes: string[]) { }); comment.createdBy = user; + comment.document = document!; const presented = presentCommentWithText(comment); return { @@ -292,6 +321,9 @@ export function commentTools(server: McpServer, scopes: string[]) { userId: user.id, }); + authorize(user, "read", comment); + authorize(user, "read", document); + if (text !== undefined) { authorize(user, "update", comment); authorize(user, "comment", document); @@ -312,6 +344,7 @@ export function commentTools(server: McpServer, scopes: string[]) { await comment.saveWithCtx(ctx, status ? { silent: true } : undefined); + comment.document = document!; const presented = presentCommentWithText(comment); return { content: [ From 4b16545b10164d20cfa1999fce0deb85eedad149 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 28 Mar 2026 15:51:05 -0400 Subject: [PATCH 17/17] Fix Comment.toPlainText using wrong schema for mention nodes (#11889) Co-authored-by: Claude Opus 4.6 --- server/models/Comment.test.ts | 189 ++++++++++++++++++++++++++++++++++ server/models/Comment.ts | 4 +- 2 files changed, 191 insertions(+), 2 deletions(-) create mode 100644 server/models/Comment.test.ts diff --git a/server/models/Comment.test.ts b/server/models/Comment.test.ts new file mode 100644 index 0000000000..1b5799d530 --- /dev/null +++ b/server/models/Comment.test.ts @@ -0,0 +1,189 @@ +import { v4 as uuidv4 } from "uuid"; +import { MentionType } from "@shared/types"; +import { buildComment, buildDocument, buildUser } from "@server/test/factories"; +import Comment from "./Comment"; + +describe("Comment", () => { + describe("toPlainText", () => { + it("should convert simple text to plain text", async () => { + const user = await buildUser(); + const document = await buildDocument({ + userId: user.id, + teamId: user.teamId, + }); + const comment = await buildComment({ + userId: user.id, + documentId: document.id, + }); + + const text = comment.toPlainText(); + expect(text).toBe("test"); + }); + + it("should convert comment with mention to plain text", async () => { + const user = await buildUser(); + const document = await buildDocument({ + userId: user.id, + teamId: user.teamId, + }); + const comment = await Comment.create({ + documentId: document.id, + createdById: user.id, + data: { + type: "doc", + content: [ + { + type: "paragraph", + content: [ + { + type: "text", + text: "Hello ", + }, + { + type: "mention", + attrs: { + type: MentionType.User, + label: "Jane", + modelId: uuidv4(), + id: uuidv4(), + }, + }, + ], + }, + ], + }, + }); + + const text = comment.toPlainText(); + expect(text).toBe("Hello @Jane"); + }); + + it("should convert comment with document mention to plain text", async () => { + const user = await buildUser(); + const document = await buildDocument({ + userId: user.id, + teamId: user.teamId, + }); + const comment = await Comment.create({ + documentId: document.id, + createdById: user.id, + data: { + type: "doc", + content: [ + { + type: "paragraph", + content: [ + { + type: "text", + text: "See ", + }, + { + type: "mention", + attrs: { + type: MentionType.Document, + label: "My Document", + modelId: uuidv4(), + id: uuidv4(), + }, + }, + ], + }, + ], + }, + }); + + const text = comment.toPlainText(); + expect(text).toBe("See My Document"); + }); + }); + + describe("resolve", () => { + it("should resolve the comment", async () => { + const user = await buildUser(); + const document = await buildDocument({ + userId: user.id, + teamId: user.teamId, + }); + const comment = await buildComment({ + userId: user.id, + documentId: document.id, + }); + + comment.resolve(user); + + expect(comment.isResolved).toBe(true); + expect(comment.resolvedById).toBe(user.id); + expect(comment.resolvedAt).toBeTruthy(); + }); + + it("should throw if already resolved", async () => { + const user = await buildUser(); + const document = await buildDocument({ + userId: user.id, + teamId: user.teamId, + }); + const comment = await buildComment({ + userId: user.id, + documentId: document.id, + }); + + comment.resolve(user); + + expect(() => comment.resolve(user)).toThrow(); + }); + + it("should throw if comment is a reply", async () => { + const user = await buildUser(); + const document = await buildDocument({ + userId: user.id, + teamId: user.teamId, + }); + const parent = await buildComment({ + userId: user.id, + documentId: document.id, + }); + const reply = await buildComment({ + userId: user.id, + documentId: document.id, + parentCommentId: parent.id, + }); + + expect(() => reply.resolve(user)).toThrow(); + }); + }); + + describe("unresolve", () => { + it("should unresolve the comment", async () => { + const user = await buildUser(); + const document = await buildDocument({ + userId: user.id, + teamId: user.teamId, + }); + const comment = await buildComment({ + userId: user.id, + documentId: document.id, + }); + + comment.resolve(user); + comment.unresolve(); + + expect(comment.isResolved).toBe(false); + expect(comment.resolvedById).toBeNull(); + expect(comment.resolvedAt).toBeNull(); + }); + + it("should throw if not resolved", async () => { + const user = await buildUser(); + const document = await buildDocument({ + userId: user.id, + teamId: user.teamId, + }); + const comment = await buildComment({ + userId: user.id, + documentId: document.id, + }); + + expect(() => comment.unresolve()).toThrow(); + }); + }); +}); diff --git a/server/models/Comment.ts b/server/models/Comment.ts index a85be23a32..59d0a79ef6 100644 --- a/server/models/Comment.ts +++ b/server/models/Comment.ts @@ -13,7 +13,7 @@ import { import type { ProsemirrorData, ReactionSummary } from "@shared/types"; import { ProsemirrorHelper } from "@shared/utils/ProsemirrorHelper"; import { CommentValidation } from "@shared/validations"; -import { basicSchema } from "@server/editor"; +import { commentSchema } from "@server/editor"; import { ValidationError } from "@server/errors"; import Document from "./Document"; import User from "./User"; @@ -137,7 +137,7 @@ class Comment extends ParanoidModel< * @returns The plain text representation of the comment data */ public toPlainText() { - const node = Node.fromJSON(basicSchema, this.data); + const node = Node.fromJSON(commentSchema, this.data); return ProsemirrorHelper.toPlainText(node); }