Taichi Masuyama пре 4 година
родитељ
комит
62aaeabed9

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

@@ -524,6 +524,8 @@ schema.statics.findAncestorsUsingParentRecursively = async function(pageId: Obje
  * @returns Promise<void>
  */
 schema.statics.removeLeafEmptyPagesById = async function(pageId: ObjectIdLike): Promise<void> {
+  const self = this;
+
   const initialLeafPage = await this.findById(pageId);
 
   if (initialLeafPage == null) {
@@ -537,7 +539,7 @@ schema.statics.removeLeafEmptyPagesById = async function(pageId: ObjectIdLike):
   const initialPageIdsToRemove = [initialLeafPage._id];
 
   async function generatePageIdsToRemove(page, pageIds: ObjectIdLike[]): Promise<ObjectIdLike[]> {
-    const nextPage = await this.findById(page.parent);
+    const nextPage = await self.findById(page.parent);
     if (!nextPage?.isEmpty) {
       return pageIds;
     }
@@ -547,7 +549,7 @@ schema.statics.removeLeafEmptyPagesById = async function(pageId: ObjectIdLike):
 
   const pageIdsToRemove = await generatePageIdsToRemove(initialLeafPage, initialPageIdsToRemove);
 
-  await this.removeMany({ _id: { $in: pageIdsToRemove } });
+  await this.deleteMany({ _id: { $in: pageIdsToRemove } });
 };
 
 export type PageCreateOptions = {

+ 2 - 1
packages/app/src/server/routes/page.js

@@ -1209,12 +1209,13 @@ module.exports = function(crowi, app) {
         await crowi.pageService.deleteCompletely(page, req.user, options, isRecursively);
       }
       else {
+        // behave like not found
         const notRecursivelyAndEmpty = page.isEmpty && !isRecursively;
         if (notRecursivelyAndEmpty) {
           return res.json(ApiResponse.error(`Page '${pageId}' is not found.`, 'notfound'));
         }
 
-        if (!page.isUpdatable(previousRevision)) {
+        if (!page.isEmpty && !page.isUpdatable(previousRevision)) {
           return res.json(ApiResponse.error('Someone could update this page, so couldn\'t delete.', 'outdated'));
         }
 

+ 18 - 10
packages/app/src/server/service/page.ts

@@ -268,6 +268,20 @@ class PageService {
     return page.grant !== Page.GRANT_RESTRICTED && page.grant !== Page.GRANT_SPECIFIED;
   }
 
+  /**
+   * Remove all empty pages at leaf position by page whose parent will change or which will be deleted.
+   * @param page Page whose parent will change or which will be deleted
+   */
+  async removeLeafEmptyPages(page): Promise<void> {
+    const Page = mongoose.model('Page') as unknown as PageModel;
+
+    // delete leaf empty pages
+    const shouldDeleteLeafEmptyPages = !(await Page.exists({ parent: page.parent, _id: { $ne: page._id } }));
+    if (shouldDeleteLeafEmptyPages) {
+      await Page.removeLeafEmptyPagesById(page.parent);
+    }
+  }
+
   /**
    * Generate read stream to operate descendants of the specified page path
    * @param {string} targetPagePath
@@ -1015,10 +1029,7 @@ class PageService {
       await this.updateDescendantCountOfAncestors(page.parent, -1, true);
 
       // delete leaf empty pages
-      const shouldDeleteLeafEmptyPages = !shouldReplace;
-      if (shouldDeleteLeafEmptyPages) {
-        await Page.removeLeafEmptyPagesById(page.parent);
-      }
+      await this.removeLeafEmptyPages(page);
     }
 
     let deletedPage;
@@ -1052,7 +1063,7 @@ class PageService {
           await this.updateDescendantCountOfAncestors(page.parent, (deletedDescendantCount + 1) * -1, true);
 
           // delete leaf empty pages
-          await Page.removeLeafEmptyPagesById(page.parent);
+          await this.removeLeafEmptyPages(page);
         }
       })();
     }
@@ -1286,10 +1297,7 @@ class PageService {
     }
 
     // delete leaf empty pages
-    const shouldDeleteLeafEmptyPages = !shouldReplace;
-    if (shouldDeleteLeafEmptyPages) {
-      await Page.removeLeafEmptyPagesById(page.parent);
-    }
+    await this.removeLeafEmptyPages(page);
 
     if (!page.isEmpty && !preventEmitting) {
       this.pageEvent.emit('deleteCompletely', page, user);
@@ -1465,7 +1473,7 @@ class PageService {
           await this.updateDescendantCountOfAncestors(page.parent, revertedDescendantCount + 1, true);
 
           // delete leaf empty pages
-          await Page.removeLeafEmptyPagesById(page.parent);
+          await this.removeLeafEmptyPages(page);
         }
       })();
     }