Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor of event insertion #5909

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 22 additions & 51 deletions server/commands/attachmentCreator.ts
@@ -1,18 +1,17 @@
import { Transaction } from "sequelize";
import { v4 as uuidv4 } from "uuid";
import { AttachmentPreset } from "@shared/types";
import { Attachment, Event, User } from "@server/models";
import { Attachment, User } from "@server/models";
import AttachmentHelper from "@server/models/helpers/AttachmentHelper";
import FileStorage from "@server/storage/files";
import { APIContext } from "@server/types";

type BaseProps = {
id?: string;
name: string;
user: User;
source?: "import";
preset: AttachmentPreset;
ip?: string;
transaction?: Transaction;
ctx: APIContext;
};

type UrlProps = BaseProps & {
Expand All @@ -32,8 +31,7 @@ export default async function attachmentCreator({
user,
source,
preset,
ip,
transaction,
ctx,
...rest
}: Props): Promise<Attachment | undefined> {
const acl = AttachmentHelper.presetToAcl(preset);
Expand All @@ -53,20 +51,15 @@ export default async function attachmentCreator({
if (!res) {
return;
}
attachment = await Attachment.create(
{
id,
key,
acl,
size: res.contentLength,
contentType: res.contentType,
teamId: user.teamId,
userId: user.id,
},
{
transaction,
}
);
attachment = await Attachment.createWithCtx(ctx, {
id,
key,
acl,
size: res.contentLength,
contentType: res.contentType,
teamId: user.teamId,
userId: user.id,
});
} else {
const { buffer, type } = rest;
await FileStorage.store({
Expand All @@ -77,38 +70,16 @@ export default async function attachmentCreator({
acl,
});

attachment = await Attachment.create(
{
id,
key,
acl,
size: buffer.length,
contentType: type,
teamId: user.teamId,
userId: user.id,
},
{
transaction,
}
);
}

await Event.create(
{
name: "attachments.create",
data: {
name,
source,
},
modelId: attachment.id,
attachment = await Attachment.createWithCtx(ctx, {
id,
key,
acl,
size: buffer.length,
contentType: type,
teamId: user.teamId,
actorId: user.id,
ip,
},
{
transaction,
}
);
userId: user.id,
});
}

return attachment;
}
57 changes: 16 additions & 41 deletions server/commands/collectionExporter.ts
@@ -1,22 +1,21 @@
import { Transaction } from "sequelize";
import { v4 as uuidv4 } from "uuid";
import {
FileOperationFormat,
FileOperationType,
FileOperationState,
} from "@shared/types";
import { traceFunction } from "@server/logging/tracing";
import { Collection, Event, Team, User, FileOperation } from "@server/models";
import { Collection, Team, User, FileOperation } from "@server/models";
import { Buckets } from "@server/models/helpers/AttachmentHelper";
import { type APIContext } from "@server/types";

type Props = {
collection?: Collection;
team: Team;
user: User;
format?: FileOperationFormat;
includeAttachments?: boolean;
ip: string;
transaction: Transaction;
ctx: APIContext;
};

function getKeyForFileOp(teamId: string, name: string) {
Expand All @@ -29,46 +28,22 @@ async function collectionExporter({
user,
format = FileOperationFormat.MarkdownZip,
includeAttachments = true,
ip,
transaction,
ctx,
}: Props) {
const collectionId = collection?.id;
const key = getKeyForFileOp(user.teamId, collection?.name || team.name);
const fileOperation = await FileOperation.create(
{
type: FileOperationType.Export,
state: FileOperationState.Creating,
format,
key,
url: null,
size: 0,
collectionId,
includeAttachments,
userId: user.id,
teamId: user.teamId,
},
{
transaction,
}
);

await Event.create(
{
name: "fileOperations.create",
teamId: user.teamId,
actorId: user.id,
modelId: fileOperation.id,
collectionId,
ip,
data: {
type: FileOperationType.Export,
format,
},
},
{
transaction,
}
);
const fileOperation = await FileOperation.createWithCtx(ctx, {
type: FileOperationType.Export,
state: FileOperationState.Creating,
format,
key,
url: null,
size: 0,
collectionId,
includeAttachments,
userId: user.id,
teamId: user.teamId,
});

fileOperation.user = user;

Expand Down
12 changes: 5 additions & 7 deletions server/commands/documentCreator.ts
@@ -1,7 +1,7 @@
import { Transaction } from "sequelize";
import { Optional } from "utility-types";
import { Document, Event, User } from "@server/models";
import TextHelper from "@server/models/helpers/TextHelper";
import { APIContext } from "@server/types";

type Props = Optional<
Pick<
Expand All @@ -27,8 +27,7 @@ type Props = Optional<
publish?: boolean;
templateDocument?: Document | null;
user: User;
ip?: string;
transaction?: Transaction;
ctx: APIContext;
};

export default async function documentCreator({
Expand All @@ -52,9 +51,9 @@ export default async function documentCreator({
editorVersion,
publishedAt,
sourceMetadata,
ip,
transaction,
ctx,
}: Props): Promise<Document> {
const { transaction, ip } = ctx;
const templateId = templateDocument ? templateDocument.id : undefined;

if (urlId) {
Expand Down Expand Up @@ -100,8 +99,7 @@ export default async function documentCreator({
user
),
user,
ip,
transaction
ctx
),
state,
},
Expand Down
12 changes: 4 additions & 8 deletions server/commands/documentDuplicator.test.ts
@@ -1,10 +1,9 @@
import { createContext } from "@server/context";
import { sequelize } from "@server/storage/database";
import { buildDocument, buildUser } from "@server/test/factories";
import documentDuplicator from "./documentDuplicator";

describe("documentDuplicator", () => {
const ip = "127.0.0.1";

it("should duplicate existing document", async () => {
const user = await buildUser();
const original = await buildDocument({
Expand All @@ -16,9 +15,8 @@ describe("documentDuplicator", () => {
documentDuplicator({
document: original,
collection: original.collection,
transaction,
user,
ip,
ctx: createContext(user, transaction),
})
);

Expand All @@ -41,9 +39,8 @@ describe("documentDuplicator", () => {
document: original,
collection: original.collection,
title: "New title",
transaction,
user,
ip,
ctx: createContext(user, transaction),
})
);

Expand Down Expand Up @@ -73,9 +70,8 @@ describe("documentDuplicator", () => {
document: original,
collection: original.collection,
user,
transaction,
recursive: true,
ip,
ctx: createContext(user, transaction),
})
);

Expand Down
18 changes: 6 additions & 12 deletions server/commands/documentDuplicator.ts
@@ -1,5 +1,6 @@
import { Transaction, Op } from "sequelize";
import { Op } from "sequelize";
import { User, Collection, Document } from "@server/models";
import { APIContext } from "@server/types";
import documentCreator from "./documentCreator";

type Props = {
Expand All @@ -17,10 +18,7 @@ type Props = {
publish?: boolean;
/** Whether to duplicate child documents */
recursive?: boolean;
/** The database transaction to use for the creation */
transaction?: Transaction;
/** The IP address of the request */
ip: string;
ctx: APIContext;
};

export default async function documentDuplicator({
Expand All @@ -31,16 +29,14 @@ export default async function documentDuplicator({
title,
publish,
recursive,
transaction,
ip,
ctx,
}: Props): Promise<Document[]> {
const newDocuments: Document[] = [];
const sharedProperties = {
user,
collectionId: collection?.id,
publish: publish ?? !!document.publishedAt,
ip,
transaction,
ctx,
};

const duplicated = await documentCreator({
Expand Down Expand Up @@ -69,9 +65,7 @@ export default async function documentDuplicator({
[Op.eq]: null,
},
},
{
transaction,
}
ctx
);

for (const childDocument of childDocuments) {
Expand Down