Files
outline/server/commands/documentDuplicator.ts
T
Tom Moor 754672e9dc Centralize document access rules for list queries (#14783)
* wip

* Route document list queries through findAllWithCtx

Add findAllWithCtx and countWithCtx on Document and Template, unsafe* methods
for system reads, and a lint rule that bans direct list queries. Remove the
cached membership list, accessibleSql and findByIds in favour of one gate.

* Remove test

* Include drafts when loading starred documents

* Scope the access gate to the user's team, move whereToSql to QueryHelper

* Move collection access to Collection.findAllWithCtx

Collection.accessibleIds keeps the cached id list inside the collection gate, and user.collectionIds() is a shortcut to it. List queries use findAllWithCtx and countWithCtx, system reads use the shared unsafe methods, and the admin listing in collections.list is explicit.

* Check permanentDelete for each document in empty_trash
2026-11-27 15:30:43 -05:00

278 lines
8.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { randomUUID } from "node:crypto";
import { Op } from "sequelize";
import { Collection, Document } from "@server/models";
import { DocumentHelper } from "@server/models/helpers/DocumentHelper";
import type { DocumentReference } from "@server/models/helpers/ProsemirrorHelper";
import { ProsemirrorHelper } from "@server/models/helpers/ProsemirrorHelper";
import type { Context } from "@server/types";
import { generateUrlId } from "@server/utils/url";
import documentCreator from "./documentCreator";
type Props = {
/** The document to duplicate */
document: Document;
/** The collection to add the duplicated document to */
collection?: Collection | null;
/** Override of the parent document to add the duplicate to */
parentDocumentId?: string;
/** Override of the duplicated document title */
title?: string;
/** Override of the duplicated document publish state */
publish?: boolean;
/** Whether to duplicate child documents */
recursive?: boolean;
};
type ManyProps = {
/** The documents to duplicate, in the order they should be created */
documents: Document[];
/** The collection to add the duplicated documents to */
collection?: Collection | null;
/** Override of the duplicated documents publish state */
publish?: boolean;
/** Whether to duplicate child documents */
recursive?: boolean;
};
/** A document to duplicate, and where its duplicate should be placed. */
type Root = {
/** The document to duplicate */
document: Document;
/** Override of the duplicated document title */
title?: string;
/** Override of the parent document to add the duplicate to */
parentDocumentId?: string;
};
/** A document to duplicate, with the identifiers assigned to its duplicate. */
type DuplicateItem = {
/** The document being duplicated */
original: Document;
/** The id assigned to the duplicate */
id: string;
/** The url identifier assigned to the duplicate */
urlId: string;
/** The title of the duplicate */
title: string;
/** The children to duplicate under the duplicate */
children: DuplicateItem[];
};
/**
* Duplicates a document, optionally including its child documents. Links
* between the duplicated documents are remapped to point at the copies.
*
* @param ctx the context containing the acting user and transaction.
* @param props the document to duplicate and optional overrides.
* @returns the duplicated documents.
*/
export default async function documentDuplicator(
ctx: Context,
{ document, collection, parentDocumentId, title, publish, recursive }: Props
): Promise<Document[]> {
return duplicateRoots(ctx, {
roots: [{ document, title, parentDocumentId }],
collection,
publish,
recursive,
});
}
/**
* Duplicates several documents as one unit, so that links between them are
* remapped to the copies no matter which of the documents they cross between.
*
* @param ctx the context containing the acting user and transaction.
* @param props the documents to duplicate and optional overrides.
* @returns the duplicated documents.
*/
export async function documentsDuplicator(
ctx: Context,
{ documents, collection, publish, recursive }: ManyProps
): Promise<Document[]> {
return duplicateRoots(ctx, {
roots: documents.map((document) => ({ document })),
collection,
publish,
recursive,
});
}
async function duplicateRoots(
ctx: Context,
{
roots,
collection,
publish,
recursive,
}: {
roots: Root[];
collection?: Collection | null;
publish?: boolean;
recursive?: boolean;
}
): Promise<Document[]> {
const newDocuments: Document[] = [];
const references = new Map<string, DocumentReference>();
const originalCollections = new Map<string, Collection | null>();
async function originalCollectionFor(original: Document) {
const collectionId = original.collectionId;
if (!collectionId) {
return null;
}
if (!originalCollections.has(collectionId)) {
originalCollections.set(
collectionId,
await Collection.findByPk(collectionId, {
attributes: {
include: ["documentStructure"],
},
})
);
}
return originalCollections.get(collectionId) ?? null;
}
async function buildItem(
original: Document,
originalCollection: Collection | null,
titleOverride?: string
): Promise<DuplicateItem> {
const id = randomUUID();
const urlId = generateUrlId();
const itemTitle = titleOverride ?? original.title;
const reference = {
id,
path: Document.getPath({ title: itemTitle, urlId }),
};
references.set(original.id, reference);
references.set(original.urlId, reference);
return {
original,
id,
urlId,
title: itemTitle,
children: recursive
? await buildChildItems(original, originalCollection)
: [],
};
}
async function buildChildItems(
original: Document,
originalCollection: Collection | null
) {
// Structural fields only – content is re-fetched per document in
// `duplicateItem`, so a large tree isn't held in memory at once.
const childDocuments = await original.findChildDocuments(
ctx,
{
archivedAt: original.archivedAt
? {
[Op.ne]: null,
}
: {
[Op.eq]: null,
},
// Drafts are private to their author, so only the actor's own are copied.
[Op.or]: [
{ publishedAt: { [Op.ne]: null } },
{ createdById: ctx.auth.user.id },
],
},
{
attributes: { exclude: ["state", "content", "text"] },
}
);
const [tree] =
(await originalCollection?.structureFor(ctx.auth.user, {
documentId: original.id,
transaction: ctx.transaction,
})) ?? [];
const sorted = DocumentHelper.sortDocumentsByStructure(
childDocuments,
tree?.children ?? []
).reverse(); // we have to reverse since the child documents will be added in reverse order
const items: DuplicateItem[] = [];
for (const childDocument of sorted) {
items.push(await buildItem(childDocument, originalCollection));
}
return items;
}
async function duplicateItem(
item: DuplicateItem,
options: { parentDocumentId?: string; publish: boolean }
) {
const original =
item.original.dataValues.content !== undefined
? item.original
: await Document.findByPk(item.original.id, {
transaction: ctx.transaction,
rejectOnEmpty: true,
});
const duplicated = await documentCreator(ctx, {
id: item.id,
urlId: item.urlId,
parentDocumentId: options.parentDocumentId,
publish: options.publish,
collectionId: collection?.id,
icon: original.icon,
color: original.color,
fullWidth: original.fullWidth,
preferences: original.preferences,
title: item.title,
content: ProsemirrorHelper.replaceDocumentReferences(
ProsemirrorHelper.removeMarks(DocumentHelper.toProsemirror(original), [
"comment",
]),
references
),
sourceMetadata: {
...original.sourceMetadata,
originalDocumentId: original.id,
},
});
duplicated.collection = collection ?? null;
newDocuments.push(duplicated);
for (const child of item.children) {
await duplicateItem(child, {
...options,
parentDocumentId: duplicated.id,
});
}
}
// The identifiers of every duplicate are assigned before any content is
// written so that links between the documents being duplicated can be
// remapped to the copies, leaving the duplicate self-contained.
const items: DuplicateItem[] = [];
for (const root of roots) {
items.push(
await buildItem(
root.document,
await originalCollectionFor(root.document),
root.title
)
);
}
for (const [index, item] of items.entries()) {
await duplicateItem(item, {
parentDocumentId: roots[index].parentDocumentId,
publish: publish ?? !!roots[index].document.publishedAt,
});
}
return newDocuments;
}