Files
outline/server/queues/processors/FileOperationDeletedProcessor.ts
T
Tom Moor 754672e9dc Centralize document access rules for list queries (#14783)
* wip

* Route document list queries through findAllWithCtx

Add findAllWithCtx and countWithCtx on Document and Template, unsafe* methods
for system reads, and a lint rule that bans direct list queries. Remove the
cached membership list, accessibleSql and findByIds in favour of one gate.

* Remove test

* Include drafts when loading starred documents

* Scope the access gate to the user's team, move whereToSql to QueryHelper

* Move collection access to Collection.findAllWithCtx

Collection.accessibleIds keeps the cached id list inside the collection gate, and user.collectionIds() is a shortcut to it. List queries use findAllWithCtx and countWithCtx, system reads use the shared unsafe methods, and the admin listing in collections.list is explicit.

* Check permanentDelete for each document in empty_trash
2026-11-27 15:30:43 -05:00

71 lines
2.0 KiB
TypeScript

import { FileOperationState, FileOperationType } from "@shared/types";
import { createContext } from "@server/context";
import Logger from "@server/logging/Logger";
import { Collection, FileOperation, User } from "@server/models";
import { sequelize } from "@server/storage/database";
import type { Event as TEvent, FileOperationEvent } from "@server/types";
import BaseProcessor from "./BaseProcessor";
export default class FileOperationDeletedProcessor extends BaseProcessor {
static applicableEvents: TEvent["name"][] = [
"fileOperations.delete",
"fileOperations.update",
];
async perform(event: FileOperationEvent) {
await sequelize.transaction(async (transaction) => {
const fileOperation = await FileOperation.unscoped().findByPk(
event.modelId,
{
rejectOnEmpty: true,
paranoid: false,
transaction,
}
);
if (fileOperation.type === FileOperationType.Export) {
return;
}
if (
event.name === "fileOperations.update" &&
fileOperation.state !== FileOperationState.Error
) {
return;
}
if (
event.name === "fileOperations.delete" &&
![FileOperationState.Complete, FileOperationState.Error].includes(
fileOperation.state
)
) {
return;
}
const user = await User.findByPk(event.actorId, {
rejectOnEmpty: true,
paranoid: false,
transaction,
});
const collections = await Collection.unsafeFindAll({
transaction,
lock: transaction.LOCK.NO_KEY_UPDATE,
where: {
teamId: fileOperation.teamId,
importId: fileOperation.id,
},
});
for (const collection of collections) {
Logger.debug("processor", "Destroying collection created from import", {
collectionId: collection.id,
});
await collection.destroyWithCtx(
createContext({ user, ip: event.ip, transaction })
);
}
});
}
}