mirror of
https://github.com/outline/outline.git
synced 2026-11-25 07:04:37 +03:00
feat: documents.empty_trash endpoint
This commit is contained in:
1 parent
16d576b4db
commit
19df7bee78
5 files changed
+162
-8
No files matched your search
@@ -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) {
|
||||
|
||||
@@ -684,6 +684,33 @@ export default class DocumentsStore extends BaseStore<Document> {
|
||||
}
|
||||
}
|
||||
|
||||
@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> {
|
||||
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();
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
@@ -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<Document>(
|
||||
{
|
||||
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");
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
Reference in new issue
Block a user