Files
outline/server/commands/documentUpdater.ts
T
Tom MoorandClaude Opus 5.5 00b124d03c chore: Rename APIContext to Context and flatten its fields (#14684)
* chore: Rename APIContext to Context and flatten its fields

Auth and transaction now live directly on the context instead of in
ctx.state and a nested ctx.context, and model helpers read them directly.
APIContext remains as a deprecated alias.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Remove deprecated APIContext alias

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* fix: Read auth from context in OAuth provider callbacks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Add withUser and withTransaction context helpers

Replaces manual createContext rebuilds that copy fields from an existing context.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Remove arbitrary property access from Koa context types

AppContext and Context no longer inherit the any index signature from
Koa's DefaultContext, custom fields are declared explicitly, and importing
Koa's loose Context type is disallowed by lint.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-23 23:17:06 -05:00

164 lines
4.9 KiB
TypeScript

import type { DocumentPreferences, TextEditMode } from "@shared/types";
import { DocumentConflictError, ValidationError } from "@server/errors";
import { Document } from "@server/models";
import { DocumentHelper } from "@server/models/helpers/DocumentHelper";
import { TextHelper } from "@server/models/helpers/TextHelper";
import type { Context } from "@server/types";
type Props = {
/** The existing document */
document: Document;
/** The new title */
title?: string;
/** The document icon */
icon?: string | null;
/** The document icon's color */
color?: string | null;
/** The new text content */
text?: string;
/** The version of the client editor that was used */
editorVersion?: string;
/** The ID of the template that was used */
templateId?: string | null;
/** If the document should be displayed full-width on the screen */
fullWidth?: boolean;
/** Display preferences for the document, merged with existing values */
preferences?: DocumentPreferences | null;
/** Whether insights should be visible on the document */
insightsEnabled?: boolean;
/** The edit mode: "replace", "append", "prepend", or "patch" */
editMode?: TextEditMode;
/** The document revision the changes are based on, the update is rejected if it no longer matches */
lastRevision?: number;
/** The markdown text to find when using "patch" edit mode */
findText?: string;
/** Whether the document should be published to the collection */
publish?: boolean;
/** The ID of the collection to publish the document to */
collectionId?: string | null;
/** The reason the document is archived or deleted. */
deprecatedReason?: string | null;
};
/**
* This command updates document properties. To update collaborative text state
* use documentCollaborativeUpdater.
*
* @param Props The properties of the document to update
* @returns Document The updated document
*/
export default async function documentUpdater(
ctx: Context,
{
document,
title,
icon,
color,
text,
editorVersion,
templateId,
fullWidth,
preferences,
insightsEnabled,
editMode,
findText,
lastRevision,
publish,
collectionId,
deprecatedReason,
}: Props
): Promise<Document> {
const { user } = ctx.auth;
const { transaction } = ctx;
const cId = collectionId || document.collectionId;
document.fill({
title: title?.trim(),
icon,
color,
editorVersion: editorVersion || undefined,
templateId: templateId || undefined,
fullWidth,
insightsEnabled,
});
if (preferences) {
document.preferences = {
...document.preferences,
...preferences,
};
}
if (text !== undefined) {
document = DocumentHelper.applyMarkdownToDocument(
document,
await TextHelper.replaceImagesWithAttachments(ctx, text, user, {
base64Only: true,
}),
editMode,
findText,
user.id
);
}
// Serialize concurrent updates to the same document by taking a row-level
// lock before writing. The wait is already bounded by the transaction's
// lock_timeout. When lastRevision is provided it becomes part of the
// predicate, so a document modified since that revision matches no row.
if (transaction) {
const locked = await Document.unscoped().findOne({
attributes: ["id"],
where: {
id: document.id,
...(lastRevision !== undefined && { revisionCount: lastRevision }),
},
transaction,
lock: transaction.LOCK.NO_KEY_UPDATE,
paranoid: false,
});
if (!locked && lastRevision !== undefined) {
throw DocumentConflictError();
}
}
if (deprecatedReason !== undefined) {
// Reload under the lock so a concurrent restore cannot leave a stale note.
await document.reload({ transaction, paranoid: false });
if (document.isActive) {
throw ValidationError("The document must be archived or deleted");
}
document.deprecatedReason = deprecatedReason?.trim() || null;
// Keep the archive attribution and time shown in the banner unchanged.
if (document.changed("deprecatedReason")) {
await document.saveWithCtx(ctx, { silent: true });
}
return document;
}
const changed = document.changed();
const { collection } = document;
if (publish && cId) {
if (!document.collectionId) {
document.collectionId = cId;
}
await document.publish(ctx, { collectionId: cId });
} else if (changed) {
document.lastModifiedById = user.id;
document.updatedBy = user;
await document.saveWithCtx(ctx);
}
// Publishing can move the document to another collection, and the save
// hooks can attach a collection that has no membership data. Reload in
// those cases so that policies stay accurate.
if (publish || document.collection !== collection) {
return await Document.findByPk(document.id, {
userId: user.id,
rejectOnEmpty: true,
transaction,
});
}
return document;
}