mirror of
https://github.com/outline/outline.git
synced 2026-11-26 15:43:48 +03:00
* 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>
101 lines
3.3 KiB
TypeScript
101 lines
3.3 KiB
TypeScript
import { traceFunction } from "@server/logging/tracing";
|
|
import { ParamRequiredError, ValidationError } from "@server/errors";
|
|
import { Collection, Revision } from "@server/models";
|
|
import type { Document } from "@server/models";
|
|
import { authorize } from "@server/policies";
|
|
import type { Context } from "@server/types";
|
|
|
|
type Props = {
|
|
/** The document to restore. Must be loaded with `paranoid: false`. */
|
|
document: Document;
|
|
/** Destination collection to restore into. Defaults to the original collection. */
|
|
collectionId?: string | null;
|
|
/** Revision to restore the document's content from, when not archived or deleted. The collection is not changed. */
|
|
revisionId?: string | null;
|
|
};
|
|
|
|
/**
|
|
* Restores a previously archived or deleted document, or restores an active
|
|
* document's content to a specific revision. Archived and deleted documents
|
|
* are re-attached to the destination collection's structure. Authorizes the
|
|
* acting user against the document before any other check.
|
|
*
|
|
* @param ctx - the context, providing the acting user and transaction.
|
|
* @param props - the document and restore options.
|
|
* @returns the restored document.
|
|
* @throws AuthorizationError if the user cannot restore the document.
|
|
* @throws ValidationError if the destination collection is not active.
|
|
* @throws NotFoundError if the given revision does not exist.
|
|
*/
|
|
async function documentRestorer(
|
|
ctx: Context,
|
|
{ document, collectionId, revisionId }: Props
|
|
): Promise<Document> {
|
|
const { user } = ctx.auth;
|
|
const { transaction } = ctx;
|
|
|
|
if (!document.deletedAt && !document.archivedAt) {
|
|
// restore an active document to a specific revision
|
|
authorize(user, "update", document);
|
|
if (!revisionId) {
|
|
throw ParamRequiredError("revisionId is required");
|
|
}
|
|
|
|
const revision = await Revision.findByPk(revisionId, {
|
|
transaction,
|
|
rejectOnEmpty: true,
|
|
});
|
|
authorize(document, "restore", revision);
|
|
|
|
await document.restoreFromRevision(revision);
|
|
await document.saveWithCtx(ctx, undefined, { name: "restore" });
|
|
return document;
|
|
}
|
|
|
|
authorize(user, document.deletedAt ? "restore" : "unarchive", document);
|
|
|
|
const sourceCollectionId = document.collectionId;
|
|
const destCollectionId = collectionId ?? sourceCollectionId;
|
|
|
|
const srcCollection = sourceCollectionId
|
|
? await Collection.findByPk(sourceCollectionId, {
|
|
userId: user.id,
|
|
includeDocumentStructure: true,
|
|
paranoid: false,
|
|
transaction,
|
|
})
|
|
: undefined;
|
|
|
|
const destCollection = destCollectionId
|
|
? await Collection.findByPk(destCollectionId, {
|
|
userId: user.id,
|
|
includeDocumentStructure: true,
|
|
transaction,
|
|
})
|
|
: undefined;
|
|
|
|
if (!destCollection?.isActive) {
|
|
throw ValidationError(
|
|
"Unable to restore, the collection may have been deleted or archived"
|
|
);
|
|
}
|
|
authorize(user, "updateDocument", destCollection);
|
|
|
|
if (sourceCollectionId && sourceCollectionId !== destCollection.id) {
|
|
authorize(user, "updateDocument", srcCollection);
|
|
await srcCollection?.removeDocumentInStructure(document, {
|
|
save: true,
|
|
transaction,
|
|
});
|
|
}
|
|
|
|
// restore a previously archived or deleted document
|
|
await document.restoreTo(ctx, { collectionId: destCollection.id });
|
|
|
|
return document;
|
|
}
|
|
|
|
export default traceFunction({
|
|
spanName: "documentRestorer",
|
|
})(documentRestorer);
|