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

Ensure parent page is not deleted by TTL

Shun Miyazawa 2 лет назад
Родитель
Сommit
a6429cc61c
2 измененных файлов с 24 добавлено и 3 удалено
  1. 5 2
      apps/app/src/server/models/page.ts
  2. 19 1
      apps/app/src/server/service/page/index.ts

+ 5 - 2
apps/app/src/server/models/page.ts

@@ -1063,9 +1063,12 @@ schema.methods.unpublish = function() {
   this.ttlTimestamp = undefined;
 };
 
-schema.methods.makeWip = function() {
+schema.methods.makeWip = function(disableTtl: boolean) {
   this.wip = true;
-  this.ttlTimestamp = new Date();
+
+  if (!disableTtl) {
+    this.ttlTimestamp = new Date();
+  }
 };
 
 /*

+ 19 - 1
apps/app/src/server/service/page/index.ts

@@ -3814,7 +3814,9 @@ class PageService implements IPageService {
 
     // Set wip
     if (options.wip) {
-      page.makeWip();
+      const children = await Page.find({ parent: page._id });
+      const disableTtl = children.length > 0;
+      page.makeWip(disableTtl);
     }
 
     // Save
@@ -3846,6 +3848,7 @@ class PageService implements IPageService {
       throw err;
     }
 
+    this.disableAncestorPagesTTL(path);
     this.createSubOperation(savedPage, user, options, pageOp._id);
 
     return savedPage;
@@ -3939,6 +3942,21 @@ class PageService implements IPageService {
     return this.canProcessCreate(path, grantData, false);
   }
 
+  private async disableAncestorPagesTTL(path: string): Promise<void> {
+    const Page = mongoose.model<PageDocument, PageModel>('Page');
+
+    const ancestorPaths = collectAncestorPaths(path);
+    const builder = new PageQueryBuilder(Page.find()); // includeEmpty false
+    const nonEmptyAncestors = await builder
+      .addConditionAsNonRootPage()
+      .addConditionToListByPathsArray(ancestorPaths)
+      .query
+      .exec();
+
+    const nonEmptyAncestorIds = nonEmptyAncestors.map(page => page._id);
+    await Page.updateMany({ _id: { $in: nonEmptyAncestorIds } }, { $set: { ttlTimestamp: null } });
+  }
+
   /**
    * @private
    * This method receives the same arguments as the PageService.create method does except for the added type '{ grantUserIds?: ObjectIdLike[] }'.