FileOperations

This commit is contained in:
Tom Moor committed 2023-10-26 08:59:03 -04:00
1 parent b1be4a9f15
commit 0d30474af1
10 files changed
+47 -101

No files matched your search

+5 -27
View File
@@ -1,4 +1,3 @@
import { Transaction } from "sequelize";
import { v4 as uuidv4 } from "uuid";
import {
FileOperationFormat,
@@ -6,8 +5,9 @@ import {
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;
@@ -15,8 +15,7 @@ type Props = {
user: User;
format?: FileOperationFormat;
includeAttachments?: boolean;
ip: string;
transaction: Transaction;
context: APIContext["context"];
};
function getKeyForFileOp(teamId: string, name: string) {
@@ -29,8 +28,7 @@ async function collectionExporter({
user,
format = FileOperationFormat.MarkdownZip,
includeAttachments = true,
ip,
transaction,
context,
}: Props) {
const collectionId = collection?.id;
const key = getKeyForFileOp(user.teamId, collection?.name || team.name);
@@ -47,27 +45,7 @@ async function collectionExporter({
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,
}
context
);
fileOperation.user = user;
-30
View File
@@ -1,30 +0,0 @@
import { Transaction } from "sequelize";
import { FileOperation, Event, User } from "@server/models";
type Props = {
fileOperation: FileOperation;
user: User;
ip: string;
transaction: Transaction;
};
export default async function fileOperationDeleter({
fileOperation,
user,
ip,
transaction,
}: Props) {
await fileOperation.destroy({ transaction });
await Event.create(
{
name: "fileOperations.delete",
teamId: user.teamId,
actorId: user.id,
modelId: fileOperation.id,
ip,
},
{
transaction,
}
);
}
+4
View File
@@ -37,6 +37,10 @@ import Fix from "./decorators/Fix";
@Table({ tableName: "file_operations", modelName: "file_operation" })
@Fix
class FileOperation extends ParanoidModel {
static eventNamespace = "fileOperations";
static eventData = ["type", "format"];
@Column(DataType.ENUM(...Object.values(FileOperationType)))
type: FileOperationType;
+26 -2
View File
@@ -73,13 +73,37 @@ class Model extends SequelizeModel {
});
}
const models = this.sequelize!.models;
const namespace = this.eventNamespace || this.tableName;
return this.sequelize?.models.event.create(
return models.event.create(
{
name: `${namespace}.${name}`,
modelId: model.id,
teamId: context.auth?.user.teamId,
collectionId:
"collectionId" in model
? model.collectionId
: model instanceof models.collection
? model.id
: undefined,
documentId:
"documentId" in model
? model.documentId
: model instanceof models.document
? model.id
: undefined,
userId:
"userId" in model
? model.userId
: model instanceof models.user
? model.id
: undefined,
teamId:
"teamId" in model
? model.teamId
: model instanceof models.team
? model.id
: context.auth?.user.teamId,
actorId: context.auth?.user.id,
ip: context.ip,
data: pick(model, this.eventData),
@@ -34,7 +34,7 @@ export default class ErrorTimedOutFileOperationsTask extends BaseTask<Props> {
fileOperations.map(async (fileOperation) => {
fileOperation.state = FileOperationState.Error;
fileOperation.error = "Timed out";
await fileOperation.save();
await fileOperation.save({ hooks: false });
})
);
Logger.info("task", `Updated ${fileOperations.length} file operations`);
-6
View File
@@ -168,12 +168,6 @@ export default abstract class ImportTask extends BaseTask<Props> {
state,
error: error ? truncate(error.message, { length: 255 }) : undefined,
});
await Event.schedule({
name: "fileOperations.update",
modelId: fileOperation.id,
teamId: fileOperation.teamId,
actorId: fileOperation.userId,
});
}
/**
+1 -1
View File
@@ -29,7 +29,7 @@ router.post(
transaction(),
async (ctx: APIContext<T.AttachmentCreateReq>) => {
const { name, documentId, contentType, size, preset } = ctx.input.body;
const { auth } = ctx.state;
const { auth, transaction } = ctx.state;
const { user } = auth;
// All user types can upload an avatar so no additional authorization is needed.
+7 -27
View File
@@ -159,10 +159,9 @@ router.post(
validate(T.CollectionsImportSchema),
transaction(),
async (ctx: APIContext<T.CollectionsImportReq>) => {
const { transaction } = ctx.state;
const { attachmentId, format } = ctx.input.body;
const { user } = ctx.state.auth;
const { transaction } = ctx.state;
authorize(user, "importCollection", user.team);
const attachment = await Attachment.findByPk(attachmentId, {
@@ -170,7 +169,7 @@ router.post(
});
authorize(user, "read", attachment);
const fileOperation = await FileOperation.create(
await FileOperation.create(
{
type: FileOperationType.Import,
state: FileOperationState.Creating,
@@ -180,24 +179,7 @@ router.post(
userId: user.id,
teamId: user.teamId,
},
{
transaction,
}
);
await Event.create(
{
name: "fileOperations.create",
teamId: user.teamId,
actorId: user.id,
modelId: fileOperation.id,
data: {
type: FileOperationType.Import,
},
},
{
transaction,
}
ctx.context
);
ctx.body = {
@@ -543,8 +525,8 @@ router.post(
validate(T.CollectionsExportSchema),
transaction(),
async (ctx: APIContext<T.CollectionsExportReq>) => {
const { transaction } = ctx.state;
const { id, format, includeAttachments } = ctx.input.body;
const { transaction } = ctx.state;
const { user } = ctx.state.auth;
const team = await Team.findByPk(user.teamId, { transaction });
@@ -561,8 +543,7 @@ router.post(
team,
format,
includeAttachments,
ip: ctx.request.ip,
transaction,
context: ctx.context,
});
ctx.body = {
@@ -581,9 +562,9 @@ router.post(
validate(T.CollectionsExportAllSchema),
transaction(),
async (ctx: APIContext<T.CollectionsExportAllReq>) => {
const { transaction } = ctx.state;
const { format, includeAttachments } = ctx.input.body;
const { user } = ctx.state.auth;
const { transaction } = ctx.state;
const team = await Team.findByPk(user.teamId, { transaction });
authorize(user, "createExport", team);
@@ -592,8 +573,7 @@ router.post(
team,
format,
includeAttachments,
ip: ctx.request.ip,
transaction,
context: ctx.context,
});
ctx.body = {
@@ -282,6 +282,7 @@ describe("#fileOperations.delete", () => {
expect(
await Event.count({
where: {
name: "fileOperations.delete",
teamId: team.id,
},
})
@@ -1,6 +1,5 @@
import Router from "koa-router";
import { WhereOptions } from "sequelize";
import fileOperationDeleter from "@server/commands/fileOperationDeleter";
import { ValidationError } from "@server/errors";
import auth from "@server/middlewares/authentication";
import { transaction } from "@server/middlewares/transaction";
@@ -115,15 +114,11 @@ router.post(
const fileOperation = await FileOperation.unscoped().findByPk(id, {
rejectOnEmpty: true,
transaction,
lock: transaction.LOCK.UPDATE,
});
authorize(user, "delete", fileOperation);
await fileOperationDeleter({
fileOperation,
user,
ip: ctx.request.ip,
transaction,
});
await fileOperation.destroy(ctx.context);
ctx.body = {
success: true,