Explorar el Código

improve fetching data for IdenticalPathPage

Yuki Takei hace 4 años
padre
commit
26d6739a08

+ 7 - 7
packages/app/src/components/IdenticalPathPage.tsx

@@ -9,7 +9,7 @@ import { useCurrentPagePath } from '~/stores/context';
 
 import { PageListItemL } from './PageList/PageListItemL';
 import { useSWRxPageInfoForList } from '~/stores/page';
-import { IPageWithMeta } from '~/interfaces/page';
+import { IPageHasId, IPageWithMeta } from '~/interfaces/page';
 
 
 type IdenticalPathAlertProps = {
@@ -57,9 +57,9 @@ const jsonNull = 'null';
 const IdenticalPathPage:FC<IdenticalPathPageProps> = (props: IdenticalPathPageProps) => {
 
   const identicalPageDocument = document.getElementById('identical-path-page');
-  const pageDataList = JSON.parse(identicalPageDocument?.getAttribute('data-identical-page-data-list') || jsonNull);
+  const pages = JSON.parse(identicalPageDocument?.getAttribute('data-identical-path-pages') || jsonNull) as IPageHasId[];
 
-  const pageIds = pageDataList.map(data => data.pageData._id) as string[];
+  const pageIds = pages.map(page => page._id) as string[];
 
   const { data: idToPageInfoMap } = useSWRxPageInfoForList(pageIds);
 
@@ -82,18 +82,18 @@ const IdenticalPathPage:FC<IdenticalPathPageProps> = (props: IdenticalPathPagePr
 
         <div className="page-list">
           <ul className="page-list-ul list-group-flush">
-            {pageDataList.map((data) => {
-              const pageId = data.pageData._id;
+            {pages.map((page) => {
+              const pageId = page._id;
               const pageInfo = (idToPageInfoMap ?? {})[pageId];
 
               const pageWithMeta: IPageWithMeta = {
-                pageData: data.pageData,
+                pageData: page,
                 pageMeta: pageInfo,
               };
 
               return (
                 <PageListItemL
-                  key={data.pageData._id}
+                  key={pageId}
                   page={pageWithMeta}
                   isSelected={false}
                   isChecked={false}

+ 10 - 21
packages/app/src/server/routes/page.js

@@ -4,6 +4,7 @@ import { body } from 'express-validator';
 import mongoose from 'mongoose';
 
 import loggerFactory from '~/utils/logger';
+import { PageQueryBuilder } from '../models/obsolete-page';
 import UpdatePost from '../models/update-post';
 import { PageRedirectModel } from '../models/page-redirect';
 
@@ -283,21 +284,6 @@ module.exports = function(crowi, app) {
     renderVars.notFoundTargetPathOrId = pathOrId;
   }
 
-  async function addRenderVarsForIdenticalPage(renderVars, pages) {
-    const pageIds = pages.map(p => p._id);
-
-    const identicalPageDataList = await Promise.all(pages.map(async(page) => {
-      page._doc.seenUserCount = (page.seenUsers && page.seenUsers.length) || 0;
-      return {
-        pageData: page,
-        pageMeta: {
-        },
-      };
-    }));
-
-    renderVars.identicalPageDataList = identicalPageDataList;
-  }
-
   function replacePlaceholdersOfTemplate(template, req) {
     if (req.user == null) {
       return '';
@@ -613,18 +599,21 @@ module.exports = function(crowi, app) {
    * redirector
    */
   async function redirector(req, res, next, path) {
-    const pages = await Page.findByPathAndViewer(path, req.user, null, false, true);
-
     const { redirectFrom } = req.query;
 
-    if (pages.length >= 2) {
+    const builder = new PageQueryBuilder(Page.find({ path }));
+    await Page.addConditionToFilteringByViewerForList(builder, req.user);
 
-      const renderVars = {};
+    const pages = await builder.query.lean().clone().exec('find');
+
+    if (pages.length >= 2) {
 
-      await addRenderVarsForIdenticalPage(renderVars, pages);
+      // populate to list
+      builder.populateDataToList(User.USER_FIELDS_EXCEPT_CONFIDENTIAL);
+      const identicalPathPages = await builder.query.lean().exec('find');
 
       return res.render('layout-growi/identical-path-page', {
-        ...renderVars,
+        identicalPathPages,
         redirectFrom,
         path,
       });

+ 1 - 1
packages/app/src/server/views/layout-growi/identical-path-page.html

@@ -18,7 +18,7 @@
       <div class="flex-grow-1 flex-basis-0 mw-0">
         <div
           id="identical-path-page"
-          data-identical-page-data-list="{{ identicalPageDataList|json }}"
+          data-identical-path-pages="{{ identicalPathPages|json }}"
         ></div>
       </div>
       <div id="page-context"></div>