|
|
@@ -11,6 +11,7 @@ import loggerFactory from '~/utils/logger';
|
|
|
import { generateGrantCondition, PageModel } from '~/server/models/page';
|
|
|
import { stringifySnapshot } from '~/models/serializers/in-app-notification-snapshot/page';
|
|
|
import ActivityDefine from '../util/activityDefine';
|
|
|
+import { IPage } from '~/interfaces/page';
|
|
|
|
|
|
const debug = require('debug')('growi:services:page');
|
|
|
|
|
|
@@ -191,21 +192,83 @@ class PageService {
|
|
|
.cursor({ batchSize: BULK_REINDEX_SIZE });
|
|
|
}
|
|
|
|
|
|
- async renamePage(page, newPagePath, user, options, isRecursively = false) {
|
|
|
+ // TODO: rewrite recursive rename
|
|
|
+ async renamePage(page, newPagePath, user, options) {
|
|
|
+ // v4 compatible process
|
|
|
+ const isV5Compatible = this.crowi.configManager.getConfig('crowi', 'app:isV5Compatible');
|
|
|
+ if (!isV5Compatible) {
|
|
|
+ return this.renamePageV4(page, newPagePath, user, options);
|
|
|
+ }
|
|
|
|
|
|
+ const Page = this.crowi.model('Page');
|
|
|
+ const {
|
|
|
+ path, grant, grantedUsers: grantedUserIds, grantedGroup: grantUserGroupId,
|
|
|
+ } = page;
|
|
|
+ const updateMetadata = options.updateMetadata || false;
|
|
|
+
|
|
|
+ // sanitize path
|
|
|
+ newPagePath = this.crowi.xss.process(newPagePath); // eslint-disable-line no-param-reassign
|
|
|
+
|
|
|
+ /*
|
|
|
+ * UserGroup & Owner validation
|
|
|
+ */
|
|
|
+ if (grant !== Page.GRANT_RESTRICTED) {
|
|
|
+ let isGrantNormalized = false;
|
|
|
+ try {
|
|
|
+ const shouldCheckDescendants = false;
|
|
|
+
|
|
|
+ isGrantNormalized = await this.crowi.pageGrantService.isGrantNormalized(path, grant, grantedUserIds, grantUserGroupId, shouldCheckDescendants);
|
|
|
+ }
|
|
|
+ catch (err) {
|
|
|
+ logger.error(`Failed to validate grant of page at "${newPagePath}" when renaming`, err);
|
|
|
+ throw err;
|
|
|
+ }
|
|
|
+ if (!isGrantNormalized) {
|
|
|
+ throw Error(`This page cannot be renamed to "${newPagePath}" since the selected grant or grantedGroup is not assignable to this page.`);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // update descendants first
|
|
|
+ await this.renameDescendantsWithStream(page, newPagePath, user, options);
|
|
|
+
|
|
|
+ /*
|
|
|
+ * TODO: https://redmine.weseek.co.jp/issues/86577
|
|
|
+ * bulkWrite PageRedirectDocument if createRedirectPage is true
|
|
|
+ */
|
|
|
+
|
|
|
+ /*
|
|
|
+ * update target
|
|
|
+ */
|
|
|
+ const update: Partial<IPage> = {};
|
|
|
+ // find or create parent
|
|
|
+ const newParent = await Page.getParentAndFillAncestors(newPagePath);
|
|
|
+ // update Page
|
|
|
+ update.path = newPagePath;
|
|
|
+ update.parent = newParent._id;
|
|
|
+ if (updateMetadata) {
|
|
|
+ update.lastUpdateUser = user;
|
|
|
+ update.updatedAt = new Date();
|
|
|
+ }
|
|
|
+ const renamedPage = await Page.findByIdAndUpdate(page._id, { $set: update }, { new: true });
|
|
|
+
|
|
|
+ this.pageEvent.emit('rename', page, user);
|
|
|
+
|
|
|
+ return renamedPage;
|
|
|
+ }
|
|
|
+
|
|
|
+ // !!renaming always include descendant pages!!
|
|
|
+ private async renamePageV4(page, newPagePath, user, options) {
|
|
|
const Page = this.crowi.model('Page');
|
|
|
const Revision = this.crowi.model('Revision');
|
|
|
const path = page.path;
|
|
|
- const createRedirectPage = options.createRedirectPage || false;
|
|
|
const updateMetadata = options.updateMetadata || false;
|
|
|
|
|
|
// sanitize path
|
|
|
newPagePath = this.crowi.xss.process(newPagePath); // eslint-disable-line no-param-reassign
|
|
|
|
|
|
// create descendants first
|
|
|
- if (isRecursively) {
|
|
|
- await this.renameDescendantsWithStream(page, newPagePath, user, options);
|
|
|
- }
|
|
|
+ await this.renameDescendantsWithStream(page, newPagePath, user, options);
|
|
|
+
|
|
|
|
|
|
const update: any = {};
|
|
|
// update Page
|
|
|
@@ -219,10 +282,10 @@ class PageService {
|
|
|
// update Rivisions
|
|
|
await Revision.updateRevisionListByPath(path, { path: newPagePath }, {});
|
|
|
|
|
|
- if (createRedirectPage) {
|
|
|
- const body = `redirect ${newPagePath}`;
|
|
|
- await Page.create(path, body, user, { redirectTo: newPagePath });
|
|
|
- }
|
|
|
+ /*
|
|
|
+ * TODO: https://redmine.weseek.co.jp/issues/86577
|
|
|
+ * bulkWrite PageRedirectDocument if createRedirectPage is true
|
|
|
+ */
|
|
|
|
|
|
this.pageEvent.emit('rename', page, user);
|
|
|
|
|
|
@@ -231,20 +294,13 @@ class PageService {
|
|
|
|
|
|
|
|
|
private async renameDescendants(pages, user, options, oldPagePathPrefix, newPagePathPrefix) {
|
|
|
- const Page = this.crowi.model('Page');
|
|
|
-
|
|
|
const pageCollection = mongoose.connection.collection('pages');
|
|
|
- const revisionCollection = mongoose.connection.collection('revisions');
|
|
|
- const { updateMetadata, createRedirectPage } = options;
|
|
|
+ const { updateMetadata } = options;
|
|
|
|
|
|
const unorderedBulkOp = pageCollection.initializeUnorderedBulkOp();
|
|
|
- const createRediectPageBulkOp = pageCollection.initializeUnorderedBulkOp();
|
|
|
- const revisionUnorderedBulkOp = revisionCollection.initializeUnorderedBulkOp();
|
|
|
- const createRediectRevisionBulkOp = revisionCollection.initializeUnorderedBulkOp();
|
|
|
|
|
|
pages.forEach((page) => {
|
|
|
const newPagePath = page.path.replace(oldPagePathPrefix, newPagePathPrefix);
|
|
|
- const revisionId = new mongoose.Types.ObjectId();
|
|
|
|
|
|
if (updateMetadata) {
|
|
|
unorderedBulkOp
|
|
|
@@ -254,25 +310,10 @@ class PageService {
|
|
|
else {
|
|
|
unorderedBulkOp.find({ _id: page._id }).update({ $set: { path: newPagePath } });
|
|
|
}
|
|
|
- if (createRedirectPage) {
|
|
|
- createRediectPageBulkOp.insert({
|
|
|
- path: page.path, revision: revisionId, creator: user._id, lastUpdateUser: user._id, status: Page.STATUS_PUBLISHED, redirectTo: newPagePath,
|
|
|
- });
|
|
|
- createRediectRevisionBulkOp.insert({
|
|
|
- _id: revisionId, path: page.path, body: `redirect ${newPagePath}`, author: user._id, format: 'markdown',
|
|
|
- });
|
|
|
- }
|
|
|
- revisionUnorderedBulkOp.find({ path: page.path }).update({ $set: { path: newPagePath } });
|
|
|
});
|
|
|
|
|
|
try {
|
|
|
await unorderedBulkOp.execute();
|
|
|
- await revisionUnorderedBulkOp.execute();
|
|
|
- // Execute after unorderedBulkOp to prevent duplication
|
|
|
- if (createRedirectPage) {
|
|
|
- await createRediectPageBulkOp.execute();
|
|
|
- await createRediectRevisionBulkOp.execute();
|
|
|
- }
|
|
|
}
|
|
|
catch (err) {
|
|
|
if (err.code !== 11000) {
|
|
|
@@ -862,7 +903,7 @@ class PageService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- async shortBodiesMapByPageIds(pageIds = [], user) {
|
|
|
+ async shortBodiesMapByPageIds(pageIds: string[] = [], user) {
|
|
|
const Page = mongoose.model('Page');
|
|
|
const MAX_LENGTH = 350;
|
|
|
|