Procházet zdrojové kódy

move method to model and remove condition

yohei0125 před 4 roky
rodič
revize
a01e1774a6

+ 24 - 5
packages/app/src/server/models/page.ts

@@ -1,19 +1,23 @@
 /* eslint-disable @typescript-eslint/no-explicit-any */
 
+import nodePath from 'path';
+
+import { getOrCreateModel, pagePathUtils, pathUtils } from '@growi/core';
+import escapeStringRegexp from 'escape-string-regexp';
 import mongoose, {
   Schema, Model, Document, AnyObject,
 } from 'mongoose';
 import mongoosePaginate from 'mongoose-paginate-v2';
 import uniqueValidator from 'mongoose-unique-validator';
-import escapeStringRegexp from 'escape-string-regexp';
-import nodePath from 'path';
-import { getOrCreateModel, pagePathUtils, pathUtils } from '@growi/core';
 
+
+import { ObjectIdLike } from '~/server/interfaces/mongoose-utils';
+
+import { IPage } from '../../interfaces/page';
 import loggerFactory from '../../utils/logger';
 import Crowi from '../crowi';
-import { IPage } from '../../interfaces/page';
+
 import { getPageSchema, extractToAncestorsPaths, populateDataToShowRevision } from './obsolete-page';
-import { ObjectIdLike } from '~/server/interfaces/mongoose-utils';
 import { PageRedirectModel } from './page-redirect';
 
 const { addTrailingSlash } = pathUtils;
@@ -952,6 +956,21 @@ export function generateGrantCondition(
 
 schema.statics.generateGrantCondition = generateGrantCondition;
 
+schema.statics.findNotEmptyClosestAncestor = async function(path: string): Promise<PageDocument> {
+  const Page = mongoose.model('Page') as unknown as PageModel;
+  const { PageQueryBuilder } = Page;
+  const builderForAncestors = new PageQueryBuilder(Page.find(), false); // empty page not included
+
+  const ancestors = await builderForAncestors
+    .addConditionToListOnlyAncestors(path) // only ancestor paths
+    .addConditionToSortPagesByDescPath() // sort by path in Desc. Long to Short.
+    .query
+    .exec();
+
+  return ancestors[0];
+};
+
+
 export type PageCreateOptions = {
   format?: string
   grantUserGroupId?: ObjectIdLike

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

@@ -2093,17 +2093,19 @@ class PageService {
   }
 
   async constructBasicPageInfo(page: IPage, operator: IUserHasId): Promise<IPageInfo | IPageInfoForEntity> {
+    const Page = mongoose.model('Page') as unknown as PageModel;
+
     const isGuestUser = operator == null;
     const isMovable = isGuestUser ? false : isMovablePage(page.path);
 
     if (page.isEmpty) {
       // Need non-empty ancestor page to get its creator id because empty page does NOT have it.
       // Use creator id of ancestor page to determine whether the empty page is deletable
-      const notEmptyClosestAncestor = await this.findNotEmptyClosestAncestor(page.path);
+      const notEmptyClosestAncestor = await Page.findNotEmptyClosestAncestor(page.path);
       const creatorId = notEmptyClosestAncestor.creator;
 
-      const isDeletable = isGuestUser && !isMovable ? false : this.canDelete(creatorId, operator, false);
-      const isAbleToDeleteCompletely = isGuestUser && !isMovable ? false : this.canDeleteCompletely(creatorId, operator, false); // use normal delete config
+      const isDeletable = !isMovable ? false : this.canDelete(creatorId, operator, false);
+      const isAbleToDeleteCompletely = !isMovable ? false : this.canDeleteCompletely(creatorId, operator, false); // use normal delete config
 
       return {
         isV5Compatible: true,
@@ -2867,20 +2869,6 @@ class PageService {
     socket.emit(SocketEventName.UpdateDescCount, data);
   }
 
-  async findNotEmptyClosestAncestor(path: string): Promise<PageDocument> {
-    const Page = mongoose.model('Page') as unknown as PageModel;
-    const { PageQueryBuilder } = Page;
-    const builderForAncestors = new PageQueryBuilder(Page.find(), false); // empty page not included
-
-    const ancestors = await builderForAncestors
-      .addConditionToListOnlyAncestors(path) // only ancestor paths
-      .addConditionToSortPagesByDescPath() // sort by path in Desc. Long to Short.
-      .query
-      .exec();
-
-    return ancestors[0];
-  }
-
 }
 
 export default PageService;