|
@@ -72,21 +72,48 @@ class PageService {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async duplicateRecursively(page, newPagePath, user) {
|
|
async duplicateRecursively(page, newPagePath, user) {
|
|
|
|
|
+
|
|
|
const Page = this.crowi.model('Page');
|
|
const Page = this.crowi.model('Page');
|
|
|
|
|
+ const Revision = this.crowi.model('Revision');
|
|
|
const newPagePathPrefix = newPagePath;
|
|
const newPagePathPrefix = newPagePath;
|
|
|
const pathRegExp = new RegExp(`^${escapeStringRegexp(page.path)}`, 'i');
|
|
const pathRegExp = new RegExp(`^${escapeStringRegexp(page.path)}`, 'i');
|
|
|
-
|
|
|
|
|
const pages = await Page.findManageableListWithDescendants(page, user);
|
|
const pages = await Page.findManageableListWithDescendants(page, user);
|
|
|
|
|
+ const revisions = await Revision.find({ path: pathRegExp });
|
|
|
|
|
|
|
|
- const promise = pages.map(async(page) => {
|
|
|
|
|
- const newPagePath = page.path.replace(pathRegExp, newPagePathPrefix);
|
|
|
|
|
- return this.duplicate(page, newPagePath, user);
|
|
|
|
|
|
|
+ // Mapping to set to the body of the new revision
|
|
|
|
|
+ const pathRevisionMapping = {};
|
|
|
|
|
+ revisions.forEach((revision) => {
|
|
|
|
|
+ pathRevisionMapping[revision.path] = revision;
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- const newPath = page.path.replace(pathRegExp, newPagePathPrefix);
|
|
|
|
|
|
|
+ const newPages = [];
|
|
|
|
|
+ const newRevisions = [];
|
|
|
|
|
|
|
|
- await Promise.allSettled(promise);
|
|
|
|
|
|
|
+ pages.forEach((page) => {
|
|
|
|
|
+ const newPagePath = page.path.replace(pathRegExp, newPagePathPrefix);
|
|
|
|
|
+ const revisionId = new mongoose.Types.ObjectId();
|
|
|
|
|
+
|
|
|
|
|
+ newPages.push({
|
|
|
|
|
+ path: newPagePath,
|
|
|
|
|
+ creator: user._id,
|
|
|
|
|
+ grant: page.grant,
|
|
|
|
|
+ grantedGroup: page.grantedGroup,
|
|
|
|
|
+ grantedUsers: page.grantedUsers,
|
|
|
|
|
+ lastUpdateUser: user._id,
|
|
|
|
|
+ redirectTo: null,
|
|
|
|
|
+ revision: revisionId,
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ newRevisions.push({
|
|
|
|
|
+ _id: revisionId, path: newPagePath, body: pathRevisionMapping[page.path].body, author: user._id, format: 'markdown',
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ });
|
|
|
|
|
|
|
|
|
|
+ await Page.insertMany(newPages, { ordered: false });
|
|
|
|
|
+ await Revision.insertMany(newRevisions, { ordered: false });
|
|
|
|
|
+
|
|
|
|
|
+ const newPath = page.path.replace(pathRegExp, newPagePathPrefix);
|
|
|
const newParentpage = await Page.findByPath(newPath);
|
|
const newParentpage = await Page.findByPath(newPath);
|
|
|
|
|
|
|
|
// TODO GW-4634 use stream
|
|
// TODO GW-4634 use stream
|