mirror of
https://github.com/outline/outline.git
synced 2026-06-13 11:25:03 +03:00
feat: Add background task for empty_trash action (#7531)
* feat: Add background task for empty_trash action * review * enqueue task only if docs are available
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
import { Op } from "sequelize";
|
||||
import documentPermanentDeleter from "@server/commands/documentPermanentDeleter";
|
||||
import { Document } from "@server/models";
|
||||
import BaseTask from "./BaseTask";
|
||||
|
||||
type Props = {
|
||||
documentIds: string[];
|
||||
};
|
||||
|
||||
export default class EmptyTrashTask extends BaseTask<Props> {
|
||||
public async perform({ documentIds }: Props) {
|
||||
if (!documentIds.length) {
|
||||
return;
|
||||
}
|
||||
const documents = await Document.unscoped().findAll({
|
||||
where: {
|
||||
id: {
|
||||
[Op.in]: documentIds,
|
||||
},
|
||||
// for safety, ensure the documents are in soft-delete state.
|
||||
deletedAt: {
|
||||
[Op.ne]: null,
|
||||
},
|
||||
},
|
||||
paranoid: false,
|
||||
});
|
||||
await documentPermanentDeleter(documents);
|
||||
}
|
||||
}
|
||||
@@ -4650,28 +4650,4 @@ describe("#documents.empty_trash", () => {
|
||||
expect(res.status).toEqual(403);
|
||||
expect(body).toMatchSnapshot();
|
||||
});
|
||||
it("should permanently delete documents", async () => {
|
||||
const user = await buildAdmin();
|
||||
const document = await buildDocument({
|
||||
userId: user.id,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
await document.delete(user);
|
||||
|
||||
const res = await server.post("/api/documents.empty_trash", {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
},
|
||||
});
|
||||
|
||||
const body = await res.json();
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.success).toEqual(true);
|
||||
|
||||
const deletedDoc = await Document.findByPk(document.id, {
|
||||
userId: user.id,
|
||||
paranoid: false,
|
||||
});
|
||||
expect(deletedDoc).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -66,6 +66,7 @@ import {
|
||||
import DocumentImportTask, {
|
||||
DocumentImportTaskResponse,
|
||||
} from "@server/queues/tasks/DocumentImportTask";
|
||||
import EmptyTrashTask from "@server/queues/tasks/EmptyTrashTask";
|
||||
import FileStorage from "@server/storage/files";
|
||||
import { APIContext } from "@server/types";
|
||||
import { RateLimiterStrategy } from "@server/utils/RateLimiter";
|
||||
@@ -1954,6 +1955,7 @@ router.post(
|
||||
collectionScope,
|
||||
"withDrafts",
|
||||
]).findAll({
|
||||
attributes: ["id"],
|
||||
where: {
|
||||
deletedAt: {
|
||||
[Op.ne]: null,
|
||||
@@ -1975,7 +1977,12 @@ router.post(
|
||||
paranoid: false,
|
||||
});
|
||||
|
||||
await documentPermanentDeleter(documents);
|
||||
if (documents.length) {
|
||||
await EmptyTrashTask.schedule({
|
||||
documentIds: documents.map((doc) => doc.id),
|
||||
});
|
||||
}
|
||||
|
||||
await Event.createFromContext(ctx, {
|
||||
name: "documents.empty_trash",
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user