Taichi Masuyama 4 سال پیش
والد
کامیت
d2180dd629

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

@@ -44,7 +44,7 @@ type TargetAndAncestorsResult = {
 export type CreateMethod = (path: string, body: string, user, options) => Promise<PageDocument & { _id: any }>
 export interface PageModel extends Model<PageDocument> {
   [x: string]: any; // for obsolete methods
-  createEmptyPagesByPaths(paths: string[], onlyMigratedAsExistingPages?: boolean, publicOnly?: boolean, andFilter?): Promise<void>
+  createEmptyPagesByPaths(paths: string[], user: any | null, onlyMigratedAsExistingPages?: boolean, andFilter?): Promise<void>
   getParentAndFillAncestors(path: string, user): Promise<PageDocument & { _id: any }>
   findByIdsAndViewer(pageIds: ObjectIdLike[], user, userGroups?, includeEmpty?: boolean): Promise<PageDocument[]>
   findByPathAndViewer(path: string | null, user, userGroups?, useFindOne?: boolean, includeEmpty?: boolean): Promise<PageDocument[]>
@@ -436,9 +436,9 @@ schema.statics.createEmptyPagesByPaths = async function(paths: string[], user: a
     });
   }
   // 3. Add custom pipeline
-  // if (filter != null) {
-  //   aggregationPipeline.push({ $match: filter });
-  // }
+  if (filter != null) {
+    aggregationPipeline.push({ $match: filter });
+  }
   // 4. Add grant conditions
   let userGroups = null;
   if (user != null) {

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

@@ -2664,17 +2664,19 @@ class PageService {
 
         // 2. Create lacking parents as empty pages
         const orFilters = [
+          { path: '/' },
           { path: { $in: publicPathsToNormalize }, grant: Page.GRANT_PUBLIC, status: Page.STATUS_PUBLISHED },
           { path: { $in: publicPathsToNormalize }, parent: { $ne: null }, status: Page.STATUS_PUBLISHED },
           { path: { $nin: publicPathsToNormalize }, status: Page.STATUS_PUBLISHED },
         ];
-        const filterForEmptyPagesToCreate = { $or: orFilters };
-        await Page.createEmptyPagesByPaths(parentPaths, user, false, filterForEmptyPagesToCreate);
+        const filterForApplicableAncestors = { $or: orFilters };
+        await Page.createEmptyPagesByPaths(parentPaths, user, false, filterForApplicableAncestors);
 
         // 3. Find parents
         const builder2 = new PageQueryBuilder(Page.find(), true);
         const parents = await builder2
           .addConditionToListByPathsArray(parentPaths)
+          .addCustomAndCondition(filterForApplicableAncestors)
           .addCustomAndCondition(grantFiltersByUser) // use addCustomAndCondition instead of addConditionToFilteringByViewerToEdit to reduce the num of queries
           .query
           .lean()
@@ -2691,6 +2693,7 @@ class PageService {
               {
                 path: { $regex: new RegExp(`^${parentPathEscaped}(\\/[^/]+)\\/?$`, 'i') }, // see: regexr.com/6889f (e.g. /parent/any_child or /any_level1)
               },
+              filterForApplicableAncestors,
               grantFiltersByUser,
             ],
           };

+ 2 - 2
packages/app/test/integration/service/v5.migration.test.js

@@ -454,7 +454,7 @@ describe('V5 page migration', () => {
         {
           path: '/normalize_17_owned/normalize_18_owned/normalize_19_owned/normalize_20_group',
           grant: Page.GRANT_USER_GROUP,
-          grantedGroup: testUser1GroupId,
+          grantedGroup: rootUserGroupId,
         },
 
         // 3
@@ -567,7 +567,7 @@ describe('V5 page migration', () => {
       const _owned19 = await Page.findOne(owned({ path: '/normalize_17_owned/normalize_18_owned/normalize_19_owned', ...notNormalized }));
       const _owned20 = await Page.findOne(owned({ path: '/normalize_17_owned/normalize_18_owned/normalize_19_owned/normalize_20_owned', ...notNormalized }));
       const _root20 = await Page.findOne(root({ path: '/normalize_17_owned/normalize_18_owned/normalize_19_owned/normalize_20_root', ...notNormalized }));
-      const _group20 = await Page.findOne(testUser1Group({ path: '/normalize_17_owned/normalize_18_owned/normalize_19_owned/normalize_20_group', ...notNormalized }));
+      const _group20 = await Page.findOne(rootUserGroup({ path: '/normalize_17_owned/normalize_18_owned/normalize_19_owned/normalize_20_group', ...notNormalized }));
 
       expect(_owned17).not.toBeNull();
       expect(_owned18).not.toBeNull();