Просмотр исходного кода

transplant findByIdAndViewer and add countByIdAndViewer

Yuki Takei 6 месяцев назад
Родитель
Сommit
aadf89e67e
2 измененных файлов с 38 добавлено и 20 удалено
  1. 0 19
      apps/app/src/server/models/obsolete-page.js
  2. 38 1
      apps/app/src/server/models/page.ts

+ 0 - 19
apps/app/src/server/models/obsolete-page.js

@@ -343,25 +343,6 @@ export const getPageSchema = (crowi) => {
     return count > 0;
   };
 
-  /**
-   * @param {string} id ObjectId
-   * @param {User} user User instance
-   * @param {UserGroup[]} userGroups List of UserGroup instances
-   */
-  pageSchema.statics.findByIdAndViewer = async function(id, user, userGroups, includeEmpty = false) {
-    const baseQuery = this.findOne({ _id: id });
-
-    const relatedUserGroups = (user != null && userGroups == null) ? [
-      ...(await UserGroupRelation.findAllUserGroupIdsRelatedToUser(user)),
-      ...(await ExternalUserGroupRelation.findAllUserGroupIdsRelatedToUser(user)),
-    ] : userGroups;
-
-    const queryBuilder = new this.PageQueryBuilder(baseQuery, includeEmpty);
-    queryBuilder.addConditionToFilteringByViewer(user, relatedUserGroups, true);
-
-    return queryBuilder.query.exec();
-  };
-
   // find page by path
   pageSchema.statics.findByPath = function(path, includeEmpty = false) {
     if (path == null) {

+ 38 - 1
apps/app/src/server/models/page.ts

@@ -35,6 +35,8 @@ import { getPageSchema, extractToAncestorsPaths, populateDataToShowRevision } fr
 import type { UserGroupDocument } from './user-group';
 import UserGroupRelation from './user-group-relation';
 
+type ObjectId = mongoose.Types.ObjectId;
+
 const logger = loggerFactory('growi:models:page');
 /*
  * define schema
@@ -80,11 +82,16 @@ export type FindRecentUpdatedPagesOption = {
 }
 
 export type CreateMethod = (path: string, body: string, user, options: IOptionsForCreate) => Promise<HydratedDocument<PageDocument>>
+type FindByPathAndViewerMethod =
+  (this: PageModel, id: string | ObjectId, user, userGroups?, includeEmpty?: boolean) => Promise<HydratedDocument<PageDocument> | null>
+type CountByPathAndViewerMethod =
+  (this: PageModel, id: string | ObjectId, user, userGroups?, includeEmpty?: boolean) => Promise<number>
 
 export interface PageModel extends Model<PageDocument> {
   [x: string]: any; // for obsolete static methods
   createEmptyPage(path: string, parent, descendantCount?: number): Promise<HydratedDocument<PageDocument>>
-  findByIdAndViewer(pageId: ObjectIdLike, user, userGroups?, includeEmpty?: boolean): Promise<HydratedDocument<PageDocument> | null>
+  findByIdAndViewer: FindByPathAndViewerMethod
+  countByIdAndViewer: CountByPathAndViewerMethod
   findByIdsAndViewer(
     pageIds: ObjectIdLike[], user, userGroups?, includeEmpty?: boolean, includeAnyoneWithTheLink?: boolean,
   ): Promise<HydratedDocument<PageDocument>[]>
@@ -589,6 +596,36 @@ schema.statics.createEmptyPage = async function(
   return page.save();
 };
 
+const findByIdAndViewer: FindByPathAndViewerMethod = async function(this, id, user, userGroups = null, includeEmpty = false) {
+  const baseQuery = this.findOne({ _id: id });
+
+  const relatedUserGroups = (user != null && userGroups == null) ? [
+    ...(await UserGroupRelation.findAllUserGroupIdsRelatedToUser(user)),
+    ...(await ExternalUserGroupRelation.findAllUserGroupIdsRelatedToUser(user)),
+  ] : userGroups;
+
+  const queryBuilder = new this.PageQueryBuilder(baseQuery, includeEmpty);
+  queryBuilder.addConditionToFilteringByViewer(user, relatedUserGroups, true);
+
+  return queryBuilder.query.exec();
+};
+schema.statics.findByIdAndViewer = findByIdAndViewer;
+
+const countByIdAndViewer: CountByPathAndViewerMethod = async function(this, id, user, userGroups = null, includeEmpty = false) {
+  const baseQuery = this.countDocuments({ _id: id });
+
+  const relatedUserGroups = (user != null && userGroups == null) ? [
+    ...(await UserGroupRelation.findAllUserGroupIdsRelatedToUser(user)),
+    ...(await ExternalUserGroupRelation.findAllUserGroupIdsRelatedToUser(user)),
+  ] : userGroups;
+
+  const queryBuilder = new this.PageQueryBuilder(baseQuery, includeEmpty);
+  queryBuilder.addConditionToFilteringByViewer(user, relatedUserGroups, true);
+
+  return queryBuilder.query.exec();
+};
+schema.statics.countByIdAndViewer = countByIdAndViewer;
+
 /**
  * Replace an existing page with an empty page.
  * It updates the children's parent to the new empty page's _id.