diff --git a/server/commands/attachmentCreator.ts b/server/commands/attachmentCreator.ts index cb0e748c24c3..4a9d86df17d7 100644 --- a/server/commands/attachmentCreator.ts +++ b/server/commands/attachmentCreator.ts @@ -1,9 +1,9 @@ -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; @@ -11,8 +11,7 @@ type BaseProps = { user: User; source?: "import"; preset: AttachmentPreset; - ip?: string; - transaction?: Transaction; + ctx: APIContext; }; type UrlProps = BaseProps & { @@ -32,8 +31,7 @@ export default async function attachmentCreator({ user, source, preset, - ip, - transaction, + ctx, ...rest }: Props): Promise { const acl = AttachmentHelper.presetToAcl(preset); @@ -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({ @@ -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; } diff --git a/server/models/helpers/DocumentHelper.tsx b/server/models/helpers/DocumentHelper.tsx index 3aca06544174..19870dc424d1 100644 --- a/server/models/helpers/DocumentHelper.tsx +++ b/server/models/helpers/DocumentHelper.tsx @@ -21,6 +21,7 @@ import { parser, schema } from "@server/editor"; import { trace } from "@server/logging/tracing"; import { Document, Revision, User } from "@server/models"; import FileStorage from "@server/storage/files"; +import { APIContext } from "@server/types"; import diff from "@server/utils/diff"; import parseAttachmentIds from "@server/utils/parseAttachmentIds"; import parseImages from "@server/utils/parseImages"; @@ -418,8 +419,13 @@ export default class DocumentHelper { url: image.src, preset: AttachmentPreset.DocumentAttachment, user, - ip, - transaction, + ctx: { + context: { + ip, + transaction, + auth: { user }, + }, + } as APIContext, }); if (attachment) { diff --git a/server/queues/tasks/ImportTask.ts b/server/queues/tasks/ImportTask.ts index b5b142c183a5..457964fd50e3 100644 --- a/server/queues/tasks/ImportTask.ts +++ b/server/queues/tasks/ImportTask.ts @@ -20,6 +20,7 @@ import { Attachment, } from "@server/models"; import { sequelize } from "@server/storage/database"; +import { APIContext } from "@server/types"; import BaseTask, { TaskPriority } from "./BaseTask"; type Props = { @@ -256,8 +257,13 @@ export default abstract class ImportTask extends BaseTask { type: item.mimeType, buffer: await item.buffer(), user, - ip, - transaction, + ctx: { + context: { + ip, + transaction, + auth: { user }, + }, + } as APIContext, }); if (attachment) { attachments.set(item.id, attachment); diff --git a/server/types.ts b/server/types.ts index 168e653610e4..c5a3aed0545c 100644 --- a/server/types.ts +++ b/server/types.ts @@ -23,8 +23,8 @@ export type AuthenticationResult = AccountProvisionerResult & { export type Authentication = { user: User; - token: string; - type: AuthenticationType; + token?: string; + type?: AuthenticationType; }; export type Pagination = { @@ -58,7 +58,7 @@ export interface APIContext context: { transaction?: Transaction; auth: Authentication; - ip: string; + ip?: string; }; }