Sfoglia il codice sorgente

Changed the method to get descendant pages

Shunm634-source 3 anni fa
parent
commit
8e1e6d7dc7

+ 14 - 17
packages/app/src/server/routes/apiv3/pages.js

@@ -501,6 +501,17 @@ module.exports = (crowi) => {
       isMoveMode: req.body.isMoveMode,
     };
 
+    const nOptions = {
+      ip: req.ip,
+      endpoint: req.originalUrl,
+      action: options.isRecursively ? SupportedAction.ACTION_PAGE_RECURSIVELY_RENAME : SupportedAction.ACTION_PAGE_RENAME,
+      user: req.user?._id,
+      snapshot: {
+        username: req.user?.username,
+      },
+    };
+    const activityId = res.locals.activity._id;
+
     if (!isCreatablePage(newPagePath)) {
       return res.apiv3Err(new ErrorV3(`Could not use the path '${newPagePath}'`, 'invalid_path'), 409);
     }
@@ -516,22 +527,15 @@ module.exports = (crowi) => {
 
     let page;
     let renamedPage;
-    let descendantPages;
 
     try {
       page = await Page.findByIdAndViewer(pageId, req.user, null, true);
-      const pages = await Page.findListWithDescendants(page.path, req.user);
-      descendantPages = pages.pages;
-      descendantPages.pop();
+      options.isRecursively = page.descendantCount > 0;
 
       if (page == null) {
         return res.apiv3Err(new ErrorV3(`Page '${pageId}' is not found or forbidden`, 'notfound_or_forbidden'), 401);
       }
 
-      if (descendantPages == null && options.isRecursively) {
-        return res.apiv3Err(new ErrorV3(`Page '${pageId}' has no descendant pages`, 'notfound_or_forbidden'), 401);
-      }
-
       // empty page does not require revisionId validation
       if (!page.isEmpty && revisionId == null) {
         return res.apiv3Err(new ErrorV3('revisionId must be a mongoId', 'invalid_body'), 400);
@@ -540,7 +544,7 @@ module.exports = (crowi) => {
       if (!page.isEmpty && !page.isUpdatable(revisionId)) {
         return res.apiv3Err(new ErrorV3('Someone could update this page, so couldn\'t delete.', 'notfound_or_forbidden'), 409);
       }
-      renamedPage = await crowi.pageService.renamePage(page, newPagePath, req.user, options);
+      renamedPage = await crowi.pageService.renamePage(page, newPagePath, req.user, options, nOptions, activityId);
     }
     catch (err) {
       logger.error(err);
@@ -556,14 +560,7 @@ module.exports = (crowi) => {
     catch (err) {
       logger.error('Move notification failed', err);
     }
-
-    const activityId = res.locals.activity._id;
-    const parameters = {
-      targetModel: SupportedTargetModel.MODEL_PAGE,
-      target: page,
-      action: options.isRecursively ? SupportedAction.ACTION_PAGE_RECURSIVELY_RENAME : SupportedAction.ACTION_PAGE_RENAME,
-    };
-    activityEvent.emit('update', activityId, parameters, page, descendantPages);
+    // activityEvent.emit('update', activityId, parameters, page, descendantPages);
 
     return res.apiv3(result);
   });

+ 24 - 6
packages/app/src/server/service/page.ts

@@ -6,6 +6,7 @@ import escapeStringRegexp from 'escape-string-regexp';
 import mongoose, { ObjectId, QueryCursor } from 'mongoose';
 import streamToPromise from 'stream-to-promise';
 
+import { SupportedAction, SupportedTargetModel } from '~/interfaces/activity';
 import { Ref } from '~/interfaces/common';
 import { V5ConversionErrCode } from '~/interfaces/errors/v5-conversion-error';
 import { HasObjectId } from '~/interfaces/has-object-id';
@@ -135,10 +136,13 @@ class PageService {
 
   tagEvent: any;
 
+  activityEvent: any;
+
   constructor(crowi) {
     this.crowi = crowi;
     this.pageEvent = crowi.event('page');
     this.tagEvent = crowi.event('tag');
+    this.activityEvent = crowi.event('activity');
 
     // init
     this.initPageEvent();
@@ -344,11 +348,18 @@ class PageService {
       .cursor({ batchSize: BULK_REINDEX_SIZE });
   }
 
-  async renamePage(page, newPagePath, user, options) {
+  async renamePage(page: IPage, newPagePath, user, options, nOptions, activityId): Promise<PageDocument | null> {
     /*
      * Common Operation
      */
     const Page = mongoose.model('Page') as unknown as PageModel;
+    this.crowi.activityService.createActivity(nOptions);
+
+    const parameters = {
+      targetModel: SupportedTargetModel.MODEL_PAGE,
+      target: page,
+      action: nOptions.action,
+    };
 
     const isExist = await Page.exists({ path: newPagePath });
     if (isExist) {
@@ -399,12 +410,15 @@ class PageService {
       logger.error('Failed to create PageOperation document.', err);
       throw err;
     }
-    const renamedPage = await this.renameMainOperation(page, newPagePath, user, options, pageOp._id);
+    const renamedPage = await this.renameMainOperation(page, newPagePath, user, options, pageOp._id, parameters, activityId);
+    if (!options.isRecursively) {
+      this.activityEvent.emit('update', activityId, parameters, page);
+    }
 
     return renamedPage;
   }
 
-  async renameMainOperation(page, newPagePath: string, user, options, pageOpId: ObjectIdLike) {
+  async renameMainOperation(page, newPagePath: string, user, options, pageOpId: ObjectIdLike, parameters, activityId): Promise<PageDocument | null> {
     const Page = mongoose.model('Page') as unknown as PageModel;
 
     const updateMetadata = options.updateMetadata || false;
@@ -489,12 +503,12 @@ class PageService {
     /*
      * Sub Operation
      */
-    this.renameSubOperation(page, newPagePath, user, options, renamedPage, pageOp._id);
+    this.renameSubOperation(page, newPagePath, user, options, renamedPage, pageOp._id, parameters, activityId);
 
     return renamedPage;
   }
 
-  async renameSubOperation(page, newPagePath: string, user, options, renamedPage, pageOpId: ObjectIdLike): Promise<void> {
+  async renameSubOperation(page, newPagePath: string, user, options, renamedPage, pageOpId: ObjectIdLike, parameters?, activityId?): Promise<void> {
     const Page = mongoose.model('Page') as unknown as PageModel;
 
     const exParentId = page.parent;
@@ -502,7 +516,8 @@ class PageService {
     const timerObj = this.crowi.pageOperationService.autoUpdateExpiryDate(pageOpId);
     try {
     // update descendants first
-      await this.renameDescendantsWithStream(page, newPagePath, user, options, false);
+      const descendantPages = await this.renameDescendantsWithStream(page, newPagePath, user, options, false);
+      this.activityEvent.emit('update', activityId, parameters, page, descendantPages);
     }
     catch (err) {
       logger.warn(err);
@@ -805,6 +820,7 @@ class PageService {
     const renameDescendants = this.renameDescendants.bind(this);
     const pageEvent = this.pageEvent;
     let count = 0;
+    let descendantPages = [];
     const writeStream = new Writable({
       objectMode: true,
       async write(batch, encoding, callback) {
@@ -813,6 +829,7 @@ class PageService {
           await renameDescendants(
             batch, user, options, pathRegExp, newPagePathPrefix, shouldUseV4Process,
           );
+          descendantPages = descendantPages.concat(batch);
           logger.debug(`Renaming pages progressing: (count=${count})`);
         }
         catch (err) {
@@ -837,6 +854,7 @@ class PageService {
       .pipe(writeStream);
 
     await streamToPromise(writeStream);
+    return descendantPages;
   }
 
   private async renameDescendantsWithStreamV4(targetPage, newPagePath, user, options = {}) {