Files
outline/server/queues/tasks/CleanupDeletedDocumentsTask.ts
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

56 lines
1.7 KiB
TypeScript

import { subDays } from "date-fns";
import { Op } from "sequelize";
import documentPermanentDeleter from "@server/commands/documentPermanentDeleter";
import Logger from "@server/logging/Logger";
import { Document } from "@server/models";
import { sequelizeReadOnly } from "@server/storage/database";
import { TaskPriority } from "./base/BaseTask";
import { Minute } from "@shared/utils/time";
import type { Props } from "./base/CronTask";
import { CronTask, TaskInterval } from "./base/CronTask";
export default class CleanupDeletedDocumentsTask extends CronTask {
public async perform({ limit, partition }: Props) {
Logger.info(
"task",
`Permanently destroying upto ${limit} documents older than 30 days…`
);
const documents = await sequelizeReadOnly.transaction((transaction) =>
Document.unsafeFindAll({
scope: null,
attributes: ["id", "teamId", "deletedAt", "content", "state", "text"],
where: {
deletedAt: {
[Op.lt]: subDays(new Date(), 30),
},
...this.getPartitionWhereClause("id", partition),
},
paranoid: false,
limit,
transaction,
})
);
const countDeletedDocument = await documentPermanentDeleter(documents);
Logger.info("task", `Destroyed ${countDeletedDocument} documents`);
}
public get options() {
return {
attempts: 3,
backoff: {
type: "exponential",
// Wait until the active partition window has ended before retrying.
delay: 15 * Minute.ms,
},
priority: TaskPriority.Background,
};
}
public get cron() {
return {
interval: TaskInterval.Hour,
partitionWindow: 15 * Minute.ms,
};
}
}