Browse Source

Implemented

Taichi Masuyama 4 years ago
parent
commit
ef08075fcb
2 changed files with 43 additions and 6 deletions
  1. 32 0
      packages/app/src/server/models/page.ts
  2. 11 6
      packages/app/src/server/service/page.ts

+ 32 - 0
packages/app/src/server/models/page.ts

@@ -518,6 +518,38 @@ schema.statics.findAncestorsUsingParentRecursively = async function(pageId: Obje
   return findAncestorsRecursively(target);
 };
 
+/**
+ * Recursively removes empty pages at leaf position.
+ * @param pageId ObjectIdLike
+ * @returns Promise<void>
+ */
+schema.statics.removeLeafEmptyPagesById = async function(pageId: ObjectIdLike): Promise<void> {
+  const initialLeafPage = await this.findById(pageId);
+
+  if (initialLeafPage == null) {
+    return;
+  }
+
+  if (!initialLeafPage.isEmpty) {
+    return;
+  }
+
+  const initialPageIdsToRemove = [initialLeafPage._id];
+
+  async function generatePageIdsToRemove(page, pageIds: ObjectIdLike[]): Promise<ObjectIdLike[]> {
+    const nextPage = await this.findById(page.parent);
+    if (!nextPage?.isEmpty) {
+      return pageIds;
+    }
+
+    return generatePageIdsToRemove(nextPage, [...pageIds, nextPage._id]);
+  }
+
+  const pageIdsToRemove = await generatePageIdsToRemove(initialLeafPage, initialPageIdsToRemove);
+
+  await this.removeMany({ _id: { $in: pageIdsToRemove } });
+};
+
 export type PageCreateOptions = {
   format?: string
   grantUserGroupId?: ObjectIdLike

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

@@ -1014,9 +1014,10 @@ class PageService {
       // update descendantCount of ancestors'
       await this.updateDescendantCountOfAncestors(page.parent, -1, true);
 
+      // delete leaf empty pages
       const shouldDeleteLeafEmptyPages = !shouldReplace;
       if (shouldDeleteLeafEmptyPages) {
-        // TODO https://redmine.weseek.co.jp/issues/87667 : delete leaf empty pages here
+        await Page.removeLeafEmptyPagesById(page.parent);
       }
     }
 
@@ -1050,7 +1051,8 @@ class PageService {
         if (page.parent != null) {
           await this.updateDescendantCountOfAncestors(page.parent, (deletedDescendantCount + 1) * -1, true);
 
-          // TODO https://redmine.weseek.co.jp/issues/87667 : delete leaf empty pages here
+          // delete leaf empty pages
+          await Page.removeLeafEmptyPagesById(page.parent);
         }
       })();
     }
@@ -1281,8 +1283,12 @@ class PageService {
 
     if (!isRecursively) {
       await this.updateDescendantCountOfAncestors(page.parent, -1, true);
+    }
 
-      // TODO https://redmine.weseek.co.jp/issues/87667 : delete leaf empty pages here
+    // delete leaf empty pages
+    const shouldDeleteLeafEmptyPages = !shouldReplace;
+    if (shouldDeleteLeafEmptyPages) {
+      await Page.removeLeafEmptyPagesById(page.parent);
     }
 
     if (!page.isEmpty && !preventEmitting) {
@@ -1299,8 +1305,6 @@ class PageService {
         if (page.parent != null) {
           await this.updateDescendantCountOfAncestors(page.parent, (deletedDescendantCount + 1) * -1, true);
         }
-
-        // TODO https://redmine.weseek.co.jp/issues/87667 : delete leaf empty pages here
       })();
     }
 
@@ -1460,7 +1464,8 @@ class PageService {
         if (page.parent != null) {
           await this.updateDescendantCountOfAncestors(page.parent, revertedDescendantCount + 1, true);
 
-          // TODO https://redmine.weseek.co.jp/issues/87667 : delete leaf empty pages here
+          // delete leaf empty pages
+          await Page.removeLeafEmptyPagesById(page.parent);
         }
       })();
     }