Explorar o código

Revert "populate on service layer"

This reverts commit 9bacd79061c83a93b3edf9f19153192051f4650b.
ryoji-s %!s(int64=2) %!d(string=hai) anos
pai
achega
70cb6ca54f
Modificáronse 2 ficheiros con 52 adicións e 50 borrados
  1. 50 43
      apps/app/src/server/models/page.ts
  2. 2 7
      apps/app/src/server/service/page.ts

+ 50 - 43
apps/app/src/server/models/page.ts

@@ -60,7 +60,7 @@ export interface PageModel extends Model<PageDocument> {
   findByPathAndViewer(path: string | null, user, userGroups?, useFindOne?: true, includeEmpty?: boolean): Promise<PageDocument & HasObjectId | null>
   findByPathAndViewer(path: string | null, user, userGroups?, useFindOne?: false, includeEmpty?: boolean): Promise<(PageDocument & HasObjectId)[]>
   countByPathAndViewer(path: string | null, user, userGroups?, includeEmpty?:boolean): Promise<number>
-  // findTargetAndAncestorsByPathOrId(pathOrId: string): Promise<TargetAndAncestorsResult>
+  findTargetAndAncestorsByPathOrId(pathOrId: string): Promise<TargetAndAncestorsResult>
   findRecentUpdatedPages(path: string, user, option, includeEmpty?: boolean): Promise<PaginatedPages>
   generateGrantCondition(
     user, userGroups, includeAnyoneWithTheLink?: boolean, showPagesRestrictedByOwner?: boolean, showPagesRestrictedByGroup?: boolean,
@@ -429,8 +429,15 @@ export class PageQueryBuilder {
     return this;
   }
 
-  addConditionToMinimizeDataForRendering(): PageQueryBuilder {
-    this.query = this.query.select('_id path isEmpty grant revision descendantCount creator');
+  async addConditionToMinimizeDataForRendering(): Promise<PageQueryBuilder> {
+    // eslint-disable-next-line rulesdir/no-populate
+    this.query = this.query
+      .select('_id path isEmpty grant revision descendantCount creator')
+      .populate({
+        path: 'creator',
+        model: 'User',
+        select: 'status',
+      });
 
     return this;
   }
@@ -636,46 +643,46 @@ schema.statics.findRecentUpdatedPages = async function(
 };
 
 
-// /*
-//  * Find all ancestor pages by path. When duplicate pages found, it uses the oldest page as a result
-//  * The result will include the target as well
-//  */
-// schema.statics.findTargetAndAncestorsByPathOrId = async function(pathOrId: string, user, userGroups): Promise<TargetAndAncestorsResult> {
-//   let path;
-//   if (!hasSlash(pathOrId)) {
-//     const _id = pathOrId;
-//     const page = await this.findOne({ _id });
-
-//     path = page == null ? '/' : page.path;
-//   }
-//   else {
-//     path = pathOrId;
-//   }
-
-//   const ancestorPaths = collectAncestorPaths(path);
-//   ancestorPaths.push(path); // include target
-
-//   // Do not populate
-//   const queryBuilder = new PageQueryBuilder(this.find(), true);
-//   await queryBuilder.addViewerCondition(user, userGroups);
-
-//   const _targetAndAncestors: PageDocument[] = await queryBuilder
-//     .addConditionAsOnTree()
-//     .addConditionToListByPathsArray(ancestorPaths)
-//     .addConditionToMinimizeDataForRendering()
-//     .addConditionToSortPagesByDescPath()
-//     .query
-//     .lean()
-//     .exec();
-
-//   // no same path pages
-//   const ancestorsMap = new Map<string, PageDocument>();
-//   _targetAndAncestors.forEach(page => ancestorsMap.set(page.path, page));
-//   const targetAndAncestors = Array.from(ancestorsMap.values());
-//   const rootPage = targetAndAncestors[targetAndAncestors.length - 1];
-
-//   return { targetAndAncestors, rootPage };
-// };
+/*
+ * Find all ancestor pages by path. When duplicate pages found, it uses the oldest page as a result
+ * The result will include the target as well
+ */
+schema.statics.findTargetAndAncestorsByPathOrId = async function(pathOrId: string, user, userGroups): Promise<TargetAndAncestorsResult> {
+  let path;
+  if (!hasSlash(pathOrId)) {
+    const _id = pathOrId;
+    const page = await this.findOne({ _id });
+
+    path = page == null ? '/' : page.path;
+  }
+  else {
+    path = pathOrId;
+  }
+
+  const ancestorPaths = collectAncestorPaths(path);
+  ancestorPaths.push(path); // include target
+
+  // Do not populate
+  const queryBuilder = new PageQueryBuilder(this.find(), true);
+  await queryBuilder.addViewerCondition(user, userGroups);
+  queryBuilder.addConditionAsOnTree();
+  queryBuilder.addConditionToListByPathsArray(ancestorPaths);
+  await queryBuilder.addConditionToMinimizeDataForRendering();
+
+  const _targetAndAncestors: PageDocument[] = await queryBuilder
+    .addConditionToSortPagesByDescPath()
+    .query
+    .lean()
+    .exec();
+
+  // no same path pages
+  const ancestorsMap = new Map<string, PageDocument>();
+  _targetAndAncestors.forEach(page => ancestorsMap.set(page.path, page));
+  const targetAndAncestors = Array.from(ancestorsMap.values());
+  const rootPage = targetAndAncestors[targetAndAncestors.length - 1];
+
+  return { targetAndAncestors, rootPage };
+};
 
 /**
  * Create empty pages at paths at which no pages exist

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

@@ -4198,17 +4198,12 @@ class PageService {
     // get pages at once
     const queryBuilder = new PageQueryBuilder(Page.find({ path: { $in: regexps } }), true);
     await queryBuilder.addViewerCondition(user, userGroups);
+    queryBuilder.addConditionAsOnTree();
+    await queryBuilder.addConditionToMinimizeDataForRendering();
 
     const pages = await queryBuilder
-      .addConditionAsOnTree()
-      .addConditionToMinimizeDataForRendering()
       .addConditionToSortPagesByAscPath()
       .query
-      .populate({
-        path: 'creator',
-        model: 'User',
-        select: 'status',
-      })
       .lean()
       .exec();