diff --git a/app/menus/EmptyTrashMenu.tsx b/app/menus/EmptyTrashMenu.tsx index 891be5c809..11ef4ae0c1 100644 --- a/app/menus/EmptyTrashMenu.tsx +++ b/app/menus/EmptyTrashMenu.tsx @@ -35,12 +35,11 @@ function EmptyTrashMenu() { try { setIsDeleting(true); - trashed.forEach( - async (doc) => - await documents.delete(doc, { - permanent: true, - }) - ); + await documents.emptyTrash(); + // If all trashed documents were not removed. + // E.g. More trashed docs than `emptyTrash` limit. + const trashedDocs = await documents.fetchDeleted(); + setTrashed(trashedDocs); showToast(t("Trash permanently deleted"), { type: "success", }); @@ -55,7 +54,7 @@ function EmptyTrashMenu() { setTrashed([]); } }, - [documents, history, showToast, t, trashed] + [documents, history, showToast, t] ); if (!trashed.length) { diff --git a/app/stores/DocumentsStore.ts b/app/stores/DocumentsStore.ts index efd4c74b13..845dd4684b 100644 --- a/app/stores/DocumentsStore.ts +++ b/app/stores/DocumentsStore.ts @@ -684,6 +684,33 @@ export default class DocumentsStore extends BaseStore { } } + @action + async emptyTrash() { + const res = await client.post("/documents.empty_trash"); + invariant(res?.data, "Data should be available"); + res.data.forEach( + ({ + documentId, + collectionId, + }: { + documentId: string; + collectionId: string; + }) => { + this.remove(documentId); + + const share = this.rootStore.shares.getByDocumentId(documentId); + if (share) { + this.rootStore.shares.remove(share.id); + } + + const collection = this.rootStore.collections.data.get(collectionId); + if (collection) { + collection.refresh(); + } + } + ); + } + @action archive = async (document: Document) => { const res = await client.post("/documents.archive", { @@ -718,7 +745,10 @@ export default class DocumentsStore extends BaseStore { document.updateFromJson(res.data); this.addPolicies(res.policies); }); - const collection = this.getCollectionForDocument(document); + + const collection = this.rootStore.collections.data.get( + document.collectionId + ); if (collection) { collection.refresh(); } diff --git a/server/routes/api/documents.test.ts b/server/routes/api/documents.test.ts index 54c0fe45c7..54905658e7 100644 --- a/server/routes/api/documents.test.ts +++ b/server/routes/api/documents.test.ts @@ -2492,3 +2492,66 @@ describe("#documents.unpublish", () => { expect(res.status).toEqual(401); }); }); + +describe("#documents.empty_trash", () => { + it("should require admin", async () => { + const viewer = await buildViewer(); + const res = await server.post("/api/documents.empty_trash", { + body: { + token: viewer.getJwtToken(), + }, + }); + expect(res.status).toEqual(403); + }); + + it("should require authentication", async () => { + const res = await server.post("/api/documents.empty_trash"); + expect(res.status).toEqual(401); + }); + + it("should permenantly delete trashed documents", async () => { + const { admin } = await seed(); + const document = await buildDocument({ + userId: admin.id, + teamId: admin.teamId, + }); + await document.delete(admin.id); + + const res0 = await server.post("/api/documents.deleted", { + body: { + token: admin.getJwtToken(), + }, + }); + const body0 = await res0.json(); + expect(res0.status).toEqual(200); + expect(body0.data.length).toEqual(1); + + const res1 = await server.post("/api/documents.empty_trash", { + body: { + token: admin.getJwtToken(), + }, + }); + const body1 = await res1.json(); + expect(res1.status).toEqual(200); + expect(body1.data.length).toEqual(1); + + const res2 = await server.post("/api/documents.deleted", { + body: { + token: admin.getJwtToken(), + }, + }); + const body2 = await res2.json(); + expect(res2.status).toEqual(200); + expect(body2.data.length).toEqual(0); + + const events = await Event.findAll(); + + expect(events.length).toEqual(1); + expect(events[0].name).toEqual("documents.permanent_delete"); + expect(events[0].documentId).toEqual(document.id); + expect(events[0].collectionId).toEqual(document.collectionId); + expect(events[0].teamId).toEqual(document.teamId); + expect(events[0].actorId).toEqual(admin.id); + expect(events[0].data.title).toEqual(document.title); + }); +}); diff --git a/server/routes/api/documents.ts b/server/routes/api/documents.ts index cb7abd6606..35826839b8 100644 --- a/server/routes/api/documents.ts +++ b/server/routes/api/documents.ts @@ -3,6 +3,7 @@ import invariant from "invariant"; import Router from "koa-router"; import { Op, ScopeOptions, WhereOptions } from "sequelize"; import { subtractDate } from "@shared/utils/date"; +import { DocumentValidation } from "@shared/validations"; import documentCreator from "@server/commands/documentCreator"; import documentImporter from "@server/commands/documentImporter"; import documentLoader from "@server/commands/documentLoader"; @@ -1063,6 +1064,64 @@ router.post("documents.delete", auth({ member: true }), async (ctx) => { }; }); +router.post("documents.empty_trash", auth({ admin: true }), async (ctx) => { + const { user } = ctx.state; + + const deleted: { documentId: string; collectionId: string }[] = []; + + await Document.findAllInBatches( + { + where: { + teamId: user.teamId, + deletedAt: { + [Op.ne]: null, + }, + }, + paranoid: false, + limit: DocumentValidation.emptyTrash, + }, + async (documents) => { + for (const document of documents) { + authorize(user, "permanentDelete", document); + + deleted.push({ + documentId: document.id, + collectionId: document.collectionId, + }); + + await Document.update( + { + parentDocumentId: null, + }, + { + where: { + parentDocumentId: document.id, + }, + paranoid: false, + } + ); + await documentPermanentDeleter([document]); + await Event.create({ + name: "documents.permanent_delete", + documentId: document.id, + collectionId: document.collectionId, + teamId: document.teamId, + actorId: user.id, + data: { + title: document.title, + }, + ip: ctx.request.ip, + }); + } + } + ); + + ctx.body = { + success: true, + data: deleted, + }; +}); + router.post("documents.unpublish", auth({ member: true }), async (ctx) => { const { id } = ctx.body; assertPresent(id, "id is required"); diff --git a/shared/validations.ts b/shared/validations.ts index e525d524d2..15fd28827a 100644 --- a/shared/validations.ts +++ b/shared/validations.ts @@ -30,6 +30,9 @@ export const CollectionValidation = { export const DocumentValidation = { /** The maximum length of the document title */ maxTitleLength: 100, + + /** The maximum number of documents allowed to be permanently deleted in one batch */ + emptyTrash: 100, }; export const PinValidation = {