Taichi Masuyama 4 лет назад
Родитель
Сommit
497296db79
1 измененных файлов с 20 добавлено и 25 удалено
  1. 20 25
      packages/app/src/server/service/page-grant.ts

+ 20 - 25
packages/app/src/server/service/page-grant.ts

@@ -259,8 +259,8 @@ class PageGrantService {
     const pathWithTrailingSlash = addTrailingSlash(targetPath);
     const pathWithTrailingSlash = addTrailingSlash(targetPath);
     const startsPattern = escapeStringRegexp(pathWithTrailingSlash);
     const startsPattern = escapeStringRegexp(pathWithTrailingSlash);
 
 
-    const descendants = await Page.aggregate([
-      {
+    const result = await Page.aggregate([
+      { // match to descendants excluding empty pages
         $match: {
         $match: {
           path: new RegExp(`^${startsPattern}`),
           path: new RegExp(`^${startsPattern}`),
           isEmpty: { $ne: true },
           isEmpty: { $ne: true },
@@ -274,37 +274,32 @@ class PageGrantService {
           grantedGroup: 1,
           grantedGroup: 1,
         },
         },
       },
       },
-      {
+      { // remove duplicates from pipeline
         $group: {
         $group: {
-          _id: {
-            grantedGroup: '$grantedGroup',
-            grantedUsers: '$grantedUsers',
-          },
-          grant: 1,
-          grantedUsers: 1,
-          grantedGroup: 1,
+          _id: '$grant',
+          grantedGroupSet: { $addToSet: '$grantGroup' },
+          grantedUsersSet: { $addToSet: '$grantedUsers' },
+        },
+      },
+      { // flatten granted user set
+        $unwind: {
+          path: '$grantedUsersSet',
         },
         },
       },
       },
     ]);
     ]);
-    console.log('かしか', descendants);
 
 
-    const isPublicExist = descendants.some(d => d.grant === Page.GRANT_PUBLIC);
+    // GRANT_PUBLIC group
+    const isPublicExist = result.some(r => r._id === Page.GRANT_PUBLIC);
+    // GRANT_OWNER group
+    const grantOwnerResult = result.filter(r => r._id === Page.GRANT_OWNER)[0]; // users of GRANT_OWNER
+    const grantedUserIds: ObjectId[] = grantOwnerResult != null ? grantOwnerResult.grantedUsersSet : null;
+    // GRANT_USER_GROUP group
+    const grantUserGroupResult = result.filter(r => r._id === Page.GRANT_USER_GROUP)[0]; // users of GRANT_OWNER
+    const grantedGroupIds = grantUserGroupResult != null ? grantUserGroupResult.grantedGroupSet : null;
 
 
-    let grantedUsersOfGrantOwner: ObjectId[] = []; // users of GRANT_OWNER
-    const grantedGroups: ObjectId[] = [];
-    descendants.forEach((d) => {
-      if (d.grantedUsers != null) {
-        grantedUsersOfGrantOwner = grantedUsersOfGrantOwner.concat(d.grantedUsers);
-      }
-      if (d.grantedGroup != null) {
-        grantedGroups.push(d.grantedGroup);
-      }
-    });
-
-    const grantedGroupIds = removeDuplicates(grantedGroups);
     return {
     return {
       isPublicExist,
       isPublicExist,
-      grantedUserIds: grantedUsersOfGrantOwner,
+      grantedUserIds,
       grantedGroupIds,
       grantedGroupIds,
     };
     };
   }
   }