|
|
@@ -1,3 +1,7 @@
|
|
|
+const mongoose = require('mongoose');
|
|
|
+const escapeStringRegexp = require('escape-string-regexp');
|
|
|
+const { serializePageSecurely } = require('../models/serializers/page-serializer');
|
|
|
+
|
|
|
class PageService {
|
|
|
|
|
|
constructor(crowi) {
|
|
|
@@ -38,6 +42,49 @@ class PageService {
|
|
|
return Promise.all(promises);
|
|
|
}
|
|
|
|
|
|
+ async duplicate(page, newPagePath, user) {
|
|
|
+ const Page = this.crowi.model('Page');
|
|
|
+ const PageTagRelation = mongoose.model('PageTagRelation');
|
|
|
+ // populate
|
|
|
+ await page.populate({ path: 'revision', model: 'Revision', select: 'body' }).execPopulate();
|
|
|
+
|
|
|
+ // create option
|
|
|
+ const options = { page };
|
|
|
+ options.grant = page.grant;
|
|
|
+ options.grantUserGroupId = page.grantedGroup;
|
|
|
+ options.grantedUsers = page.grantedUsers;
|
|
|
+
|
|
|
+ const createdPage = await Page.create(
|
|
|
+ newPagePath, page.revision.body, user, options,
|
|
|
+ );
|
|
|
+
|
|
|
+ // take over tags
|
|
|
+ const originTags = await page.findRelatedTagsById();
|
|
|
+ let savedTags = [];
|
|
|
+ if (originTags != null) {
|
|
|
+ await PageTagRelation.updatePageTags(createdPage.id, originTags);
|
|
|
+ savedTags = await PageTagRelation.listTagNamesByPage(createdPage.id);
|
|
|
+ }
|
|
|
+
|
|
|
+ return { page: serializePageSecurely(createdPage), tags: savedTags };
|
|
|
+ }
|
|
|
+
|
|
|
+ async duplicateRecursively(page, newPagePath, user) {
|
|
|
+ const Page = this.crowi.model('Page');
|
|
|
+ const newPagePathPrefix = newPagePath;
|
|
|
+ const pathRegExp = new RegExp(`^${escapeStringRegexp(page.path)}`, 'i');
|
|
|
+
|
|
|
+ const pages = await Page.findManageableListWithDescendants(page, user);
|
|
|
+
|
|
|
+ const promise = pages.map(async(page) => {
|
|
|
+ const newPagePath = page.path.replace(pathRegExp, newPagePathPrefix);
|
|
|
+ return this.duplicate(page, newPagePath, user);
|
|
|
+ });
|
|
|
+
|
|
|
+ // TODO GW-4634 use stream
|
|
|
+ return Promise.allSettled(promise);
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
}
|
|
|
|