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

WIP: refactor models/page.js

* refactor pageShowForGrowiBehavior
* create Page.notFound action
Yuki Takei 7 лет назад
Родитель
Сommit
2cce4ae8cd
2 измененных файлов с 89 добавлено и 89 удалено
  1. 2 2
      src/server/routes/index.js
  2. 87 87
      src/server/routes/page.js

+ 2 - 2
src/server/routes/index.js

@@ -228,6 +228,6 @@ module.exports = function(crowi, app) {
   // API v3
   // API v3
   app.use('/_api/v3', require('./apiv3')(crowi));
   app.use('/_api/v3', require('./apiv3')(crowi));
 
 
-  app.get('/*/$'                   , loginRequired(crowi, app, false) , page.pageListShowWrapper);
-  app.get('/*'                     , loginRequired(crowi, app, false) , page.pageShowWrapper);
+  app.get('/*/$'                   , loginRequired(crowi, app, false) , page.pageListShowWrapper, page.notFound);
+  app.get('/*'                     , loginRequired(crowi, app, false) , page.pageShowWrapper, page.notFound);
 };
 };

+ 87 - 87
src/server/routes/page.js

@@ -108,30 +108,86 @@ module.exports = function(crowi, app) {
     }
     }
   }
   }
 
 
+  function addRendarVarsForPage(renderVars, page) {
+    renderVars.page = page;
+    renderVars.path = page.path;
+    renderVars.revision = page.revision;
+    renderVars.author = page.revision.author;
+    renderVars.pageIdOnHackmd = page.pageIdOnHackmd;
+    renderVars.revisionHackmdSynced = page.revisionHackmdSynced;
+    renderVars.hasDraftOnHackmd = page.hasDraftOnHackmd;
+  }
+
+  async function addRenderVarsForUserPage(renderVars, page, requestUser) {
+    const userData = await User.findUserByUsername(User.getUsernameByPath(page.path));
+    if (userData != null) {
+      renderVars.pageUser = userData;
+      renderVars.bookmarkList = await Bookmark.findByUser(userData, {limit: 10, populatePage: true, requestUser: requestUser});
+      renderVars.createdList = await Page.findListByCreator(userData, {limit: 10}, requestUser);
+    }
+  }
+
+  async function addRenderVarsForHistory(renderVars, page) {
+    renderVars.tree = await Revision.findRevisionList(page.path, {});
+  }
+
+  async function addRenderVarsForSlack(renderVars, page) {
+    renderVars.slack = await getSlackChannels(page);
+  }
+
+  async function addRenderVarsForDescendants(renderVars, page, requestUser, offset, limit) {
+    const SEENER_THRESHOLD = 10;
+
+    const queryOptions = {
+      offset: offset,
+      limit: limit + 1,
+      isPopulateRevisionBody: Config.isEnabledTimeline(config),
+      includeDeletedPage: page.path.startsWith('/trash/'),
+    };
+    const pageList = await Page.findListWithDescendants(page.path, requestUser, queryOptions);
+    if (pageList.length > limit) {
+      pageList.pop();
+    }
+
+    // index page
+    const pagerOptions = {
+      offset: offset,
+      limit: limit
+    };
+    pagerOptions.length = pageList.length;
+
+    renderVars.viewConfig = {
+      seener_threshold: SEENER_THRESHOLD,
+    };
+    renderVars.pager = generatePager(pagerOptions);
+    renderVars.pages = pagePathUtils.encodePagesPath(pageList);
+  }
+
+
   /**
   /**
    * switch action by behaviorType
    * switch action by behaviorType
    */
    */
-  actions.pageListShowWrapper = function(req, res) {
+  actions.pageListShowWrapper = function(req, res, next) {
     const behaviorType = Config.behaviorType(config);
     const behaviorType = Config.behaviorType(config);
 
 
     if (!behaviorType || 'crowi' === behaviorType) {
     if (!behaviorType || 'crowi' === behaviorType) {
-      return actions.pageListShow(req, res);
+      return actions.pageListShow(req, res, next);
     }
     }
     else {
     else {
-      return actions.pageListShowForGrowiBehavior(req, res);
+      return actions.pageListShowForGrowiBehavior(req, res, next);
     }
     }
   };
   };
   /**
   /**
    * switch action by behaviorType
    * switch action by behaviorType
    */
    */
