import path from "node:path"; import fractionalIndex from "fractional-index"; import fs from "fs-extra"; import invariant from "invariant"; import contentDisposition from "content-disposition"; import Router from "@koa/router"; import { escapeRegExp, uniq } from "es-toolkit/compat"; import mime from "mime-types"; import type { Order, WhereOptions } from "sequelize"; import type { Filter } from "@shared/helpers/FilterHelper"; import { Op, Sequelize } from "sequelize"; import { randomUUID } from "node:crypto"; import type { DirectionFilter, SortFilter } from "@shared/types"; import { type NavigationNode } from "@shared/types"; import { DocumentPermission, ExportContentType, FileOperationFormat, StatusFilter, UserRole, } from "@shared/types"; import { subtractDate } from "@shared/utils/date"; import slugify from "@server/utils/slugify"; import { Day } from "@shared/utils/time"; import documentCreator, { authorizeDocumentCreate, authorizeDocumentPublish, } from "@server/commands/documentCreator"; import documentDuplicator from "@server/commands/documentDuplicator"; import documentLoader from "@server/commands/documentLoader"; import documentMover from "@server/commands/documentMover"; import documentPermanentDeleter from "@server/commands/documentPermanentDeleter"; import documentRestorer from "@server/commands/documentRestorer"; import documentUpdater from "@server/commands/documentUpdater"; import env from "@server/env"; import { InvalidRequestError, AuthenticationError, ValidationError, IncorrectEditionError, NotFoundError, } from "@server/errors"; import auth from "@server/middlewares/authentication"; import multipart from "@server/middlewares/multipart"; import { rateLimiter } from "@server/middlewares/rateLimiter"; import { transaction } from "@server/middlewares/transaction"; import validate from "@server/middlewares/validate"; import { Attachment, Relationship, Collection, Document, DocumentInsight, Event, SearchQuery, Template, User, View, UserMembership, Group, GroupUser, GroupMembership, FileOperation, } from "@server/models"; import { SearchQuerySource } from "@server/models/SearchQuery"; import AttachmentHelper from "@server/models/helpers/AttachmentHelper"; import { DocumentHelper } from "@server/models/helpers/DocumentHelper"; import { authorizeFilterFields, buildWhere, collectEqValues, combineFilters, expandDocumentIdInFilter, extractTopLevelEqValue, hasFieldInFilter, legacyParamsToFilter, mapFilterFields, } from "@server/models/helpers/Filters"; import HTMLHelper from "@server/models/helpers/HTMLHelper"; import { ProsemirrorHelper } from "@server/models/helpers/ProsemirrorHelper"; import TextBundleHelper from "@server/models/helpers/TextBundleHelper"; import SearchProviderManager from "@server/utils/SearchProviderManager"; import { TextHelper } from "@server/models/helpers/TextHelper"; import { authorize, can, cannot } from "@server/policies"; import { presentDocument, presentDocumentInsight, presentDocuments, presentPolicies, presentTemplate, presentMembership, presentUser, presentGroupMembership, presentGroup, presentFileOperation, } from "@server/presenters"; import DocumentImportTask from "@server/queues/tasks/DocumentImportTask"; import EmptyTrashTask from "@server/queues/tasks/EmptyTrashTask"; import FileStorage from "@server/storage/files"; import type { Context } from "@server/types"; import { toHookContext } from "@server/context"; import { RateLimiterStrategy } from "@server/utils/RateLimiter"; import { convertBareUrlsToEmbedMarkdown } from "@server/utils/embeds"; import { getClientAbortSignal, streamZipResponse } from "@server/utils/koa"; import { QueryHelper } from "@server/storage/QueryHelper"; import { getTeamFromContext } from "@server/utils/passport"; import pagination, { paginateQuery } from "../middlewares/pagination"; import * as T from "./schema"; import { loadPublicShare, getAllIdsInSharedTree, } from "@server/commands/shareLoader"; const router = new Router(); /** * Resolve every `documentId` leaf in a search filter to an `id in [...]` leaf * containing the document and all of its descendants. When a user is given, * each referenced document is authorized for read access. In share contexts * the expansion is only scoped to the share's team — results are constrained * to the share's own document subtree by the provider, so no per-document * authorization is needed. * * @param filter the search filter to transform. * @param context the user performing the search, or the share's team scope. * @returns the filter with documentId leaves expanded, or the original filter * if none are present. */ async function expandDocumentIdLeaves( filter: Filter, context: { user: User } | { teamId: string } ): Promise { const documentIds = uniq(collectEqValues(filter, "documentId")); if (documentIds.length === 0) { return filter; } const expandedIds = new Map(); await Promise.all( documentIds.map(async (documentId) => { if ("user" in context) { const document = await Document.findByPk(documentId, { userId: context.user.id, }); authorize(context.user, "read", document); expandedIds.set(documentId, [ documentId, ...(await document.findAllChildDocumentIds()), ]); } else { const document = await Document.unscoped().findOne({ where: { id: documentId, teamId: context.teamId }, }); expandedIds.set( documentId, document ? [documentId, ...(await document.findAllChildDocumentIds())] : [documentId] ); } }) ); return expandDocumentIdInFilter(filter, expandedIds); } router.post( "documents.list", rateLimiter(RateLimiterStrategy.OneHundredPerMinute), auth(), pagination(), validate(T.DocumentsListSchema), async (ctx: Context) => { const { sort, direction, backlinkDocumentId, parentDocumentId: legacyParentDocumentId, userId: legacyUserId, statusFilter, filters: rawFilters, } = ctx.input.body; let { collectionId: legacyCollectionId } = ctx.input.body; const { offset, limit } = ctx.state.pagination; // always filter by the current team const { user } = ctx.auth; const where: WhereOptions & { [Op.and]: WhereOptions[]; } = { [Op.and]: [ { deletedAt: { [Op.eq]: null, }, }, ], }; // Resolve the parent document being targeted from either the legacy // top-level param or the filters DSL, so the membership escape below // applies in both cases. `isNull` leaves resolve to undefined here // (no specific parent to authorize against). const normalizedFilter = combineFilters(rawFilters); const parentDocumentId = legacyParentDocumentId ?? (normalizedFilter ? extractTopLevelEqValue(normalizedFilter, "parentDocumentId") : undefined); // A member of the parent document may not have access to its collection, so // the legacy collectionId filter is dropped for them. if (parentDocumentId) { const [groupMembership, membership] = await Promise.all([ GroupMembership.findOne({ where: { documentId: parentDocumentId }, include: [ { model: Group, required: true, include: [ { model: GroupUser, required: true, where: { userId: user.id }, }, ], }, ], }), UserMembership.findOne({ where: { userId: user.id, documentId: parentDocumentId }, }), ]); if (groupMembership || membership) { legacyCollectionId = undefined; } } // The schema rejects callers that combine `filters` with the deprecated // top-level params, so exactly one of these is set. const filter = normalizedFilter ?? legacyParamsToFilter({ userId: legacyUserId, collectionId: legacyCollectionId, parentDocumentId: legacyParentDocumentId, }); // Exclude archived docs by default. Suppressed when the caller targets a // specific status, or when their filter already references archivedAt. const filterIncludesArchivedAt = filter !== undefined && hasFieldInFilter(filter, "archivedAt"); if (!statusFilter && !filterIncludesArchivedAt) { where[Op.and].push({ archivedAt: { [Op.eq]: null } }); } // Sort=index needs the collection's documentStructure for ordering and // pagination. Only meaningful when the filter targets a single collection. let documentIds: string[] = []; let collection: Collection | null | undefined; let indexTotal: number | undefined; const explicitCollectionId = filter !== undefined ? extractTopLevelEqValue(filter, "collectionId") : undefined; if (explicitCollectionId && sort === "index") { collection = await Collection.findByPk(explicitCollectionId, { userId: user.id, includeDocumentStructure: true, }); authorize(user, "readDocument", collection); const structure = await collection.structureFor(user); indexTotal = structure.length; // One extra id lets client requests detect a next page. documentIds = ( direction === "DESC" ? [...structure].reverse() : structure ) .slice(offset, offset + limit + 1) .map((node) => node.id); where[Op.and].push({ id: documentIds }); } // Apply filter and re-run authorize() for any auth-bearing fields. The // public-API `documentId` field is renamed to the underlying `id` column; // `userId` is handled inside `buildWhere` (maps to `collaboratorIds`). if (filter) { await authorizeFilterFields(user, filter, { collections: collection ? [collection] : undefined, }); const mapped = mapFilterFields(filter, { documentId: "id" }); where[Op.and].push(buildWhere(mapped, user.teamId)); } if (backlinkDocumentId) { const sourceDocumentIds = await Relationship.findSourceDocumentIds(backlinkDocumentId); where[Op.and].push({ id: sourceDocumentIds }); } // A filter referencing publishedAt can surface drafts (the replacement // for the deprecated statusFilter=draft). const filterIncludesDrafts = !statusFilter && filter !== undefined && hasFieldInFilter(filter, "publishedAt"); const statusQuery = []; if (statusFilter?.includes(StatusFilter.Published)) { statusQuery.push({ [Op.and]: [ { publishedAt: { [Op.ne]: null, }, archivedAt: { [Op.eq]: null, }, }, ], }); } if (statusFilter?.includes(StatusFilter.Draft)) { statusQuery.push({ [Op.and]: [ { publishedAt: { [Op.eq]: null, }, archivedAt: { [Op.eq]: null, }, }, ], }); } if (statusFilter?.includes(StatusFilter.Archived)) { statusQuery.push({ archivedAt: { [Op.ne]: null, }, }); } if (statusQuery.length) { where[Op.and].push({ [Op.or]: statusQuery, }); } // When sorting by index, use array_position to sort by the document order // in the collection structure directly in SQL, enabling correct pagination const orderClause = sort === "index" ? documentIds.length > 0 ? [ [ Sequelize.literal( `array_position(ARRAY[:documentIds]::uuid[], "document"."id")` ), "ASC", ], ] : undefined : [[sort, direction]]; const includeDrafts = !!statusFilter?.includes(StatusFilter.Draft) || filterIncludesDrafts; // The withDrafts scope drops the defaultScope filters, so re-apply the // ones we still want — templates and trial-import documents should never // appear in this listing. if (includeDrafts) { where[Op.and].push({ template: false, sourceMetadata: { trial: { [Op.is]: null } }, }); } // When sorting by index, pagination is already handled by slicing documentIds, // so we skip the SQL-level offset to avoid double-pagination const { results: documents, pagination } = await paginateQuery( ctx, ({ offset: queryOffset, limit: queryLimit }) => Document.findAllWithCtx(ctx, { includeDrafts, where, order: orderClause as Order, offset: sort === "index" ? 0 : queryOffset, limit: queryLimit, replacements: { documentIds, }, }), // The membership includes are LEFT JOINs that never filter rows, so // the total can be counted without them. async () => indexTotal ?? Document.countWithCtx(ctx, { includeDrafts, where, }) ); const data = await presentDocuments(ctx, documents); const policies = presentPolicies(user, documents); ctx.body = { pagination, data, policies, }; } ); router.post( "documents.archived", auth({ role: UserRole.Member }), pagination(), validate(T.DocumentsArchivedSchema), async (ctx: Context) => { const { sort, direction, collectionId } = ctx.input.body; const { user } = ctx.auth; if (sort === "index") { throw ValidationError( "Sorting archived documents by index is not supported" ); } // if a specific collection is passed then we need to check auth to view it if (collectionId) { const collection = await Collection.findByPk(collectionId, { userId: user.id, }); authorize(user, "readDocument", collection); } const documents = await Document.findAllWithCtx(ctx, { where: { archivedAt: { [Op.ne]: null, }, ...(collectionId && { collectionId }), }, order: [[sort, direction]], offset: ctx.state.pagination.offset, limit: ctx.state.pagination.limit, }); const data = await presentDocuments(ctx, documents); const policies = presentPolicies(user, documents); ctx.body = { pagination: ctx.state.pagination, data, policies, }; } ); router.post( "documents.deleted", auth({ role: UserRole.Member }), pagination(), validate(T.DocumentsDeletedSchema), async (ctx: Context) => { const { sort, direction, filters: rawFilters } = ctx.input.body; const { user } = ctx.auth; const where: WhereOptions & { [Op.and]: WhereOptions[]; } = { deletedAt: { [Op.ne]: null, }, [Op.and]: [], }; const filter = combineFilters(rawFilters); if (filter) { await authorizeFilterFields(user, filter); where[Op.and].push(buildWhere(filter, user.teamId)); } const documents = await Document.findAllWithCtx(ctx, { includeDrafts: true, where, paranoid: false, order: [[sort, direction]], offset: ctx.state.pagination.offset, limit: ctx.state.pagination.limit, }); const data = await presentDocuments(ctx, documents); const policies = presentPolicies(user, documents); ctx.body = { pagination: ctx.state.pagination, data, policies, }; } ); router.post( "documents.viewed", auth(), pagination(), validate(T.DocumentsViewedSchema), async (ctx: Context) => { const { sort, direction } = ctx.input.body; const { user } = ctx.auth; const viewIds = await View.findAccessibleIds(user, { sort, direction, offset: ctx.state.pagination.offset, limit: ctx.state.pagination.limit, }); const views = viewIds.length ? await View.findAll({ where: { id: viewIds, }, order: [[sort, direction]], include: [ { model: Document.scope([ "withDrafts", "withoutStateOrText", { method: ["withMembership", user.id] }, ]), required: true, }, ], }) : []; const documents = views.map((view) => { const document = view.document; document.views = [view]; return document; }); const data = await presentDocuments(ctx, documents); const policies = presentPolicies(user, documents); ctx.body = { pagination: ctx.state.pagination, data, policies, }; } ); router.post( "documents.drafts", auth(), pagination(), validate(T.DocumentsDraftsSchema), async (ctx: Context) => { const { collectionId, dateFilter, direction, sort } = ctx.input.body; const { user } = ctx.auth; if (collectionId) { const collection = await Collection.findByPk(collectionId, { userId: user.id, }); authorize(user, "readDocument", collection); } const where: WhereOptions = { createdById: user.id, publishedAt: { [Op.is]: null, }, ...(collectionId && { collectionId }), }; if (dateFilter) { where.updatedAt = { [Op.gte]: subtractDate(new Date(), dateFilter), }; } else { delete where.updatedAt; } const documents = await Document.findAllWithCtx(ctx, { includeDrafts: true, where, order: [[sort, direction]], offset: ctx.state.pagination.offset, limit: ctx.state.pagination.limit, }); const data = await presentDocuments(ctx, documents); const policies = presentPolicies(user, documents); ctx.body = { pagination: ctx.state.pagination, data, policies, }; } ); router.post( "documents.info", auth({ optional: true }), validate(T.DocumentsInfoSchema), async (ctx: Context) => { const { id, shareId } = ctx.input.body; const { user } = ctx.auth; const apiVersion = getAPIVersion(ctx); let document: Document | null; let serializedDocument: Record | undefined; let isPublic = false; if (shareId) { const teamFromCtx = await getTeamFromContext(ctx, { includeOAuthState: false, }); const result = await loadPublicShare({ id: shareId, documentId: id, teamId: teamFromCtx?.id, }); document = result.document; if (!document) { throw NotFoundError("Document could not be found for shareId"); } const { sharedTree } = result; const [scopedDocument, backlinkIds] = await Promise.all([ // reload with membership scope if user is authenticated user ? Document.findByPk(document.id, { userId: user.id, rejectOnEmpty: true, }) : document, // Get backlinks that are within the shared tree sharedTree ? Relationship.findSourceDocumentIdsInSharedTree( document.id, getAllIdsInSharedTree(sharedTree) ) : undefined, ]); document = scopedDocument; isPublic = cannot(user, "read", document); serializedDocument = await presentDocument(ctx, document, { isPublic, shareId, includeUpdatedAt: result.share.showLastUpdated, backlinkIds, }); } else { if (!user) { throw AuthenticationError("Authentication required"); } document = await documentLoader({ id: id!, // validation ensures id will be present here user, }); serializedDocument = await presentDocument(ctx, document); } ctx.body = { // Passing apiVersion=2 has a single effect, to change the response payload to // include top level keys for document. data: apiVersion >= 2 ? { document: serializedDocument, } : serializedDocument, policies: isPublic ? undefined : presentPolicies(user, [document]), }; } ); router.post( "documents.insights", auth(), validate(T.DocumentsInsightsSchema), async (ctx: Context) => { const { id, startDate, endDate } = ctx.input.body; const { user } = ctx.auth; const document = await Document.findByPk(id, { userId: user.id, includeContent: false, }); authorize(user, "listViews", document); if (!document.insightsEnabled) { throw ValidationError("Insights are not enabled for this document"); } const end = endDate ?? new Date(); const start = startDate ?? new Date(end.getTime() - 30 * Day.ms); const insights = await DocumentInsight.findAll({ where: { documentId: document.id, date: { [Op.gte]: start.toISOString().slice(0, 10), [Op.lte]: end.toISOString().slice(0, 10), }, }, order: [["date", "ASC"]], }); ctx.body = { data: insights.map(presentDocumentInsight), }; } ); router.post( "documents.users", auth(), pagination(), validate(T.DocumentsUsersSchema), async (ctx: Context) => { const { id, userId, query } = ctx.input.body; const actor = ctx.auth.user; const document = await Document.findByPk(id, { userId: actor.id, includeContent: false, }); authorize(actor, "read", document); authorize(actor, "listUsers", actor.team); let where: WhereOptions = { teamId: document.teamId, suspendedAt: { [Op.is]: null, }, }; const [collection, memberIds, collectionMemberIds] = await Promise.all([ document.$get("collection"), Document.membershipUserIds(document.id), document.collectionId ? Collection.membershipUserIds(document.collectionId) : [], ]); where = { ...where, [Op.or]: [ { id: { [Op.in]: uniq([...memberIds, ...collectionMemberIds]), }, }, collection?.permission ? { role: { [Op.ne]: UserRole.Guest, }, } : {}, ], }; if (query) { where = { ...where, [Op.and]: [ Sequelize.literal( `unaccent(LOWER(name)) like unaccent(LOWER(:query))` ), ], }; } if (userId) { where = { ...where, id: userId, }; } const replacements = { query: QueryHelper.likeContains(query ?? "") }; const { results: users, pagination } = await paginateQuery( ctx, (opts) => User.findAll({ where, replacements, ...opts }), () => User.count({ where, replacements, }) as unknown as Promise ); ctx.body = { pagination, data: users.map((user) => presentUser(user)), policies: presentPolicies(actor, users), }; } ); router.post( "documents.documents", auth(), validate(T.DocumentsChildrenSchema), async (ctx: Context) => { const { id } = ctx.input.body; const { user } = ctx.auth; const document = await Document.findByPk(id, { userId: user.id, includeContent: false, }); authorize(user, "read", document); let documentTree: NavigationNode | undefined; if (document.collectionId) { const collection = await Collection.findByPk(document.collectionId, { includeDocumentStructure: true, }); [documentTree] = (await collection?.structureFor(user, { documentId: document.id })) ?? []; } ctx.body = { data: documentTree, }; } ); router.post( "documents.export", rateLimiter(RateLimiterStrategy.TwentyFivePerMinute), auth(), validate(T.DocumentsExportSchema), async (ctx: Context) => { const { id, signedUrls, includeChildDocuments } = ctx.input.body; const { user } = ctx.auth; const accept = ctx.request.headers["accept"]; const document = await documentLoader({ id, user, // We need the collaborative state to generate HTML, but not for the // formats that are written from markdown. includeState: !( accept?.includes("text/markdown") || accept?.includes(ExportContentType.TextBundle) ), }); authorize(user, "download", document); const format = accept?.includes("text/html") ? FileOperationFormat.HTMLZip : accept?.includes("text/markdown") ? FileOperationFormat.MarkdownZip : accept?.includes(ExportContentType.TextBundle) ? FileOperationFormat.TextBundleZip : accept?.includes("application/pdf") ? FileOperationFormat.PDF : null; if (format === FileOperationFormat.PDF) { throw IncorrectEditionError( "PDF export is not available in the community edition" ); } if (includeChildDocuments) { if (!format) { throw InvalidRequestError( "format needed for exporting nested documents" ); } const fileOperation = await FileOperation.createExport(ctx, { document, format, }); ctx.body = { success: true, data: { fileOperation: presentFileOperation(fileOperation), }, }; return; } let contentType: string; let content: string; const toMarkdown = async () => DocumentHelper.toMarkdown(document, { signedUrls, teamId: user.teamId, commonMark: true, }); // A TextBundle is a directory of files, so unlike the other formats it has // no self-contained single-file form to fall back to. const isTextBundle = format === FileOperationFormat.TextBundleZip; if (format === FileOperationFormat.HTMLZip) { contentType = "text/html"; content = await DocumentHelper.toHTML(document, { centered: true, includeMermaid: true, }); } else if (isTextBundle || format === FileOperationFormat.MarkdownZip) { contentType = "text/markdown"; content = await toMarkdown(); } else { ctx.body = { data: await toMarkdown(), }; return; } // Override the extension for Markdown as it's incorrect in the mime-types // library until a new release > 2.1.35 const extension = contentType === "text/markdown" ? "md" : mime.extension(contentType); const fileName = slugify(document.displayTitle); const attachmentIds = ProsemirrorHelper.parseAttachmentIds( DocumentHelper.toProsemirror(document) ); const attachments = attachmentIds.length ? await Attachment.findAll({ where: { teamId: document.teamId, id: attachmentIds, }, }) : []; // When exporting HTML, inline small images that are referenced a single // time as base64 data URIs. Only these are read into memory; the other // attachments are streamed into a zip with the document. const signal = getClientAbortSignal(ctx.res); const externalAttachments: Attachment[] = []; let inlinedSize = 0; for (const attachment of attachments) { if (signal.aborted) { return; } if ( contentType === "text/html" && HTMLHelper.canInlineImage( content, attachment.redirectUrl, attachment.contentType, // BIGINT columns are returned as strings. Number(attachment.size), inlinedSize ) ) { const buffer = await AttachmentHelper.readBuffer(attachment); const inlined = HTMLHelper.inlineImage( content, attachment.redirectUrl, attachment.contentType, buffer, inlinedSize ); if (inlined !== null) { content = inlined; inlinedSize += buffer.length; continue; } } externalAttachments.push(attachment); } if (isTextBundle) { const root = `${fileName}.${TextBundleHelper.bundleExtension}`; const usedAssetNames = new Set(); streamZipResponse( ctx, `${fileName}.${TextBundleHelper.packExtension}`, (zip, zipSignal) => { for (const attachment of externalAttachments) { const reference = TextBundleHelper.assetPath( attachment.name, usedAssetNames ); AttachmentHelper.addToArchive( zip, attachment, path.join(root, reference), zipSignal ); content = content.replace( new RegExp(escapeRegExp(attachment.redirectUrl), "g"), encodeURI(reference) ); } zip.addBuffer( Buffer.from(TextBundleHelper.info(document)), path.join(root, TextBundleHelper.infoFileName), { mtime: document.updatedAt } ); zip.addBuffer( Buffer.from(content), path.join(root, TextBundleHelper.textFileName), { mtime: document.updatedAt } ); } ); return; } // When there are no external attachments the document is self-contained and // can be served directly rather than bundled in a zip. if (externalAttachments.length === 0) { ctx.set("Content-Type", contentType); ctx.set( "Content-Disposition", contentDisposition(`${fileName}.${extension}`, { type: "attachment", }) ); ctx.body = content; return; } streamZipResponse(ctx, `${fileName}.zip`, (zip, zipSignal) => { for (const attachment of externalAttachments) { const location = path.join( "attachments", `${attachment.id}.${mime.extension(attachment.contentType)}` ); AttachmentHelper.addToArchive(zip, attachment, location, zipSignal); content = content.replace( new RegExp(escapeRegExp(attachment.redirectUrl), "g"), location ); } zip.addBuffer(Buffer.from(content), `${fileName}.${extension}`, { mtime: document.updatedAt, }); }); } ); router.post( "documents.restore", auth({ role: UserRole.Member }), rateLimiter(RateLimiterStrategy.OneHundredPerMinute), validate(T.DocumentsRestoreSchema), transaction(), async (ctx: Context) => { const { id, collectionId, revisionId } = ctx.input.body; const { user } = ctx.auth; const { transaction } = ctx; const document = await Document.findByPk(id, { userId: user.id, paranoid: false, transaction, }); authorize(user, "read", document); await documentRestorer(ctx, { document, collectionId, revisionId }); ctx.body = { data: await presentDocument(ctx, document), policies: presentPolicies(user, [document]), }; } ); router.post( "documents.search_titles", auth(), pagination(), rateLimiter(RateLimiterStrategy.OneHundredPerMinute), validate(T.DocumentsSearchTitlesSchema), async (ctx: Context) => { const { query, sort, direction, filters: rawFilters } = ctx.input.body; const { collectionId, userId, documentId, statusFilter, dateFilter } = ctx.input.body; const { offset, limit } = ctx.state.pagination; const { user } = ctx.auth; const filter = combineFilters(rawFilters) ?? legacyParamsToFilter({ collectionId, userId, documentId, statusFilter, dateFilter, }); if (filter) { await authorizeFilterFields(user, filter); } const resolvedFilter = filter ? await expandDocumentIdLeaves(filter, { user }) : undefined; const documents = await SearchProviderManager.getProvider().searchTitlesForUser(user, { query, filter: resolvedFilter, offset, limit, sort: sort as SortFilter, direction: direction as DirectionFilter, }); const policies = presentPolicies(user, documents); const data = await presentDocuments(ctx, documents); ctx.body = { pagination: ctx.state.pagination, data, policies, }; } ); router.post( "documents.search", auth({ optional: true }), pagination(), rateLimiter(RateLimiterStrategy.OneHundredPerMinute), validate(T.DocumentsSearchSchema), async (ctx: Context) => { const { query, shareId, snippetMinWords, snippetMaxWords, sort, direction, filters: rawFilters, } = ctx.input.body; const { collectionId, documentId, userId, dateFilter, statusFilter } = ctx.input.body; const filter = combineFilters(rawFilters) ?? legacyParamsToFilter({ collectionId, userId, documentId, statusFilter, dateFilter, }); const { offset, limit } = ctx.state.pagination; const { user } = ctx.auth; let teamId; let response; let share; let isPublic = false; const searchStartedAt = Date.now(); if (shareId) { const teamFromCtx = await getTeamFromContext(ctx, { includeOAuthState: false, }); const result = await loadPublicShare({ id: shareId, teamId: teamFromCtx?.id, }); share = result.share; const { collection, document } = result; // One of collection or document should be available if (share.documentId && !share?.includeChildDocuments) { throw InvalidRequestError("Child documents cannot be searched"); } teamId = share.teamId; const team = share.team; invariant(team, "Share must belong to a team"); const shareScopeId = collection?.id || document?.collectionId; const shareFilter = combineFilters([ ...(filter ? [filter] : []), ...(shareScopeId ? [ { field: "collectionId", operator: "eq" as const, value: shareScopeId, }, ] : []), ]); const [resolvedShareFilter, scopedCollection, scopedDocument] = await Promise.all([ shareFilter ? expandDocumentIdLeaves(shareFilter, { teamId: share.teamId }) : undefined, // reload with membership scope if user is authenticated user && collection ? Collection.findByPk(collection.id, { userId: user.id }) : collection, user && document ? Document.findByPk(document.id, { userId: user.id }) : document, ]); isPublic = scopedCollection ? cannot(user, "read", scopedCollection) : cannot(user, "read", scopedDocument); response = await SearchProviderManager.getProvider().searchForTeam(team, { query, filter: resolvedShareFilter, share, offset, limit, snippetMinWords, snippetMaxWords, sort: sort as SortFilter, direction: direction as DirectionFilter, usePopularityBoost: false, }); } else { if (!user) { throw AuthenticationError("Authentication error"); } teamId = user.teamId; if (filter) { await authorizeFilterFields(user, filter); } const resolvedFilter = filter ? await expandDocumentIdLeaves(filter, { user }) : undefined; response = await SearchProviderManager.getProvider().searchForUser(user, { query, filter: resolvedFilter, offset, limit, snippetMinWords, snippetMaxWords, sort: sort as SortFilter, direction: direction as DirectionFilter, }); } const { results, total } = response; const documents = results.map((result) => result.document); const [data] = await Promise.all([ Promise.all( results.map(async (result) => { const document = await presentDocument(ctx, result.document, { isPublic, shareId, }); return { ...result, document }; }) ), // When requesting subsequent pages of search results we don't want to record // duplicate search query records query && offset === 0 ? SearchQuery.record({ userId: user?.id, teamId, shareId: share?.id, // auth.type values are a subset of search sources; unauthenticated share searches default to "app" source: (ctx.auth.type as unknown as SearchQuerySource) || SearchQuerySource.App, query, results: total, duration: Date.now() - searchStartedAt, }) : undefined, ]); ctx.body = { pagination: { ...ctx.state.pagination, total }, data, policies: user ? presentPolicies(user, documents) : null, }; } ); router.post( "documents.templatize", auth({ role: UserRole.Member }), rateLimiter(RateLimiterStrategy.TwentyFivePerMinute), validate(T.DocumentsTemplatizeSchema), transaction(), async (ctx: Context) => { const { id, collectionId, publish } = ctx.input.body; const { user } = ctx.auth; const { transaction } = ctx; const original = await Document.findByPk(id, { userId: user.id, transaction, }); authorize(user, "update", original); let collection: Collection | null = null; if (collectionId) { collection = await Collection.findByPk(collectionId, { userId: user.id, transaction, }); authorize(user, "createTemplate", collection); } else { authorize(user, "createTemplate", user.team); } const template = await Template.createWithCtx(ctx, { editorVersion: original.editorVersion, collectionId: collection?.id, teamId: user.teamId, publishedAt: publish ? new Date() : null, lastModifiedById: user.id, createdById: user.id, icon: original.icon, color: original.color, title: original.title, content: original.content, }); // reload to get all of the data needed to present (user, collection etc) const reloaded = await Template.findByPk(template.id, { userId: user.id, transaction, }); invariant(reloaded, "template not found"); ctx.body = { data: presentTemplate(reloaded), policies: presentPolicies(user, [reloaded]), }; } ); router.post( "documents.update", auth(), rateLimiter(RateLimiterStrategy.OneHundredPerMinute), validate(T.DocumentsUpdateSchema), transaction(), async (ctx: Context) => { const { transaction } = ctx; const { id, insightsEnabled, publish, collectionId, ...input } = ctx.input.body; const updatingDeprecatedReason = input.deprecatedReason !== undefined && Object.keys(ctx.input.body).every( (key) => key === "id" || key === "deprecatedReason" ); const editorVersion = ctx.headers["x-editor-version"] as string | undefined; const { user } = ctx.auth; let collection: Collection | null | undefined; let document = await Document.findByPk(id, { userId: user.id, includeState: true, paranoid: !updatingDeprecatedReason, transaction, }); collection = document?.collection; authorize( user, updatingDeprecatedReason ? "updateDeprecatedReason" : "update", document ); if (!updatingDeprecatedReason && input.deprecatedReason !== undefined) { authorize(user, "updateDeprecatedReason", document); } if (collection && insightsEnabled !== undefined) { authorize(user, "updateInsights", document); } if (input.ownerIds) { authorize(user, "manageOwners", document); } if (publish) { await authorizeDocumentPublish(ctx, document, collectionId); } if (input.templateId && input.templateId !== document.templateId) { const template = await Template.findByPk(input.templateId, { userId: user.id, transaction, }); authorize(user, "read", template); } document = await documentUpdater(ctx, { document, ...input, publish, collectionId, insightsEnabled, editorVersion, }); ctx.body = { data: await presentDocument(ctx, document), policies: presentPolicies(user, [document]), }; } ); router.post( "documents.duplicate", auth(), rateLimiter(RateLimiterStrategy.TwentyFivePerMinute), validate(T.DocumentsDuplicateSchema), transaction(), async (ctx: Context) => { const { transaction } = ctx; const { id, title, publish, recursive, comments, collectionId, parentDocumentId, } = ctx.input.body; const { user } = ctx.auth; const document = await Document.findByPk(id, { userId: user.id, transaction, }); authorize(user, "duplicate", document); if (comments) { authorize(user, "listComments", user.team); } let collection: Collection | null | undefined; if (parentDocumentId) { const parent = await Document.findByPk(parentDocumentId, { userId: user.id, transaction, }); authorize(user, "update", parent); if (!parent.publishedAt) { throw InvalidRequestError("Cannot duplicate document inside a draft"); } if (collectionId && collectionId !== parent.collectionId) { throw InvalidRequestError( "collectionId must match the collection of the parent document" ); } // The copy is nested under the parent, so it belongs to the parent's // collection. collection = parent.collectionId ? await Collection.findByPk(parent.collectionId, { userId: user.id, transaction, }) : undefined; } else { collection = collectionId ? await Collection.findByPk(collectionId, { userId: user.id, transaction, }) : document?.collection; // The copy is created in the destination collection, so create permission // is required there rather than on the source. if (collectionId || collection) { authorize(user, "createDocument", collection); } } const response = await documentDuplicator(ctx, { collection, document, title, publish, recursive, comments, parentDocumentId, }); ctx.body = { data: { documents: await presentDocuments(ctx, response), }, policies: presentPolicies(user, response), }; } ); router.post( "documents.move", auth(), rateLimiter(RateLimiterStrategy.OneHundredPerMinute), validate(T.DocumentsMoveSchema), transaction(), async (ctx: Context) => { const { transaction } = ctx; const { id, parentDocumentId, index } = ctx.input.body; let collectionId = ctx.input.body.collectionId; const { user } = ctx.auth; const document = await Document.findByPk(id, { userId: user.id, transaction, }); authorize(user, "move", document); if (parentDocumentId) { const parent = await Document.findByPk(parentDocumentId, { userId: user.id, transaction, }); authorize(user, "update", parent); collectionId = parent.collectionId; if (!parent.publishedAt) { throw InvalidRequestError("Cannot move document inside a draft"); } } else if (collectionId) { const collection = await Collection.findByPk(collectionId, { userId: user.id, transaction, }); authorize(user, "updateDocument", collection); } else { throw InvalidRequestError("collectionId is required to move a document"); } const { documents, collectionChanged } = await documentMover(ctx, { document, collectionId: collectionId ?? null, parentDocumentId, index, }); ctx.body = { data: { documents: await presentDocuments(ctx, documents), // Included for backwards compatibility collections: [], }, policies: collectionChanged ? presentPolicies(user, documents) : [], }; } ); router.post( "documents.archive", auth(), rateLimiter(RateLimiterStrategy.OneHundredPerMinute), validate(T.DocumentsArchiveSchema), transaction(), async (ctx: Context) => { const { id, reason } = ctx.input.body; const { user } = ctx.auth; const { transaction } = ctx; const document = await Document.findByPk(id, { userId: user.id, rejectOnEmpty: true, transaction, }); authorize(user, "archive", document); if (reason !== undefined) { document.deprecatedReason = reason || null; } await document.archiveWithCtx(ctx); ctx.body = { data: await presentDocument(ctx, document), policies: presentPolicies(user, [document]), }; } ); router.post( "documents.delete", auth(), rateLimiter(RateLimiterStrategy.OneHundredPerMinute), validate(T.DocumentsDeleteSchema), transaction(), async (ctx: Context) => { const { transaction } = ctx; const { id, permanent, reason } = ctx.input.body; const { user } = ctx.auth; if (permanent) { const document = await Document.findByPk(id, { userId: user.id, paranoid: false, transaction, }); authorize(user, "permanentDelete", document); await documentPermanentDeleter([document]); await Event.createFromContext(ctx, { name: "documents.permanent_delete", documentId: document.id, collectionId: document.collectionId, data: { title: document.title, }, }); } else { const document = await Document.findByPk(id, { userId: user.id, transaction, }); authorize(user, "delete", document); if (reason !== undefined) { document.deprecatedReason = reason || null; } await document.destroyWithCtx(ctx); } ctx.body = { success: true, }; } ); router.post( "documents.unpublish", auth(), rateLimiter(RateLimiterStrategy.OneHundredPerMinute), validate(T.DocumentsUnpublishSchema), transaction(), async (ctx: Context) => { const { id, detach } = ctx.input.body; const { user } = ctx.auth; const document = await Document.findByPk(id, { userId: user.id, }); authorize(user, "unpublish", document); await document.unpublishWithCtx(ctx, { detach }); ctx.body = { data: await presentDocument(ctx, document), policies: presentPolicies(user, [document]), }; } ); router.post( "documents.import", auth(), rateLimiter(RateLimiterStrategy.TwentyFivePerMinute), multipart({ maximumFileSize: env.FILE_STORAGE_IMPORT_MAX_SIZE, optional: true, }), validate(T.DocumentsImportSchema), async (ctx: Context) => { const { collectionId, parentDocumentId, publish, attachmentId } = ctx.input.body; const { user } = ctx.auth; if (!attachmentId && !ctx.input.file) { throw ValidationError("one of attachmentId or file is required"); } const { collection } = await authorizeDocumentCreate(ctx, { collectionId, parentDocumentId, }); let key: string; let fileName: string; let mimeType: string; if (attachmentId) { const attachment = await Attachment.findByPk(attachmentId); authorize(user, "read", attachment); // The import task deletes the file, so only accept temporary import uploads. if (!attachment.expiresAt) { throw ValidationError("Attachment is not an import upload"); } key = attachment.key; fileName = attachment.name; mimeType = attachment.contentType; } else { const file = ctx.input.file!; const buffer = await fs.readFile(file.filepath); fileName = file.originalFilename ?? file.newFilename; mimeType = file.mimetype ?? ""; key = AttachmentHelper.getKey({ id: randomUUID(), name: fileName, userId: user.id, }); await FileStorage.store({ body: buffer, contentType: mimeType, contentLength: buffer.length, key, acl: "private", }); } const document = await DocumentImportTask.scheduleAndWait({ key, sourceMetadata: { fileName, mimeType, }, userId: user.id, collectionId: collection?.id, parentDocumentId, publish, authType: ctx.auth.type, ip: ctx.request.ip, }); ctx.body = { data: await presentDocument(ctx, document), policies: presentPolicies(user, [document]), }; } ); router.post( "documents.create", auth(), rateLimiter(RateLimiterStrategy.TwentyFivePerMinute), validate(T.DocumentsCreateSchema), transaction(), async (ctx: Context) => { const { id, title, text, icon, color, publish, index, collectionId, parentDocumentId, fullWidth, preferences, templateId, createdAt, ownerIds, } = ctx.input.body; const editorVersion = ctx.headers["x-editor-version"] as string | undefined; const { transaction } = ctx; const { user } = ctx.auth; const { collection } = await authorizeDocumentCreate(ctx, { collectionId, parentDocumentId, }); let template: Template | null | undefined; if (templateId) { template = await Template.findByPk(templateId, { userId: user.id, transaction, }); authorize(user, "read", template); } // Pre-process text to convert bare embed URLs to markdown link format const processedText = text ? convertBareUrlsToEmbedMarkdown(text) : text; const document = await documentCreator(ctx, { id, title, text: processedText ? await TextHelper.replaceImagesWithAttachments( ctx, processedText, user ) : processedText, icon, color, createdAt, publish, index, collectionId: collection?.id, parentDocumentId, template, fullWidth, preferences, editorVersion, ownerIds, }); if (collection) { document.collection = collection; } ctx.body = { data: await presentDocument(ctx, document), policies: presentPolicies(user, [document]), }; } ); router.post( "documents.add_user", auth(), validate(T.DocumentsAddUserSchema), rateLimiter(RateLimiterStrategy.OneHundredPerHour), transaction(), async (ctx: Context) => { const { transaction } = ctx; const { user: actor } = ctx.auth; const { id, userId, permission, owner } = ctx.input.body; // Users can only change their own ownership, not their own access. const isSelf = userId === actor.id; if (isSelf && owner === undefined) { throw ValidationError("You cannot invite yourself"); } const [document, user] = await Promise.all([ Document.findByPk(id, { userId: actor.id, rejectOnEmpty: true, transaction, }), User.findByPk(userId, { rejectOnEmpty: true, transaction, }), ]); authorize(actor, "manageUsers", document); authorize(actor, "read", user); const previousOwnerIds = document.ownerIds ?? []; if (isSelf && !owner) { await document.removeOwner(ctx, userId); const selfMembership = await UserMembership.findOne({ where: { documentId: document.id, userId }, transaction, }); ctx.body = { data: { users: [presentUser(user)], memberships: selfMembership ? [presentMembership(selfMembership)] : [], documents: [await presentDocument(ctx, document)], }, }; return; } if (owner) { authorize(actor, "manageOwners", document); await document.addOwner(ctx, userId); const membership = await UserMembership.findOne({ where: { documentId: document.id, userId }, transaction, rejectOnEmpty: true, }); ctx.body = { data: { users: [presentUser(user)], memberships: [presentMembership(membership)], documents: [await presentDocument(ctx, document)], }, }; return; } const UserMemberships = await UserMembership.findAll({ where: { userId, }, attributes: ["id", "index", "updatedAt"], limit: 1, order: [ // using LC_COLLATE:"C" because we need byte order to drive the sorting // find only the first star so we can create an index before it Sequelize.literal('"user_permission"."index" collate "C"'), ["updatedAt", "DESC"], ], transaction, }); // create membership at the beginning of their "Shared with me" section const index = fractionalIndex( null, UserMemberships.length ? UserMemberships[0].index : null ); let membership = await UserMembership.findOneForUpdate( { documentId: document.id, userId }, { transaction } ); if (membership) { if (permission) { membership.permission = permission; // disconnect from the source if the permission is manually updated membership.sourceId = null; await membership.save(toHookContext(ctx)); } } else { membership = await UserMembership.create( { documentId: document.id, userId, index, permission: permission || user.defaultDocumentPermission, createdById: actor.id, }, toHookContext(ctx) ); } // Owners must be able to manage the document, so other access removes the // ownership. if (owner === false || membership.permission !== DocumentPermission.Admin) { await document.removeOwner(ctx, userId); } ctx.body = { data: { users: [presentUser(user)], memberships: [presentMembership(membership)], documents: document.ownerIds !== previousOwnerIds ? [await presentDocument(ctx, document)] : [], }, }; } ); router.post( "documents.remove_user", auth(), rateLimiter(RateLimiterStrategy.OneHundredPerHour), validate(T.DocumentsRemoveUserSchema), transaction(), async (ctx: Context) => { const { transaction } = ctx; const { user: actor } = ctx.auth; const { id, userId } = ctx.input.body; const [document, user] = await Promise.all([ Document.findByPk(id, { userId: actor.id, rejectOnEmpty: true, transaction, }), User.findByPk(userId, { rejectOnEmpty: true, transaction, }), ]); if (actor.id !== userId) { authorize(actor, "manageUsers", document); authorize(actor, "read", user); } const membership = await UserMembership.findOne({ where: { documentId: document.id, userId, }, transaction, lock: transaction.LOCK.UPDATE, rejectOnEmpty: true, }); if (membership.sourceId) { throw ValidationError( "Cannot remove access that is inherited from a parent document" ); } await membership.destroy(toHookContext(ctx)); await document.removeOwner(ctx, userId); ctx.body = { success: true, }; } ); router.post( "documents.add_group", auth(), rateLimiter(RateLimiterStrategy.OneHundredPerHour), validate(T.DocumentsAddGroupSchema), transaction(), async (ctx: Context) => { const { id, groupId, permission } = ctx.input.body; const { transaction } = ctx; const { user } = ctx.auth; const [document, group] = await Promise.all([ Document.findByPk(id, { userId: user.id, rejectOnEmpty: true, transaction, }), Group.findByPk(groupId, { rejectOnEmpty: true, transaction, }), ]); authorize(user, "manageUsers", document); authorize(user, "read", group); let membership = await GroupMembership.findOneForUpdate( { documentId: document.id, groupId }, { transaction } ); if (membership) { if (permission) { membership.permission = permission; // disconnect from the source if the permission is manually updated membership.sourceId = null; await membership.save(toHookContext(ctx)); } } else { membership = await GroupMembership.create( { documentId: document.id, groupId, permission: permission ?? DocumentPermission.ReadWrite, createdById: user.id, }, toHookContext(ctx) ); } ctx.body = { data: { groupMemberships: [presentGroupMembership(membership)], }, }; } ); router.post( "documents.remove_group", auth(), rateLimiter(RateLimiterStrategy.OneHundredPerHour), validate(T.DocumentsRemoveGroupSchema), transaction(), async (ctx: Context) => { const { transaction } = ctx; const { user } = ctx.auth; const { id, groupId } = ctx.input.body; const [document, group] = await Promise.all([ Document.findByPk(id, { userId: user.id, rejectOnEmpty: true, transaction, }), Group.findByPk(groupId, { rejectOnEmpty: true, transaction, }), ]); authorize(user, "manageUsers", document); authorize(user, "read", group); const membership = await GroupMembership.findOne({ where: { documentId: document.id, groupId, }, transaction, lock: transaction.LOCK.NO_KEY_UPDATE, rejectOnEmpty: true, }); if (membership.sourceId) { throw ValidationError( "Cannot remove access that is inherited from a parent document" ); } await membership.destroy(toHookContext(ctx)); ctx.body = { success: true, }; } ); router.post( "documents.memberships", auth(), pagination(), validate(T.DocumentsMembershipsSchema), async (ctx: Context) => { const { id, query, permission } = ctx.input.body; const { user: actor } = ctx.auth; const document = await Document.findByPk(id, { userId: actor.id }); authorize(actor, "update", document); let where: WhereOptions = { documentId: document.id, }; let userWhere; if (query) { userWhere = { name: { [Op.iLike]: QueryHelper.likeContains(query) }, }; } if (permission) { where = { ...where, permission }; } const options = { where, include: [ { model: User, as: "user", where: userWhere, required: true, }, ], }; const { results: memberships, pagination } = await paginateQuery( ctx, (opts) => UserMembership.findAll({ ...options, order: [["createdAt", "DESC"]], ...opts, }), () => UserMembership.count(options) ); ctx.body = { pagination, data: { memberships: memberships.map(presentMembership), users: memberships.map((membership) => presentUser(membership.user)), }, }; } ); router.post( "documents.group_memberships", auth(), pagination(), validate(T.DocumentsMembershipsSchema), async (ctx: Context) => { const { id, query, permission } = ctx.input.body; const { user } = ctx.auth; const document = await Document.findByPk(id, { userId: user.id }); authorize(user, "update", document); let where: WhereOptions = { documentId: document.id, }; let groupWhere; if (query) { groupWhere = { name: { [Op.iLike]: QueryHelper.likeContains(query) }, }; } if (permission) { where = { ...where, permission }; } const options = { where, include: [ { model: Group, as: "group", where: groupWhere, required: true, }, ], }; const { results: memberships, pagination } = await paginateQuery( ctx, (opts) => GroupMembership.findAll({ ...options, order: [["createdAt", "DESC"]], ...opts, }), () => GroupMembership.count(options) ); const groupMemberships = memberships.map(presentGroupMembership); ctx.body = { pagination, data: { groupMemberships, groups: await Promise.all( memberships.map((membership) => presentGroup(membership.group)) ), }, }; } ); router.post( "documents.empty_trash", auth({ role: UserRole.Admin }), rateLimiter(RateLimiterStrategy.TenPerHour), async (ctx: Context) => { const { user } = ctx.auth; const documents = await Document.findAllWithCtx(ctx, { includeDrafts: true, includeViews: false, // The columns that the permanentDelete policy reads. attributes: ["id", "teamId", "collectionId", "deletedAt"], where: { deletedAt: { [Op.ne]: null, }, }, paranoid: false, }); // The task deletes without a policy check, and read access alone does not // allow a permanent delete. const documentIds = documents .filter((document) => can(user, "permanentDelete", document)) .map((document) => document.id); if (documentIds.length) { await new EmptyTrashTask().schedule({ documentIds }); } await Event.createFromContext(ctx, { name: "documents.empty_trash", }); ctx.body = { success: true, }; } ); // Remove this helper once apiVersion is removed (#6175) function getAPIVersion(ctx: Context) { return Number( ctx.headers["x-api-version"] ?? (typeof ctx.input.body === "object" && ctx.input.body && "apiVersion" in ctx.input.body && ctx.input.body.apiVersion) ?? 0 ); } export default router;