Explorar o código

separate normalize page method

yuken %!s(int64=4) %!d(string=hai) anos
pai
achega
0b9910e35a

+ 3 - 9
packages/app/src/components/PrivateLegacyPages.tsx

@@ -170,13 +170,8 @@ export const PrivateLegacyPages = (props: Props): JSX.Element => {
   const { data: socket } = useGlobalSocket();
   const { data: socket } = useGlobalSocket();
 
 
   useEffect(() => {
   useEffect(() => {
-
-    socket?.on(SocketEventName.PageMigrationStarted, () => {
-      // page migration started
-    });
-
-    socket?.on(SocketEventName.PageMigrationEnded, () => {
-      // page migration ended
+    socket?.on(SocketEventName.PageMigrationSuccess, () => {
+      // page migration success
     });
     });
 
 
     socket?.on(SocketEventName.PageMigrationError, () => {
     socket?.on(SocketEventName.PageMigrationError, () => {
@@ -184,8 +179,7 @@ export const PrivateLegacyPages = (props: Props): JSX.Element => {
     });
     });
 
 
     return () => {
     return () => {
-      socket?.off(SocketEventName.PageMigrationStarted);
-      socket?.off(SocketEventName.PageMigrationEnded);
+      socket?.off(SocketEventName.PageMigrationSuccess);
       socket?.off(SocketEventName.PageMigrationError);
       socket?.off(SocketEventName.PageMigrationError);
     };
     };
   }, [socket]);
   }, [socket]);

+ 1 - 2
packages/app/src/interfaces/websocket.ts

@@ -9,8 +9,7 @@ export const SocketEventName = {
   PMEnded: 'PublicMigrationEnded',
   PMEnded: 'PublicMigrationEnded',
 
 
   // Page migration
   // Page migration
-  PageMigrationStarted: 'PageMigrationStarted',
-  PageMigrationEnded: 'PageMigrationEnded',
+  PageMigrationSuccess: 'PageMigrationSuccess',
   PageMigrationError: 'PageMigrationError',
   PageMigrationError: 'PageMigrationError',
 } as const;
 } as const;
 export type SocketEventName = typeof SocketEventName[keyof typeof SocketEventName];
 export type SocketEventName = typeof SocketEventName[keyof typeof SocketEventName];

+ 6 - 1
packages/app/src/server/routes/apiv3/pages.js

@@ -791,7 +791,12 @@ module.exports = (crowi) => {
     }
     }
 
 
     try {
     try {
-      await crowi.pageService.normalizeParentByPageIds(pageIds, req.user, isRecursively);
+      if (isRecursively) {
+        await crowi.pageService.normalizeParentByPageIdsRecursively(pageIds, req.user);
+      }
+      else {
+        await crowi.pageService.normalizeParentByPageIds(pageIds, req.user);
+      }
     }
     }
     catch (err) {
     catch (err) {
       return res.apiv3Err(new ErrorV3(`Failed to migrate pages: ${err.message}`), 500);
       return res.apiv3Err(new ErrorV3(`Failed to migrate pages: ${err.message}`), 500);

+ 18 - 23
packages/app/src/server/service/page.ts

@@ -2251,55 +2251,50 @@ class PageService {
     await inAppNotificationService.emitSocketIo(targetUsers);
     await inAppNotificationService.emitSocketIo(targetUsers);
   }
   }
 
 
-  async normalizeParentByPageIds(pageIds: ObjectIdLike[], user, isRecursively: boolean): Promise<void> {
+  async normalizeParentByPageIdsRecursively(pageIds: ObjectIdLike[], user): Promise<void> {
     const Page = mongoose.model('Page') as unknown as PageModel;
     const Page = mongoose.model('Page') as unknown as PageModel;
 
 
     const socket = this.crowi.socketIoService.getDefaultSocket();
     const socket = this.crowi.socketIoService.getDefaultSocket();
 
 
-    socket.emit(SocketEventName.PageMigrationStarted);
+    const pages = await Page.findByIdsAndViewer(pageIds, user, null);
 
 
-    if (isRecursively) {
-      const pages = await Page.findByIdsAndViewer(pageIds, user, null);
+    this.normalizeParentRecursivelyByPages(pages, user);
 
 
-      // DO NOT await !!
-      (async() => {
-        try {
-          await this.normalizeParentRecursivelyByPages(pages, user);
-          socket.emit(SocketEventName.PageMigrationEnded);
-        }
-        catch {
-          socket.emit(SocketEventName.PageMigrationError);
-        }
-      })();
+    return;
+  }
 
 
-      return;
-    }
+  normalizeParentByPageIds(pageIds: ObjectIdLike[], user): void {
+    const Page = mongoose.model('Page') as unknown as PageModel;
+
+    const socket = this.crowi.socketIoService.getDefaultSocket();
+
+    // socket.emit(SocketEventName.PageMigrationStarted);
 
 
-    for await (const pageId of pageIds) {
-      const page = await Page.findById(pageId);
+    for (const pageId of pageIds) {
+      const page = Page.findById(pageId);
       if (page == null) {
       if (page == null) {
         continue;
         continue;
       }
       }
 
 
       try {
       try {
-        const canOperate = await this.crowi.pageOperationService.canOperate(false, page.path, page.path);
+        const canOperate = this.crowi.pageOperationService.canOperate(false, page.path, page.path);
         if (!canOperate) {
         if (!canOperate) {
           throw Error(`Cannot operate normalizeParent to path "${page.path}" right now.`);
           throw Error(`Cannot operate normalizeParent to path "${page.path}" right now.`);
         }
         }
 
 
-        const normalizedPage = await this.normalizeParentByPage(page, user);
+        const normalizedPage = this.normalizeParentByPage(page, user);
 
 
         if (normalizedPage == null) {
         if (normalizedPage == null) {
           logger.error(`Failed to update descendantCount of page of id: "${pageId}"`);
           logger.error(`Failed to update descendantCount of page of id: "${pageId}"`);
-          socket.emit(SocketEventName.PageMigrationError);
+          // socket.emit(SocketEventName.PageMigrationError);
         }
         }
         else {
         else {
-          socket.emit(SocketEventName.PageMigrationEnded);
+          // socket.emit(SocketEventName.PageMigrationEnded);
         }
         }
       }
       }
       catch (err) {
       catch (err) {
         logger.error('Something went wrong while normalizing parent.', err);
         logger.error('Something went wrong while normalizing parent.', err);
-        socket.emit(SocketEventName.PageMigrationError);
+        // socket.emit(SocketEventName.PageMigrationError);
       }
       }
     }
     }
   }
   }