mirror of
https://github.com/outline/outline.git
synced 2026-11-22 22:02:35 +03:00
* feat: Personal collections backend Adds an owner to collections. A personal collection is private, one per user per team, created lazily through collections.personal, and only its owner can see or manage it. Admin bypasses in policies, lists, exports, events, webhooks and file operations exclude the personal collections of other users. Turning off the PersonalDocs preference makes the collection read-only. * test: Access matrix for personal collections Checks that collection and document endpoints, list and search results, MCP tools, collaboration and websocket rooms agree with the read policy for the owner, collaborators, members, admins, guests and other teams. * feat: Personal collection in the sidebar Adds a Personal sidebar section that renders the personal collection with the collection document tree, creates the collection on the first new document, and keeps it out of the Collections section. Adds the workspace setting, the personal sidebar context, and hides collection sharing and membership UI for personal collections. * fix: Drag and drop in shared with me trees Anchors the reorder drop cursor inside the row, and prefetches documents on hover so that nested rows that were never opened can be dragged. * feat: Personal collection owner for collaborators, no collection page Presents the owner of the personal collection on documents, so that collaborators see the owner in the share popover. Root rows in the Personal section show the default document icon. A personal collection has no page: the scene shows not found, and the breadcrumb, command bar and delete redirect no longer link to it. * fix: Personal sidebar indent and breadcrumb items without a link Children of rows at depth 0 skip a level so that they start beyond the row icon. Breadcrumb items without a destination, such as the personal and deleted collection, are shown as plain text. * test: Reduce personal collection tests to list paths Endpoints that authorize a single model are already covered by the policy tests. Keeps the lists and searches, which filter in queries, and the presenter. * test: Move personal collection list tests into collections tests * feat: Create documents in the personal collection by alias documents.create, documents.import and the MCP create_document tool accept the collectionId "personal", which refers to the personal collection of the user and creates it when it does not exist yet. * refactor: Remove collections.personal route The sidebar creates its first personal document with the personal collectionId alias, so documents.create is the only path that creates the personal collection. * feat: Personal collection name, starring, templates and default Personal collections cannot be starred, renamed or hold templates, and the PersonalDocs preference is on by default. The app shows collections through a displayName getter, which is the translated "Personal" for a personal collection. * fix: Use the same policy for starring and unstarring collections * fix: No pins or viewer insights for personal documents * revert: Show personal collection events in the audit log Admins with audit access should see events in personal collections, even when they cannot open the documents. * fix: Load collection owner in share scopes * fix: Keep personal collections out of workspace exports and webhooks Workspace exports skip all personal collections, while an explicit export of a personal collection stays available to its owner. Events in personal collections, including attachments and documents moved after queueing, are never delivered to workspace webhooks. Personal collections cannot be renamed or made public through any path, cannot hold pins, and are not offered as mentions. * Change icon to avatar * fix: Personal documents cannot be unpublished * feat: Personal collection context for MCP Collections are presented with an isPersonal flag, the server instructions explain the personal collection, and move_document and update_document accept the personal collectionId alias. * fix: Keep moved personal exports private and remove pins on move Document exports record the owner of the personal collection they came from, so they stay private after the document moves. Moving documents into a personal collection removes their pins, including pins to home. * fix: Only create the personal collection in MCP when it is used
284 lines
7.6 KiB
TypeScript
284 lines
7.6 KiB
TypeScript
import type { Optional } from "utility-types";
|
|
import { PersonalCollectionId } from "@shared/constants";
|
|
import { TextHelper } from "@shared/utils/TextHelper";
|
|
import { Collection, Document, type Template } from "@server/models";
|
|
import { DocumentHelper } from "@server/models/helpers/DocumentHelper";
|
|
import { ProsemirrorHelper } from "@server/models/helpers/ProsemirrorHelper";
|
|
import { authorize } from "@server/policies";
|
|
import type { APIContext } from "@server/types";
|
|
import { assertPresent } from "@server/validation";
|
|
import personalCollectionLoader from "./personalCollectionLoader";
|
|
|
|
type Props = Optional<
|
|
Pick<
|
|
Document,
|
|
| "id"
|
|
| "urlId"
|
|
| "title"
|
|
| "text"
|
|
| "content"
|
|
| "icon"
|
|
| "color"
|
|
| "collectionId"
|
|
| "parentDocumentId"
|
|
| "importId"
|
|
| "apiImportId"
|
|
| "fullWidth"
|
|
| "preferences"
|
|
| "sourceMetadata"
|
|
| "editorVersion"
|
|
| "publishedAt"
|
|
| "createdAt"
|
|
| "updatedAt"
|
|
| "createdById"
|
|
| "lastModifiedById"
|
|
>
|
|
> & {
|
|
state?: Buffer;
|
|
publish?: boolean;
|
|
template?: Template | null;
|
|
index?: number;
|
|
};
|
|
|
|
type CreateLocation = {
|
|
/** The collection to place the document in, if any. */
|
|
collectionId?: string | null;
|
|
/** The parent document to nest the new document under, if any. */
|
|
parentDocumentId?: string | null;
|
|
};
|
|
|
|
/**
|
|
* Authorizes the creation of a document at the requested location and resolves
|
|
* the collection and parent document it will belong to. Shared by the
|
|
* documents.create API route and the MCP create_document tool so that both
|
|
* enforce identical permissions, including the team-level check that prevents
|
|
* viewers and guests from creating drafts with no collection. The collectionId
|
|
* "personal" refers to the personal collection of the user, which is created
|
|
* when it does not exist yet.
|
|
*
|
|
* @param ctx the API context containing the acting user.
|
|
* @param location the requested collection and/or parent document.
|
|
* @returns the resolved collection and parent document, when applicable.
|
|
* @throws AuthorizationError when the user may not create the document.
|
|
*/
|
|
export async function authorizeDocumentCreate(
|
|
ctx: APIContext,
|
|
{ collectionId, parentDocumentId }: CreateLocation
|
|
): Promise<{
|
|
collection?: Collection | null;
|
|
parentDocument?: Document | null;
|
|
}> {
|
|
const { user } = ctx.state.auth;
|
|
const { transaction } = ctx.state;
|
|
|
|
if (parentDocumentId) {
|
|
const parentDocument = await Document.findByPk(parentDocumentId, {
|
|
userId: user.id,
|
|
transaction,
|
|
});
|
|
const collection = parentDocument?.collectionId
|
|
? await Collection.findByPk(parentDocument.collectionId, {
|
|
userId: user.id,
|
|
transaction,
|
|
})
|
|
: undefined;
|
|
authorize(user, "createChildDocument", parentDocument, { collection });
|
|
return { collection, parentDocument };
|
|
}
|
|
|
|
if (collectionId) {
|
|
const collection =
|
|
collectionId === PersonalCollectionId
|
|
? await personalCollectionLoader(ctx)
|
|
: await Collection.findByPk(collectionId, {
|
|
userId: user.id,
|
|
transaction,
|
|
});
|
|
authorize(user, "createDocument", collection);
|
|
return { collection };
|
|
}
|
|
|
|
authorize(user, "createDocument", user.team);
|
|
return {};
|
|
}
|
|
|
|
/**
|
|
* Authorizes publishing a document into a collection and resolves the target
|
|
* collection. Shared by the documents.update API route and the MCP
|
|
* update_document tool. Publishing places a document into a collection, so it
|
|
* requires create permission on the destination — separate from the update
|
|
* permission that governs editing a draft's content.
|
|
*
|
|
* @param ctx the API context containing the acting user.
|
|
* @param document the document being published.
|
|
* @param collectionId the destination collection, required when publishing a draft that has none.
|
|
* @returns the resolved destination collection.
|
|
* @throws AuthorizationError when the user may not publish into the collection.
|
|
*/
|
|
export async function authorizeDocumentPublish(
|
|
ctx: APIContext,
|
|
document: Document,
|
|
collectionId?: string | null
|
|
): Promise<Collection | null | undefined> {
|
|
const { user } = ctx.state.auth;
|
|
const { transaction } = ctx.state;
|
|
let collection = document.collection;
|
|
|
|
if (document.isDraft) {
|
|
authorize(user, "publish", document);
|
|
}
|
|
|
|
if (!document.collectionId) {
|
|
assertPresent(
|
|
collectionId,
|
|
"collectionId is required to publish a draft without collection"
|
|
);
|
|
collection = await Collection.findByPk(collectionId!, {
|
|
userId: user.id,
|
|
transaction,
|
|
});
|
|
}
|
|
|
|
if (document.parentDocumentId) {
|
|
const parentDocument = await Document.findByPk(document.parentDocumentId, {
|
|
userId: user.id,
|
|
transaction,
|
|
});
|
|
authorize(user, "createChildDocument", parentDocument, { collection });
|
|
} else {
|
|
authorize(user, "createDocument", collection);
|
|
}
|
|
|
|
return collection;
|
|
}
|
|
|
|
export default async function documentCreator(
|
|
ctx: APIContext,
|
|
{
|
|
title,
|
|
text,
|
|
icon,
|
|
color,
|
|
state,
|
|
id,
|
|
urlId,
|
|
publish,
|
|
index,
|
|
collectionId,
|
|
parentDocumentId,
|
|
content,
|
|
template,
|
|
fullWidth,
|
|
preferences,
|
|
importId,
|
|
apiImportId,
|
|
createdAt,
|
|
// allows override for import
|
|
updatedAt,
|
|
editorVersion,
|
|
publishedAt,
|
|
sourceMetadata,
|
|
createdById,
|
|
lastModifiedById,
|
|
}: Props
|
|
): Promise<Document> {
|
|
const { user } = ctx.state.auth;
|
|
const { transaction } = ctx.state;
|
|
const templateId = template ? template.id : undefined;
|
|
const eventData = importId || apiImportId ? { source: "import" } : undefined;
|
|
|
|
if (state && template) {
|
|
throw new Error(
|
|
"State cannot be set when creating a document from a template"
|
|
);
|
|
}
|
|
|
|
if (urlId) {
|
|
const existing = await Document.unscoped().findOne({
|
|
attributes: ["id"],
|
|
transaction,
|
|
where: {
|
|
urlId,
|
|
},
|
|
});
|
|
if (existing) {
|
|
urlId = undefined;
|
|
}
|
|
}
|
|
|
|
const titleWithReplacements =
|
|
title ??
|
|
(template ? TextHelper.replaceTemplateVariables(template.title, user) : "");
|
|
|
|
const contentWithReplacements = content
|
|
? content
|
|
: text
|
|
? ProsemirrorHelper.toProsemirror(text).toJSON()
|
|
: template
|
|
? ProsemirrorHelper.replaceTemplateVariables(
|
|
await DocumentHelper.toJSON(template),
|
|
user
|
|
)
|
|
: ProsemirrorHelper.toProsemirror("").toJSON();
|
|
|
|
const document = Document.build({
|
|
id,
|
|
urlId,
|
|
parentDocumentId,
|
|
editorVersion,
|
|
collectionId,
|
|
teamId: user.teamId,
|
|
createdAt,
|
|
updatedAt: updatedAt ?? createdAt,
|
|
lastModifiedById: lastModifiedById ?? createdById ?? user.id,
|
|
createdById: createdById ?? user.id,
|
|
templateId,
|
|
publishedAt,
|
|
importId,
|
|
apiImportId,
|
|
sourceMetadata,
|
|
fullWidth: fullWidth ?? template?.fullWidth,
|
|
preferences,
|
|
icon: icon ?? template?.icon,
|
|
color: color ?? template?.color,
|
|
title: titleWithReplacements,
|
|
content: contentWithReplacements,
|
|
state,
|
|
});
|
|
|
|
document.text = await DocumentHelper.toMarkdown(document, {
|
|
includeTitle: false,
|
|
});
|
|
|
|
await document.saveWithCtx(
|
|
ctx,
|
|
{
|
|
silent: !!createdAt,
|
|
},
|
|
{ data: eventData }
|
|
);
|
|
|
|
if (publish) {
|
|
if (!collectionId) {
|
|
throw new Error("Collection ID is required to publish");
|
|
}
|
|
|
|
await document.publish(ctx, {
|
|
collectionId,
|
|
silent: true,
|
|
index,
|
|
event: !!document.title,
|
|
data: eventData,
|
|
});
|
|
}
|
|
|
|
// reload to get all of the data needed to present (user, collection etc)
|
|
// we need to specify publishedAt to bypass default scope that only returns
|
|
// published documents
|
|
return Document.findByPk(document.id, {
|
|
userId: user.id,
|
|
rejectOnEmpty: true,
|
|
transaction,
|
|
});
|
|
}
|