mirror of
https://github.com/outline/outline.git
synced 2026-11-24 23:00:18 +03:00
- presentDocuments: batch-load collection deletedAt for archived/deleted documents so trash and archive listings no longer issue one Collection query per document via isCollectionDeleted - documents.search: present results through the batched presentDocuments helper instead of one presentDocument call per result - revisions.list: load collaborators for the whole page in a single User query instead of one per revision, and stop fetching the unused content and text columns when listing without content - TextHelper.attachmentsToSignedUrls: fetch all attachments in one query instead of one findOne per attachment id - BacklinksProcessor: resolve linked documents and existing backlinks in bulk queries and create missing backlinks with a single bulkCreate - NotificationHelper: batch the per-recipient View lookup when filtering comment notification recipients - Add indexes covering hot queries that previously scanned: events (teamId, createdAt) for the audit log, and shares (documentId) / (collectionId) for shareLoader lookups Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TTaaEHNxBrmBnoVa5iSJV6
212 lines
6.3 KiB
TypeScript
212 lines
6.3 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 Collection from "@server/models/Collection";
|
|
import FileOperation from "@server/models/FileOperation";
|
|
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,
|
|
deletedAt: document.deletedAt,
|
|
collaboratorIds: [],
|
|
revision: document.revisionCount,
|
|
fullWidth: document.fullWidth,
|
|
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;
|
|
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 (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
|
|
* Collection records in a single query each 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;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Preload the collections needed by isCollectionDeleted so that deleted
|
|
// and archived listings don't trigger one Collection query per document.
|
|
const collectionIds = [
|
|
...new Set(
|
|
documents
|
|
.filter(
|
|
(doc) =>
|
|
(doc.deletedAt || doc.archivedAt) &&
|
|
doc.collectionId &&
|
|
!doc.collection
|
|
)
|
|
.map((doc) => doc.collectionId!)
|
|
),
|
|
];
|
|
|
|
if (collectionIds.length > 0) {
|
|
const collections = await Collection.unscoped().findAll({
|
|
attributes: ["id", "deletedAt"],
|
|
where: { id: { [Op.in]: collectionIds } },
|
|
paranoid: false,
|
|
});
|
|
const collectionMap = new Map(collections.map((c) => [c.id, c]));
|
|
|
|
for (const doc of documents) {
|
|
if (
|
|
(doc.deletedAt || doc.archivedAt) &&
|
|
doc.collectionId &&
|
|
!doc.collection
|
|
) {
|
|
doc.collection = collectionMap.get(doc.collectionId) ?? null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return Promise.all(
|
|
documents.map((document) => presentDocument(ctx, document, opts))
|
|
);
|
|
}
|