Quellcode durchsuchen

Merge branch 'dev/7.1.x' into feat/openai-vector-searching

Yuki Takei vor 1 Jahr
Ursprung
Commit
85b613c707

+ 3 - 2
apps/app/src/server/routes/apiv3/page/update-page.ts

@@ -8,6 +8,7 @@ import { isTopPage, isUsersProtectedPages } from '@growi/core/dist/utils/page-pa
 import type { Request, RequestHandler } from 'express';
 import type { ValidationChain } from 'express-validator';
 import { body } from 'express-validator';
+import type { HydratedDocument } from 'mongoose';
 import mongoose from 'mongoose';
 
 import { SupportedAction, SupportedTargetModel } from '~/interfaces/activity';
@@ -70,7 +71,7 @@ export const updatePageHandlersFactory: UpdatePageHandlersFactory = (crowi) => {
   ];
 
 
-  async function postAction(req: UpdatePageRequest, res: ApiV3Response, updatedPage: PageDocument, previousRevision: IRevisionHasId | null) {
+  async function postAction(req: UpdatePageRequest, res: ApiV3Response, updatedPage: HydratedDocument<PageDocument>, previousRevision: IRevisionHasId | null) {
     // Reflect the updates in ydoc
     const origin = req.body.origin;
     if (origin === Origin.View || origin === undefined) {
@@ -179,7 +180,7 @@ export const updatePageHandlersFactory: UpdatePageHandlersFactory = (crowi) => {
         return res.apiv3Err(new ErrorV3('Posted param "revisionId" is outdated.', PageUpdateErrorCode.CONFLICT, undefined, { returnLatestRevision }), 409);
       }
 
-      let updatedPage: PageDocument;
+      let updatedPage: HydratedDocument<PageDocument>;
       let previousRevision: IRevisionHasId | null;
       try {
         const {

+ 16 - 18
apps/app/src/server/service/page/index.ts

@@ -97,14 +97,10 @@ class PageCursorsForDescendantsFactory {
 
   private initialCursor: Cursor<any> | never[]; // TODO: wait for mongoose update
 
-  private Page: PageModel;
-
   constructor(user: any, rootPage: any, shouldIncludeEmpty: boolean) {
     this.user = user;
     this.rootPage = rootPage;
     this.shouldIncludeEmpty = shouldIncludeEmpty;
-
-    this.Page = mongoose.model('Page') as unknown as PageModel;
   }
 
   // prepare initial cursor
@@ -151,9 +147,10 @@ class PageCursorsForDescendantsFactory {
       return [];
     }
 
-    const { PageQueryBuilder } = this.Page;
+    const Page = mongoose.model<HydratedDocument<PageDocument>, PageModel>('Page');
+    const { PageQueryBuilder } = Page;
 
-    const builder = new PageQueryBuilder(this.Page.find(), this.shouldIncludeEmpty);
+    const builder = new PageQueryBuilder(Page.find(), this.shouldIncludeEmpty);
     builder.addConditionToFilteringByParentId(page._id);
 
     const cursor = builder.query.lean().cursor({ batchSize: BULK_REINDEX_SIZE }) as Cursor<any>;
@@ -3667,13 +3664,13 @@ class PageService implements IPageService {
 
   // --------- Create ---------
 
-  private async preparePageDocumentToCreate(path: string, shouldNew: boolean): Promise<PageDocument> {
-    const Page = mongoose.model('Page') as unknown as PageModel;
+  private async preparePageDocumentToCreate(path: string, shouldNew: boolean): Promise<HydratedDocument<PageDocument>> {
+    const Page = mongoose.model<HydratedDocument<PageDocument>, PageModel>('Page');
 
     const emptyPage = await Page.findOne({ path, isEmpty: true });
 
     // Use empty page if exists, if not, create a new page
-    let page;
+    let page: HydratedDocument<PageDocument>;
     if (shouldNew) {
       page = new Page();
     }
@@ -3787,7 +3784,7 @@ class PageService implements IPageService {
    * Create a page
    * Set options.isSynchronously to true to await all process when you want to run this method multiple times at short intervals.
    */
-  async create(_path: string, body: string, user: HasObjectId, options: IOptionsForCreate = {}): Promise<PageDocument> {
+  async create(_path: string, body: string, user: HasObjectId, options: IOptionsForCreate = {}): Promise<HydratedDocument<PageDocument>> {
     // Switch method
     const isV5Compatible = this.crowi.configManager.getConfig('crowi', 'app:isV5Compatible');
     if (!isV5Compatible) {
@@ -3798,7 +3795,7 @@ class PageService implements IPageService {
     const path: string = generalXssFilter.process(_path); // sanitize path
 
     // Retrieve closest ancestor document
-    const Page = mongoose.model<PageDocument, PageModel>('Page');
+    const Page = mongoose.model<HydratedDocument<PageDocument>, PageModel>('Page');
     const closestAncestor = await Page.findNonEmptyClosestAncestor(path);
 
     // Determine grantData
@@ -3916,7 +3913,7 @@ class PageService implements IPageService {
    * V4 compatible create method
    */
   private async createV4(path, body, user, options: any = {}) {
-    const Page = mongoose.model('Page') as unknown as PageModel;
+    const Page = mongoose.model<HydratedDocument<PageDocument>, PageModel>('Page');
 
     const format = options.format || 'markdown';
     const grantUserGroupIds = options.grantUserGroupIds || null;
@@ -4092,7 +4089,7 @@ class PageService implements IPageService {
   }
 
   async updatePageSubOperation(page, user, exPage, options: IOptionsForUpdate, pageOpId: ObjectIdLike): Promise<void> {
-    const Page = mongoose.model('Page') as unknown as PageModel;
+    const Page = mongoose.model<HydratedDocument<PageDocument>, PageModel>('Page');
 
     const currentPage = page;
 
@@ -4157,13 +4154,13 @@ class PageService implements IPageService {
   }
 
   async updatePage(
-      pageData: PageDocument,
+      pageData: HydratedDocument<PageDocument>,
       body: string | null,
       previousBody: string | null,
       user: IUserHasId,
       options: IOptionsForUpdate = {},
-  ): Promise<PageDocument> {
-    const Page = mongoose.model('Page') as unknown as PageModel;
+  ): Promise<HydratedDocument<PageDocument>> {
+    const Page = mongoose.model<HydratedDocument<PageDocument>, PageModel>('Page');
 
     const wasOnTree = pageData.parent != null || isTopPage(pageData.path);
     const isV5Compatible = this.crowi.configManager.getConfig('crowi', 'app:isV5Compatible');
@@ -4305,8 +4302,9 @@ class PageService implements IPageService {
   }
 
 
-  async updatePageV4(pageData: PageDocument, body, previousBody, user, options: IOptionsForUpdate = {}): Promise<PageDocument> {
-    const Page = mongoose.model('Page') as unknown as PageModel;
+  async updatePageV4(
+      pageData: HydratedDocument<PageDocument>, body, previousBody, user, options: IOptionsForUpdate = {},
+  ): Promise<HydratedDocument<PageDocument>> {
 
     // use the previous data if absent
     const grant = options.grant || pageData.grant;

+ 5 - 3
apps/app/src/server/service/page/page-service.ts

@@ -4,7 +4,7 @@ import type {
   HasObjectId,
   IPageInfo, IPageInfoForEntity, IUser,
 } from '@growi/core';
-import type { Types } from 'mongoose';
+import type { HydratedDocument, Types } from 'mongoose';
 
 import type { IOptionsForCreate, IOptionsForUpdate } from '~/interfaces/page';
 import type { PopulatedGrantedGroup } from '~/interfaces/page-grant';
@@ -13,9 +13,11 @@ import type { ObjectIdLike } from '~/server/interfaces/mongoose-utils';
 import type { PageDocument } from '~/server/models/page';
 
 export interface IPageService {
-  create(path: string, body: string, user: HasObjectId, options: IOptionsForCreate): Promise<PageDocument>,
+  create(path: string, body: string, user: HasObjectId, options: IOptionsForCreate): Promise<HydratedDocument<PageDocument>>,
   forceCreateBySystem(path: string, body: string, options: IOptionsForCreate): Promise<PageDocument>,
-  updatePage(pageData: PageDocument, body: string | null, previousBody: string | null, user: IUser, options: IOptionsForUpdate,): Promise<PageDocument>,
+  updatePage(
+    pageData: HydratedDocument<PageDocument>, body: string | null, previousBody: string | null, user: IUser, options: IOptionsForUpdate
+  ): Promise<HydratedDocument<PageDocument>>,
   updateDescendantCountOfAncestors: (pageId: ObjectIdLike, inc: number, shouldIncludeTarget: boolean) => Promise<void>,
   deleteCompletelyOperation: (pageIds: ObjectIdLike[], pagePaths: string[]) => Promise<void>,
   getEventEmitter: () => EventEmitter,