mirror of
https://github.com/outline/outline.git
synced 2026-12-05 18:55:10 +03:00
* 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
139 lines
4.5 KiB
TypeScript
139 lines
4.5 KiB
TypeScript
import { chunk } from "es-toolkit/compat";
|
||
import { Op, QueryTypes } from "sequelize";
|
||
import { sleep } from "@shared/utils/timers";
|
||
import Logger from "@server/logging/Logger";
|
||
import { Document, Attachment } from "@server/models";
|
||
import { DocumentHelper } from "@server/models/helpers/DocumentHelper";
|
||
import { ProsemirrorHelper } from "@server/models/helpers/ProsemirrorHelper";
|
||
import DeleteAttachmentTask from "@server/queues/tasks/DeleteAttachmentTask";
|
||
import { sequelizeReadOnly } from "@server/storage/database";
|
||
|
||
export default async function documentPermanentDeleter(documents: Document[]) {
|
||
const activeDocument = documents.find((doc) => !doc.deletedAt);
|
||
|
||
if (activeDocument) {
|
||
throw new Error(
|
||
`Cannot permanently delete ${activeDocument.id} document. Please delete it and try again.`
|
||
);
|
||
}
|
||
|
||
const query = `
|
||
SELECT COUNT(id)
|
||
FROM documents
|
||
WHERE "searchVector" @@ to_tsquery('english', :query) AND
|
||
"teamId" = :teamId AND
|
||
"id" NOT IN (:documentIds)
|
||
`;
|
||
const documentIds = documents.map((document) => document.id);
|
||
const checkedAttachmentIds = new Set<string>();
|
||
|
||
for (const document of documents) {
|
||
// Find any attachments that are referenced in the text content
|
||
const attachmentIdsInText = ProsemirrorHelper.parseAttachmentIds(
|
||
DocumentHelper.toProsemirror(document)
|
||
);
|
||
|
||
// Find attachments owned by this team that were uploaded to this document
|
||
// or are referenced in its text, skipping those already handled in this batch.
|
||
const attachmentIds = (
|
||
await Attachment.findAll({
|
||
attributes: ["id"],
|
||
where: {
|
||
teamId: document.teamId,
|
||
[Op.or]: [{ documentId: document.id }, { id: attachmentIdsInText }],
|
||
},
|
||
})
|
||
)
|
||
.map((attachment) => attachment.id)
|
||
.filter((attachmentId) => !checkedAttachmentIds.has(attachmentId));
|
||
attachmentIds.forEach((attachmentId) =>
|
||
checkedAttachmentIds.add(attachmentId)
|
||
);
|
||
|
||
await Promise.all(
|
||
attachmentIds.map(async (attachmentId) => {
|
||
// Check if the attachment is referenced in any documents outside this
|
||
// batch – this is needed as it's easy to copy and paste content between
|
||
// documents. An uploaded attachment may end up referenced in multiple
|
||
// documents.
|
||
const [{ count }] = await sequelizeReadOnly.query<{ count: string }>(
|
||
query,
|
||
{
|
||
type: QueryTypes.SELECT,
|
||
replacements: {
|
||
documentIds,
|
||
teamId: document.teamId,
|
||
query: attachmentId,
|
||
},
|
||
}
|
||
);
|
||
|
||
// If the attachment is not referenced in any other documents then
|
||
// delete it from the database and the storage provider.
|
||
if (parseInt(count) === 0) {
|
||
Logger.info(
|
||
"commands",
|
||
`Attachment ${attachmentId} scheduled for deletion`
|
||
);
|
||
await new DeleteAttachmentTask().schedule({
|
||
attachmentId,
|
||
teamId: document.teamId,
|
||
});
|
||
}
|
||
})
|
||
);
|
||
}
|
||
|
||
// Re-check deletedAt in the database to exclude documents that were restored
|
||
// between the caller's query and now. Otherwise the parentDocumentId clear
|
||
// below would detach children of a restored parent, breaking the hierarchy.
|
||
const stillDeleted = await Document.unsafeFindAll({
|
||
scope: null,
|
||
attributes: ["id"],
|
||
where: {
|
||
id: documentIds,
|
||
deletedAt: { [Op.ne]: null },
|
||
},
|
||
paranoid: false,
|
||
});
|
||
const deletedIds = stillDeleted.map((document) => document.id);
|
||
|
||
for (const batch of chunk(deletedIds, 100)) {
|
||
// Unscoped so that drafts and templates are detached too, otherwise the
|
||
// destroy below cascades and removes them.
|
||
await Document.unscoped().update(
|
||
{
|
||
parentDocumentId: null,
|
||
},
|
||
{
|
||
where: {
|
||
parentDocumentId: {
|
||
[Op.in]: batch,
|
||
},
|
||
},
|
||
paranoid: false,
|
||
}
|
||
);
|
||
}
|
||
|
||
// Small batch size and inter-batch sleep keep the exclusive lock window short
|
||
// enough to avoid blocking concurrent web requests, since each delete
|
||
// cascades into vectors, attachments, revisions, comments, and notifications.
|
||
const destroyBatches = chunk(deletedIds, 10);
|
||
|
||
let totalDeleted = 0;
|
||
for (let i = 0; i < destroyBatches.length; i++) {
|
||
totalDeleted += await Document.scope("withDrafts").destroy({
|
||
where: {
|
||
id: destroyBatches[i],
|
||
deletedAt: { [Op.ne]: null },
|
||
},
|
||
force: true,
|
||
});
|
||
if (i < destroyBatches.length - 1) {
|
||
await sleep(100);
|
||
}
|
||
}
|
||
return totalDeleted;
|
||
}
|