-  actions.pageShowWrapper = function(req, res) {
+  actions.pageShowWrapper = function(req, res, next) {
     const behaviorType = Config.behaviorType(config);
     const behaviorType = Config.behaviorType(config);
 
 
     if (!behaviorType || 'crowi' === behaviorType) {
     if (!behaviorType || 'crowi' === behaviorType) {
-      return actions.pageShow(req, res);
+      return actions.pageShow(req, res, next);
     }
     }
     else {
     else {
-      return actions.pageShowForGrowiBehavior(req, res);
+      return actions.pageShowForGrowiBehavior(req, res, next);
     }
     }
   };
   };
   /**
   /**
@@ -181,61 +237,28 @@ module.exports = function(crowi, app) {
     }
     }
   };
   };
 
 
+  actions.notFound = async function(req, res) {
+    const path = getPathFromRequest(req);
 
 
-  function addRendarVarsForPage(renderVars, page) {
-    renderVars.page = page;
-    renderVars.path = page.path;
-    renderVars.revision = page.revision;
-    renderVars.author = page.revision.author;
-    renderVars.pageIdOnHackmd = page.pageIdOnHackmd;
-    renderVars.revisionHackmdSynced = page.revisionHackmdSynced;
-    renderVars.hasDraftOnHackmd = page.hasDraftOnHackmd;
-  }
+    let view;
+    const renderVars = {};
 
 
-  async function addRenderVarsForUserPage(renderVars, page, requestUser) {
-    const userData = await User.findUserByUsername(User.getUsernameByPath(page.path));
-    if (userData != null) {
-      renderVars.pageUser = userData;
-      renderVars.bookmarkList = await Bookmark.findByUser(userData, {limit: 10, populatePage: true, requestUser: requestUser});
-      renderVars.createdList = await Page.findListByCreator(userData, {limit: 10}, requestUser);
+    if (req.isForbidden) {
+      view = 'customlayout-selector/forbidden';
     }
     }
-  }
-
-  async function addRenderVarsForHistory(renderVars, page) {
-    renderVars.tree = await Revision.findRevisionList(page.path, {});
-  }
-
-  async function addRenderVarsForSlack(renderVars, page) {
-    renderVars.slack = await getSlackChannels(page);
-  }
-
-  async function addRenderVarsForDescendants(renderVars, page, requestUser, offset, limit) {
-    const SEENER_THRESHOLD = 10;
+    else {
+      view = 'customlayout-selector/not_found';
 
 
-    const queryOptions = {
-      offset: offset,
-      limit: limit + 1,
-      isPopulateRevisionBody: Config.isEnabledTimeline(config),
-      includeDeletedPage: page.path.startsWith('/trash/'),
-    };
-    const pageList = await Page.findListWithDescendants(page.path, requestUser, queryOptions);
-    if (pageList.length > limit) {
-      pageList.pop();
+      // retrieve templates
+      let template = await Page.findTemplate(path);
+      if (template != null) {
+        template = replacePlaceholders(template, req);
+        renderVars.template = template;
+      }
     }
     }
 
 
-    // index page
-    const pagerOptions = {
-      offset: offset,
-      limit: limit
-    };
-    pagerOptions.length = pageList.length;
-
-    renderVars.viewConfig = {
-      seener_threshold: SEENER_THRESHOLD,
-    };
-    renderVars.pager = generatePager(pagerOptions);
-    renderVars.pages = pagePathUtils.encodePagesPath(pageList);
-  }
+    return res.render(view, renderVars);
+  };
 
 
   actions.pageListShow = function(req, res) {
   actions.pageListShow = function(req, res) {
     let path = getPathFromRequest(req);
     let path = getPathFromRequest(req);
@@ -323,18 +346,18 @@ module.exports = function(crowi, app) {
     return res.redirect(path);
     return res.redirect(path);
   };
   };
 
 
-  actions.pageShowForGrowiBehavior = async function(req, res) {
+  actions.pageShowForGrowiBehavior = async function(req, res, next) {
     const path = getPathFromRequest(req);
     const path = getPathFromRequest(req);
     const revisionId = req.query.revision;
     const revisionId = req.query.revision;
 
 
-    const limit = 50;
-    const offset = parseInt(req.query.offset)  || 0;
-
     let page = await Page.findPageByPathAndViewer(path, req.user);
     let page = await Page.findPageByPathAndViewer(path, req.user);
 
 
     if (page == null) {
     if (page == null) {
-      // https://weseek.myjetbrains.com/youtrack/issue/GC-1224
-      // TODO notfound or forbidden
+      // check the page is forbidden or just does not exist.
+      const isForbidden = await Page.count({path}) > 0;
+      // inject to req
+      req.isForbidden = isForbidden;
+      return next();
     }
     }
     else if (page.redirectTo) {
     else if (page.redirectTo) {
       debug(`Redirect to '${page.redirectTo}'`);
       debug(`Redirect to '${page.redirectTo}'`);
@@ -343,6 +366,8 @@ module.exports = function(crowi, app) {
 
 
     debug('Page found', page._id, page.path);
     debug('Page found', page._id, page.path);
 
 
+    const limit = 50;
+    const offset = parseInt(req.query.offset)  || 0;
     const renderVars = {};
     const renderVars = {};
 
 
     // Presentation Mode
     // Presentation Mode
@@ -370,32 +395,6 @@ module.exports = function(crowi, app) {
 
 
     await interceptorManager.process('beforeRenderPage', req, res, renderVars);
     await interceptorManager.process('beforeRenderPage', req, res, renderVars);
     return res.render(view, renderVars);
     return res.render(view, renderVars);
-
-    // https://weseek.myjetbrains.com/youtrack/issue/GC-1224
-    // TODO render if not found or user is forbidden
-
-    // let isForbidden = false;
-    // if (err.name === 'UserHasNoGrantException') {
-    //   isForbidden = true;
-    // }
-
-    // if (isForbidden) {
-    //   view = 'customlayout-selector/forbidden';
-    //   return;
-    // }
-    // else {
-    //   view = 'customlayout-selector/not_found';
-
-    //   // look for templates
-    //   return Page.findTemplate(path)
-    //     .then(template => {
-    //       if (template) {
-    //         template = replacePlaceholders(template, req);
-    //       }
-
-    //       renderVars.template = template;
-    //     });
-    // }
   };
   };
 
 
   const getSlackChannels = async page => {
   const getSlackChannels = async page => {
@@ -409,6 +408,7 @@ module.exports = function(crowi, app) {
     }
     }
   };
   };
 
 
+  // TODO rename to replacePlaceholdersOfTemplate
   const replacePlaceholders = (template, req) => {
   const replacePlaceholders = (template, req) => {
     const definitions = {
     const definitions = {
       pagepath: getPathFromRequest(req),
       pagepath: getPathFromRequest(req),