Просмотр исходного кода

update descendant count on duplication

yohei0125 4 лет назад
Родитель
Сommit
6f007f8712
2 измененных файлов с 39 добавлено и 18 удалено
  1. 4 2
      packages/app/src/server/models/page.ts
  2. 35 16
      packages/app/src/server/service/page.ts

+ 4 - 2
packages/app/src/server/models/page.ts

@@ -143,7 +143,7 @@ schema.statics.createEmptyPagesByPaths = async function(paths: string[], publicO
 };
 };
 
 
 schema.statics.createEmptyPage = async function(
 schema.statics.createEmptyPage = async function(
-    path: string, parent: any, descendantCount: number, // TODO: improve type including IPage at https://redmine.weseek.co.jp/issues/86506
+    path: string, parent: any, descendantCount = 0, // TODO: improve type including IPage at https://redmine.weseek.co.jp/issues/86506
 ): Promise<PageDocument & { _id: any }> {
 ): Promise<PageDocument & { _id: any }> {
   if (parent == null) {
   if (parent == null) {
     throw Error('parent must not be null');
     throw Error('parent must not be null');
@@ -608,7 +608,9 @@ export default (crowi: Crowi): any => {
 
 
     let savedPage = await page.save();
     let savedPage = await page.save();
 
 
-    await crowi.pageService?.updateDescendantCountOfAncestors(page._id, 1, false);
+    if (crowi.pageService != null) {
+      await crowi.pageService.updateDescendantCountOfAncestors(page._id, 1, false);
+    }
 
 
     /*
     /*
      * After save
      * After save

+ 35 - 16
packages/app/src/server/service/page.ts

@@ -675,9 +675,24 @@ class PageService {
 
 
     newPagePath = this.crowi.xss.process(newPagePath); // eslint-disable-line no-param-reassign
     newPagePath = this.crowi.xss.process(newPagePath); // eslint-disable-line no-param-reassign
 
 
-    const createdPage = await (Page.create as CreateMethod)(
-      newPagePath, page.revision.body, user, options,
-    );
+    let createdPage;
+
+    if (page.isEmpty) {
+      const parent = await Page.getParentAndFillAncestors(newPagePath);
+      createdPage = await Page.createEmptyPage(newPagePath, parent);
+    }
+    else {
+      createdPage = await (Page.create as CreateMethod)(
+        newPagePath, page.revision.body, user, options,
+      );
+    }
+
+    (async() => {
+      if (isRecursively) {
+        const descendantCountAppliedToAncestors = await this.duplicateDescendantsWithStream(page, newPagePath, user, shouldUseV4Process);
+        await this.updateDescendantCountOfAncestors(createdPage._id, descendantCountAppliedToAncestors, false);
+      }
+    })();
 
 
     // take over tags
     // take over tags
     const originTags = await page.findRelatedTagsById();
     const originTags = await page.findRelatedTagsById();
@@ -802,14 +817,7 @@ class PageService {
       pageIdMapping[page._id] = newPageId;
       pageIdMapping[page._id] = newPageId;
 
 
       let newPage;
       let newPage;
-      if (page.isEmpty) {
-        newPage = {
-          _id: newPageId,
-          path: newPagePath,
-          isEmpty: true,
-        };
-      }
-      else {
+      if (!page.isEmpty) {
         newPage = {
         newPage = {
           _id: newPageId,
           _id: newPageId,
           path: newPagePath,
           path: newPagePath,
@@ -820,14 +828,13 @@ class PageService {
           lastUpdateUser: user._id,
           lastUpdateUser: user._id,
           revision: revisionId,
           revision: revisionId,
         };
         };
-      }
+        newRevisions.push({
+          _id: revisionId, pageId: newPageId, body: pageIdRevisionMapping[page._id].body, author: user._id, format: 'markdown',
+        });
 
 
+      }
       newPages.push(newPage);
       newPages.push(newPage);
 
 
-      newRevisions.push({
-        _id: revisionId, pageId: newPageId, body: pageIdRevisionMapping[page._id].body, author: user._id, format: 'markdown',
-      });
-
     });
     });
 
 
     await Page.insertMany(newPages, { ordered: false });
     await Page.insertMany(newPages, { ordered: false });
@@ -937,6 +944,15 @@ class PageService {
       .pipe(createBatchStream(BULK_REINDEX_SIZE))
       .pipe(createBatchStream(BULK_REINDEX_SIZE))
       .pipe(writeStream);
       .pipe(writeStream);
 
 
+    // ******************************************************************************
+    // * Returns all the data objects in an array
+    // * https://github.com/bendrucker/stream-to-promise/blob/master/index.js#L15
+    // * https://github.com/stream-utils/stream-to-array#toarraystream-callbackerr-arr
+    // ******************************************************************************
+    const data = await streamToPromise(readStream);
+    const nonEmptyPagesCount = (data.filter(page => !page.isEmpty)).length; // count of pages with 'isEmpty: true'
+
+    return nonEmptyPagesCount;
   }
   }
 
 
   private async duplicateDescendantsWithStreamV4(page, newPagePath, user) {
   private async duplicateDescendantsWithStreamV4(page, newPagePath, user) {
@@ -1891,6 +1907,7 @@ class PageService {
   }
   }
 
 
   private async normalizeParentAndDescendantCountOfDescendants(path: string): Promise<void> {
   private async normalizeParentAndDescendantCountOfDescendants(path: string): Promise<void> {
+
     const escapedPath = escapeStringRegexp(path);
     const escapedPath = escapeStringRegexp(path);
     const regexps = [new RegExp(`^${escapedPath}`, 'i')];
     const regexps = [new RegExp(`^${escapedPath}`, 'i')];
     await this.normalizeParentRecursively(null, regexps);
     await this.normalizeParentRecursively(null, regexps);
@@ -1969,7 +1986,9 @@ class PageService {
       async write(pages, encoding, callback) {
       async write(pages, encoding, callback) {
         // make list to create empty pages
         // make list to create empty pages
         const parentPathsSet = new Set<string>(pages.map(page => pathlib.dirname(page.path)));
         const parentPathsSet = new Set<string>(pages.map(page => pathlib.dirname(page.path)));
+
         const parentPaths = Array.from(parentPathsSet);
         const parentPaths = Array.from(parentPathsSet);
+        console.log(parentPaths);
 
 
         // fill parents with empty pages
         // fill parents with empty pages
         await Page.createEmptyPagesByPaths(parentPaths, publicOnly);
         await Page.createEmptyPagesByPaths(parentPaths, publicOnly);