Yuki Takei 2 лет назад
Родитель
Сommit
aa0284853b

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

@@ -66,6 +66,7 @@ export type CreateMethod = (path: string, body: string, user, options: PageCreat
 export interface PageModel extends Model<PageDocument> {
   [x: string]: any; // for obsolete static methods
   findByIdsAndViewer(pageIds: ObjectIdLike[], user, userGroups?, includeEmpty?: boolean, includeAnyoneWithTheLink?: boolean): Promise<PageDocument[]>
+  findByPath(path: string, includeEmpty?: boolean): Promise<PageDocument | null>
   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>

+ 14 - 8
apps/app/src/server/service/page/delete-completely-user-home-by-system.ts

@@ -1,7 +1,7 @@
 import { Writable } from 'stream';
 
 import { getIdForRef } from '@growi/core';
-import type { IPage } from '@growi/core';
+import type { IPage, Ref } from '@growi/core';
 import { isUsersHomepage } from '@growi/core/dist/utils/page-path-utils';
 import mongoose from 'mongoose';
 import streamToPromise from 'stream-to-promise';
@@ -16,6 +16,13 @@ import { shouldUseV4Process } from './should-use-v4-process';
 
 const logger = loggerFactory('growi:services:page');
 
+
+type IPageUnderV5 = Omit<IPage, 'parent'> & { parent: Ref<IPage> }
+
+const _shouldUseV5Process = (page: IPage): page is IPageUnderV5 => {
+  return !shouldUseV4Process(page);
+};
+
 /**
    * @description This function is intended to be used exclusively for forcibly deleting the user homepage by the system.
    * It should only be called from within the appropriate context and with caution as it performs a system-level operation.
@@ -40,30 +47,29 @@ export const deleteCompletelyUserHomeBySystem = async(userHomepagePath: string,
     throw new Error(msg);
   }
 
-  const isShouldUseV4Process = shouldUseV4Process(userHomepage);
+  const shouldUseV5Process = _shouldUseV5Process(userHomepage);
 
   const ids = [userHomepage._id];
   const paths = [userHomepage.path];
-  const parentId = getIdForRef(userHomepage.parent);
 
   try {
-    if (!isShouldUseV4Process) {
+    if (shouldUseV5Process) {
       // Ensure consistency of ancestors
       const inc = userHomepage.isEmpty ? -userHomepage.descendantCount : -(userHomepage.descendantCount + 1);
-      await pageService.updateDescendantCountOfAncestors(parentId, inc, true);
+      await pageService.updateDescendantCountOfAncestors(getIdForRef(userHomepage.parent), inc, true);
     }
 
     // Delete the user's homepage
     await pageService.deleteCompletelyOperation(ids, paths);
 
-    if (!isShouldUseV4Process) {
+    if (shouldUseV5Process) {
       // Remove leaf empty pages
-      await Page.removeLeafEmptyPagesRecursively(parentId);
+      await Page.removeLeafEmptyPagesRecursively(getIdForRef(userHomepage.parent));
     }
 
     if (!userHomepage.isEmpty) {
       // Emit an event for the search service
-      pageService.pageEvent.emit('deleteCompletely', userHomepage);
+      pageService.getEventEmitter().emit('deleteCompletely', userHomepage);
     }
 
     const { PageQueryBuilder } = Page;