2
0
Эх сурвалжийг харах

refactor findListByStartWith function of models/page.js

Yuki Takei 9 жил өмнө
parent
commit
dd6d70d811
1 өөрчлөгдсөн 43 нэмэгдсэн , 37 устгасан
  1. 43 37
      lib/models/page.js

+ 43 - 37
lib/models/page.js

@@ -593,8 +593,6 @@ module.exports = function(crowi) {
   pageSchema.statics.findListByStartWith = function(path, userData, option) {
     var Page = this;
     var User = crowi.model('User');
-    var pathCondition = [];
-    var includeDeletedPage = option.includeDeletedPage || false
 
     if (!option) {
       option = {sort: 'updatedAt', desc: -1, offset: 0, limit: 50};
@@ -607,52 +605,60 @@ module.exports = function(crowi) {
     };
     var sortOpt = {};
     sortOpt[opt.sort] = opt.desc;
-    var queryReg = new RegExp('^' + path);
-    var sliceOption = option.revisionSlice || {$slice: 1};
-
-    pathCondition.push({path: queryReg});
-    if (path.match(/\/$/)) {
-      debug('Page list by ending with /, so find also upper level page');
-      pathCondition.push({path: path.substr(0, path.length -1)});
-    }
 
     return new Promise(function(resolve, reject) {
-      // FIXME: might be heavy
-      var q = Page.find({
-        redirectTo: null,
-        $or: [
-          {grant: null},
-          {grant: GRANT_PUBLIC},
-          {grant: GRANT_RESTRICTED, grantedUsers: userData._id},
-          {grant: GRANT_SPECIFIED, grantedUsers: userData._id},
-          {grant: GRANT_OWNER, grantedUsers: userData._id},
-        ],})
+      var q = Page.generateQueryToListByStartWith(path, userData, option)
         .populate('revision')
-        .and({
-          $or: pathCondition
-        })
         .sort(sortOpt)
         .skip(opt.offset)
         .limit(opt.limit);
 
-      if (!includeDeletedPage) {
-        q.and({
-          $or: [
-            {status: null},
-            {status: STATUS_PUBLISHED},
-          ],
-        });
-      }
-
       q.exec()
-      .then(function(pages) {
-        Page.populate(pages, {path: 'revision.author', model: 'User', select: User.USER_PUBLIC_FIELDS})
-        .then(resolve)
-        .catch(reject);
-      })
+        .then(function(pages) {
+          Page.populate(pages, {path: 'revision.author', model: 'User', select: User.USER_PUBLIC_FIELDS})
+          .then(resolve)
+          .catch(reject);
+        })
     });
   };
 
+  pageSchema.statics.generateQueryToListByStartWith = function(path, userData, option) {
+    var Page = this;
+    var pathCondition = [];
+    var includeDeletedPage = option.includeDeletedPage || false;
+
+    var queryReg = new RegExp('^' + path);
+    pathCondition.push({path: queryReg});
+    if (path.match(/\/$/)) {
+      debug('Page list by ending with /, so find also upper level page');
+      pathCondition.push({path: path.substr(0, path.length -1)});
+    }
+
+    var q = Page.find({
+      redirectTo: null,
+      $or: [
+        {grant: null},
+        {grant: GRANT_PUBLIC},
+        {grant: GRANT_RESTRICTED, grantedUsers: userData._id},
+        {grant: GRANT_SPECIFIED, grantedUsers: userData._id},
+        {grant: GRANT_OWNER, grantedUsers: userData._id},
+      ],})
+      .and({
+        $or: pathCondition
+      });
+
+    if (!includeDeletedPage) {
+      q.and({
+        $or: [
+          {status: null},
+          {status: STATUS_PUBLISHED},
+        ],
+      });
+    }
+
+    return q;
+  }
+
   pageSchema.statics.updatePageProperty = function(page, updateData) {
     var Page = this;
     return new Promise(function(resolve, reject) {