mirror of
https://github.com/outline/outline.git
synced 2026-11-22 05:51:43 +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
219 lines
6.7 KiB
TypeScript
219 lines
6.7 KiB
TypeScript
import { Op } from "sequelize";
|
|
import { Hour } from "@shared/utils/time";
|
|
import { traceFunction } from "@server/logging/tracing";
|
|
import type { Document } from "@server/models";
|
|
import FileOperation from "@server/models/FileOperation";
|
|
import User from "@server/models/User";
|
|
import { DocumentHelper } from "@server/models/helpers/DocumentHelper";
|
|
import type { APIContext } from "@server/types";
|
|
import presentUser from "./user";
|
|
|
|
type Options = {
|
|
/** Whether to render the document's public fields. */
|
|
isPublic?: boolean;
|
|
/** The root share ID when presenting a shared document. */
|
|
shareId?: string;
|
|
/** Always include the text of the document in the payload. */
|
|
includeText?: boolean;
|
|
/** Always include the data of the document in the payload. */
|
|
includeData?: boolean;
|
|
/** Include the updatedAt timestamp for public documents. */
|
|
includeUpdatedAt?: boolean;
|
|
/** Include the unresolved comment count. Each call triggers a Redis lookup
|
|
* so only enable when the consumer needs the signal (e.g. MCP). */
|
|
includeCommentCount?: boolean;
|
|
/** Array of backlink document IDs to include in the response. */
|
|
backlinkIds?: string[];
|
|
};
|
|
|
|
async function presentDocument(
|
|
ctx: APIContext | undefined,
|
|
document: Document,
|
|
options: Options | null | undefined = {}
|
|
) {
|
|
options = {
|
|
isPublic: false,
|
|
...options,
|
|
};
|
|
|
|
const asData = !ctx || Number(ctx?.headers["x-api-version"] ?? 0) >= 3;
|
|
|
|
const data = await DocumentHelper.toJSON(
|
|
document,
|
|
options.isPublic
|
|
? {
|
|
signedUrls: Hour.seconds,
|
|
teamId: document.teamId,
|
|
removeMarks: ["comment"],
|
|
internalUrlBase: `/s/${options.shareId}`,
|
|
}
|
|
: undefined
|
|
);
|
|
|
|
const text =
|
|
!asData || options?.includeText
|
|
? await DocumentHelper.toMarkdown(data, { includeTitle: false })
|
|
: undefined;
|
|
|
|
const res: Record<string, unknown> = {
|
|
id: document.id,
|
|
url: document.path,
|
|
urlId: document.urlId,
|
|
title: document.title,
|
|
data:
|
|
options?.includeData === false
|
|
? undefined
|
|
: asData || options?.includeData
|
|
? data
|
|
: undefined,
|
|
text,
|
|
icon: document.icon,
|
|
color: document.color,
|
|
tasks: {
|
|
completed: 0,
|
|
total: 0,
|
|
},
|
|
language: document.language,
|
|
createdAt: document.createdAt,
|
|
createdBy: undefined,
|
|
updatedAt: document.updatedAt,
|
|
updatedBy: undefined,
|
|
publishedAt: document.publishedAt,
|
|
archivedAt: document.archivedAt,
|
|
deprecatedReason: document.deprecatedReason,
|
|
deletedAt: document.deletedAt,
|
|
deletedBy: undefined,
|
|
collaboratorIds: [],
|
|
revision: document.revisionCount,
|
|
fullWidth: document.fullWidth,
|
|
preferences: document.preferences,
|
|
collectionId: undefined,
|
|
parentDocumentId: undefined,
|
|
lastViewedAt: undefined,
|
|
isCollectionDeleted: undefined,
|
|
backlinkIds: options?.backlinkIds,
|
|
};
|
|
|
|
if (!!document.views && document.views.length > 0) {
|
|
res.lastViewedAt = document.views[0].updatedAt;
|
|
}
|
|
|
|
if (options.isPublic && !options.includeUpdatedAt) {
|
|
delete res.updatedAt;
|
|
}
|
|
|
|
if (document.summary) {
|
|
res.summary = document.summary;
|
|
}
|
|
|
|
if (!options.isPublic) {
|
|
res.tasks = document.tasks;
|
|
res.isCollectionDeleted = await document.isCollectionDeleted();
|
|
res.collectionId = document.collectionId;
|
|
// The owner of the personal collection that the document is in. Left out
|
|
// when the collection is not loaded, so that the value is not cleared.
|
|
if (!document.collectionId) {
|
|
res.ownerId = null;
|
|
} else if (document.collection !== undefined) {
|
|
res.ownerId = document.collection?.ownerId ?? null;
|
|
}
|
|
res.parentDocumentId = document.parentDocumentId;
|
|
res.createdBy = presentUser(document.createdBy);
|
|
res.updatedBy = presentUser(document.updatedBy);
|
|
res.collaboratorIds = document.collaboratorIds ?? [];
|
|
res.templateId = document.templateId;
|
|
res.insightsEnabled = document.insightsEnabled;
|
|
res.popularityScore = document.popularityScore;
|
|
if (document.deletedById) {
|
|
const deletedBy =
|
|
document.deletedBy ??
|
|
(await document.$get("deletedBy", { paranoid: false }));
|
|
if (deletedBy) {
|
|
res.deletedBy = presentUser(deletedBy);
|
|
}
|
|
}
|
|
if (options.includeCommentCount) {
|
|
res.commentCount = await document.commentCount;
|
|
}
|
|
if (document.sourceMetadata) {
|
|
const source = document.import ?? (await document.$get("import"));
|
|
res.sourceMetadata = {
|
|
importedAt: source?.createdAt ?? document.createdAt,
|
|
importType: source?.format,
|
|
createdByName: document.sourceMetadata.createdByName,
|
|
fileName: document.sourceMetadata?.fileName,
|
|
originalDocumentId: document.sourceMetadata?.originalDocumentId,
|
|
};
|
|
}
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
export default traceFunction({
|
|
spanName: "presenters",
|
|
})(presentDocument);
|
|
|
|
/**
|
|
* Batch-present multiple documents, fetching all related FileOperation and
|
|
* deleting User records in a single query instead of one per document.
|
|
*
|
|
* @param ctx the API context.
|
|
* @param documents the documents to present.
|
|
* @param options presentation options forwarded to presentDocument.
|
|
* @returns array of presented document objects.
|
|
*/
|
|
export async function presentDocuments(
|
|
ctx: APIContext | undefined,
|
|
documents: Document[],
|
|
options?: Options | null
|
|
) {
|
|
const opts = { isPublic: false, ...options };
|
|
|
|
if (!opts.isPublic) {
|
|
const importIds = documents
|
|
.filter((doc) => doc.sourceMetadata && doc.importId)
|
|
.map((doc) => doc.importId!);
|
|
|
|
if (importIds.length > 0) {
|
|
const sources = await FileOperation.unscoped().findAll({
|
|
where: { id: { [Op.in]: importIds } },
|
|
});
|
|
const sourceMap = new Map(sources.map((s) => [s.id, s]));
|
|
|
|
for (const doc of documents) {
|
|
if (doc.importId) {
|
|
doc.import = sourceMap.get(doc.importId) ?? null;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Deduplicated because a page of trashed documents is often the work of a
|
|
// single person, and only for documents that arrived without the
|
|
// association so that an eager loaded one is not overwritten.
|
|
const deletedByIds = new Set(
|
|
documents
|
|
.filter((doc) => doc.deletedById && !doc.deletedBy)
|
|
.map((doc) => doc.deletedById!)
|
|
);
|
|
|
|
if (deletedByIds.size > 0) {
|
|
const users = await User.unscoped().findAll({
|
|
where: { id: { [Op.in]: Array.from(deletedByIds) } },
|
|
paranoid: false,
|
|
});
|
|
const userMap = new Map(users.map((user) => [user.id, user]));
|
|
|
|
for (const doc of documents) {
|
|
if (doc.deletedById && !doc.deletedBy) {
|
|
doc.deletedBy = userMap.get(doc.deletedById) ?? null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return Promise.all(
|
|
documents.map((document) => presentDocument(ctx, document, opts))
|
|
);
|
|
}
|