mirror of
https://github.com/outline/outline.git
synced 2026-11-30 09:00:18 +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
79 lines
2.3 KiB
TypeScript
79 lines
2.3 KiB
TypeScript
import { Op } from "sequelize";
|
|
import { documentsDuplicator } from "@server/commands/documentDuplicator";
|
|
import { createContext } from "@server/context";
|
|
import { Collection, Document, User } from "@server/models";
|
|
import { DocumentHelper } from "@server/models/helpers/DocumentHelper";
|
|
import { sequelize } from "@server/storage/database";
|
|
import { BaseTask } from "./base/BaseTask";
|
|
|
|
type Props = {
|
|
/** The collection to duplicate documents into */
|
|
collectionId: string;
|
|
/** The collection to duplicate documents from */
|
|
originalCollectionId: string;
|
|
/** The user that initiated the duplication */
|
|
actorId: string;
|
|
/** The IP address of the user that initiated the duplication */
|
|
ip: string | null;
|
|
};
|
|
|
|
export default class DuplicateCollectionDocumentsTask extends BaseTask<Props> {
|
|
protected jobId({ collectionId }: Props) {
|
|
return `duplicate-collection:${collectionId}`;
|
|
}
|
|
|
|
async perform(props: Props) {
|
|
const [collection, original, actor] = await Promise.all([
|
|
Collection.findByPk(props.collectionId),
|
|
Collection.findByPk(props.originalCollectionId),
|
|
User.findByPk(props.actorId),
|
|
]);
|
|
|
|
if (!actor || !original || !collection?.isActive) {
|
|
return;
|
|
}
|
|
|
|
const rootDocuments = await Document.findAllWithCtx(
|
|
createContext({ user: actor }),
|
|
{
|
|
includeViews: false,
|
|
where: {
|
|
collectionId: original.id,
|
|
parentDocumentId: {
|
|
[Op.is]: null,
|
|
},
|
|
archivedAt: {
|
|
[Op.is]: null,
|
|
},
|
|
},
|
|
}
|
|
);
|
|
|
|
if (!rootDocuments.length) {
|
|
return;
|
|
}
|
|
|
|
const structure = await original.structureFor(actor);
|
|
const sorted = DocumentHelper.sortDocumentsByStructure(
|
|
rootDocuments,
|
|
structure
|
|
).reverse(); // we have to reverse since the root documents will be added in reverse order
|
|
|
|
return sequelize.transaction(async (transaction) => {
|
|
const ctx = createContext({
|
|
user: actor,
|
|
ip: props.ip,
|
|
transaction,
|
|
});
|
|
|
|
// The whole collection is duplicated as one unit so that links between
|
|
// documents in different trees are remapped to the copies.
|
|
await documentsDuplicator(ctx, {
|
|
documents: sorted,
|
|
collection,
|
|
recursive: true,
|
|
});
|
|
});
|
|
}
|
|
}
|