2
0
Taichi Masuyama 4 жил өмнө
parent
commit
d362081fed

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

@@ -141,12 +141,22 @@ class PageQueryBuilder {
   }
 
   /**
-   * Do not use this method if any substitutional method exists
-   * Or, instead, create another method on PageQueryBuilder and use it.
+   * Used for filtering the pages at specified paths not to include unintentional pages.
+   * @param pathsToFilter The paths to have additional filters as to be applicable
+   * @returns PageQueryBuilder
    */
-  addCustomAndCondition(condition) {
+  addConditionToFilterByApplicableAncestors(pathsToFilter: string[]) {
     this.query = this.query
-      .and(condition);
+      .and(
+        {
+          $or: [
+            { path: '/' },
+            { path: { $in: pathsToFilter }, grant: GRANT_PUBLIC, status: STATUS_PUBLISHED },
+            { path: { $in: pathsToFilter }, parent: { $ne: null }, status: STATUS_PUBLISHED },
+            { path: { $nin: pathsToFilter }, status: STATUS_PUBLISHED },
+          ],
+        },
+      );
 
     return this;
   }

+ 7 - 2
packages/app/src/server/service/page.ts

@@ -2669,11 +2669,16 @@ class PageService {
         await Page.createEmptyPagesByPaths(parentPaths, user, false, filterForApplicableAncestors);
 
         // 3. Find parents
+        const addGrantCondition = (builder) => {
+          builder.query = builder.query.and(grantFiltersByUser);
+
+          return builder;
+        };
         const builder2 = new PageQueryBuilder(Page.find(), true);
+        addGrantCondition(builder2);
         const parents = await builder2
           .addConditionToListByPathsArray(parentPaths)
-          .addCustomAndCondition(filterForApplicableAncestors)
-          .addCustomAndCondition(grantFiltersByUser) // use addCustomAndCondition instead of addConditionToFilteringByViewerToEdit to reduce the num of queries
+          .addConditionToFilterByApplicableAncestors(publicPathsToNormalize)
           .query
           .lean()
           .exec();