mirror of
https://github.com/outline/outline.git
synced 2026-11-29 00:29:36 +03:00
refactor: move revision and export logic out of commands (#13856)
This commit is contained in:
1 parent
ac48c91a7e
commit
de33f87aea
10 files changed
+196
-167
No files matched your search
@@ -1,63 +0,0 @@
|
||||
import {
|
||||
FileOperationFormat,
|
||||
FileOperationType,
|
||||
FileOperationState,
|
||||
} from "@shared/types";
|
||||
import { traceFunction } from "@server/logging/tracing";
|
||||
import type { Collection, Team, User } from "@server/models";
|
||||
import { FileOperation } from "@server/models";
|
||||
import { type APIContext } from "@server/types";
|
||||
|
||||
type Props = {
|
||||
collection?: Collection;
|
||||
team: Team;
|
||||
user: User;
|
||||
format?: FileOperationFormat;
|
||||
includeAttachments?: boolean;
|
||||
includePrivate?: boolean;
|
||||
ctx: APIContext;
|
||||
};
|
||||
|
||||
async function collectionExporter({
|
||||
collection,
|
||||
team,
|
||||
user,
|
||||
format = FileOperationFormat.MarkdownZip,
|
||||
includeAttachments = true,
|
||||
includePrivate = true,
|
||||
ctx,
|
||||
}: Props) {
|
||||
const collectionId = collection?.id;
|
||||
const key = FileOperation.getExportKey({
|
||||
name: collection?.name || team.name,
|
||||
teamId: user.teamId,
|
||||
format,
|
||||
});
|
||||
const fileOperation = await FileOperation.createWithCtx(ctx, {
|
||||
type: FileOperationType.Export,
|
||||
state: FileOperationState.Creating,
|
||||
format,
|
||||
key,
|
||||
url: null,
|
||||
size: 0,
|
||||
collectionId,
|
||||
options: {
|
||||
includeAttachments,
|
||||
includePrivate,
|
||||
},
|
||||
userId: user.id,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
|
||||
fileOperation.user = user;
|
||||
|
||||
if (collection) {
|
||||
fileOperation.collection = collection;
|
||||
}
|
||||
|
||||
return fileOperation;
|
||||
}
|
||||
|
||||
export default traceFunction({
|
||||
spanName: "collectionExporter",
|
||||
})(collectionExporter);
|
||||
@@ -1,34 +0,0 @@
|
||||
import { Event } from "@server/models";
|
||||
import { buildDocument, buildUser } from "@server/test/factories";
|
||||
import type { DocumentEvent } from "@server/types";
|
||||
import { AuthenticationType } from "@server/types";
|
||||
import revisionCreator from "./revisionCreator";
|
||||
|
||||
describe("revisionCreator", () => {
|
||||
it("should create revision model from document", async () => {
|
||||
const user = await buildUser();
|
||||
const document = await buildDocument({
|
||||
userId: user.id,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
const revision = await revisionCreator({
|
||||
document,
|
||||
user,
|
||||
collaboratorIds: [user.id],
|
||||
event: {
|
||||
name: "documents.update",
|
||||
authType: AuthenticationType.APP,
|
||||
} as DocumentEvent,
|
||||
});
|
||||
const event = await Event.findLatest({
|
||||
teamId: user.teamId,
|
||||
});
|
||||
expect(revision.documentId).toEqual(document.id);
|
||||
expect(revision.userId).toEqual(user.id);
|
||||
expect(revision.createdAt).toEqual(document.updatedAt);
|
||||
expect(revision.sourceMetadata?.authType).toEqual(AuthenticationType.APP);
|
||||
expect(event!.name).toEqual("revisions.create");
|
||||
expect(event!.modelId).toEqual(revision.id);
|
||||
expect(event!.authType).toEqual(AuthenticationType.APP);
|
||||
});
|
||||
});
|
||||
@@ -1,31 +0,0 @@
|
||||
import { createContext } from "@server/context";
|
||||
import type { User, Document } from "@server/models";
|
||||
import { Revision } from "@server/models";
|
||||
import { sequelize } from "@server/storage/database";
|
||||
import type { DocumentEvent, RevisionEvent } from "@server/types";
|
||||
|
||||
export default async function revisionCreator({
|
||||
event,
|
||||
document,
|
||||
collaboratorIds,
|
||||
user,
|
||||
}: {
|
||||
event: DocumentEvent | RevisionEvent;
|
||||
document: Document;
|
||||
collaboratorIds: string[];
|
||||
user: User;
|
||||
}) {
|
||||
return sequelize.transaction(
|
||||
async (transaction) =>
|
||||
await Revision.createFromDocument(
|
||||
createContext({
|
||||
user,
|
||||
authType: event.authType,
|
||||
ip: event.ip,
|
||||
transaction,
|
||||
}),
|
||||
document,
|
||||
collaboratorIds
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -1,7 +1,13 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import path from "node:path";
|
||||
import { FileOperationFormat } from "@shared/types";
|
||||
import {
|
||||
FileOperationFormat,
|
||||
FileOperationState,
|
||||
FileOperationType,
|
||||
} from "@shared/types";
|
||||
import { createContext } from "@server/context";
|
||||
import {
|
||||
buildCollection,
|
||||
buildFileOperation,
|
||||
buildTeam,
|
||||
buildUser,
|
||||
@@ -10,6 +16,46 @@ import { ValidateKey } from "@server/validation";
|
||||
import FileOperation from "./FileOperation";
|
||||
|
||||
describe("FileOperation", () => {
|
||||
describe("createExport", () => {
|
||||
it("should create a team export with the requested options", async () => {
|
||||
const team = await buildTeam();
|
||||
const user = await buildUser({ teamId: team.id });
|
||||
|
||||
const fileOperation = await FileOperation.createExport(
|
||||
createContext({ user }),
|
||||
{ team, includeAttachments: false, includePrivate: false }
|
||||
);
|
||||
|
||||
expect(fileOperation.type).toBe(FileOperationType.Export);
|
||||
expect(fileOperation.state).toBe(FileOperationState.Creating);
|
||||
expect(fileOperation.format).toBe(FileOperationFormat.MarkdownZip);
|
||||
expect(fileOperation.key).toContain(`uploads/${team.id}/`);
|
||||
expect(fileOperation.options).toEqual({
|
||||
includeAttachments: false,
|
||||
includePrivate: false,
|
||||
});
|
||||
expect(fileOperation.user.id).toBe(user.id);
|
||||
});
|
||||
|
||||
it("should set the collection association for a collection export", async () => {
|
||||
const team = await buildTeam();
|
||||
const user = await buildUser({ teamId: team.id });
|
||||
const collection = await buildCollection({ teamId: team.id });
|
||||
|
||||
const fileOperation = await FileOperation.createExport(
|
||||
createContext({ user }),
|
||||
{ team, collection }
|
||||
);
|
||||
|
||||
expect(fileOperation.collectionId).toBe(collection.id);
|
||||
expect(fileOperation.collection?.id).toBe(collection.id);
|
||||
expect(fileOperation.options).toEqual({
|
||||
includeAttachments: true,
|
||||
includePrivate: true,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("getExportKey", () => {
|
||||
it("should write to the uploads bucket", () => {
|
||||
const teamId = randomUUID();
|
||||
|
||||
@@ -18,9 +18,14 @@ import {
|
||||
DataType,
|
||||
} from "sequelize-typescript";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import type { CollectionPermission, FileOperationFormat } from "@shared/types";
|
||||
import { FileOperationState, FileOperationType } from "@shared/types";
|
||||
import type { CollectionPermission } from "@shared/types";
|
||||
import {
|
||||
FileOperationFormat,
|
||||
FileOperationState,
|
||||
FileOperationType,
|
||||
} from "@shared/types";
|
||||
import FileStorage from "@server/storage/files";
|
||||
import type { APIContext } from "@server/types";
|
||||
import { ValidateKey } from "@server/validation";
|
||||
import Collection from "./Collection";
|
||||
import Document from "./Document";
|
||||
@@ -40,6 +45,23 @@ type AdditionalFindOptions = {
|
||||
rejectOnEmpty?: boolean | Error;
|
||||
};
|
||||
|
||||
interface CreateCollectionExportOptions {
|
||||
collection?: Collection;
|
||||
team: Team;
|
||||
format?: FileOperationFormat;
|
||||
includeAttachments?: boolean;
|
||||
includePrivate?: boolean;
|
||||
}
|
||||
|
||||
interface CreateDocumentExportOptions {
|
||||
document: Document;
|
||||
format: FileOperationFormat;
|
||||
}
|
||||
|
||||
type CreateExportOptions =
|
||||
| CreateCollectionExportOptions
|
||||
| CreateDocumentExportOptions;
|
||||
|
||||
@DefaultScope(() => ({
|
||||
include: [
|
||||
{
|
||||
@@ -113,6 +135,63 @@ class FileOperation extends ParanoidModel<
|
||||
/** The number of days a completed file operation remains downloadable. */
|
||||
static expiryDays = 15;
|
||||
|
||||
/**
|
||||
* Create a file operation for a team, collection, or document export.
|
||||
*
|
||||
* @param ctx the request context with the acting user and transaction.
|
||||
* @param options the source and settings for the export.
|
||||
* @returns the new file operation with its source associations set.
|
||||
*/
|
||||
static async createExport(
|
||||
ctx: APIContext,
|
||||
options: CreateExportOptions
|
||||
): Promise<FileOperation> {
|
||||
const { user } = ctx.state.auth;
|
||||
const format = options.format ?? FileOperationFormat.MarkdownZip;
|
||||
const source =
|
||||
"document" in options
|
||||
? {
|
||||
name: options.document.titleWithDefault,
|
||||
documentId: options.document.id,
|
||||
teamId: options.document.teamId,
|
||||
}
|
||||
: {
|
||||
name: options.collection?.name || options.team.name,
|
||||
collectionId: options.collection?.id,
|
||||
teamId: user.teamId,
|
||||
options: {
|
||||
includeAttachments: options.includeAttachments ?? true,
|
||||
includePrivate: options.includePrivate ?? true,
|
||||
},
|
||||
};
|
||||
const { name, ...attributes } = source;
|
||||
const key = this.getExportKey({
|
||||
name,
|
||||
teamId: source.teamId,
|
||||
format,
|
||||
});
|
||||
const fileOperation = await this.createWithCtx(ctx, {
|
||||
type: FileOperationType.Export,
|
||||
state: FileOperationState.Creating,
|
||||
format,
|
||||
key,
|
||||
url: null,
|
||||
size: 0,
|
||||
userId: user.id,
|
||||
...attributes,
|
||||
});
|
||||
|
||||
fileOperation.user = user;
|
||||
|
||||
if ("document" in options) {
|
||||
fileOperation.document = options.document;
|
||||
} else if (options.collection) {
|
||||
fileOperation.collection = options.collection;
|
||||
}
|
||||
|
||||
return fileOperation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Overrides the standard findByPk behavior to allow loading the exported
|
||||
* collection or document with memberships for a user passed in by `userId`.
|
||||
|
||||
@@ -1,14 +1,19 @@
|
||||
import { createContext } from "@server/context";
|
||||
import { Document, Revision } from "@server/models";
|
||||
import { Document, Event, Revision } from "@server/models";
|
||||
import Redis from "@server/storage/redis";
|
||||
import { buildDocument, buildUser } from "@server/test/factories";
|
||||
import { AuthenticationType } from "@server/types";
|
||||
import RevisionsProcessor from "./RevisionsProcessor";
|
||||
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
describe("documents.update.debounced", () => {
|
||||
test("should create a revision", async () => {
|
||||
const document = await buildDocument();
|
||||
const user = await buildUser();
|
||||
const document = await buildDocument({
|
||||
teamId: user.teamId,
|
||||
userId: user.id,
|
||||
});
|
||||
|
||||
const processor = new RevisionsProcessor();
|
||||
await processor.perform({
|
||||
@@ -19,14 +24,24 @@ describe("documents.update.debounced", () => {
|
||||
actorId: document.createdById,
|
||||
createdAt: new Date().toISOString(),
|
||||
data: { done: true },
|
||||
authType: AuthenticationType.APP,
|
||||
ip,
|
||||
});
|
||||
const amount = await Revision.count({
|
||||
const revision = await Revision.findOne({
|
||||
where: {
|
||||
documentId: document.id,
|
||||
},
|
||||
rejectOnEmpty: true,
|
||||
});
|
||||
expect(amount).toBe(1);
|
||||
const event = await Event.findLatest({ teamId: document.teamId });
|
||||
|
||||
expect(revision.documentId).toBe(document.id);
|
||||
expect(revision.userId).toBe(document.createdById);
|
||||
expect(revision.createdAt).toEqual(document.updatedAt);
|
||||
expect(revision.sourceMetadata?.authType).toBe(AuthenticationType.APP);
|
||||
expect(event?.name).toBe("revisions.create");
|
||||
expect(event?.modelId).toBe(revision.id);
|
||||
expect(event?.authType).toBe(AuthenticationType.APP);
|
||||
});
|
||||
|
||||
test("should not create a revision if identical to previous", async () => {
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import isEqual from "fast-deep-equal";
|
||||
import { createContext } from "@server/context";
|
||||
import Redis from "@server/storage/redis";
|
||||
import revisionCreator from "@server/commands/revisionCreator";
|
||||
import { Revision, Document, User } from "@server/models";
|
||||
import { sequelize } from "@server/storage/database";
|
||||
import type { DocumentEvent, RevisionEvent, Event } from "@server/types";
|
||||
import DocumentUpdateTextTask from "../tasks/DocumentUpdateTextTask";
|
||||
import BaseProcessor from "./BaseProcessor";
|
||||
@@ -64,12 +65,18 @@ export default class RevisionsProcessor extends BaseProcessor {
|
||||
rejectOnEmpty: true,
|
||||
});
|
||||
|
||||
await revisionCreator({
|
||||
event,
|
||||
user,
|
||||
collaboratorIds,
|
||||
document,
|
||||
});
|
||||
await sequelize.transaction((transaction) =>
|
||||
Revision.createFromDocument(
|
||||
createContext({
|
||||
user,
|
||||
authType: event.authType,
|
||||
ip: event.ip,
|
||||
transaction,
|
||||
}),
|
||||
document,
|
||||
collaboratorIds
|
||||
)
|
||||
);
|
||||
if (sequence !== undefined) {
|
||||
// A subsequent edit by the same user has a higher score and survives
|
||||
// this cleanup. Leave all attribution intact if revision creation fails.
|
||||
|
||||
@@ -14,7 +14,6 @@ import {
|
||||
} from "@shared/types";
|
||||
import { ImportValidation } from "@shared/validations";
|
||||
import collectionDuplicator from "@server/commands/collectionDuplicator";
|
||||
import collectionExporter from "@server/commands/collectionExporter";
|
||||
import teamUpdater from "@server/commands/teamUpdater";
|
||||
import auth from "@server/middlewares/authentication";
|
||||
import { rateLimiter } from "@server/middlewares/rateLimiter";
|
||||
@@ -30,6 +29,7 @@ import {
|
||||
Attachment,
|
||||
Document,
|
||||
Import,
|
||||
FileOperation,
|
||||
} from "@server/models";
|
||||
import {
|
||||
buildWhere,
|
||||
@@ -552,13 +552,11 @@ router.post(
|
||||
});
|
||||
authorize(user, "export", collection);
|
||||
|
||||
const fileOperation = await collectionExporter({
|
||||
const fileOperation = await FileOperation.createExport(ctx, {
|
||||
collection,
|
||||
team,
|
||||
user,
|
||||
format,
|
||||
includeAttachments,
|
||||
ctx,
|
||||
});
|
||||
|
||||
ctx.body = {
|
||||
@@ -583,13 +581,11 @@ router.post(
|
||||
const team = await Team.findByPk(user.teamId, { transaction });
|
||||
authorize(user, "createExport", team);
|
||||
|
||||
const fileOperation = await collectionExporter({
|
||||
user,
|
||||
const fileOperation = await FileOperation.createExport(ctx, {
|
||||
team,
|
||||
format,
|
||||
includeAttachments,
|
||||
includePrivate,
|
||||
ctx,
|
||||
});
|
||||
|
||||
ctx.body = {
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
CollectionPermission,
|
||||
DocumentPermission,
|
||||
ExportContentType,
|
||||
FileOperationFormat,
|
||||
HeadingPrefixStyle,
|
||||
StatusFilter,
|
||||
UserRole,
|
||||
@@ -23,6 +24,7 @@ import {
|
||||
UserMembership,
|
||||
SearchQuery,
|
||||
Event,
|
||||
FileOperation,
|
||||
GroupMembership,
|
||||
Relationship,
|
||||
} from "@server/models";
|
||||
@@ -616,6 +618,34 @@ describe("#documents.export", () => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("should create a file operation for a document export with children", async () => {
|
||||
const user = await buildUser();
|
||||
const document = await buildDocument({
|
||||
title: "Nested Export",
|
||||
userId: user.id,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
|
||||
const res = await server.post("/api/documents.export", user, {
|
||||
body: { id: document.id, includeChildDocuments: true },
|
||||
headers: { accept: "text/markdown" },
|
||||
});
|
||||
const body = await res.json();
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(body.data.fileOperation.name).toBe(document.title);
|
||||
expect(body.data.fileOperation.state).toBe("creating");
|
||||
|
||||
const fileOperation = await FileOperation.findByPk(
|
||||
body.data.fileOperation.id,
|
||||
{ rejectOnEmpty: true }
|
||||
);
|
||||
expect(fileOperation.documentId).toBe(document.id);
|
||||
expect(fileOperation.teamId).toBe(document.teamId);
|
||||
expect(fileOperation.format).toBe(FileOperationFormat.MarkdownZip);
|
||||
expect(fileOperation.options).toBeNull();
|
||||
});
|
||||
|
||||
it("should return published document", async () => {
|
||||
const user = await buildUser();
|
||||
const document = await buildDocument({
|
||||
|
||||
@@ -15,8 +15,6 @@ import { type NavigationNode } from "@shared/types";
|
||||
import {
|
||||
ExportContentType,
|
||||
FileOperationFormat,
|
||||
FileOperationState,
|
||||
FileOperationType,
|
||||
StatusFilter,
|
||||
UserRole,
|
||||
} from "@shared/types";
|
||||
@@ -983,25 +981,11 @@ router.post(
|
||||
);
|
||||
}
|
||||
|
||||
const fileOperation = await FileOperation.createWithCtx(ctx, {
|
||||
type: FileOperationType.Export,
|
||||
state: FileOperationState.Creating,
|
||||
const fileOperation = await FileOperation.createExport(ctx, {
|
||||
document,
|
||||
format,
|
||||
key: FileOperation.getExportKey({
|
||||
name: document.titleWithDefault,
|
||||
teamId: document.teamId,
|
||||
format,
|
||||
}),
|
||||
url: null,
|
||||
size: 0,
|
||||
documentId: document.id,
|
||||
userId: user.id,
|
||||
teamId: document.teamId,
|
||||
});
|
||||
|
||||
fileOperation.user = user;
|
||||
fileOperation.document = document;
|
||||
|
||||
ctx.body = {
|
||||
success: true,
|
||||
data: {
|
||||
|
||||
Reference in new issue
Block a user